cb0b8710d9ec92e0fdca6bc1fc39acfe97363c7c
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / input / AbstractBulkEditorInput.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.bulkeditor.input;
11
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.Collections;
15 import java.util.Comparator;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.UUID;
19
20 import org.eclipse.jface.resource.ImageDescriptor;
21 import org.eclipse.ui.IEditorInput;
22 import org.eclipse.ui.IPersistableElement;
23
24 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
25 import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
26 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
27 import eu.etaxonomy.cdm.model.common.CdmBase;
28 import eu.etaxonomy.cdm.model.common.ICdmBase;
29 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
30 import eu.etaxonomy.cdm.model.common.MarkerType;
31 import eu.etaxonomy.cdm.strategy.merge.IMergable;
32 import eu.etaxonomy.cdm.strategy.merge.MergeException;
33 import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreator;
34 import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService;
35 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
36 import eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorSortProvider;
37 import eu.etaxonomy.taxeditor.bulkeditor.input.sortprovider.CdmBaseSortProvider;
38 import eu.etaxonomy.taxeditor.bulkeditor.input.sortprovider.TitleCacheComparator;
39 import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
40 import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
41 import eu.etaxonomy.taxeditor.model.MessagingUtils;
42 import eu.etaxonomy.taxeditor.store.CdmStore;
43
44 /**
45 * <p>Abstract AbstractBulkEditorInput class.</p>
46 *
47 * @author p.ciardelli
48 * @created 25.06.2009
49 * @version 1.0
50 * @param <T>
51 */
52 public abstract class AbstractBulkEditorInput<T extends ICdmBase> extends CdmEntitySessionInput implements IEditorInput ,
53 IEntityPersistenceService<T> {
54
55 private UUID entityUuid;
56
57 private List<T> model;
58
59 private IEntityCreator<T> entityCreator;
60 private final ConversationHolder conversation;
61
62 private static Class serviceClass;
63
64 public AbstractBulkEditorInput() {
65 super(true);
66 this.conversation = CdmStore.createConversation();
67 }
68
69 /**
70 * <p>NewInstance</p>
71 *
72 * @param inputType a {@link eu.etaxonomy.taxeditor.bulkeditor.input.BulkEditorInputTypeValues.BulkEditorInputType} object.
73 * @return a {@link eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput} object.
74 */
75 static public AbstractBulkEditorInput NewInstance(BulkEditorInputType inputType) {
76
77 return BulkEditorInputType.getInput(inputType);
78 }
79
80 /**
81 * <p>NewInstance</p>
82 *
83 * @param entity a {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity} object.
84 * @return a {@link eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput} object.
85 */
86 public static AbstractBulkEditorInput NewInstance(IdentifiableEntity entity) {
87
88
89 BulkEditorInputType inputType = BulkEditorInputType.getByType(entity.getClass());
90
91 AbstractBulkEditorInput editorInput = NewInstance(inputType);
92
93 editorInput.setEntityUuid(entity.getUuid());
94
95 return editorInput;
96 }
97
98 /**
99 * <p>listEntities</p>
100 *
101 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
102 * @return a {@link java.util.List} object.
103 */
104 protected abstract List<T> listEntities(IIdentifiableEntityServiceConfigurator configurator);
105
106 /**
107 * <p>loadEntity</p>
108 *
109 * @param entityUuid a {@link java.util.UUID} object.
110 * @return a T object.
111 */
112 protected T loadEntity(UUID entityUuid) {
113 List<String> propertyPaths = Arrays.asList(new String[]{});
114 return (T) CdmStore.getService(serviceClass).load(entityUuid, propertyPaths);
115 }
116
117 private void setEntityUuid(UUID entityUuid){
118 this.entityUuid = entityUuid;
119 }
120
121 /**
122 * <p>Getter for the field <code>entityUuid</code>.</p>
123 *
124 * @return a {@link java.util.UUID} object.
125 */
126 public UUID getEntityUuid() {
127 return entityUuid;
128 }
129
130
131 /* (non-Javadoc)
132 * @see org.eclipse.ui.IEditorInput#exists()
133 */
134 /**
135 * <p>exists</p>
136 *
137 * @return a boolean.
138 */
139 @Override
140 public boolean exists() {
141 // TODO Auto-generated method stub
142 return false;
143 }
144
145 /* (non-Javadoc)
146 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
147 */
148 /**
149 * <p>getImageDescriptor</p>
150 *
151 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
152 */
153 @Override
154 public ImageDescriptor getImageDescriptor() {
155 // TODO Auto-generated method stub
156 return null;
157 }
158
159 /* (non-Javadoc)
160 * @see org.eclipse.ui.IEditorInput#getPersistable()
161 */
162 /**
163 * <p>getPersistable</p>
164 *
165 * @return a {@link org.eclipse.ui.IPersistableElement} object.
166 */
167 @Override
168 public IPersistableElement getPersistable() {
169 return null;
170 }
171
172 /* (non-Javadoc)
173 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
174 */
175 /** {@inheritDoc} */
176 @Override
177 @SuppressWarnings("unchecked")
178 public Object getAdapter(Class adapter) {
179 return null;
180 }
181
182 /**
183 * <p>Setter for the field <code>query</code>.</p>
184 *
185 * @param bulkEditorQuery a {@link eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorQuery} object.
186 */
187 public void performSearch(final BulkEditorQuery bulkEditorQuery) {
188
189 List<T> entityList = new ArrayList<T>();
190
191 if(getEntityUuid() != null){
192
193 T entity = loadEntity(getEntityUuid());
194 entityList.add(entity);
195 model = entityList;
196 }
197 else if(bulkEditorQuery != null){
198
199 IIdentifiableEntityServiceConfigurator configurator = bulkEditorQuery.getSearchConfigurator();
200 Comparator queryComparator = (bulkEditorQuery.getComparator() != null) ? bulkEditorQuery.getComparator() : new TitleCacheComparator();
201
202 entityList = listEntities(configurator);
203
204 Collections.sort(entityList, queryComparator);
205
206
207 }
208
209 model = entityList;
210 }
211
212 /**
213 * <p>isMergingEnabled</p>
214 *
215 * @return a boolean.
216 */
217 public boolean isMergingEnabled() {
218 return false;
219 }
220
221 /**
222 * <p>isMergingEnabled</p>
223 *
224 * @return a boolean.
225 */
226 public boolean isConvertingEnabled() {
227 return false;
228 }
229 /**
230 * <p>isMarkerTypeEditingEnabled</p>
231 *
232 * @param markerType a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
233 * @return a boolean.
234 */
235 public boolean isMarkerTypeEditingEnabled(MarkerType markerType) {
236 return false;
237 }
238
239
240 /** {@inheritDoc} */
241 @Override
242 public boolean merge(T entity, T mergeTarget) {
243 if (entity instanceof IMergable) {
244 try {
245 CdmStore.getCommonService().merge(mergeTarget.getUuid(), entity.getUuid(), (Class<? extends CdmBase>)entity.getClass());
246 } catch (MergeException e) {
247 MessagingUtils.errorDialog("Bulk Editor Merge Error",
248 this,
249 "Could not merge chosen objects of type " + entity.getClass().getName(),
250 TaxeditorBulkeditorPlugin.PLUGIN_ID,
251 e,
252 true);
253 }
254 }
255 return true;
256 }
257
258
259 /** {@inheritDoc} */
260 @Override
261 public T create(T entity) {
262 return save(entity);
263 }
264
265 public IEntityCreator<T> getEntityCreator(){
266 if(entityCreator == null){
267 entityCreator = createEntityCreator();
268 }
269 return entityCreator;
270 }
271
272 /**
273 * @return
274 */
275 protected abstract IEntityCreator<T> createEntityCreator();
276
277 /**
278 * The default implementation returns an empty list of sort providers.
279 * @return
280 */
281 public List<IBulkEditorSortProvider<T>> getSortProviders(){
282 List<IBulkEditorSortProvider<T>> sortProviders = new ArrayList<IBulkEditorSortProvider<T>>();
283
284 sortProviders.add(new CdmBaseSortProvider<T>());
285
286 return sortProviders;
287 }
288
289 /**
290 * Returns a textual representation given object. The default implementation
291 * in the abstract base class returns the simple name of the class, this may
292 * be overwritten to something more specific in subclasses.
293 *
294 * @param entity
295 * @return a textual representation given object.
296 */
297 public String getTypeText(Object entity){
298 return entity.getClass().getSimpleName();
299 }
300
301 /**
302 * @param entity
303 * @return
304 */
305 public String getText(T entity) {
306 if(entity instanceof IdentifiableEntity){
307 IdentifiableEntity identifiableEntity = (IdentifiableEntity) HibernateProxyHelper.deproxy(entity);
308
309 return identifiableEntity.getTitleCache();
310 }
311
312 return "No text. Implement in subclass";
313 }
314
315 /**
316 * @return
317 */
318 public List<T> getModel() {
319 return model;
320 }
321
322 protected boolean replaceInModel(T entity) {
323 int index = model.indexOf(entity);
324 if(index >= 0) {
325 model.set(index, entity);
326 return true;
327 } else {
328 return false;
329 }
330 }
331
332 /* (non-Javadoc)
333 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
334 */
335 @Override
336 public List<T> getRootEntities() {
337 return getModel();
338 }
339
340
341 /* (non-Javadoc)
342 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getPropertyPathsMap()
343 */
344 @Override
345 public Map<Object, List<String>> getPropertyPathsMap() {
346 // TODO Auto-generated method stub
347 return null;
348 }
349
350 public ConversationHolder getConversation() {
351 return conversation;
352 }
353 }