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.Collection;
|
13
|
import java.util.Comparator;
|
14
|
import java.util.HashMap;
|
15
|
import java.util.HashSet;
|
16
|
import java.util.List;
|
17
|
import java.util.Map;
|
18
|
import java.util.Map.Entry;
|
19
|
import java.util.Set;
|
20
|
import java.util.UUID;
|
21
|
import java.util.stream.Collectors;
|
22
|
|
23
|
import org.eclipse.core.runtime.ICoreRunnable;
|
24
|
import org.eclipse.core.runtime.jobs.Job;
|
25
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
26
|
|
27
|
import ca.odell.glazedlists.BasicEventList;
|
28
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
29
|
import eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase;
|
30
|
import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
|
31
|
import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
|
32
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
33
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
34
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
35
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
36
|
import eu.etaxonomy.cdm.persistence.dto.MergeResult;
|
37
|
import eu.etaxonomy.cdm.strategy.merge.IMergable;
|
38
|
import eu.etaxonomy.cdm.strategy.merge.MergeException;
|
39
|
import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreator;
|
40
|
import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService;
|
41
|
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
|
42
|
import eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorSortProvider;
|
43
|
import eu.etaxonomy.taxeditor.bulkeditor.input.sortprovider.CdmBaseSortProvider;
|
44
|
import eu.etaxonomy.taxeditor.bulkeditor.input.sortprovider.TitleCacheComparator;
|
45
|
import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
|
46
|
import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
|
47
|
import eu.etaxonomy.taxeditor.event.EventUtility;
|
48
|
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
|
49
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
50
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
51
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
52
|
|
53
|
/**
|
54
|
* @author p.ciardelli
|
55
|
* @created 25.06.2009
|
56
|
*/
|
57
|
public abstract class AbstractBulkEditorInput<T extends CdmBase>
|
58
|
extends CdmEntitySessionInput<T>
|
59
|
implements IEntityPersistenceService<T> {
|
60
|
|
61
|
private static final String PROPERTY_PROTECTED_TITLECACHE = "Protect TitleCache";
|
62
|
private static final String TYPE_PROPERTY = Messages.BulkEditorE4_TYPE;
|
63
|
private static final String ID_PROPERTY = "Id"; //$NON-NLS-1$
|
64
|
private static final String UUID_PROPERTY = "Uuid"; //$NON-NLS-1$
|
65
|
private static final String CREATED_BY_PROPERTY = "Created by"; //$NON-NLS-1$
|
66
|
|
67
|
private UUID entityUuid;
|
68
|
|
69
|
private BasicEventList<T> model = new BasicEventList<>();
|
70
|
|
71
|
private Map<T, DeleteConfiguratorBase> toDelete = new HashMap<>();
|
72
|
public Map<T, DeleteConfiguratorBase> getToDelete() {
|
73
|
return toDelete;
|
74
|
}
|
75
|
|
76
|
private Set<T> saveCandidates = new HashSet<>();
|
77
|
|
78
|
private Set<T> markedMergeCandidates = new HashSet<>();
|
79
|
private T markedMergeTarget = null;
|
80
|
|
81
|
private HashMap<T, Set<T>> mergedEntities = new HashMap<>();
|
82
|
|
83
|
private IEntityCreator<T> entityCreator;
|
84
|
private final ConversationHolder conversation;
|
85
|
|
86
|
private Job searchJob;
|
87
|
|
88
|
public AbstractBulkEditorInput() {
|
89
|
super(true);
|
90
|
this.conversation = CdmStore.createConversation();
|
91
|
}
|
92
|
|
93
|
static public AbstractBulkEditorInput<?> NewInstance(BulkEditorInputType inputType) {
|
94
|
|
95
|
return BulkEditorInputType.getInput(inputType);
|
96
|
}
|
97
|
|
98
|
public static AbstractBulkEditorInput<?> NewInstance(IdentifiableEntity entity) {
|
99
|
|
100
|
BulkEditorInputType inputType = BulkEditorInputType.getByType(entity.getClass());
|
101
|
AbstractBulkEditorInput<?> editorInput = NewInstance(inputType);
|
102
|
editorInput.setEntityUuid(entity.getUuid());
|
103
|
return editorInput;
|
104
|
}
|
105
|
|
106
|
public static AbstractBulkEditorInput<?> NewInstance(Class clazz, UUID uuid) {
|
107
|
|
108
|
BulkEditorInputType inputType = BulkEditorInputType.getByType(clazz);
|
109
|
AbstractBulkEditorInput<?> editorInput = NewInstance(inputType);
|
110
|
editorInput.setEntityUuid(uuid);
|
111
|
return editorInput;
|
112
|
}
|
113
|
|
114
|
public abstract String getName();
|
115
|
|
116
|
public String getEditorName(){
|
117
|
return getName();
|
118
|
}
|
119
|
|
120
|
protected int getPageSize(){
|
121
|
return 100;
|
122
|
}
|
123
|
|
124
|
protected abstract List<T> listEntities(IIdentifiableEntityServiceConfigurator configurator);
|
125
|
|
126
|
protected abstract long countEntities(IIdentifiableEntityServiceConfigurator configurator);
|
127
|
|
128
|
protected abstract T loadEntity(UUID entityUuid);
|
129
|
|
130
|
public List<String> getPropertyKeys(){
|
131
|
List<String> properties = new ArrayList<>();
|
132
|
properties.add(getName());
|
133
|
properties.add(PROPERTY_PROTECTED_TITLECACHE);
|
134
|
properties.addAll(getPropertyKeys_internal());
|
135
|
properties.add(TYPE_PROPERTY);
|
136
|
properties.add(CREATED_BY_PROPERTY);
|
137
|
properties.add(ID_PROPERTY);
|
138
|
properties.add(UUID_PROPERTY);
|
139
|
|
140
|
return properties;
|
141
|
}
|
142
|
|
143
|
protected abstract List<String> getPropertyKeys_internal();
|
144
|
|
145
|
|
146
|
public Object getPropertyValue(T cdmBase, String property){
|
147
|
if(property.equals(getName())){
|
148
|
return getText(cdmBase);
|
149
|
}
|
150
|
else if(property.equals(PROPERTY_PROTECTED_TITLECACHE)
|
151
|
&&cdmBase.isInstanceOf(IdentifiableEntity.class)){
|
152
|
return HibernateProxyHelper.deproxy(cdmBase, IdentifiableEntity.class).isProtectedTitleCache();
|
153
|
}
|
154
|
else if(property.equals(TYPE_PROPERTY)){
|
155
|
return getTypeText(cdmBase);
|
156
|
}
|
157
|
else if(property.equals(UUID_PROPERTY)){
|
158
|
return cdmBase.getUuid();
|
159
|
}
|
160
|
else if(property.equals(ID_PROPERTY)){
|
161
|
return cdmBase.getId();
|
162
|
}
|
163
|
else if(property.equals(CREATED_BY_PROPERTY)){
|
164
|
if (cdmBase.getCreatedBy() != null){
|
165
|
return cdmBase.getCreatedBy().getUsername();
|
166
|
} else {
|
167
|
return null;
|
168
|
}
|
169
|
}
|
170
|
return null;
|
171
|
}
|
172
|
|
173
|
public boolean isBooleanProperty(String property) {
|
174
|
if(property.equals(PROPERTY_PROTECTED_TITLECACHE)){
|
175
|
return true;
|
176
|
}
|
177
|
return false;
|
178
|
}
|
179
|
|
180
|
public boolean isCacheProperty(String property) {
|
181
|
if(property.equals(PROPERTY_PROTECTED_TITLECACHE)){
|
182
|
return true;
|
183
|
}
|
184
|
return false;
|
185
|
}
|
186
|
|
187
|
public <T extends IdentifiableEntity> Comparator<T> getTitleComparator(){
|
188
|
return new TitleCacheComparator<T>();
|
189
|
}
|
190
|
|
191
|
public void setMergeTarget(T t){
|
192
|
markedMergeTarget = t;
|
193
|
}
|
194
|
|
195
|
public Set<T> getMergeCandidates() {
|
196
|
return markedMergeCandidates;
|
197
|
}
|
198
|
|
199
|
public T getMergeTarget() {
|
200
|
return markedMergeTarget;
|
201
|
}
|
202
|
|
203
|
public void removeMergeTarget(){
|
204
|
markedMergeTarget = null;
|
205
|
}
|
206
|
|
207
|
public void addMergeCandidate(T t){
|
208
|
markedMergeCandidates.add(t);
|
209
|
}
|
210
|
|
211
|
public void removeMergeCandidate(T t){
|
212
|
markedMergeCandidates.remove(t);
|
213
|
}
|
214
|
|
215
|
public void addToDelete(T t, DeleteConfiguratorBase config) {
|
216
|
toDelete.put(t, config);
|
217
|
}
|
218
|
public void addSaveCandidate(T t){
|
219
|
saveCandidates.add(t);
|
220
|
}
|
221
|
public void setEntityUuid(UUID entityUuid){
|
222
|
this.entityUuid = entityUuid;
|
223
|
}
|
224
|
|
225
|
public UUID getEntityUuid() {
|
226
|
return entityUuid;
|
227
|
}
|
228
|
|
229
|
public void performSearch(final BulkEditorQuery bulkEditorQuery, IStructuredSelection selection) {
|
230
|
//cancel previous search job
|
231
|
if(searchJob!=null && searchJob.getState()!=Job.NONE){
|
232
|
boolean isCanceled = searchJob.cancel();
|
233
|
if (!isCanceled){
|
234
|
while (!isCanceled){
|
235
|
try {
|
236
|
Thread.sleep(200);
|
237
|
} catch (InterruptedException e) {
|
238
|
}
|
239
|
isCanceled = searchJob.cancel();
|
240
|
}
|
241
|
}
|
242
|
searchJob = null;
|
243
|
// /*
|
244
|
// * wait for a little while for the job to finish
|
245
|
// * to avoid asynchronously loaded results of the
|
246
|
// * previous search being shown in the next search
|
247
|
// * (not critical but explicitly waiting for the job to finish
|
248
|
// * could run into an endless loop by mistake)
|
249
|
// */
|
250
|
// try {
|
251
|
// Thread.sleep(500);
|
252
|
// } catch (InterruptedException e) {
|
253
|
// }
|
254
|
}
|
255
|
model.clear();
|
256
|
markedMergeCandidates.clear();
|
257
|
markedMergeTarget = null;
|
258
|
|
259
|
if(getEntityUuid() != null){
|
260
|
T entity = loadEntity(getEntityUuid());
|
261
|
model.add(entity);
|
262
|
setEntityUuid(null);
|
263
|
}
|
264
|
else if(bulkEditorQuery != null){
|
265
|
IIdentifiableEntityServiceConfigurator<?> configurator = bulkEditorQuery.getSearchConfigurator();
|
266
|
|
267
|
// check for UUID search
|
268
|
String titleSearchString = configurator.getTitleSearchString();
|
269
|
try {
|
270
|
UUID uuid = UUID.fromString(titleSearchString);
|
271
|
T entity = loadEntity(uuid);
|
272
|
//UUID search found -> add entity to list if not null and return
|
273
|
if (entity != null){
|
274
|
model.add(entity);
|
275
|
}
|
276
|
return;
|
277
|
} catch (IllegalArgumentException e) {
|
278
|
// search string was no UUID
|
279
|
}
|
280
|
//-> continue with standard search
|
281
|
|
282
|
int pageSize = configurator.getPageSize()!=null?configurator.getPageSize():getPageSize();
|
283
|
configurator.setPageSize(pageSize);
|
284
|
long count = countEntities(configurator);
|
285
|
int totalWork = count>Integer.MAX_VALUE?Integer.MAX_VALUE:(int)count;
|
286
|
String jobLabel = String.format(Messages.AbstractBulkEditorInput_LOADING, getName(), bulkEditorQuery.getSearchString());
|
287
|
searchJob = Job.create(jobLabel, (ICoreRunnable) monitor -> {
|
288
|
monitor.beginTask(jobLabel, totalWork);
|
289
|
int pageNumber = 0;
|
290
|
List<T> entities;
|
291
|
|
292
|
//load previously selected element
|
293
|
UUID selectedUuid = null;
|
294
|
if(selection!=null && selection.getFirstElement() instanceof CdmBase){
|
295
|
selectedUuid = ((CdmBase) selection.getFirstElement()).getUuid();
|
296
|
T entity = loadEntity(selectedUuid);
|
297
|
model.add(entity);
|
298
|
}
|
299
|
|
300
|
do {
|
301
|
if (monitor.isCanceled()) {
|
302
|
break;
|
303
|
}
|
304
|
configurator.setPageNumber(pageNumber);
|
305
|
entities = listEntities(configurator);
|
306
|
|
307
|
addToModel(entities, selectedUuid);
|
308
|
|
309
|
pageNumber++;
|
310
|
monitor.worked(pageSize);
|
311
|
long workedLong = pageSize*pageNumber;
|
312
|
int loadedCount = workedLong>Integer.MAX_VALUE?Integer.MAX_VALUE:(int)workedLong;
|
313
|
monitor.setTaskName(String.format(Messages.AbstractBulkEditorInput_LOADED, loadedCount, totalWork, getName()));
|
314
|
|
315
|
//Update selection
|
316
|
EventUtility.postAsyncEvent(WorkbenchEventConstants.BULK_EDITOR_SEARCH_FINISHED, selection);
|
317
|
} while (!entities.isEmpty());
|
318
|
monitor.done();
|
319
|
EventUtility.postAsyncEvent(WorkbenchEventConstants.BULK_EDITOR_SEARCH_FINISHED, selection);
|
320
|
});
|
321
|
searchJob.schedule();
|
322
|
}
|
323
|
}
|
324
|
|
325
|
private void addToModel(List<T> entities, UUID selectedUuid){
|
326
|
//filter pre-loaded previously selected element
|
327
|
if(selectedUuid!=null){
|
328
|
entities = entities.stream().filter(entity->!entity.getUuid().equals(selectedUuid)).collect(Collectors.toList());
|
329
|
}
|
330
|
/*
|
331
|
* IMPORTANT!
|
332
|
* Entities have to be loaded into the main session because they are
|
333
|
* loaded in a parallel asynchronous thread
|
334
|
*/
|
335
|
if(getCdmEntitySession()!=null){//is null when closing the bulk editor during loading
|
336
|
getCdmEntitySession().load(entities, true);
|
337
|
}
|
338
|
model.addAll(entities);
|
339
|
}
|
340
|
|
341
|
public boolean isMergingEnabled() {
|
342
|
return false;
|
343
|
}
|
344
|
|
345
|
public boolean isConvertingEnabled() {
|
346
|
return false;
|
347
|
}
|
348
|
|
349
|
public boolean isMarkerTypeEditingEnabled(MarkerType markerType) {
|
350
|
return false;
|
351
|
}
|
352
|
|
353
|
@Override
|
354
|
public boolean merge(T entity, T mergeTarget) {
|
355
|
if (entity instanceof IMergable) {
|
356
|
try {
|
357
|
CdmStore.getCommonService().merge(mergeTarget.getUuid(), entity.getUuid(), (Class<? extends CdmBase>)entity.getClass());
|
358
|
} catch (MergeException e) {
|
359
|
MessagingUtils.errorDialog(Messages.AbstractBulkEditorInput_MERGE_ERROR_TITLE,
|
360
|
this,
|
361
|
String.format(Messages.AbstractBulkEditorInput_MERGE_ERROR_MESSAGE, entity.getClass().getName()),
|
362
|
TaxeditorBulkeditorPlugin.PLUGIN_ID,
|
363
|
e,
|
364
|
true);
|
365
|
}
|
366
|
}
|
367
|
return true;
|
368
|
}
|
369
|
|
370
|
public void saveModel(){
|
371
|
saveModel(true);
|
372
|
}
|
373
|
|
374
|
public void saveModel(boolean resetMerge){
|
375
|
//delete entities
|
376
|
for(Entry<T, DeleteConfiguratorBase> entry:toDelete.entrySet()){
|
377
|
try {
|
378
|
delete(entry.getKey(), entry.getValue());
|
379
|
} catch (ReferencedObjectUndeletableException e) {
|
380
|
e.printStackTrace();
|
381
|
}
|
382
|
}
|
383
|
if (!saveCandidates.isEmpty()){
|
384
|
List<MergeResult<T>> results = CdmStore.getService(saveCandidates.iterator().next()).merge(new ArrayList<>(saveCandidates), true);
|
385
|
for (MergeResult<T> result: results){
|
386
|
if (result.getMergedEntity() != null){
|
387
|
T entity = result.getMergedEntity();
|
388
|
}
|
389
|
}
|
390
|
}
|
391
|
if(resetMerge){
|
392
|
//merge entities
|
393
|
for(T mergeTarget:mergedEntities.keySet()){
|
394
|
for (T mergeCandidate: mergedEntities.get(mergeTarget)){
|
395
|
merge(mergeCandidate, mergeTarget);
|
396
|
}
|
397
|
}
|
398
|
}
|
399
|
toDelete.clear();
|
400
|
saveCandidates.clear();
|
401
|
mergedEntities.clear();
|
402
|
}
|
403
|
|
404
|
@Override
|
405
|
public T create(T entity) {
|
406
|
return save(entity);
|
407
|
}
|
408
|
|
409
|
public IEntityCreator<T> getEntityCreator(){
|
410
|
if(entityCreator == null){
|
411
|
entityCreator = createEntityCreator();
|
412
|
}
|
413
|
return entityCreator;
|
414
|
}
|
415
|
|
416
|
protected abstract IEntityCreator<T> createEntityCreator();
|
417
|
|
418
|
/**
|
419
|
* The default implementation returns an empty list of sort providers.
|
420
|
* @return
|
421
|
*/
|
422
|
public List<IBulkEditorSortProvider<T>> getSortProviders(){
|
423
|
List<IBulkEditorSortProvider<T>> sortProviders = new ArrayList<>();
|
424
|
|
425
|
sortProviders.add(new CdmBaseSortProvider<>());
|
426
|
|
427
|
return sortProviders;
|
428
|
}
|
429
|
|
430
|
/**
|
431
|
* Returns a textual representation given object. The default implementation
|
432
|
* in the abstract base class returns the simple name of the class, this may
|
433
|
* be overwritten to something more specific in subclasses.
|
434
|
*
|
435
|
* @param entity
|
436
|
* @return a textual representation given object.
|
437
|
*/
|
438
|
public String getTypeText(Object entity){
|
439
|
return entity.getClass().getSimpleName();
|
440
|
}
|
441
|
|
442
|
public String getText(T entity) {
|
443
|
if(entity.isInstanceOf(IdentifiableEntity.class)){
|
444
|
IdentifiableEntity<?> identifiableEntity = HibernateProxyHelper.deproxy(entity, IdentifiableEntity.class);
|
445
|
String text = identifiableEntity.getTitleCache();
|
446
|
return text;
|
447
|
}
|
448
|
|
449
|
return "No text. Implement in subclass"; //$NON-NLS-1$
|
450
|
}
|
451
|
|
452
|
public BasicEventList<T> getModel() {
|
453
|
return model;
|
454
|
}
|
455
|
|
456
|
public boolean replaceInModel(T entity) {
|
457
|
int index = model.indexOf(entity);
|
458
|
if(index >= 0) {
|
459
|
model.set(index, entity);
|
460
|
return true;
|
461
|
} else {
|
462
|
return false;
|
463
|
}
|
464
|
}
|
465
|
|
466
|
@Override
|
467
|
public Collection<T> getRootEntities() {
|
468
|
return getModel();
|
469
|
}
|
470
|
|
471
|
@Override
|
472
|
public Map<Object, List<String>> getPropertyPathsMap() {
|
473
|
// TODO Auto-generated method stub
|
474
|
return null;
|
475
|
}
|
476
|
|
477
|
public ConversationHolder getConversation() {
|
478
|
return conversation;
|
479
|
}
|
480
|
|
481
|
public Set<T> getSaveCandidates() {
|
482
|
return saveCandidates;
|
483
|
}
|
484
|
|
485
|
public HashMap<T, Set<T>> getMergedEntities() {
|
486
|
return mergedEntities;
|
487
|
}
|
488
|
|
489
|
public void setMergedEntities(HashMap<T, Set<T>> mergedEntities) {
|
490
|
this.mergedEntities = mergedEntities;
|
491
|
}
|
492
|
|
493
|
}
|