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
|
|
10
|
package eu.etaxonomy.taxeditor.editor.e4;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.Arrays;
|
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
|
|
22
|
import org.eclipse.e4.ui.di.UISynchronize;
|
23
|
|
24
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
25
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
26
|
import eu.etaxonomy.cdm.api.service.IClassificationService;
|
27
|
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
|
28
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
29
|
import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
|
30
|
import eu.etaxonomy.cdm.api.service.config.TaxonBaseDeletionConfigurator;
|
31
|
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
|
32
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
33
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
34
|
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
|
35
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
36
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
37
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
38
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
39
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
|
40
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
41
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
42
|
import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
|
43
|
import eu.etaxonomy.taxeditor.editor.ChooseFromMultipleTaxonNodesDialog;
|
44
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
45
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
46
|
import eu.etaxonomy.taxeditor.model.DataChangeBridge;
|
47
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
48
|
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
|
49
|
import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
|
50
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
51
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
52
|
|
53
|
|
54
|
/**
|
55
|
*
|
56
|
* @author pplitzner
|
57
|
* @date Aug 24, 2017
|
58
|
*
|
59
|
*/
|
60
|
public class TaxonEditorInputE4 extends CdmEntitySessionInput implements IConversationEnabled {
|
61
|
|
62
|
private static final String INCORRECT_STATE = Messages.TaxonEditorInput_INCORRECT_STATE;
|
63
|
|
64
|
private final ConversationHolder conversation;
|
65
|
|
66
|
private TaxonNode taxonNode;
|
67
|
|
68
|
private Map<TaxonBase, TaxonBaseDeletionConfigurator> toDelete = new HashMap<>();
|
69
|
|
70
|
private Set<AbstractPostTaxonOperation> operations = new HashSet<>();
|
71
|
|
72
|
private TaxonEditorInputDataChangeBehaviourE4 dataChangeBehavior;
|
73
|
|
74
|
private TaxonBase initiallySelectedTaxonBase;
|
75
|
|
76
|
UISynchronize sync;
|
77
|
|
78
|
|
79
|
public void setSync(UISynchronize sync) {
|
80
|
this.sync = sync;
|
81
|
}
|
82
|
|
83
|
private enum CdmType {
|
84
|
TAXON_NODE,
|
85
|
TAXON_BASE,
|
86
|
PARENT_TAXON_NODE
|
87
|
}
|
88
|
|
89
|
private TaxonEditorInputE4(UUID uuid, CdmType type) {
|
90
|
super(true);
|
91
|
this.conversation = CdmStore.createConversation();
|
92
|
switch(type) {
|
93
|
case PARENT_TAXON_NODE:
|
94
|
initForParentTaxonNode(uuid);
|
95
|
break;
|
96
|
case TAXON_BASE:
|
97
|
initForTaxonBase(uuid);
|
98
|
break;
|
99
|
case TAXON_NODE:
|
100
|
initForTaxonNode(uuid);
|
101
|
break;
|
102
|
}
|
103
|
}
|
104
|
|
105
|
private void init(TaxonNode taxonNode) {
|
106
|
this.taxonNode = taxonNode;
|
107
|
}
|
108
|
|
109
|
|
110
|
private void initForTaxonNode(UUID taxonNodeUuid) {
|
111
|
this.getCdmEntitySession().bind();
|
112
|
TaxonNode taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNodeUuid, getTaxonNodePropertyPaths());
|
113
|
if(taxonNode == null){
|
114
|
MessagingUtils.warningDialog(Messages.TaxonEditorInput_NOT_IMPLEMENTED, TaxonEditorInputE4.class, Messages.TaxonEditorInput_NOT_IMPLEMENTED_MESSAGE);
|
115
|
}
|
116
|
init(taxonNode);
|
117
|
setInitiallySelectedTaxonBase(getTaxon());
|
118
|
|
119
|
}
|
120
|
|
121
|
private void initForTaxonBase(UUID taxonBaseUuid) {
|
122
|
this.getCdmEntitySession().bind();
|
123
|
TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).load(taxonBaseUuid, getTaxonBasePropertyPaths());
|
124
|
if (taxonBase != null){
|
125
|
if(taxonBase.isInstanceOf(Taxon.class)){
|
126
|
Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
|
127
|
setInitiallySelectedTaxonBase(taxon);
|
128
|
|
129
|
if (taxon.getTaxonNodes().size() == 0 && taxon.isMisapplication()){
|
130
|
// TODO get accepted taxon
|
131
|
MessagingUtils.info(Messages.TaxonEditorInput_OPEN_MISSAPPLIED_NAME);
|
132
|
|
133
|
Set<Taxon> acceptedTaxa = new HashSet<Taxon>();
|
134
|
Set<TaxonRelationship> relations = taxon.getRelationsFromThisTaxon();
|
135
|
for(TaxonRelationship relation : relations){
|
136
|
if(relation.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
|
137
|
acceptedTaxa.add(relation.getToTaxon());
|
138
|
}
|
139
|
}
|
140
|
setInputForMultipleTaxa(conversation, acceptedTaxa);
|
141
|
|
142
|
}else{
|
143
|
setInputForMultipleNodes(conversation, taxon.getTaxonNodes());
|
144
|
}
|
145
|
}else if(taxonBase instanceof Synonym){
|
146
|
Synonym synonym = (Synonym) taxonBase;
|
147
|
|
148
|
Set<Taxon> taxa = new HashSet<>();
|
149
|
Taxon taxon = synonym.getAcceptedTaxon();
|
150
|
if (taxon != null){
|
151
|
taxa.add(taxon);
|
152
|
}
|
153
|
setInputForMultipleTaxa(conversation, taxa);
|
154
|
setInitiallySelectedTaxonBase(synonym);
|
155
|
}
|
156
|
}
|
157
|
}
|
158
|
|
159
|
|
160
|
private void initForParentTaxonNode(UUID parentNodeUuid){
|
161
|
this.getCdmEntitySession().bind();
|
162
|
TaxonName name = PreferencesUtil.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
|
163
|
ITaxonTreeNode parentNode = CdmStore.getService(IClassificationService.class).getTreeNodeByUuid(parentNodeUuid);
|
164
|
|
165
|
Taxon newTaxon = Taxon.NewInstance(name, parentNode.getReference());
|
166
|
TaxonNode newTaxonNode = parentNode.addChildTaxon(newTaxon, parentNode.getReference(), parentNode.getMicroReference());
|
167
|
|
168
|
// add the new taxon to the editors persistence context
|
169
|
UUID newTaxonNodeUuid = CdmStore.getService(ITaxonNodeService.class).save(newTaxonNode).getUuid();
|
170
|
|
171
|
initForTaxonNode(newTaxonNodeUuid);
|
172
|
}
|
173
|
|
174
|
private void setInputForMultipleNodes(ConversationHolder conversation, Set<TaxonNode> taxonNodes){
|
175
|
if(taxonNodes.size() == 1){
|
176
|
TaxonNode taxonNode = taxonNodes.iterator().next();
|
177
|
taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid(), getTaxonNodePropertyPaths());
|
178
|
init(taxonNode);
|
179
|
}else if(taxonNodes.size() > 1){
|
180
|
TaxonNode taxonNode = ChooseFromMultipleTaxonNodesDialog.choose(taxonNodes);
|
181
|
taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid(), getTaxonNodePropertyPaths());
|
182
|
if(taxonNode != null){
|
183
|
init(taxonNode);
|
184
|
}
|
185
|
} else if (taxonNodes.size() == 0) {
|
186
|
// this is an undesired state
|
187
|
MessagingUtils.warningDialog(INCORRECT_STATE,TaxonEditorInputE4.class,Messages.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION);
|
188
|
}
|
189
|
}
|
190
|
|
191
|
private void setInputForMultipleTaxa(ConversationHolder conversation, Set<Taxon> taxa){
|
192
|
if(taxa.size() == 1){
|
193
|
Taxon taxon = taxa.iterator().next();
|
194
|
Set<TaxonNode> nodes = taxon.getTaxonNodes();
|
195
|
setInputForMultipleNodes(conversation, nodes);
|
196
|
}else if(taxa.size() > 1){
|
197
|
Set<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
|
198
|
for ( Taxon taxon : taxa ){
|
199
|
taxonNodes.addAll(taxon.getTaxonNodes());
|
200
|
}
|
201
|
setInputForMultipleNodes(conversation, taxonNodes);
|
202
|
}else if(taxa.size() == 0){
|
203
|
// this is an undesired state
|
204
|
MessagingUtils.warningDialog(INCORRECT_STATE, TaxonEditorInputE4.class, Messages.TaxonEditorInput_NO_ACCEPTED_TAXON_PRESENT);
|
205
|
}
|
206
|
}
|
207
|
|
208
|
public static TaxonEditorInputE4 NewInstance(UUID taxonNodeUuid) {
|
209
|
return new TaxonEditorInputE4(taxonNodeUuid, CdmType.TAXON_NODE);
|
210
|
|
211
|
}
|
212
|
|
213
|
public static TaxonEditorInputE4 NewInstanceFromTaxonBase(UUID taxonBaseUuid){
|
214
|
return new TaxonEditorInputE4(taxonBaseUuid, CdmType.TAXON_BASE);
|
215
|
}
|
216
|
|
217
|
|
218
|
public static TaxonEditorInputE4 NewEmptyInstance(UUID parentNodeUuid){
|
219
|
return new TaxonEditorInputE4(parentNodeUuid, CdmType.PARENT_TAXON_NODE);
|
220
|
}
|
221
|
|
222
|
public Object getAdapter(Class adapter) {
|
223
|
|
224
|
if (adapter == Taxon.class) {
|
225
|
return getTaxon();
|
226
|
}
|
227
|
|
228
|
if (adapter == TaxonNode.class) {
|
229
|
return taxonNode;
|
230
|
}
|
231
|
|
232
|
return null;
|
233
|
}
|
234
|
|
235
|
/**
|
236
|
* {@inheritDoc}
|
237
|
*
|
238
|
* Overrides equals to ensure that a taxon can only be edited by
|
239
|
* one editor at a time.
|
240
|
*/
|
241
|
@Override
|
242
|
public boolean equals(Object obj) {
|
243
|
if (TaxonEditorInputE4.class.equals(obj.getClass())
|
244
|
&& getTaxon() != null
|
245
|
&& getTaxon().equals(((TaxonEditorInputE4) obj).getTaxon())) {
|
246
|
if (((TaxonEditorInputE4) obj).getInitiallySelectedTaxonBase() != null){
|
247
|
setInitiallySelectedTaxonBase(((TaxonEditorInputE4) obj).getInitiallySelectedTaxonBase());
|
248
|
}
|
249
|
return true;
|
250
|
}
|
251
|
return false;
|
252
|
}
|
253
|
|
254
|
public Taxon getTaxon(){
|
255
|
if(taxonNode!=null){
|
256
|
Taxon taxon = CdmBase.deproxy(taxonNode.getTaxon(), Taxon.class);
|
257
|
return taxon;
|
258
|
}
|
259
|
return null;
|
260
|
}
|
261
|
|
262
|
public TaxonNode getTaxonNode() {
|
263
|
return taxonNode;
|
264
|
}
|
265
|
|
266
|
@Override
|
267
|
public ConversationHolder getConversationHolder() {
|
268
|
return conversation;
|
269
|
}
|
270
|
|
271
|
/** {@inheritDoc} */
|
272
|
@Override
|
273
|
public void update(CdmDataChangeMap events) {
|
274
|
if(dataChangeBehavior == null){
|
275
|
dataChangeBehavior = new TaxonEditorInputDataChangeBehaviourE4(this);
|
276
|
}
|
277
|
|
278
|
DataChangeBridge.handleDataChange(events, dataChangeBehavior);
|
279
|
}
|
280
|
|
281
|
public void setInitiallySelectedTaxonBase(TaxonBase taxonBase) {
|
282
|
this.initiallySelectedTaxonBase = taxonBase;
|
283
|
}
|
284
|
|
285
|
public TaxonBase getInitiallySelectedTaxonBase() {
|
286
|
return initiallySelectedTaxonBase;
|
287
|
}
|
288
|
|
289
|
public Set<AbstractPostTaxonOperation> getOperations() {
|
290
|
return operations;
|
291
|
}
|
292
|
|
293
|
public void setOperations(Set<AbstractPostTaxonOperation> operations) {
|
294
|
this.operations = operations;
|
295
|
}
|
296
|
|
297
|
public void addOperation(AbstractPostTaxonOperation operation) {
|
298
|
this.operations.add(operation);
|
299
|
}
|
300
|
|
301
|
@Override
|
302
|
public List<TaxonNode> getRootEntities() {
|
303
|
return Arrays.asList(taxonNode);
|
304
|
}
|
305
|
|
306
|
@Override
|
307
|
public void merge() {
|
308
|
if (!this.getCdmEntitySession().isActive()){
|
309
|
this.getCdmEntitySession().bind();
|
310
|
}
|
311
|
for(Entry<TaxonBase, TaxonBaseDeletionConfigurator> entry:toDelete.entrySet()){
|
312
|
delete(entry.getKey(), entry.getValue());
|
313
|
}
|
314
|
toDelete.clear();
|
315
|
for(AbstractPostOperation entry:operations){
|
316
|
EditorUtil.executeOperation(entry,sync);
|
317
|
}
|
318
|
operations.clear();
|
319
|
|
320
|
CdmStore.getService(ITaxonNodeService.class).merge(taxonNode, true);
|
321
|
|
322
|
}
|
323
|
|
324
|
/**
|
325
|
* @param key
|
326
|
* @param value
|
327
|
*/
|
328
|
private void delete(TaxonBase key, TaxonBaseDeletionConfigurator value) {
|
329
|
if (key instanceof Synonym){
|
330
|
CdmStore.getService(ITaxonService.class).deleteSynonym(((Synonym)key).getUuid(), (SynonymDeletionConfigurator) value);
|
331
|
}else{
|
332
|
CdmStore.getService(ITaxonService.class).deleteTaxon(((Taxon)key).getUuid(), (TaxonDeletionConfigurator) value, null);
|
333
|
}
|
334
|
|
335
|
}
|
336
|
|
337
|
@Override
|
338
|
public Map<Object, List<String>> getPropertyPathsMap() {
|
339
|
return null;
|
340
|
}
|
341
|
|
342
|
private List<String> getTaxonNodePropertyPaths() {
|
343
|
List<String> taxonNodePropertyPaths = new ArrayList<String>();
|
344
|
for(String propertyPath : getTaxonBasePropertyPaths()) {
|
345
|
taxonNodePropertyPaths.add("taxon." + propertyPath); //$NON-NLS-1$
|
346
|
}
|
347
|
return taxonNodePropertyPaths;
|
348
|
}
|
349
|
|
350
|
private List<String> getTaxonBasePropertyPaths() {
|
351
|
List<String> taxonBasePropertyPaths = Arrays.asList(new String[] {
|
352
|
"sec", //$NON-NLS-1$
|
353
|
"createdBy", //$NON-NLS-1$
|
354
|
"updatedBy", //$NON-NLS-1$
|
355
|
"annotations", //$NON-NLS-1$
|
356
|
"markers", //$NON-NLS-1$
|
357
|
"credits", //$NON-NLS-1$
|
358
|
"extensions", //$NON-NLS-1$
|
359
|
"rights", //$NON-NLS-1$
|
360
|
"sources", //$NON-NLS-1$
|
361
|
"identifiers",
|
362
|
"descriptions", //$NON-NLS-1$
|
363
|
"relationsToThisTaxon.relatedFrom.name.taxonBases.taxonNodes", //$NON-NLS-1$
|
364
|
"relationsToThisTaxon.relatedFrom.name.taxonBases.relationsFromThisTaxon", //$NON-NLS-1$
|
365
|
"relationsToThisTaxon.relatedFrom.name.taxonBases.relationsToThisTaxon", //$NON-NLS-1$
|
366
|
"relationsFromThisTaxon.relatedTo.name.taxonBases.taxonNodes", //$NON-NLS-1$
|
367
|
"relationsFromThisTaxon.relatedTo.name.taxonBases.relationsFromThisTaxon", //$NON-NLS-1$
|
368
|
"relationsFromThisTaxon.relatedTo.name.taxonBases.relationsToThisTaxon", //$NON-NLS-1$
|
369
|
"taxonNodes", //$NON-NLS-1$
|
370
|
"descriptions.descriptionElements.feature", //$NON-NLS-1$
|
371
|
"descriptions.descriptionElements.area", //$NON-NLS-1$
|
372
|
"descriptions.descriptionElements.status", //$NON-NLS-1$
|
373
|
"descriptions.markers", //$NON-NLS-1$
|
374
|
"name.descriptions", //$NON-NLS-1$
|
375
|
"name.typeDesignations", //$NON-NLS-1$
|
376
|
"name.status", //$NON-NLS-1$
|
377
|
"name.nomenclaturalReference.inReference", //$NON-NLS-1$
|
378
|
"name.taxonBases.taxonNodes", //$NON-NLS-1$
|
379
|
"name.relationsFromThisName", //$NON-NLS-1$
|
380
|
"name.relationsToThisName", //$NON-NLS-1$
|
381
|
"name.homotypicalGroup.typifiedNames.taxonBases.synonyms.synonym.name.status", //$NON-NLS-1$
|
382
|
"name.homotypicalGroup.typifiedNames.relationsToThisName.fromName", //$NON-NLS-1$
|
383
|
"synonyms.name.status.type", //$NON-NLS-1$
|
384
|
"synonyms.name.relationsToThisName.fromName", //$NON-NLS-1$
|
385
|
"synonyms.name.nomenclaturalReference.inReference.authorship", //$NON-NLS-1$
|
386
|
"synonyms.name.nomenclaturalReference.authorship", //$NON-NLS-1$
|
387
|
// "synonyms.name.homotypicalGroup", //$NON-NLS-1$
|
388
|
// "synonyms.synonym.name.homotypicalGroup.typifiedNames", //$NON-NLS-1$
|
389
|
"synonyms.name.homotypicalGroup.typifiedNames.taxonBases.synonyms" //$NON-NLS-1$
|
390
|
});
|
391
|
|
392
|
return taxonBasePropertyPaths;
|
393
|
}
|
394
|
|
395
|
/**
|
396
|
* @param selectedElement
|
397
|
* @param deleteConfig
|
398
|
*/
|
399
|
public void addTaxonBaseToDelete(TaxonBase selectedElement, TaxonBaseDeletionConfigurator deleteConfig) {
|
400
|
this.toDelete.put(selectedElement, deleteConfig);
|
401
|
|
402
|
}
|
403
|
|
404
|
}
|