ref #6932 Fix new element handler and save
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / input / AbstractBulkEditorInput.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.bulkeditor.input;
10
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.Comparator;
14 import java.util.List;
15 import java.util.Map;
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.conversation.ConversationHolder;
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.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.e4.AnnotatedTableItem;
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.editor.CdmEntitySessionInput;
39 import eu.etaxonomy.taxeditor.model.MessagingUtils;
40 import eu.etaxonomy.taxeditor.store.CdmStore;
41
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 CdmBase> extends CdmEntitySessionInput implements IEditorInput ,
49 IEntityPersistenceService<T> {
50
51 private UUID entityUuid;
52
53 private List<T> model = new ArrayList<>();
54
55 private IEntityCreator<T> entityCreator;
56 private final ConversationHolder conversation;
57
58 public AbstractBulkEditorInput() {
59 super(true);
60 this.conversation = CdmStore.createConversation();
61 }
62
63 static public AbstractBulkEditorInput NewInstance(BulkEditorInputType inputType) {
64
65 return BulkEditorInputType.getInput(inputType);
66 }
67
68 public static AbstractBulkEditorInput NewInstance(IdentifiableEntity entity) {
69
70 BulkEditorInputType inputType = BulkEditorInputType.getByType(entity.getClass());
71
72 AbstractBulkEditorInput editorInput = NewInstance(inputType);
73
74 editorInput.setEntityUuid(entity.getUuid());
75
76 return editorInput;
77 }
78
79 protected abstract List<T> listEntities(IIdentifiableEntityServiceConfigurator configurator);
80
81 protected abstract T loadEntity(UUID entityUuid);
82
83 private void setEntityUuid(UUID entityUuid){
84 this.entityUuid = entityUuid;
85 }
86
87 public UUID getEntityUuid() {
88 return entityUuid;
89 }
90
91 @Override
92 public boolean exists() {
93 // TODO Auto-generated method stub
94 return false;
95 }
96
97 @Override
98 public ImageDescriptor getImageDescriptor() {
99 // TODO Auto-generated method stub
100 return null;
101 }
102
103 @Override
104 public IPersistableElement getPersistable() {
105 return null;
106 }
107
108 /** {@inheritDoc} */
109 @Override
110 @SuppressWarnings("unchecked")
111 public Object getAdapter(Class adapter) {
112 return null;
113 }
114
115 public void performSearch(final BulkEditorQuery bulkEditorQuery) {
116
117 List<T> entityList = new ArrayList<T>();
118
119 if(getEntityUuid() != null){
120
121 T entity = loadEntity(getEntityUuid());
122 entityList.add(entity);
123 model = entityList;
124 }
125 else if(bulkEditorQuery != null){
126
127 IIdentifiableEntityServiceConfigurator configurator = bulkEditorQuery.getSearchConfigurator();
128 Comparator queryComparator = (bulkEditorQuery.getComparator() != null) ? bulkEditorQuery.getComparator() : new TitleCacheComparator();
129
130 entityList = listEntities(configurator);
131
132 Collections.sort(entityList, queryComparator);
133
134
135 }
136
137 model = entityList;
138 }
139
140 public boolean isMergingEnabled() {
141 return false;
142 }
143
144 public boolean isConvertingEnabled() {
145 return false;
146 }
147
148 public boolean isMarkerTypeEditingEnabled(MarkerType markerType) {
149 return false;
150 }
151
152
153 /** {@inheritDoc} */
154 @Override
155 public boolean merge(T entity, T mergeTarget) {
156 if (entity instanceof IMergable) {
157 try {
158 CdmStore.getCommonService().merge(mergeTarget.getUuid(), entity.getUuid(), (Class<? extends CdmBase>)entity.getClass());
159 } catch (MergeException e) {
160 MessagingUtils.errorDialog("Bulk Editor Merge Error",
161 this,
162 "Could not merge chosen objects of type " + entity.getClass().getName(),
163 TaxeditorBulkeditorPlugin.PLUGIN_ID,
164 e,
165 true);
166 }
167 }
168 return true;
169 }
170
171
172 /** {@inheritDoc} */
173 @Override
174 public T create(T entity) {
175 return save(entity);
176 }
177
178 public IEntityCreator<T> getEntityCreator(){
179 if(entityCreator == null){
180 entityCreator = createEntityCreator();
181 }
182 return entityCreator;
183 }
184
185 protected abstract IEntityCreator<T> createEntityCreator();
186
187 /**
188 * The default implementation returns an empty list of sort providers.
189 * @return
190 */
191 public List<IBulkEditorSortProvider<T>> getSortProviders(){
192 List<IBulkEditorSortProvider<T>> sortProviders = new ArrayList<IBulkEditorSortProvider<T>>();
193
194 sortProviders.add(new CdmBaseSortProvider<T>());
195
196 return sortProviders;
197 }
198
199 /**
200 * Returns a textual representation given object. The default implementation
201 * in the abstract base class returns the simple name of the class, this may
202 * be overwritten to something more specific in subclasses.
203 *
204 * @param entity
205 * @return a textual representation given object.
206 */
207 public String getTypeText(Object entity){
208 return entity.getClass().getSimpleName();
209 }
210
211 public String getText(T entity) {
212 if(entity instanceof IdentifiableEntity){
213 IdentifiableEntity identifiableEntity = (IdentifiableEntity) HibernateProxyHelper.deproxy(entity);
214
215 return identifiableEntity.getTitleCache();
216 }
217
218 return "No text. Implement in subclass";
219 }
220
221 public List<T> getModel() {
222 return model;
223 }
224
225 public List<AnnotatedTableItem<T>> getWrappedModel() {
226 List<AnnotatedTableItem<T>> wrappetItems = new ArrayList<>();
227 for(T t:model){
228 wrappetItems.add(new AnnotatedTableItem<T>(t));
229 }
230 return wrappetItems;
231 }
232
233 protected boolean replaceInModel(T entity) {
234 int index = model.indexOf(entity);
235 if(index >= 0) {
236 model.set(index, entity);
237 return true;
238 } else {
239 return false;
240 }
241 }
242
243 @Override
244 public List<T> getRootEntities() {
245 return getModel();
246 }
247
248
249 @Override
250 public Map<Object, List<String>> getPropertyPathsMap() {
251 // TODO Auto-generated method stub
252 return null;
253 }
254
255 public ConversationHolder getConversation() {
256 return conversation;
257 }
258 }