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