2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.editor
.e4
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Arrays
;
13 import java
.util
.HashMap
;
14 import java
.util
.HashSet
;
15 import java
.util
.Iterator
;
16 import java
.util
.List
;
18 import java
.util
.Map
.Entry
;
20 import java
.util
.UUID
;
22 import org
.eclipse
.core
.commands
.ExecutionException
;
23 import org
.eclipse
.core
.commands
.operations
.IOperationHistory
;
24 import org
.eclipse
.core
.runtime
.IAdaptable
;
25 import org
.eclipse
.core
.runtime
.IStatus
;
26 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
27 import org
.eclipse
.core
.runtime
.Status
;
28 import org
.eclipse
.e4
.ui
.di
.UISynchronize
;
29 import org
.eclipse
.ui
.ide
.undo
.WorkspaceUndoUtil
;
31 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
32 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
33 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
34 import eu
.etaxonomy
.cdm
.api
.service
.INameService
;
35 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonNodeService
;
36 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
37 import eu
.etaxonomy
.cdm
.api
.service
.config
.SynonymDeletionConfigurator
;
38 import eu
.etaxonomy
.cdm
.api
.service
.config
.TaxonBaseDeletionConfigurator
;
39 import eu
.etaxonomy
.cdm
.api
.service
.config
.TaxonDeletionConfigurator
;
40 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
41 import eu
.etaxonomy
.cdm
.model
.name
.HybridRelationship
;
42 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
43 import eu
.etaxonomy
.cdm
.model
.taxon
.ITaxonTreeNode
;
44 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
45 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
46 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
47 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
48 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
49 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
50 import eu
.etaxonomy
.taxeditor
.editor
.CdmEntitySessionInput
;
51 import eu
.etaxonomy
.taxeditor
.editor
.ChooseFromMultipleAcceptedTaxaDialog
;
52 import eu
.etaxonomy
.taxeditor
.editor
.ChooseFromMultipleTaxonNodesDialog
;
53 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
54 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
55 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
56 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
57 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
58 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
59 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
60 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
61 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
67 public class TaxonEditorInputE4
extends CdmEntitySessionInput
<TaxonNode
>
68 implements IConversationEnabled
{
70 private static final String INCORRECT_STATE
= Messages
.TaxonEditorInput_INCORRECT_STATE
;
72 private final ConversationHolder conversation
;
74 private TaxonNode taxonNode
;
76 private Map
<TaxonBase
, TaxonBaseDeletionConfigurator
> toDeletes
= new HashMap
<>();
77 private Set
<Synonym
> toSaveNewSynonyms
= new HashSet
<>();
78 private List
<TaxonBase
> toSaveConcepts
= new ArrayList
<>();
79 private List
<TaxonName
> toSaveNewNames
= new ArrayList
<>();
81 private Set
<AbstractPostOperation
> operations
= new HashSet
<>();
83 private TaxonEditorInputDataChangeBehaviourE4 dataChangeBehavior
;
85 private TaxonBase
<?
> initiallySelectedTaxonBase
;
87 private UISynchronize sync
;
90 public void setSync(UISynchronize sync
) {
94 private enum CdmType
{
100 private TaxonEditorInputE4(UUID uuid
, CdmType type
) {
102 this.conversation
= CdmStore
.createConversation();
104 case PARENT_TAXON_NODE
:
105 initForParentTaxonNode(uuid
);
108 initForTaxonBase(uuid
);
111 initForTaxonNode(uuid
);
116 private void init(TaxonNode taxonNode
) {
117 this.taxonNode
= taxonNode
;
120 private void initForTaxonNode(UUID taxonNodeUuid
) {
121 this.getCdmEntitySession().bind();
122 TaxonNode taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNodeUuid
, getTaxonNodePropertyPaths());
123 // TaxonNode taxonNode = getCdmEntitySession().remoteLoad(CdmStore.getService(ITaxonNodeService.class), taxonNodeUuid, getTaxonNodePropertyPaths());
124 if(taxonNode
== null){
125 MessagingUtils
.warningDialog(Messages
.TaxonEditorInput_NOT_IMPLEMENTED
, TaxonEditorInputE4
.class, Messages
.TaxonEditorInput_NOT_IMPLEMENTED_MESSAGE
);
128 setInitiallySelectedTaxonBase(getTaxon());
132 private void initForTaxonBase(UUID taxonBaseUuid
) {
133 this.getCdmEntitySession().bind();
134 // TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).load(taxonBaseUuid, getTaxonBasePropertyPaths());
135 TaxonBase
<?
> taxonBase
= getCdmEntitySession().remoteLoad(CdmStore
.getService(ITaxonService
.class), taxonBaseUuid
, getTaxonBasePropertyPaths());
136 if (taxonBase
!= null){
137 if(taxonBase
.isInstanceOf(Taxon
.class)){
138 Taxon taxon
= CdmBase
.deproxy(taxonBase
, Taxon
.class);
139 setInitiallySelectedTaxonBase(taxon
);
141 if ( taxon
.isMisapplication() || taxon
.isProparteSynonym()){
142 // TODO get accepted taxon
143 MessagingUtils
.info(Messages
.TaxonEditorInput_OPEN_MISSAPPLIED_NAME
);
145 Set
<Taxon
> acceptedTaxa
= new HashSet
<Taxon
>();
146 Set
<TaxonRelationship
> relations
= taxon
.getRelationsFromThisTaxon();
147 for(TaxonRelationship relation
: relations
){
148 if(relation
.getType().isAnyMisappliedName() || relation
.getType().isAnySynonym()){
149 acceptedTaxa
.add(relation
.getToTaxon());
152 if (taxon
.getTaxonNodes().size() > 0){
153 acceptedTaxa
.add(taxon
);
155 setInputForRelatedTaxa(conversation
, acceptedTaxa
);
158 setInputForMultipleNodes(conversation
, taxon
.getTaxonNodes());
160 }else if(taxonBase
instanceof Synonym
){
161 Synonym synonym
= (Synonym
) taxonBase
;
163 Set
<Taxon
> taxa
= new HashSet
<>();
164 Taxon taxon
= synonym
.getAcceptedTaxon();
168 setInputForMultipleTaxa(conversation
, taxa
);
169 setInitiallySelectedTaxonBase(synonym
);
175 private void initForParentTaxonNode(UUID parentNodeUuid
){
176 this.getCdmEntitySession().bind();
177 TaxonName name
= PreferencesUtil
.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
178 ITaxonTreeNode parentNode
= CdmStore
.getService(IClassificationService
.class).getTreeNodeByUuid(parentNodeUuid
);
180 Taxon newTaxon
= Taxon
.NewInstance(name
, parentNode
.getReference());
181 TaxonNode newTaxonNode
= parentNode
.addChildTaxon(newTaxon
, parentNode
.getReference(), parentNode
.getMicroReference());
183 // add the new taxon to the editors persistence context
184 UUID newTaxonNodeUuid
= CdmStore
.getService(ITaxonNodeService
.class).save(newTaxonNode
).getUuid();
186 initForTaxonNode(newTaxonNodeUuid
);
189 private void setInputForMultipleNodes(ConversationHolder conversation
, Set
<TaxonNode
> taxonNodes
){
190 if(taxonNodes
.size() == 1){
191 TaxonNode taxonNode
= taxonNodes
.iterator().next();
192 taxonNode
= getCdmEntitySession().remoteLoad(CdmStore
.getService(ITaxonNodeService
.class), taxonNode
.getUuid(), getTaxonNodePropertyPaths());
194 }else if(taxonNodes
.size() > 1){
196 TaxonNode taxonNode
= ChooseFromMultipleTaxonNodesDialog
.choose(taxonNodes
);
197 if(taxonNode
!= null){
198 taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNode
.getUuid(), getTaxonNodePropertyPaths());
200 if(taxonNode
!= null){
203 } else if (taxonNodes
.size() == 0) {
204 // this is an undesired state
205 MessagingUtils
.warningDialog(INCORRECT_STATE
,TaxonEditorInputE4
.class,Messages
.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION
);
209 private void setInputForMultipleTaxa(ConversationHolder conversation
, Set
<Taxon
> taxa
){
210 if(taxa
.size() == 1){
211 Taxon taxon
= taxa
.iterator().next();
212 Set
<TaxonNode
> nodes
= taxon
.getTaxonNodes();
213 setInputForMultipleNodes(conversation
, nodes
);
214 }else if(taxa
.size() > 1){
215 Set
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
216 for ( Taxon taxon
: taxa
){
217 taxonNodes
.addAll(taxon
.getTaxonNodes());
219 setInputForMultipleNodes(conversation
, taxonNodes
);
220 }else if(taxa
.size() == 0){
221 // this is an undesired state
222 MessagingUtils
.warningDialog(INCORRECT_STATE
, TaxonEditorInputE4
.class, Messages
.TaxonEditorInput_NO_ACCEPTED_TAXON_PRESENT
);
226 private void setInputForRelatedTaxa(ConversationHolder conversation
, Set
<Taxon
> taxa
){
227 if(taxa
.size() == 1){
228 Taxon taxon
= taxa
.iterator().next();
229 Set
<TaxonNode
> nodes
= taxon
.getTaxonNodes();
230 TaxonNode taxonNode
= null;
232 taxonNode
= ChooseFromMultipleTaxonNodesDialog
.choose(nodes
);
233 }else if (nodes
.size()==1){
234 taxonNode
= nodes
.iterator().next();
236 MessagingUtils
.warningDialog(INCORRECT_STATE
,TaxonEditorInputE4
.class,Messages
.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION
);
239 }else if(taxa
.size() > 1){
240 Iterator
<Taxon
> taxonIterator
= taxa
.iterator();
241 Set
<TaxonNode
> nodes
= new HashSet
<>();
242 while (taxonIterator
.hasNext()){
244 nodes
.addAll(taxonIterator
.next().getTaxonNodes());
246 TaxonNode taxonNode
= ChooseFromMultipleAcceptedTaxaDialog
.choose(nodes
);
247 if(taxonNode
!= null){
248 taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNode
.getUuid(), getTaxonNodePropertyPaths());
250 if(taxonNode
!= null){
253 } else if (taxa
.size() == 0) {
254 // this is an undesired state
255 MessagingUtils
.warningDialog(INCORRECT_STATE
,TaxonEditorInputE4
.class,Messages
.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION
);
259 public static TaxonEditorInputE4
NewInstance(UUID taxonNodeUuid
) {
260 return new TaxonEditorInputE4(taxonNodeUuid
, CdmType
.TAXON_NODE
);
264 public static TaxonEditorInputE4
NewInstanceFromTaxonBase(UUID taxonBaseUuid
){
265 return new TaxonEditorInputE4(taxonBaseUuid
, CdmType
.TAXON_BASE
);
269 public static TaxonEditorInputE4
NewEmptyInstance(UUID parentNodeUuid
){
270 return new TaxonEditorInputE4(parentNodeUuid
, CdmType
.PARENT_TAXON_NODE
);
273 public Object
getAdapter(Class adapter
) {
275 if (adapter
== Taxon
.class) {
279 if (adapter
== TaxonNode
.class) {
289 * Overrides equals to ensure that a taxon can only be edited by
290 * one editor at a time.
293 public boolean equals(Object obj
) {
294 if (TaxonEditorInputE4
.class.equals(obj
.getClass())
295 && getTaxon() != null
296 && getTaxon().equals(((TaxonEditorInputE4
) obj
).getTaxon())) {
297 if (((TaxonEditorInputE4
) obj
).getInitiallySelectedTaxonBase() != null){
298 setInitiallySelectedTaxonBase(((TaxonEditorInputE4
) obj
).getInitiallySelectedTaxonBase());
305 public Taxon
getTaxon(){
307 Taxon taxon
= CdmBase
.deproxy(taxonNode
.getTaxon(), Taxon
.class);
313 public TaxonNode
getTaxonNode() {
318 public ConversationHolder
getConversationHolder() {
323 public void update(CdmDataChangeMap events
) {
324 if(dataChangeBehavior
== null){
325 dataChangeBehavior
= new TaxonEditorInputDataChangeBehaviourE4(this);
328 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
331 public void setInitiallySelectedTaxonBase(TaxonBase taxonBase
) {
332 this.initiallySelectedTaxonBase
= taxonBase
;
335 public TaxonBase
getInitiallySelectedTaxonBase() {
336 return initiallySelectedTaxonBase
;
339 public void addOperation(AbstractPostOperation operation
) {
340 this.operations
.add(operation
);
343 public void addToSaveNewSynonym(Synonym toSaveNew
) {
344 this.toSaveNewSynonyms
.add(toSaveNew
);
346 public void addToSaveConcept(Taxon toSaveNew
) {
347 this.toSaveConcepts
.add(toSaveNew
);
351 public List
<TaxonNode
> getRootEntities() {
352 return Arrays
.asList(taxonNode
);
356 public void merge() {
357 if (!this.getCdmEntitySession().isActive()){
358 this.getCdmEntitySession().bind();
360 for(Entry
<TaxonBase
, TaxonBaseDeletionConfigurator
> entry
:toDeletes
.entrySet()){
361 delete(entry
.getKey(), entry
.getValue());
365 // new concepts needs to be saved separately, the list contains all concepts, the
366 for (TaxonBase
<?
> concept
: toSaveConcepts
){
367 if (concept
!= null){
368 if (concept
.getName() != null){
369 for (HybridRelationship rel
: concept
.getName().getHybridChildRelations()){
370 toSaveNewNames
.add(rel
.getParentName());
371 toSaveNewNames
.add(rel
.getHybridName());
377 //handle cascading for hybrid relationshis
379 if (taxonNode
.getTaxon().getName() != null){
380 for (HybridRelationship rel
: taxonNode
.getTaxon().getName().getHybridChildRelations()){
381 toSaveNewNames
.add(rel
.getParentName());
382 toSaveNewNames
.add(rel
.getHybridName());
386 for (TaxonName synonymName
: taxonNode
.getTaxon().getSynonymNames()){
387 if (synonymName
!= null){
388 for (HybridRelationship rel
: synonymName
.getHybridChildRelations()){
389 toSaveNewNames
.add(rel
.getParentName());
390 toSaveNewNames
.add(rel
.getHybridName());
394 // for (TaxonName name:toSaveNewNames){
398 if (!toSaveNewNames
.isEmpty()){
399 CdmStore
.getService(INameService
.class).merge(toSaveNewNames
, true);
401 if (!toSaveConcepts
.isEmpty()){
402 CdmStore
.getService(ITaxonService
.class).merge(toSaveConcepts
, true);
405 toSaveNewNames
.clear();
406 toSaveConcepts
.clear();
408 for(AbstractPostOperation
<?
> entry
:operations
){
409 IStatus status
= Status
.CANCEL_STATUS
;
410 final IAdaptable uiInfoAdapter
= WorkspaceUndoUtil
411 .getUIInfoAdapter(AbstractUtility
.getShell());
412 String operationlabel
= entry
.getLabel();
414 entry
.addContext(IOperationHistory
.GLOBAL_UNDO_CONTEXT
);
415 status
= entry
.execute(new NullProgressMonitor(), uiInfoAdapter
);
416 } catch (ExecutionException e
) {
417 MessagingUtils
.operationDialog(AbstractUtility
.class, e
, TaxeditorStorePlugin
.PLUGIN_ID
, operationlabel
, null);
420 IPostOperationEnabled postOperationEnabled
= entry
421 .getPostOperationEnabled();
422 if (postOperationEnabled
!= null) {
423 postOperationEnabled
.onComplete();
426 //AbstractUtility.executeOperation(entry,sync);
430 CdmStore
.getService(ITaxonNodeService
.class).merge(taxonNode
, true);
433 private void delete(TaxonBase key
, TaxonBaseDeletionConfigurator value
) {
434 if (key
instanceof Synonym
){
435 CdmStore
.getService(ITaxonService
.class).deleteSynonym(((Synonym
)key
).getUuid(), (SynonymDeletionConfigurator
) value
);
437 CdmStore
.getService(ITaxonService
.class).deleteTaxon(((Taxon
)key
).getUuid(), (TaxonDeletionConfigurator
) value
, null);
442 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
446 private List
<String
> getTaxonNodePropertyPaths() {
447 List
<String
> taxonNodePropertyPaths
= new ArrayList
<String
>();
448 for(String propertyPath
: getTaxonBasePropertyPaths()) {
449 taxonNodePropertyPaths
.add("taxon." + propertyPath
); //$NON-NLS-1$
451 return taxonNodePropertyPaths
;
454 private List
<String
> getTaxonBasePropertyPaths() {
455 List
<String
> taxonBasePropertyPaths
= Arrays
.asList(new String
[] {
457 "createdBy", //$NON-NLS-1$
458 "updatedBy", //$NON-NLS-1$
459 "annotations", //$NON-NLS-1$
460 "markers", //$NON-NLS-1$
461 "credits", //$NON-NLS-1$
462 "extensions", //$NON-NLS-1$
463 "rights", //$NON-NLS-1$
464 "sources", //$NON-NLS-1$
465 "sources.links", //$NON-NLS-1$
467 "descriptions", //$NON-NLS-1$
468 "taxonNodes", //$NON-NLS-1$
469 "descriptions.descriptionElements.feature", //$NON-NLS-1$
470 "descriptions.descriptionElements.area", //$NON-NLS-1$
471 "descriptions.descriptionElements.status", //$NON-NLS-1$
472 "descriptions.descriptionElements.markers", //$NON-NLS-1$
473 "descriptions.descriptionElements.sources", //$NON-NLS-1$
474 "descriptions.descriptionElements.annotations", //$NON-NLS-1$
475 "descriptions.markers", //$NON-NLS-1$
476 "descriptions.descriptionSources", //$NON-NLS-1$
477 "descriptions.sources", //$NON-NLS-1$
478 "descriptions.annotations", //$NON-NLS-1$
479 "descriptions.links", //$NON-NLS-1$
480 "name.descriptions", //$NON-NLS-1$
481 "name.typeDesignations.typeSpecimen.derivedFrom.originals", //$NON-NLS-1$
482 "name.typeDesignations.source", //$NON-NLS-1$
483 "name.typeDesignations.source.nameUsedInSource", //$NON-NLS-1$
484 "name.typeDesignations.source.citation", //$NON-NLS-1$
485 "name.typeDesignations.source.links", //$NON-NLS-1$
486 "name.typeDesignations.text", //$NON-NLS-1$
487 "name.typeDesignations.sources", //$NON-NLS-1$
488 "name.typeDesignations.sources.links", //$NON-NLS-1$
489 "name.status", //$NON-NLS-1$
490 "name.nomenclaturalSource", //$NON-NLS-1$
491 "name.nomenclaturalSource.nameUsedInSource", //$NON-NLS-1$
492 "name.nomenclaturalSource.links.description", //$NON-NLS-1$
493 "name.nomenclaturalSource.citation", //$NON-NLS-1$
494 "name.nomenclaturalSource.citation.inReference", //$NON-NLS-1$
495 "name.nomenclaturalSource.cdmSource", //$NON-NLS-1$
496 "name.nomenclaturalSource.cdmSource.description", //$NON-NLS-1$
497 "name.status.source", //$NON-NLS-1$
498 "name.status.source.links", //$NON-NLS-1$
499 "name.taxonBases.taxonNodes", //$NON-NLS-1$
500 "name.relationsFromThisName", //$NON-NLS-1$
501 "name.relationsFromThisName.source", //$NON-NLS-1$
502 "name.relationsFromThisName.source.links", //$NON-NLS-1$
503 "name.relationsToThisName", //$NON-NLS-1$
504 "name.relationsToThisName.source", //$NON-NLS-1$
505 "name.relationsToThisName.source.links", //$NON-NLS-1$
506 "name.homotypicalGroup.typifiedNames.taxonBases.synonyms.synonym.name.status", //$NON-NLS-1$
507 "name.homotypicalGroup.typifiedNames.relationsToThisName.fromName", //$NON-NLS-1$
508 "name.rank.vocabulary", //$NON-NLS-1$
509 "synonyms.name.status.type", //$NON-NLS-1$
510 "synonyms.name.relationsToThisName.relatedFrom", //$NON-NLS-1$
511 "synonyms.name.relationsFromThisName.relatedTo", //$NON-NLS-1$
512 "synonyms.name.nomenclaturalSource", //$NON-NLS-1$
513 "synonyms.name.nomenclaturalSource.nameUsedInSource", //$NON-NLS-1$
514 "synonyms.name.nomenclaturalSource.links", //$NON-NLS-1$
515 "synonyms.name.nomenclaturalSource.citation", //$NON-NLS-1$
516 "synonyms.name.nomenclaturalSource.citation.inReference", //$NON-NLS-1$
517 "synonyms.name.nomenclaturalSource.cdmSource", //$NON-NLS-1$
518 "synonyms.name.nomenclaturalSource.cdmSource.description", //$NON-NLS-1$
519 "synonyms.name.nomenclaturalSource.citation.inReference.authorship", //$NON-NLS-1$
520 "synonyms.name.nomenclaturalSource.citation.authorship", //$NON-NLS-1$
521 "synonyms.name.homotypicalGroup.typifiedNames.taxonBases.synonyms", //$NON-NLS-1$
522 "relationsFromThisTaxon",//$NON-NLS-1$
523 "relationsToThisTaxon"//$NON-NLS-1$
527 return taxonBasePropertyPaths
;
530 public void addTaxonBaseToDelete(TaxonBase selectedElement
, TaxonBaseDeletionConfigurator deleteConfig
) {
531 this.toDeletes
.put(selectedElement
, deleteConfig
);
535 public void addToSaveNewName(TaxonName newName
) {
536 this.toSaveNewNames
.add(newName
);