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
.e4
.ui
.di
.UISynchronize
;
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
;
63 public class TaxonEditorInputE4
extends CdmEntitySessionInput
implements IConversationEnabled
{
65 private static final String INCORRECT_STATE
= Messages
.TaxonEditorInput_INCORRECT_STATE
;
67 private final ConversationHolder conversation
;
69 private TaxonNode taxonNode
;
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
<>();
77 private Set
<AbstractPostTaxonOperation
> operations
= new HashSet
<>();
79 private TaxonEditorInputDataChangeBehaviourE4 dataChangeBehavior
;
81 private TaxonBase
<?
> initiallySelectedTaxonBase
;
83 private UISynchronize sync
;
86 public void setSync(UISynchronize sync
) {
90 private enum CdmType
{
96 private TaxonEditorInputE4(UUID uuid
, CdmType type
) {
98 this.conversation
= CdmStore
.createConversation();
100 case PARENT_TAXON_NODE
:
101 initForParentTaxonNode(uuid
);
104 initForTaxonBase(uuid
);
107 initForTaxonNode(uuid
);
112 private void init(TaxonNode taxonNode
) {
113 this.taxonNode
= taxonNode
;
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
);
125 setInitiallySelectedTaxonBase(getTaxon());
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
);
138 if ( taxon
.isMisapplication() || taxon
.isProparteSynonym()){
139 // TODO get accepted taxon
140 MessagingUtils
.info(Messages
.TaxonEditorInput_OPEN_MISSAPPLIED_NAME
);
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());
149 if (taxon
.getTaxonNodes().size() > 0){
150 acceptedTaxa
.add(taxon
);
152 setInputForRelatedTaxa(conversation
, acceptedTaxa
);
155 setInputForMultipleNodes(conversation
, taxon
.getTaxonNodes());
157 }else if(taxonBase
instanceof Synonym
){
158 Synonym synonym
= (Synonym
) taxonBase
;
160 Set
<Taxon
> taxa
= new HashSet
<>();
161 Taxon taxon
= synonym
.getAcceptedTaxon();
165 setInputForMultipleTaxa(conversation
, taxa
);
166 setInitiallySelectedTaxonBase(synonym
);
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
);
177 Taxon newTaxon
= Taxon
.NewInstance(name
, parentNode
.getReference());
178 TaxonNode newTaxonNode
= parentNode
.addChildTaxon(newTaxon
, parentNode
.getReference(), parentNode
.getMicroReference());
180 // add the new taxon to the editors persistence context
181 UUID newTaxonNodeUuid
= CdmStore
.getService(ITaxonNodeService
.class).save(newTaxonNode
).getUuid();
183 initForTaxonNode(newTaxonNodeUuid
);
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());
191 }else if(taxonNodes
.size() > 1){
193 TaxonNode taxonNode
= ChooseFromMultipleTaxonNodesDialog
.choose(taxonNodes
);
194 if(taxonNode
!= null){
195 taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNode
.getUuid(), getTaxonNodePropertyPaths());
197 if(taxonNode
!= null){
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
);
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());
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
);
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;
229 taxonNode
= ChooseFromMultipleTaxonNodesDialog
.choose(nodes
);
230 }else if (nodes
.size()==1){
231 taxonNode
= nodes
.iterator().next();
233 MessagingUtils
.warningDialog(INCORRECT_STATE
,TaxonEditorInputE4
.class,Messages
.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION
);
236 }else if(taxa
.size() > 1){
237 Iterator
<Taxon
> taxonIterator
= taxa
.iterator();
238 Set
<TaxonNode
> nodes
= new HashSet
<>();
239 while (taxonIterator
.hasNext()){
241 nodes
.addAll(taxonIterator
.next().getTaxonNodes());
243 TaxonNode taxonNode
= ChooseFromMultipleAcceptedTaxaDialog
.choose(nodes
);
244 if(taxonNode
!= null){
245 taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNode
.getUuid(), getTaxonNodePropertyPaths());
247 if(taxonNode
!= null){
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
);
256 public static TaxonEditorInputE4
NewInstance(UUID taxonNodeUuid
) {
257 return new TaxonEditorInputE4(taxonNodeUuid
, CdmType
.TAXON_NODE
);
261 public static TaxonEditorInputE4
NewInstanceFromTaxonBase(UUID taxonBaseUuid
){
262 return new TaxonEditorInputE4(taxonBaseUuid
, CdmType
.TAXON_BASE
);
266 public static TaxonEditorInputE4
NewEmptyInstance(UUID parentNodeUuid
){
267 return new TaxonEditorInputE4(parentNodeUuid
, CdmType
.PARENT_TAXON_NODE
);
270 public Object
getAdapter(Class adapter
) {
272 if (adapter
== Taxon
.class) {
276 if (adapter
== TaxonNode
.class) {
286 * Overrides equals to ensure that a taxon can only be edited by
287 * one editor at a time.
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());
302 public Taxon
getTaxon(){
304 Taxon taxon
= CdmBase
.deproxy(taxonNode
.getTaxon(), Taxon
.class);
310 public TaxonNode
getTaxonNode() {
315 public ConversationHolder
getConversationHolder() {
321 public void update(CdmDataChangeMap events
) {
322 if(dataChangeBehavior
== null){
323 dataChangeBehavior
= new TaxonEditorInputDataChangeBehaviourE4(this);
326 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
329 public void setInitiallySelectedTaxonBase(TaxonBase taxonBase
) {
330 this.initiallySelectedTaxonBase
= taxonBase
;
333 public TaxonBase
getInitiallySelectedTaxonBase() {
334 return initiallySelectedTaxonBase
;
337 public Set
<AbstractPostTaxonOperation
> getOperations() {
341 public void setOperations(Set
<AbstractPostTaxonOperation
> operations
) {
342 this.operations
= operations
;
345 public void addOperation(AbstractPostTaxonOperation operation
) {
346 this.operations
.add(operation
);
349 public void addToSaveNewSynonym(Synonym toSaveNew
) {
350 this.toSaveNewSynonyms
.add(toSaveNew
);
352 public void addToSaveNewConcept(Taxon toSaveNew
) {
353 this.toSaveNewConcepts
.add(toSaveNew
);
358 public List
<TaxonNode
> getRootEntities() {
359 return Arrays
.asList(taxonNode
);
363 public void merge() {
364 if (!this.getCdmEntitySession().isActive()){
365 this.getCdmEntitySession().bind();
367 for(Entry
<TaxonBase
, TaxonBaseDeletionConfigurator
> entry
:toDeletes
.entrySet()){
368 delete(entry
.getKey(), entry
.getValue());
371 for(AbstractPostOperation entry
:operations
){
372 EditorUtil
.executeOperation(entry
,sync
);
376 for (Synonym syn
: toSaveNewSynonyms
){
377 for (HybridRelationship rel
: syn
.getName().getHybridChildRelations()){
378 // if (!rel.getParentName().isPersited()) {
379 toSaveNewNames
.add(rel
.getParentName());
381 // if (!rel.getHybridName().isPersited()) {
382 toSaveNewNames
.add(rel
.getHybridName());
388 for (TaxonBase concept
: toSaveNewConcepts
){
389 for (HybridRelationship rel
: concept
.getName().getHybridChildRelations()){
390 // if (!rel.getParentName().isPersited()) {
391 toSaveNewNames
.add(rel
.getParentName());
393 // if (!rel.getHybridName().isPersited()) {
394 toSaveNewNames
.add(rel
.getHybridName());
399 CdmStore
.getService(INameService
.class).merge(toSaveNewNames
, true);
400 CdmStore
.getService(ITaxonService
.class).merge(toSaveNewConcepts
, true);
402 toSaveNewNames
.clear();
403 toSaveNewConcepts
.clear();
405 CdmStore
.getService(ITaxonNodeService
.class).merge(taxonNode
, true);
413 private void delete(TaxonBase key
, TaxonBaseDeletionConfigurator value
) {
414 if (key
instanceof Synonym
){
415 CdmStore
.getService(ITaxonService
.class).deleteSynonym(((Synonym
)key
).getUuid(), (SynonymDeletionConfigurator
) value
);
417 CdmStore
.getService(ITaxonService
.class).deleteTaxon(((Taxon
)key
).getUuid(), (TaxonDeletionConfigurator
) value
, null);
423 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
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$
432 return taxonNodePropertyPaths
;
435 private List
<String
> getTaxonBasePropertyPaths() {
436 List
<String
> taxonBasePropertyPaths
= Arrays
.asList(new String
[] {
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$
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$
469 return taxonBasePropertyPaths
;
473 * @param selectedElement
474 * @param deleteConfig
476 public void addTaxonBaseToDelete(TaxonBase selectedElement
, TaxonBaseDeletionConfigurator deleteConfig
) {
477 this.toDeletes
.put(selectedElement
, deleteConfig
);
484 public void addToSaveNewName(TaxonName newName
) {
485 this.toSaveNewNames
.add(newName
);