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