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