Fixes #2418
[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.Collections;
14 import java.util.Comparator;
15 import java.util.List;
16 import java.util.UUID;
17
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.ui.IEditorInput;
20 import org.eclipse.ui.IPersistableElement;
21
22 import eu.etaxonomy.cdm.api.service.ICommonService;
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.BulkEditorUtil;
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.store.CdmStore;
38
39 /**
40 * <p>Abstract AbstractBulkEditorInput class.</p>
41 *
42 * @author p.ciardelli
43 * @created 25.06.2009
44 * @version 1.0
45 * @param <T>
46 */
47 public abstract class AbstractBulkEditorInput<T extends ICdmBase> implements IEditorInput, IEntityPersistenceService<T> {
48
49 /**
50 *
51 */
52 private static final long serialVersionUID = 416414530232743735L;
53
54 private UUID entityUuid;
55
56 private List<T> model;
57
58 private IEntityCreator<T> entityCreator;
59
60 /**
61 * <p>NewInstance</p>
62 *
63 * @param inputType a {@link eu.etaxonomy.taxeditor.bulkeditor.input.BulkEditorInputTypeValues.BulkEditorInputType} object.
64 * @return a {@link eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput} object.
65 */
66 static public AbstractBulkEditorInput NewInstance(BulkEditorInputType inputType) {
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 abstract T loadEntity(UUID entityUuid);
103
104 private void setEntityUuid(UUID entityUuid){
105 this.entityUuid = entityUuid;
106 }
107
108 /**
109 * <p>Getter for the field <code>entityUuid</code>.</p>
110 *
111 * @return a {@link java.util.UUID} object.
112 */
113 public UUID getEntityUuid() {
114 return entityUuid;
115 }
116
117
118 /* (non-Javadoc)
119 * @see org.eclipse.ui.IEditorInput#exists()
120 */
121 /**
122 * <p>exists</p>
123 *
124 * @return a boolean.
125 */
126 public boolean exists() {
127 // TODO Auto-generated method stub
128 return false;
129 }
130
131 /* (non-Javadoc)
132 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
133 */
134 /**
135 * <p>getImageDescriptor</p>
136 *
137 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
138 */
139 public ImageDescriptor getImageDescriptor() {
140 // TODO Auto-generated method stub
141 return null;
142 }
143
144 /* (non-Javadoc)
145 * @see org.eclipse.ui.IEditorInput#getPersistable()
146 */
147 /**
148 * <p>getPersistable</p>
149 *
150 * @return a {@link org.eclipse.ui.IPersistableElement} object.
151 */
152 public IPersistableElement getPersistable() {
153 return null;
154 }
155
156 /* (non-Javadoc)
157 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
158 */
159 /** {@inheritDoc} */
160 @SuppressWarnings("unchecked")
161 public Object getAdapter(Class adapter) {
162 return null;
163 }
164
165 /**
166 * <p>Setter for the field <code>query</code>.</p>
167 *
168 * @param bulkEditorQuery a {@link eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorQuery} object.
169 */
170 public void performSearch(final BulkEditorQuery bulkEditorQuery) {
171
172 List<T> entityList = new ArrayList<T>();
173
174 if(getEntityUuid() != null){
175
176 T entity = loadEntity(getEntityUuid());
177 entityList.add(entity);
178 model = entityList;
179 }
180 else if(bulkEditorQuery != null){
181
182 String queryString = bulkEditorQuery.getSearchString();
183 IIdentifiableEntityServiceConfigurator configurator = bulkEditorQuery.getSearchConfigurator();
184 Comparator queryComparator = (bulkEditorQuery.getComparator() != null) ? bulkEditorQuery.getComparator() : new TitleCacheComparator();
185
186 entityList = listEntities(configurator);
187
188 Collections.sort(entityList, queryComparator);
189
190 }
191
192 model = entityList;
193 }
194
195 /**
196 * <p>isMergingEnabled</p>
197 *
198 * @return a boolean.
199 */
200 public boolean isMergingEnabled() {
201 return false;
202 }
203
204 /**
205 * <p>isMarkerTypeEditingEnabled</p>
206 *
207 * @param markerType a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
208 * @return a boolean.
209 */
210 public boolean isMarkerTypeEditingEnabled(MarkerType markerType) {
211 return false;
212 }
213
214 /*
215 * (non-Javadoc)
216 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService#merge(eu.etaxonomy.cdm.model.common.IdentifiableEntity, eu.etaxonomy.cdm.model.common.IdentifiableEntity)
217 */
218 /** {@inheritDoc} */
219 public boolean merge(T entity, T mergeTarget) {
220 if (entity instanceof IMergable) {
221 try {
222 CdmStore.getService(ICommonService.class).merge((IMergable)mergeTarget, (IMergable)entity, null);
223 } catch (MergeException e) {
224 BulkEditorUtil.error(getClass(), e);
225 }
226 }
227 return true;
228 };
229
230 /*
231 * (non-Javadoc)
232 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService#create(eu.etaxonomy.cdm.model.common.IdentifiableEntity)
233 */
234 /** {@inheritDoc} */
235 public boolean create(T entity) {
236 return save(entity);
237 };
238
239 public IEntityCreator<T> getEntityCreator(){
240 if(entityCreator == null){
241 entityCreator = createEntityCreator();
242 }
243 return entityCreator;
244 }
245
246 /**
247 * @return
248 */
249 protected abstract IEntityCreator<T> createEntityCreator();
250
251 /**
252 * The default implementation returns an empty list of sort providers.
253 * @return
254 */
255 public List<IBulkEditorSortProvider<T>> getSortProviders(){
256 List<IBulkEditorSortProvider<T>> sortProviders = new ArrayList<IBulkEditorSortProvider<T>>();
257
258 sortProviders.add(new CdmBaseSortProvider<T>());
259
260 return sortProviders;
261 }
262
263 /**
264 * @param entity
265 * @return
266 */
267 public String getTypeText(Object entity){
268 return entity.getClass().getSimpleName();
269 }
270
271 /**
272 * @param entity
273 * @return
274 */
275 public String getText(T entity) {
276 if(entity instanceof IdentifiableEntity){
277 IdentifiableEntity identifiableEntity = (IdentifiableEntity) HibernateProxyHelper.deproxy(entity);
278
279 return identifiableEntity.getTitleCache();
280 }
281
282 return "No text. Implement in subclass";
283 }
284
285 /**
286 * @return
287 */
288 public List<?> getModel() {
289 return model;
290 }
291
292
293 }