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