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.
10 package eu
.etaxonomy
.taxeditor
.editor
.e4
;
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
;
19 import java
.util
.Map
.Entry
;
21 import java
.util
.UUID
;
23 import org
.eclipse
.core
.commands
.ExecutionException
;
24 import org
.eclipse
.core
.commands
.operations
.IOperationHistory
;
25 import org
.eclipse
.core
.runtime
.IAdaptable
;
26 import org
.eclipse
.core
.runtime
.IStatus
;
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
;
70 public class TaxonEditorInputE4
extends CdmEntitySessionInput
implements IConversationEnabled
{
72 private static final String INCORRECT_STATE
= Messages
.TaxonEditorInput_INCORRECT_STATE
;
74 private final ConversationHolder conversation
;
76 private TaxonNode taxonNode
;
78 private Map
<TaxonBase
, TaxonBaseDeletionConfigurator
> toDeletes
= new HashMap
<>();
79 private Set
<Synonym
> toSaveNewSynonyms
= new HashSet
<>();
80 private List
<TaxonBase
> toSaveNewConcepts
= new ArrayList
<>();
81 private List
<TaxonName
> toSaveNewNames
= new ArrayList
<>();
84 private Set
<AbstractPostOperation
> operations
= new HashSet
<>();
86 private TaxonEditorInputDataChangeBehaviourE4 dataChangeBehavior
;
88 private TaxonBase
<?
> initiallySelectedTaxonBase
;
90 private UISynchronize sync
;
93 public void setSync(UISynchronize sync
) {
97 private enum CdmType
{
103 private TaxonEditorInputE4(UUID uuid
, CdmType type
) {
105 this.conversation
= CdmStore
.createConversation();
107 case PARENT_TAXON_NODE
:
108 initForParentTaxonNode(uuid
);
111 initForTaxonBase(uuid
);
114 initForTaxonNode(uuid
);
119 private void init(TaxonNode taxonNode
) {
120 this.taxonNode
= taxonNode
;
124 private void initForTaxonNode(UUID taxonNodeUuid
) {
125 this.getCdmEntitySession().bind();
126 TaxonNode taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNodeUuid
, getTaxonNodePropertyPaths());
127 // TaxonNode taxonNode = getCdmEntitySession().remoteLoad(CdmStore.getService(ITaxonNodeService.class), taxonNodeUuid, getTaxonNodePropertyPaths());
128 if(taxonNode
== null){
129 MessagingUtils
.warningDialog(Messages
.TaxonEditorInput_NOT_IMPLEMENTED
, TaxonEditorInputE4
.class, Messages
.TaxonEditorInput_NOT_IMPLEMENTED_MESSAGE
);
132 setInitiallySelectedTaxonBase(getTaxon());
136 private void initForTaxonBase(UUID taxonBaseUuid
) {
137 this.getCdmEntitySession().bind();
138 // TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).load(taxonBaseUuid, getTaxonBasePropertyPaths());
139 TaxonBase taxonBase
= getCdmEntitySession().remoteLoad(CdmStore
.getService(ITaxonService
.class), taxonBaseUuid
, getTaxonBasePropertyPaths());
140 if (taxonBase
!= null){
141 if(taxonBase
.isInstanceOf(Taxon
.class)){
142 Taxon taxon
= CdmBase
.deproxy(taxonBase
, Taxon
.class);
143 setInitiallySelectedTaxonBase(taxon
);
145 if ( taxon
.isMisapplication() || taxon
.isProparteSynonym()){
146 // TODO get accepted taxon
147 MessagingUtils
.info(Messages
.TaxonEditorInput_OPEN_MISSAPPLIED_NAME
);
149 Set
<Taxon
> acceptedTaxa
= new HashSet
<Taxon
>();
150 Set
<TaxonRelationship
> relations
= taxon
.getRelationsFromThisTaxon();
151 for(TaxonRelationship relation
: relations
){
152 if(relation
.getType().isAnyMisappliedName() || relation
.getType().isAnySynonym()){
153 acceptedTaxa
.add(relation
.getToTaxon());
156 if (taxon
.getTaxonNodes().size() > 0){
157 acceptedTaxa
.add(taxon
);
159 setInputForRelatedTaxa(conversation
, acceptedTaxa
);
162 setInputForMultipleNodes(conversation
, taxon
.getTaxonNodes());
164 }else if(taxonBase
instanceof Synonym
){
165 Synonym synonym
= (Synonym
) taxonBase
;
167 Set
<Taxon
> taxa
= new HashSet
<>();
168 Taxon taxon
= synonym
.getAcceptedTaxon();
172 setInputForMultipleTaxa(conversation
, taxa
);
173 setInitiallySelectedTaxonBase(synonym
);
179 private void initForParentTaxonNode(UUID parentNodeUuid
){
180 this.getCdmEntitySession().bind();
181 TaxonName name
= PreferencesUtil
.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
182 ITaxonTreeNode parentNode
= CdmStore
.getService(IClassificationService
.class).getTreeNodeByUuid(parentNodeUuid
);
184 Taxon newTaxon
= Taxon
.NewInstance(name
, parentNode
.getReference());
185 TaxonNode newTaxonNode
= parentNode
.addChildTaxon(newTaxon
, parentNode
.getReference(), parentNode
.getMicroReference());
187 // add the new taxon to the editors persistence context
188 UUID newTaxonNodeUuid
= CdmStore
.getService(ITaxonNodeService
.class).save(newTaxonNode
).getUuid();
190 initForTaxonNode(newTaxonNodeUuid
);
193 private void setInputForMultipleNodes(ConversationHolder conversation
, Set
<TaxonNode
> taxonNodes
){
194 if(taxonNodes
.size() == 1){
195 TaxonNode taxonNode
= taxonNodes
.iterator().next();
196 taxonNode
= getCdmEntitySession().remoteLoad(CdmStore
.getService(ITaxonNodeService
.class), taxonNode
.getUuid(), getTaxonNodePropertyPaths());
198 }else if(taxonNodes
.size() > 1){
200 TaxonNode taxonNode
= ChooseFromMultipleTaxonNodesDialog
.choose(taxonNodes
);
201 if(taxonNode
!= null){
202 taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNode
.getUuid(), getTaxonNodePropertyPaths());
204 if(taxonNode
!= null){
207 } else if (taxonNodes
.size() == 0) {
208 // this is an undesired state
209 MessagingUtils
.warningDialog(INCORRECT_STATE
,TaxonEditorInputE4
.class,Messages
.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION
);
213 private void setInputForMultipleTaxa(ConversationHolder conversation
, Set
<Taxon
> taxa
){
214 if(taxa
.size() == 1){
215 Taxon taxon
= taxa
.iterator().next();
216 Set
<TaxonNode
> nodes
= taxon
.getTaxonNodes();
217 setInputForMultipleNodes(conversation
, nodes
);
218 }else if(taxa
.size() > 1){
219 Set
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
220 for ( Taxon taxon
: taxa
){
221 taxonNodes
.addAll(taxon
.getTaxonNodes());
223 setInputForMultipleNodes(conversation
, taxonNodes
);
224 }else if(taxa
.size() == 0){
225 // this is an undesired state
226 MessagingUtils
.warningDialog(INCORRECT_STATE
, TaxonEditorInputE4
.class, Messages
.TaxonEditorInput_NO_ACCEPTED_TAXON_PRESENT
);
230 private void setInputForRelatedTaxa(ConversationHolder conversation
, Set
<Taxon
> taxa
){
231 if(taxa
.size() == 1){
232 Taxon taxon
= taxa
.iterator().next();
233 Set
<TaxonNode
> nodes
= taxon
.getTaxonNodes();
234 TaxonNode taxonNode
= null;
236 taxonNode
= ChooseFromMultipleTaxonNodesDialog
.choose(nodes
);
237 }else if (nodes
.size()==1){
238 taxonNode
= nodes
.iterator().next();
240 MessagingUtils
.warningDialog(INCORRECT_STATE
,TaxonEditorInputE4
.class,Messages
.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION
);
243 }else if(taxa
.size() > 1){
244 Iterator
<Taxon
> taxonIterator
= taxa
.iterator();
245 Set
<TaxonNode
> nodes
= new HashSet
<>();
246 while (taxonIterator
.hasNext()){
248 nodes
.addAll(taxonIterator
.next().getTaxonNodes());
250 TaxonNode taxonNode
= ChooseFromMultipleAcceptedTaxaDialog
.choose(nodes
);
251 if(taxonNode
!= null){
252 taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNode
.getUuid(), getTaxonNodePropertyPaths());
254 if(taxonNode
!= null){
257 } else if (taxa
.size() == 0) {
258 // this is an undesired state
259 MessagingUtils
.warningDialog(INCORRECT_STATE
,TaxonEditorInputE4
.class,Messages
.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION
);
263 public static TaxonEditorInputE4
NewInstance(UUID taxonNodeUuid
) {
264 return new TaxonEditorInputE4(taxonNodeUuid
, CdmType
.TAXON_NODE
);
268 public static TaxonEditorInputE4
NewInstanceFromTaxonBase(UUID taxonBaseUuid
){
269 return new TaxonEditorInputE4(taxonBaseUuid
, CdmType
.TAXON_BASE
);
273 public static TaxonEditorInputE4
NewEmptyInstance(UUID parentNodeUuid
){
274 return new TaxonEditorInputE4(parentNodeUuid
, CdmType
.PARENT_TAXON_NODE
);
277 public Object
getAdapter(Class adapter
) {
279 if (adapter
== Taxon
.class) {
283 if (adapter
== TaxonNode
.class) {
293 * Overrides equals to ensure that a taxon can only be edited by
294 * one editor at a time.
297 public boolean equals(Object obj
) {
298 if (TaxonEditorInputE4
.class.equals(obj
.getClass())
299 && getTaxon() != null
300 && getTaxon().equals(((TaxonEditorInputE4
) obj
).getTaxon())) {
301 if (((TaxonEditorInputE4
) obj
).getInitiallySelectedTaxonBase() != null){
302 setInitiallySelectedTaxonBase(((TaxonEditorInputE4
) obj
).getInitiallySelectedTaxonBase());
309 public Taxon
getTaxon(){
311 Taxon taxon
= CdmBase
.deproxy(taxonNode
.getTaxon(), Taxon
.class);
317 public TaxonNode
getTaxonNode() {
322 public ConversationHolder
getConversationHolder() {
328 public void update(CdmDataChangeMap events
) {
329 if(dataChangeBehavior
== null){
330 dataChangeBehavior
= new TaxonEditorInputDataChangeBehaviourE4(this);
333 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
336 public void setInitiallySelectedTaxonBase(TaxonBase taxonBase
) {
337 this.initiallySelectedTaxonBase
= taxonBase
;
340 public TaxonBase
getInitiallySelectedTaxonBase() {
341 return initiallySelectedTaxonBase
;
344 public Set
<AbstractPostOperation
> getOperations() {
348 public void setOperations(Set
<AbstractPostOperation
> operations
) {
349 this.operations
= operations
;
352 public void addOperation(AbstractPostOperation operation
) {
353 this.operations
.add(operation
);
356 public void addToSaveNewSynonym(Synonym toSaveNew
) {
357 this.toSaveNewSynonyms
.add(toSaveNew
);
359 public void addToSaveNewConcept(Taxon toSaveNew
) {
360 this.toSaveNewConcepts
.add(toSaveNew
);
365 public List
<TaxonNode
> getRootEntities() {
366 return Arrays
.asList(taxonNode
);
370 public void merge() {
371 if (!this.getCdmEntitySession().isActive()){
372 this.getCdmEntitySession().bind();
374 for(Entry
<TaxonBase
, TaxonBaseDeletionConfigurator
> entry
:toDeletes
.entrySet()){
375 delete(entry
.getKey(), entry
.getValue());
378 for(AbstractPostOperation entry
:operations
){
379 IStatus status
= Status
.CANCEL_STATUS
;
380 final IAdaptable uiInfoAdapter
= WorkspaceUndoUtil
381 .getUIInfoAdapter(AbstractUtility
.getShell());
382 String operationlabel
= entry
.getLabel();
384 entry
.addContext(IOperationHistory
.GLOBAL_UNDO_CONTEXT
);
385 status
= entry
.execute(null, uiInfoAdapter
);
386 } catch (ExecutionException e
) {
388 MessagingUtils
.operationDialog(AbstractUtility
.class, e
, TaxeditorStorePlugin
.PLUGIN_ID
, operationlabel
, null);
392 String statusString
= status
.equals(Status
.OK_STATUS
) ?
"completed"
395 IPostOperationEnabled postOperationEnabled
= entry
396 .getPostOperationEnabled();
397 if (postOperationEnabled
!= null) {
398 postOperationEnabled
.onComplete();
402 //AbstractUtility.executeOperation(entry,sync);
406 for (Synonym syn
: toSaveNewSynonyms
){
407 for (HybridRelationship rel
: syn
.getName().getHybridChildRelations()){
408 // if (!rel.getParentName().isPersited()) {
409 toSaveNewNames
.add(rel
.getParentName());
411 // if (!rel.getHybridName().isPersited()) {
412 toSaveNewNames
.add(rel
.getHybridName());
418 for (TaxonBase concept
: toSaveNewConcepts
){
419 for (HybridRelationship rel
: concept
.getName().getHybridChildRelations()){
420 // if (!rel.getParentName().isPersited()) {
421 toSaveNewNames
.add(rel
.getParentName());
423 // if (!rel.getHybridName().isPersited()) {
424 toSaveNewNames
.add(rel
.getHybridName());
429 CdmStore
.getService(INameService
.class).merge(toSaveNewNames
, true);
430 CdmStore
.getService(ITaxonService
.class).merge(toSaveNewConcepts
, true);
432 toSaveNewNames
.clear();
433 toSaveNewConcepts
.clear();
435 CdmStore
.getService(ITaxonNodeService
.class).merge(taxonNode
, true);
444 private void delete(TaxonBase key
, TaxonBaseDeletionConfigurator value
) {
445 if (key
instanceof Synonym
){
446 CdmStore
.getService(ITaxonService
.class).deleteSynonym(((Synonym
)key
).getUuid(), (SynonymDeletionConfigurator
) value
);
448 CdmStore
.getService(ITaxonService
.class).deleteTaxon(((Taxon
)key
).getUuid(), (TaxonDeletionConfigurator
) value
, null);
454 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
458 private List
<String
> getTaxonNodePropertyPaths() {
459 List
<String
> taxonNodePropertyPaths
= new ArrayList
<String
>();
460 for(String propertyPath
: getTaxonBasePropertyPaths()) {
461 taxonNodePropertyPaths
.add("taxon." + propertyPath
); //$NON-NLS-1$
463 return taxonNodePropertyPaths
;
466 private List
<String
> getTaxonBasePropertyPaths() {
467 List
<String
> taxonBasePropertyPaths
= Arrays
.asList(new String
[] {
469 "createdBy", //$NON-NLS-1$
470 "updatedBy", //$NON-NLS-1$
471 "annotations", //$NON-NLS-1$
472 "markers", //$NON-NLS-1$
473 "credits", //$NON-NLS-1$
474 "extensions", //$NON-NLS-1$
475 "rights", //$NON-NLS-1$
476 "sources", //$NON-NLS-1$
478 "descriptions", //$NON-NLS-1$
479 "taxonNodes", //$NON-NLS-1$
480 "descriptions.descriptionElements.feature", //$NON-NLS-1$
481 "descriptions.descriptionElements.area", //$NON-NLS-1$
482 "descriptions.descriptionElements.status", //$NON-NLS-1$
483 "descriptions.markers", //$NON-NLS-1$
484 "name.descriptions", //$NON-NLS-1$
485 "name.typeDesignations.typeSpecimen.derivedFrom.originals", //$NON-NLS-1$
486 "name.status", //$NON-NLS-1$
487 "name.nomenclaturalReference.inReference", //$NON-NLS-1$
488 "name.taxonBases.taxonNodes", //$NON-NLS-1$
489 "name.relationsFromThisName", //$NON-NLS-1$
490 "name.relationsToThisName", //$NON-NLS-1$
491 "name.homotypicalGroup.typifiedNames.taxonBases.synonyms.synonym.name.status", //$NON-NLS-1$
492 "name.homotypicalGroup.typifiedNames.relationsToThisName.fromName", //$NON-NLS-1$
493 "synonyms.name.status.type", //$NON-NLS-1$
494 "synonyms.name.relationsToThisName.fromName", //$NON-NLS-1$
495 "synonyms.name.nomenclaturalReference.inReference.authorship", //$NON-NLS-1$
496 "synonyms.name.nomenclaturalReference.authorship", //$NON-NLS-1$
497 "synonyms.name.homotypicalGroup.typifiedNames.taxonBases.synonyms" //$NON-NLS-1$
500 return taxonBasePropertyPaths
;
504 * @param selectedElement
505 * @param deleteConfig
507 public void addTaxonBaseToDelete(TaxonBase selectedElement
, TaxonBaseDeletionConfigurator deleteConfig
) {
508 this.toDeletes
.put(selectedElement
, deleteConfig
);
515 public void addToSaveNewName(TaxonName newName
) {
516 this.toSaveNewNames
.add(newName
);