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.HashMap;
|
15
|
import java.util.List;
|
16
|
import java.util.Map;
|
17
|
import java.util.Map.Entry;
|
18
|
import java.util.UUID;
|
19
|
|
20
|
import org.eclipse.jface.resource.ImageDescriptor;
|
21
|
import org.eclipse.ui.IEditorInput;
|
22
|
import org.eclipse.ui.IPersistableElement;
|
23
|
|
24
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
25
|
import eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase;
|
26
|
import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
|
27
|
import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
|
28
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
29
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
30
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
31
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
32
|
import eu.etaxonomy.cdm.strategy.merge.IMergable;
|
33
|
import eu.etaxonomy.cdm.strategy.merge.MergeException;
|
34
|
import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreator;
|
35
|
import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService;
|
36
|
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
|
37
|
import eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorSortProvider;
|
38
|
import eu.etaxonomy.taxeditor.bulkeditor.input.sortprovider.CdmBaseSortProvider;
|
39
|
import eu.etaxonomy.taxeditor.bulkeditor.input.sortprovider.TitleCacheComparator;
|
40
|
import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
|
41
|
import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
|
42
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
43
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
44
|
|
45
|
/**
|
46
|
* @author p.ciardelli
|
47
|
* @created 25.06.2009
|
48
|
* @version 1.0
|
49
|
* @param <T>
|
50
|
*/
|
51
|
public abstract class AbstractBulkEditorInput<T extends CdmBase> extends CdmEntitySessionInput implements IEditorInput ,
|
52
|
IEntityPersistenceService<T> {
|
53
|
|
54
|
private UUID entityUuid;
|
55
|
|
56
|
private List<T> model = new ArrayList<>();
|
57
|
|
58
|
private Map<T, DeleteConfiguratorBase> toDelete = new HashMap<>();
|
59
|
|
60
|
private List<T> mergeCandidates = new ArrayList<>();
|
61
|
private T mergeTarget = null;
|
62
|
|
63
|
private IEntityCreator<T> entityCreator;
|
64
|
private final ConversationHolder conversation;
|
65
|
|
66
|
public AbstractBulkEditorInput() {
|
67
|
super(true);
|
68
|
this.conversation = CdmStore.createConversation();
|
69
|
}
|
70
|
|
71
|
static public AbstractBulkEditorInput NewInstance(BulkEditorInputType inputType) {
|
72
|
|
73
|
return BulkEditorInputType.getInput(inputType);
|
74
|
}
|
75
|
|
76
|
public static AbstractBulkEditorInput NewInstance(IdentifiableEntity entity) {
|
77
|
|
78
|
BulkEditorInputType inputType = BulkEditorInputType.getByType(entity.getClass());
|
79
|
|
80
|
AbstractBulkEditorInput editorInput = NewInstance(inputType);
|
81
|
|
82
|
editorInput.setEntityUuid(entity.getUuid());
|
83
|
|
84
|
return editorInput;
|
85
|
}
|
86
|
|
87
|
protected abstract List<T> listEntities(IIdentifiableEntityServiceConfigurator configurator);
|
88
|
|
89
|
protected abstract T loadEntity(UUID entityUuid);
|
90
|
|
91
|
public void setMergeTarget(T t){
|
92
|
mergeTarget = t;
|
93
|
}
|
94
|
|
95
|
public List<T> getMergeCandidates() {
|
96
|
return mergeCandidates;
|
97
|
}
|
98
|
|
99
|
public T getMergeTarget() {
|
100
|
return mergeTarget;
|
101
|
}
|
102
|
|
103
|
public void removeMergeTarget(){
|
104
|
mergeTarget = null;
|
105
|
}
|
106
|
|
107
|
public void addMergeCandidate(T t){
|
108
|
mergeCandidates.add(t);
|
109
|
}
|
110
|
|
111
|
public void removeMergeCandidate(T t){
|
112
|
mergeCandidates.remove(t);
|
113
|
}
|
114
|
|
115
|
public void addToDelete(T t, DeleteConfiguratorBase config) {
|
116
|
toDelete.put(t, config);
|
117
|
}
|
118
|
|
119
|
private void setEntityUuid(UUID entityUuid){
|
120
|
this.entityUuid = entityUuid;
|
121
|
}
|
122
|
|
123
|
public UUID getEntityUuid() {
|
124
|
return entityUuid;
|
125
|
}
|
126
|
|
127
|
@Override
|
128
|
public boolean exists() {
|
129
|
// TODO Auto-generated method stub
|
130
|
return false;
|
131
|
}
|
132
|
|
133
|
@Override
|
134
|
public ImageDescriptor getImageDescriptor() {
|
135
|
// TODO Auto-generated method stub
|
136
|
return null;
|
137
|
}
|
138
|
|
139
|
@Override
|
140
|
public IPersistableElement getPersistable() {
|
141
|
return null;
|
142
|
}
|
143
|
|
144
|
/** {@inheritDoc} */
|
145
|
@Override
|
146
|
@SuppressWarnings("unchecked")
|
147
|
public Object getAdapter(Class adapter) {
|
148
|
return null;
|
149
|
}
|
150
|
|
151
|
public void performSearch(final BulkEditorQuery bulkEditorQuery) {
|
152
|
|
153
|
List<T> entityList = new ArrayList<T>();
|
154
|
|
155
|
if(getEntityUuid() != null){
|
156
|
|
157
|
T entity = loadEntity(getEntityUuid());
|
158
|
entityList.add(entity);
|
159
|
model = entityList;
|
160
|
}
|
161
|
else if(bulkEditorQuery != null){
|
162
|
|
163
|
IIdentifiableEntityServiceConfigurator configurator = bulkEditorQuery.getSearchConfigurator();
|
164
|
Comparator queryComparator = (bulkEditorQuery.getComparator() != null) ? bulkEditorQuery.getComparator() : new TitleCacheComparator();
|
165
|
|
166
|
entityList = listEntities(configurator);
|
167
|
|
168
|
Collections.sort(entityList, queryComparator);
|
169
|
|
170
|
|
171
|
}
|
172
|
|
173
|
model = entityList;
|
174
|
}
|
175
|
|
176
|
public boolean isMergingEnabled() {
|
177
|
return false;
|
178
|
}
|
179
|
|
180
|
public boolean isConvertingEnabled() {
|
181
|
return false;
|
182
|
}
|
183
|
|
184
|
public boolean isMarkerTypeEditingEnabled(MarkerType markerType) {
|
185
|
return false;
|
186
|
}
|
187
|
|
188
|
|
189
|
/** {@inheritDoc} */
|
190
|
@Override
|
191
|
public boolean merge(T entity, T mergeTarget) {
|
192
|
if (entity instanceof IMergable) {
|
193
|
try {
|
194
|
CdmStore.getCommonService().merge(mergeTarget.getUuid(), entity.getUuid(), (Class<? extends CdmBase>)entity.getClass());
|
195
|
} catch (MergeException e) {
|
196
|
MessagingUtils.errorDialog("Bulk Editor Merge Error",
|
197
|
this,
|
198
|
"Could not merge chosen objects of type " + entity.getClass().getName(),
|
199
|
TaxeditorBulkeditorPlugin.PLUGIN_ID,
|
200
|
e,
|
201
|
true);
|
202
|
}
|
203
|
}
|
204
|
return true;
|
205
|
}
|
206
|
|
207
|
public void saveModel(){
|
208
|
//delete entities
|
209
|
for(Entry<T, DeleteConfiguratorBase> entry:toDelete.entrySet()){
|
210
|
try {
|
211
|
delete(entry.getKey(), entry.getValue());
|
212
|
} catch (ReferencedObjectUndeletableException e) {
|
213
|
e.printStackTrace();
|
214
|
}
|
215
|
}
|
216
|
toDelete.clear();
|
217
|
//merge entities
|
218
|
for(T mergeCandidate:mergeCandidates){
|
219
|
merge(mergeCandidate, mergeTarget);
|
220
|
}
|
221
|
mergeCandidates.clear();
|
222
|
mergeTarget = null;
|
223
|
}
|
224
|
|
225
|
|
226
|
/** {@inheritDoc} */
|
227
|
@Override
|
228
|
public T create(T entity) {
|
229
|
return save(entity);
|
230
|
}
|
231
|
|
232
|
public IEntityCreator<T> getEntityCreator(){
|
233
|
if(entityCreator == null){
|
234
|
entityCreator = createEntityCreator();
|
235
|
}
|
236
|
return entityCreator;
|
237
|
}
|
238
|
|
239
|
protected abstract IEntityCreator<T> createEntityCreator();
|
240
|
|
241
|
/**
|
242
|
* The default implementation returns an empty list of sort providers.
|
243
|
* @return
|
244
|
*/
|
245
|
public List<IBulkEditorSortProvider<T>> getSortProviders(){
|
246
|
List<IBulkEditorSortProvider<T>> sortProviders = new ArrayList<IBulkEditorSortProvider<T>>();
|
247
|
|
248
|
sortProviders.add(new CdmBaseSortProvider<T>());
|
249
|
|
250
|
return sortProviders;
|
251
|
}
|
252
|
|
253
|
/**
|
254
|
* Returns a textual representation given object. The default implementation
|
255
|
* in the abstract base class returns the simple name of the class, this may
|
256
|
* be overwritten to something more specific in subclasses.
|
257
|
*
|
258
|
* @param entity
|
259
|
* @return a textual representation given object.
|
260
|
*/
|
261
|
public String getTypeText(Object entity){
|
262
|
return entity.getClass().getSimpleName();
|
263
|
}
|
264
|
|
265
|
public String getText(T entity) {
|
266
|
if(entity instanceof IdentifiableEntity){
|
267
|
IdentifiableEntity identifiableEntity = (IdentifiableEntity) HibernateProxyHelper.deproxy(entity);
|
268
|
|
269
|
return identifiableEntity.getTitleCache();
|
270
|
}
|
271
|
|
272
|
return "No text. Implement in subclass";
|
273
|
}
|
274
|
|
275
|
public List<T> getModel() {
|
276
|
return model;
|
277
|
}
|
278
|
|
279
|
protected boolean replaceInModel(T entity) {
|
280
|
int index = model.indexOf(entity);
|
281
|
if(index >= 0) {
|
282
|
model.set(index, entity);
|
283
|
return true;
|
284
|
} else {
|
285
|
return false;
|
286
|
}
|
287
|
}
|
288
|
|
289
|
@Override
|
290
|
public List<T> getRootEntities() {
|
291
|
return getModel();
|
292
|
}
|
293
|
|
294
|
|
295
|
@Override
|
296
|
public Map<Object, List<String>> getPropertyPathsMap() {
|
297
|
// TODO Auto-generated method stub
|
298
|
return null;
|
299
|
}
|
300
|
|
301
|
public ConversationHolder getConversation() {
|
302
|
return conversation;
|
303
|
}
|
304
|
}
|