Merge branch 'release/4.3.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.Collections;
14 import java.util.Comparator;
15 import java.util.List;
16 import java.util.Map;
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.conversation.ConversationHolder;
24 import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
25 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
26 import eu.etaxonomy.cdm.model.common.CdmBase;
27 import eu.etaxonomy.cdm.model.common.ICdmBase;
28 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
29 import eu.etaxonomy.cdm.model.common.MarkerType;
30 import eu.etaxonomy.cdm.strategy.merge.IMergable;
31 import eu.etaxonomy.cdm.strategy.merge.MergeException;
32 import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreator;
33 import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService;
34 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
35 import eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorSortProvider;
36 import eu.etaxonomy.taxeditor.bulkeditor.input.sortprovider.CdmBaseSortProvider;
37 import eu.etaxonomy.taxeditor.bulkeditor.input.sortprovider.TitleCacheComparator;
38 import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
39 import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
40 import eu.etaxonomy.taxeditor.model.MessagingUtils;
41 import eu.etaxonomy.taxeditor.store.CdmStore;
42
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 private final ConversationHolder conversation;
58
59 public AbstractBulkEditorInput() {
60 super(true);
61 this.conversation = CdmStore.createConversation();
62 }
63
64 static public AbstractBulkEditorInput NewInstance(BulkEditorInputType inputType) {
65
66 return BulkEditorInputType.getInput(inputType);
67 }
68
69 public static AbstractBulkEditorInput NewInstance(IdentifiableEntity entity) {
70
71 BulkEditorInputType inputType = BulkEditorInputType.getByType(entity.getClass());
72
73 AbstractBulkEditorInput editorInput = NewInstance(inputType);
74
75 editorInput.setEntityUuid(entity.getUuid());
76
77 return editorInput;
78 }
79
80 protected abstract List<T> listEntities(IIdentifiableEntityServiceConfigurator configurator);
81
82 protected abstract T loadEntity(UUID entityUuid);
83
84 private void setEntityUuid(UUID entityUuid){
85 this.entityUuid = entityUuid;
86 }
87
88 public UUID getEntityUuid() {
89 return entityUuid;
90 }
91
92 @Override
93 public boolean exists() {
94 // TODO Auto-generated method stub
95 return false;
96 }
97
98 @Override
99 public ImageDescriptor getImageDescriptor() {
100 // TODO Auto-generated method stub
101 return null;
102 }
103
104 @Override
105 public IPersistableElement getPersistable() {
106 return null;
107 }
108
109 /** {@inheritDoc} */
110 @Override
111 @SuppressWarnings("unchecked")
112 public Object getAdapter(Class adapter) {
113 return null;
114 }
115
116 public void performSearch(final BulkEditorQuery bulkEditorQuery) {
117
118 List<T> entityList = new ArrayList<T>();
119
120 if(getEntityUuid() != null){
121
122 T entity = loadEntity(getEntityUuid());
123 entityList.add(entity);
124 model = entityList;
125 }
126 else if(bulkEditorQuery != null){
127
128 IIdentifiableEntityServiceConfigurator configurator = bulkEditorQuery.getSearchConfigurator();
129 Comparator queryComparator = (bulkEditorQuery.getComparator() != null) ? bulkEditorQuery.getComparator() : new TitleCacheComparator();
130
131 entityList = listEntities(configurator);
132
133 Collections.sort(entityList, queryComparator);
134
135
136 }
137
138 model = entityList;
139 }
140
141 public boolean isMergingEnabled() {
142 return false;
143 }
144
145 public boolean isConvertingEnabled() {
146 return false;
147 }
148
149 public boolean isMarkerTypeEditingEnabled(MarkerType markerType) {
150 return false;
151 }
152
153
154 /** {@inheritDoc} */
155 @Override
156 public boolean merge(T entity, T mergeTarget) {
157 if (entity instanceof IMergable) {
158 try {
159 CdmStore.getCommonService().merge(mergeTarget.getUuid(), entity.getUuid(), (Class<? extends CdmBase>)entity.getClass());
160 } catch (MergeException e) {
161 MessagingUtils.errorDialog("Bulk Editor Merge Error",
162 this,
163 "Could not merge chosen objects of type " + entity.getClass().getName(),
164 TaxeditorBulkeditorPlugin.PLUGIN_ID,
165 e,
166 true);
167 }
168 }
169 return true;
170 }
171
172
173 /** {@inheritDoc} */
174 @Override
175 public T create(T entity) {
176 return save(entity);
177 }
178
179 public IEntityCreator<T> getEntityCreator(){
180 if(entityCreator == null){
181 entityCreator = createEntityCreator();
182 }
183 return entityCreator;
184 }
185
186 protected abstract IEntityCreator<T> createEntityCreator();
187
188 /**
189 * The default implementation returns an empty list of sort providers.
190 * @return
191 */
192 public List<IBulkEditorSortProvider<T>> getSortProviders(){
193 List<IBulkEditorSortProvider<T>> sortProviders = new ArrayList<IBulkEditorSortProvider<T>>();
194
195 sortProviders.add(new CdmBaseSortProvider<T>());
196
197 return sortProviders;
198 }
199
200 /**
201 * Returns a textual representation given object. The default implementation
202 * in the abstract base class returns the simple name of the class, this may
203 * be overwritten to something more specific in subclasses.
204 *
205 * @param entity
206 * @return a textual representation given object.
207 */
208 public String getTypeText(Object entity){
209 return entity.getClass().getSimpleName();
210 }
211
212 public String getText(T entity) {
213 if(entity instanceof IdentifiableEntity){
214 IdentifiableEntity identifiableEntity = (IdentifiableEntity) HibernateProxyHelper.deproxy(entity);
215
216 return identifiableEntity.getTitleCache();
217 }
218
219 return "No text. Implement in subclass";
220 }
221
222 public List<T> getModel() {
223 return model;
224 }
225
226 protected boolean replaceInModel(T entity) {
227 int index = model.indexOf(entity);
228 if(index >= 0) {
229 model.set(index, entity);
230 return true;
231 } else {
232 return false;
233 }
234 }
235
236 @Override
237 public List<T> getRootEntities() {
238 return getModel();
239 }
240
241
242 @Override
243 public Map<Object, List<String>> getPropertyPathsMap() {
244 // TODO Auto-generated method stub
245 return null;
246 }
247
248 public ConversationHolder getConversation() {
249 return conversation;
250 }
251 }