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
.List
;
18 import java
.util
.Map
.Entry
;
20 import java
.util
.UUID
;
22 import org
.eclipse
.e4
.ui
.di
.UISynchronize
;
24 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
25 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
26 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
27 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonNodeService
;
28 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
29 import eu
.etaxonomy
.cdm
.api
.service
.config
.SynonymDeletionConfigurator
;
30 import eu
.etaxonomy
.cdm
.api
.service
.config
.TaxonBaseDeletionConfigurator
;
31 import eu
.etaxonomy
.cdm
.api
.service
.config
.TaxonDeletionConfigurator
;
32 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
33 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.ITaxonTreeNode
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
39 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
40 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
41 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
42 import eu
.etaxonomy
.taxeditor
.editor
.CdmEntitySessionInput
;
43 import eu
.etaxonomy
.taxeditor
.editor
.ChooseFromMultipleTaxonNodesDialog
;
44 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
45 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
46 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
47 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
48 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
49 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostTaxonOperation
;
50 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
51 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
60 public class TaxonEditorInputE4
extends CdmEntitySessionInput
implements IConversationEnabled
{
62 private static final String INCORRECT_STATE
= Messages
.TaxonEditorInput_INCORRECT_STATE
;
64 private final ConversationHolder conversation
;
66 private TaxonNode taxonNode
;
68 private Map
<TaxonBase
, TaxonBaseDeletionConfigurator
> toDelete
= new HashMap
<>();
69 private Set
<Synonym
> toSaveNewSynonym
= new HashSet
<>();
70 private Set
<Taxon
> toSaveNewConcept
= new HashSet
<>();
73 private Set
<AbstractPostTaxonOperation
> operations
= new HashSet
<>();
75 private TaxonEditorInputDataChangeBehaviourE4 dataChangeBehavior
;
77 private TaxonBase initiallySelectedTaxonBase
;
82 public void setSync(UISynchronize sync
) {
86 private enum CdmType
{
92 private TaxonEditorInputE4(UUID uuid
, CdmType type
) {
94 this.conversation
= CdmStore
.createConversation();
96 case PARENT_TAXON_NODE
:
97 initForParentTaxonNode(uuid
);
100 initForTaxonBase(uuid
);
103 initForTaxonNode(uuid
);
108 private void init(TaxonNode taxonNode
) {
109 this.taxonNode
= taxonNode
;
113 private void initForTaxonNode(UUID taxonNodeUuid
) {
114 this.getCdmEntitySession().bind();
115 TaxonNode taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNodeUuid
, getTaxonNodePropertyPaths());
116 if(taxonNode
== null){
117 MessagingUtils
.warningDialog(Messages
.TaxonEditorInput_NOT_IMPLEMENTED
, TaxonEditorInputE4
.class, Messages
.TaxonEditorInput_NOT_IMPLEMENTED_MESSAGE
);
120 setInitiallySelectedTaxonBase(getTaxon());
124 private void initForTaxonBase(UUID taxonBaseUuid
) {
125 this.getCdmEntitySession().bind();
126 TaxonBase taxonBase
= CdmStore
.getService(ITaxonService
.class).load(taxonBaseUuid
, getTaxonBasePropertyPaths());
127 if (taxonBase
!= null){
128 if(taxonBase
.isInstanceOf(Taxon
.class)){
129 Taxon taxon
= CdmBase
.deproxy(taxonBase
, Taxon
.class);
130 setInitiallySelectedTaxonBase(taxon
);
132 if (taxon
.getTaxonNodes().size() == 0 && taxon
.isMisapplication()){
133 // TODO get accepted taxon
134 MessagingUtils
.info(Messages
.TaxonEditorInput_OPEN_MISSAPPLIED_NAME
);
136 Set
<Taxon
> acceptedTaxa
= new HashSet
<Taxon
>();
137 Set
<TaxonRelationship
> relations
= taxon
.getRelationsFromThisTaxon();
138 for(TaxonRelationship relation
: relations
){
139 if(relation
.getType().equals(TaxonRelationshipType
.MISAPPLIED_NAME_FOR())){
140 acceptedTaxa
.add(relation
.getToTaxon());
143 setInputForMultipleTaxa(conversation
, acceptedTaxa
);
146 setInputForMultipleNodes(conversation
, taxon
.getTaxonNodes());
148 }else if(taxonBase
instanceof Synonym
){
149 Synonym synonym
= (Synonym
) taxonBase
;
151 Set
<Taxon
> taxa
= new HashSet
<>();
152 Taxon taxon
= synonym
.getAcceptedTaxon();
156 setInputForMultipleTaxa(conversation
, taxa
);
157 setInitiallySelectedTaxonBase(synonym
);
163 private void initForParentTaxonNode(UUID parentNodeUuid
){
164 this.getCdmEntitySession().bind();
165 TaxonName name
= PreferencesUtil
.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
166 ITaxonTreeNode parentNode
= CdmStore
.getService(IClassificationService
.class).getTreeNodeByUuid(parentNodeUuid
);
168 Taxon newTaxon
= Taxon
.NewInstance(name
, parentNode
.getReference());
169 TaxonNode newTaxonNode
= parentNode
.addChildTaxon(newTaxon
, parentNode
.getReference(), parentNode
.getMicroReference());
171 // add the new taxon to the editors persistence context
172 UUID newTaxonNodeUuid
= CdmStore
.getService(ITaxonNodeService
.class).save(newTaxonNode
).getUuid();
174 initForTaxonNode(newTaxonNodeUuid
);
177 private void setInputForMultipleNodes(ConversationHolder conversation
, Set
<TaxonNode
> taxonNodes
){
178 if(taxonNodes
.size() == 1){
179 TaxonNode taxonNode
= taxonNodes
.iterator().next();
180 taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNode
.getUuid(), getTaxonNodePropertyPaths());
182 }else if(taxonNodes
.size() > 1){
183 TaxonNode taxonNode
= ChooseFromMultipleTaxonNodesDialog
.choose(taxonNodes
);
184 taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNode
.getUuid(), getTaxonNodePropertyPaths());
185 if(taxonNode
!= null){
188 } else if (taxonNodes
.size() == 0) {
189 // this is an undesired state
190 MessagingUtils
.warningDialog(INCORRECT_STATE
,TaxonEditorInputE4
.class,Messages
.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION
);
194 private void setInputForMultipleTaxa(ConversationHolder conversation
, Set
<Taxon
> taxa
){
195 if(taxa
.size() == 1){
196 Taxon taxon
= taxa
.iterator().next();
197 Set
<TaxonNode
> nodes
= taxon
.getTaxonNodes();
198 setInputForMultipleNodes(conversation
, nodes
);
199 }else if(taxa
.size() > 1){
200 Set
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
201 for ( Taxon taxon
: taxa
){
202 taxonNodes
.addAll(taxon
.getTaxonNodes());
204 setInputForMultipleNodes(conversation
, taxonNodes
);
205 }else if(taxa
.size() == 0){
206 // this is an undesired state
207 MessagingUtils
.warningDialog(INCORRECT_STATE
, TaxonEditorInputE4
.class, Messages
.TaxonEditorInput_NO_ACCEPTED_TAXON_PRESENT
);
211 public static TaxonEditorInputE4
NewInstance(UUID taxonNodeUuid
) {
212 return new TaxonEditorInputE4(taxonNodeUuid
, CdmType
.TAXON_NODE
);
216 public static TaxonEditorInputE4
NewInstanceFromTaxonBase(UUID taxonBaseUuid
){
217 return new TaxonEditorInputE4(taxonBaseUuid
, CdmType
.TAXON_BASE
);
221 public static TaxonEditorInputE4
NewEmptyInstance(UUID parentNodeUuid
){
222 return new TaxonEditorInputE4(parentNodeUuid
, CdmType
.PARENT_TAXON_NODE
);
225 public Object
getAdapter(Class adapter
) {
227 if (adapter
== Taxon
.class) {
231 if (adapter
== TaxonNode
.class) {
241 * Overrides equals to ensure that a taxon can only be edited by
242 * one editor at a time.
245 public boolean equals(Object obj
) {
246 if (TaxonEditorInputE4
.class.equals(obj
.getClass())
247 && getTaxon() != null
248 && getTaxon().equals(((TaxonEditorInputE4
) obj
).getTaxon())) {
249 if (((TaxonEditorInputE4
) obj
).getInitiallySelectedTaxonBase() != null){
250 setInitiallySelectedTaxonBase(((TaxonEditorInputE4
) obj
).getInitiallySelectedTaxonBase());
257 public Taxon
getTaxon(){
259 Taxon taxon
= CdmBase
.deproxy(taxonNode
.getTaxon(), Taxon
.class);
265 public TaxonNode
getTaxonNode() {
270 public ConversationHolder
getConversationHolder() {
276 public void update(CdmDataChangeMap events
) {
277 if(dataChangeBehavior
== null){
278 dataChangeBehavior
= new TaxonEditorInputDataChangeBehaviourE4(this);
281 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
284 public void setInitiallySelectedTaxonBase(TaxonBase taxonBase
) {
285 this.initiallySelectedTaxonBase
= taxonBase
;
288 public TaxonBase
getInitiallySelectedTaxonBase() {
289 return initiallySelectedTaxonBase
;
292 public Set
<AbstractPostTaxonOperation
> getOperations() {
296 public void setOperations(Set
<AbstractPostTaxonOperation
> operations
) {
297 this.operations
= operations
;
300 public void addOperation(AbstractPostTaxonOperation operation
) {
301 this.operations
.add(operation
);
304 public void addToSaveNewSynonym(Synonym toSaveNew
) {
305 this.toSaveNewSynonym
.add(toSaveNew
);
307 public void addToSaveNewConcept(Taxon toSaveNew
) {
308 this.toSaveNewConcept
.add(toSaveNew
);
312 public List
<TaxonNode
> getRootEntities() {
313 return Arrays
.asList(taxonNode
);
317 public void merge() {
318 if (!this.getCdmEntitySession().isActive()){
319 this.getCdmEntitySession().bind();
321 for(Entry
<TaxonBase
, TaxonBaseDeletionConfigurator
> entry
:toDelete
.entrySet()){
322 delete(entry
.getKey(), entry
.getValue());
325 for(AbstractPostOperation entry
:operations
){
326 EditorUtil
.executeOperation(entry
,sync
);
330 for (Synonym syn
: toSaveNewSynonym
){
331 CdmStore
.getService(ITaxonService
.class).createSynonym(taxonNode
.getTaxon().getUuid(),syn
);
333 // for (Taxon taxon: toSaveNewConcept){
334 // CdmStore.getService(ITaxonService.class).createConcept(taxonNode.getTaxon().getUuid(),taxon);
337 CdmStore
.getService(ITaxonNodeService
.class).merge(taxonNode
, true);
345 private void delete(TaxonBase key
, TaxonBaseDeletionConfigurator value
) {
346 if (key
instanceof Synonym
){
347 CdmStore
.getService(ITaxonService
.class).deleteSynonym(((Synonym
)key
).getUuid(), (SynonymDeletionConfigurator
) value
);
349 CdmStore
.getService(ITaxonService
.class).deleteTaxon(((Taxon
)key
).getUuid(), (TaxonDeletionConfigurator
) value
, null);
355 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
359 private List
<String
> getTaxonNodePropertyPaths() {
360 List
<String
> taxonNodePropertyPaths
= new ArrayList
<String
>();
361 for(String propertyPath
: getTaxonBasePropertyPaths()) {
362 taxonNodePropertyPaths
.add("taxon." + propertyPath
); //$NON-NLS-1$
364 return taxonNodePropertyPaths
;
367 private List
<String
> getTaxonBasePropertyPaths() {
368 List
<String
> taxonBasePropertyPaths
= Arrays
.asList(new String
[] {
370 "createdBy", //$NON-NLS-1$
371 "updatedBy", //$NON-NLS-1$
372 "annotations", //$NON-NLS-1$
373 "markers", //$NON-NLS-1$
374 "credits", //$NON-NLS-1$
375 "extensions", //$NON-NLS-1$
376 "rights", //$NON-NLS-1$
377 "sources", //$NON-NLS-1$
379 "descriptions", //$NON-NLS-1$
380 "relationsToThisTaxon.relatedFrom.name.taxonBases.taxonNodes", //$NON-NLS-1$
381 "relationsToThisTaxon.relatedFrom.name.taxonBases.relationsFromThisTaxon", //$NON-NLS-1$
382 "relationsToThisTaxon.relatedFrom.name.taxonBases.relationsToThisTaxon", //$NON-NLS-1$
383 "relationsFromThisTaxon.relatedTo.name.taxonBases.taxonNodes", //$NON-NLS-1$
384 "relationsFromThisTaxon.relatedTo.name.taxonBases.relationsFromThisTaxon", //$NON-NLS-1$
385 "relationsFromThisTaxon.relatedTo.name.taxonBases.relationsToThisTaxon", //$NON-NLS-1$
386 "taxonNodes", //$NON-NLS-1$
387 "descriptions.descriptionElements.feature", //$NON-NLS-1$
388 "descriptions.descriptionElements.area", //$NON-NLS-1$
389 "descriptions.descriptionElements.status", //$NON-NLS-1$
390 "descriptions.markers", //$NON-NLS-1$
391 "name.descriptions", //$NON-NLS-1$
392 "name.typeDesignations", //$NON-NLS-1$
393 "name.status", //$NON-NLS-1$
394 "name.nomenclaturalReference.inReference", //$NON-NLS-1$
395 "name.taxonBases.taxonNodes", //$NON-NLS-1$
396 "name.relationsFromThisName", //$NON-NLS-1$
397 "name.relationsToThisName", //$NON-NLS-1$
398 "name.homotypicalGroup.typifiedNames.taxonBases.synonyms.synonym.name.status", //$NON-NLS-1$
399 "name.homotypicalGroup.typifiedNames.relationsToThisName.fromName", //$NON-NLS-1$
400 "synonyms.name.status.type", //$NON-NLS-1$
401 "synonyms.name.relationsToThisName.fromName", //$NON-NLS-1$
402 "synonyms.name.nomenclaturalReference.inReference.authorship", //$NON-NLS-1$
403 "synonyms.name.nomenclaturalReference.authorship", //$NON-NLS-1$
404 // "synonyms.name.homotypicalGroup", //$NON-NLS-1$
405 // "synonyms.synonym.name.homotypicalGroup.typifiedNames", //$NON-NLS-1$
406 "synonyms.name.homotypicalGroup.typifiedNames.taxonBases.synonyms" //$NON-NLS-1$
409 return taxonBasePropertyPaths
;
413 * @param selectedElement
414 * @param deleteConfig
416 public void addTaxonBaseToDelete(TaxonBase selectedElement
, TaxonBaseDeletionConfigurator deleteConfig
) {
417 this.toDelete
.put(selectedElement
, deleteConfig
);