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