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