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