664035344691867fae48a1e4a3e91aab11ef56a9
[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.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
60 private static Class serviceClass;
61
62 public AbstractBulkEditorInput() {
63
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 /**
104 * <p>loadEntity</p>
105 *
106 * @param entityUuid a {@link java.util.UUID} object.
107 * @return a T object.
108 */
109 protected T loadEntity(UUID entityUuid) {
110 List<String> propertyPaths = Arrays.asList(new String[]{});
111 return (T) CdmStore.getService(serviceClass).load(entityUuid, propertyPaths);
112 }
113
114 private void setEntityUuid(UUID entityUuid){
115 this.entityUuid = entityUuid;
116 }
117
118 /**
119 * <p>Getter for the field <code>entityUuid</code>.</p>
120 *
121 * @return a {@link java.util.UUID} object.
122 */
123 public UUID getEntityUuid() {
124 return entityUuid;
125 }
126
127
128 /* (non-Javadoc)
129 * @see org.eclipse.ui.IEditorInput#exists()
130 */
131 /**
132 * <p>exists</p>
133 *
134 * @return a boolean.
135 */
136 @Override
137 public boolean exists() {
138 // TODO Auto-generated method stub
139 return false;
140 }
141
142 /* (non-Javadoc)
143 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
144 */
145 /**
146 * <p>getImageDescriptor</p>
147 *
148 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
149 */
150 @Override
151 public ImageDescriptor getImageDescriptor() {
152 // TODO Auto-generated method stub
153 return null;
154 }
155
156 /* (non-Javadoc)
157 * @see org.eclipse.ui.IEditorInput#getPersistable()
158 */
159 /**
160 * <p>getPersistable</p>
161 *
162 * @return a {@link org.eclipse.ui.IPersistableElement} object.
163 */
164 @Override
165 public IPersistableElement getPersistable() {
166 return null;
167 }
168
169 /* (non-Javadoc)
170 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
171 */
172 /** {@inheritDoc} */
173 @Override
174 @SuppressWarnings("unchecked")
175 public Object getAdapter(Class adapter) {
176 return null;
177 }
178
179 /**
180 * <p>Setter for the field <code>query</code>.</p>
181 *
182 * @param bulkEditorQuery a {@link eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorQuery} object.
183 */
184 public void performSearch(final BulkEditorQuery bulkEditorQuery) {
185
186 List<T> entityList = new ArrayList<T>();
187
188 if(getEntityUuid() != null){
189
190 T entity = loadEntity(getEntityUuid());
191 entityList.add(entity);
192 model = entityList;
193 }
194 else if(bulkEditorQuery != null){
195
196 IIdentifiableEntityServiceConfigurator configurator = bulkEditorQuery.getSearchConfigurator();
197 Comparator queryComparator = (bulkEditorQuery.getComparator() != null) ? bulkEditorQuery.getComparator() : new TitleCacheComparator();
198
199 entityList = listEntities(configurator);
200
201 Collections.sort(entityList, queryComparator);
202
203
204 }
205
206 model = entityList;
207 }
208
209 /**
210 * <p>isMergingEnabled</p>
211 *
212 * @return a boolean.
213 */
214 public boolean isMergingEnabled() {
215 return false;
216 }
217
218 /**
219 * <p>isMergingEnabled</p>
220 *
221 * @return a boolean.
222 */
223 public boolean isConvertingEnabled() {
224 return false;
225 }
226 /**
227 * <p>isMarkerTypeEditingEnabled</p>
228 *
229 * @param markerType a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
230 * @return a boolean.
231 */
232 public boolean isMarkerTypeEditingEnabled(MarkerType markerType) {
233 return false;
234 }
235
236
237 /** {@inheritDoc} */
238 @Override
239 public boolean merge(T entity, T mergeTarget) {
240 if (entity instanceof IMergable) {
241 try {
242 CdmStore.getCommonService().merge(mergeTarget.getId(), entity.getId(), (Class<? extends CdmBase>)entity.getClass());
243 } catch (MergeException e) {
244 MessagingUtils.errorDialog("Bulk Editor Merge Error",
245 this,
246 "Could not merge chosen objects of type " + entity.getClass().getName(),
247 TaxeditorBulkeditorPlugin.PLUGIN_ID,
248 e,
249 true);
250 }
251 }
252 return true;
253 }
254
255
256 /** {@inheritDoc} */
257 @Override
258 public T create(T entity) {
259 return save(entity);
260 }
261
262 public IEntityCreator<T> getEntityCreator(){
263 if(entityCreator == null){
264 entityCreator = createEntityCreator();
265 }
266 return entityCreator;
267 }
268
269 /**
270 * @return
271 */
272 protected abstract IEntityCreator<T> createEntityCreator();
273
274 /**
275 * The default implementation returns an empty list of sort providers.
276 * @return
277 */
278 public List<IBulkEditorSortProvider<T>> getSortProviders(){
279 List<IBulkEditorSortProvider<T>> sortProviders = new ArrayList<IBulkEditorSortProvider<T>>();
280
281 sortProviders.add(new CdmBaseSortProvider<T>());
282
283 return sortProviders;
284 }
285
286 /**
287 * Returns a textual representation given object. The default implementation
288 * in the abstract base class returns the simple name of the class, this may
289 * be overwritten to something more specific in subclasses.
290 *
291 * @param entity
292 * @return a textual representation given object.
293 */
294 public String getTypeText(Object entity){
295 return entity.getClass().getSimpleName();
296 }
297
298 /**
299 * @param entity
300 * @return
301 */
302 public String getText(T entity) {
303 if(entity instanceof IdentifiableEntity){
304 IdentifiableEntity identifiableEntity = (IdentifiableEntity) HibernateProxyHelper.deproxy(entity);
305
306 return identifiableEntity.getTitleCache();
307 }
308
309 return "No text. Implement in subclass";
310 }
311
312 /**
313 * @return
314 */
315 public List<T> getModel() {
316 return model;
317 }
318
319 protected boolean replaceInModel(T entity) {
320 int index = model.indexOf(entity);
321 if(index >= 0) {
322 model.set(index, entity);
323 return true;
324 } else {
325 return false;
326 }
327 }
328
329 /* (non-Javadoc)
330 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
331 */
332 @Override
333 public List<T> getRootEntities() {
334 return getModel();
335 }
336
337
338 /* (non-Javadoc)
339 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getPropertyPathsMap()
340 */
341 @Override
342 public Map<Object, List<String>> getPropertyPathsMap() {
343 // TODO Auto-generated method stub
344 return null;
345 }
346 }