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