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