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
.INameService
;
28 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonNodeService
;
29 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
30 import eu
.etaxonomy
.cdm
.api
.service
.config
.SynonymDeletionConfigurator
;
31 import eu
.etaxonomy
.cdm
.api
.service
.config
.TaxonBaseDeletionConfigurator
;
32 import eu
.etaxonomy
.cdm
.api
.service
.config
.TaxonDeletionConfigurator
;
33 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
34 import eu
.etaxonomy
.cdm
.model
.name
.HybridRelationship
;
35 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.ITaxonTreeNode
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
39 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
40 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
41 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
42 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
43 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
44 import eu
.etaxonomy
.taxeditor
.editor
.CdmEntitySessionInput
;
45 import eu
.etaxonomy
.taxeditor
.editor
.ChooseFromMultipleTaxonNodesDialog
;
46 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
47 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
48 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
49 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
50 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
51 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostTaxonOperation
;
52 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
53 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
62 public class TaxonEditorInputE4
extends CdmEntitySessionInput
implements IConversationEnabled
{
64 private static final String INCORRECT_STATE
= Messages
.TaxonEditorInput_INCORRECT_STATE
;
66 private final ConversationHolder conversation
;
68 private TaxonNode taxonNode
;
70 private Map
<TaxonBase
, TaxonBaseDeletionConfigurator
> toDelete
= new HashMap
<>();
71 private Set
<Synonym
> toSaveNewSynonym
= new HashSet
<>();
72 private Set
<TaxonBase
> toSaveNewConcept
= new HashSet
<>();
73 private Set
<TaxonName
> toSaveNewName
= new HashSet
<>();
76 private Set
<AbstractPostTaxonOperation
> operations
= new HashSet
<>();
78 private TaxonEditorInputDataChangeBehaviourE4 dataChangeBehavior
;
80 private TaxonBase initiallySelectedTaxonBase
;
85 public void setSync(UISynchronize sync
) {
89 private enum CdmType
{
95 private TaxonEditorInputE4(UUID uuid
, CdmType type
) {
97 this.conversation
= CdmStore
.createConversation();
99 case PARENT_TAXON_NODE
:
100 initForParentTaxonNode(uuid
);
103 initForTaxonBase(uuid
);
106 initForTaxonNode(uuid
);
111 private void init(TaxonNode taxonNode
) {
112 this.taxonNode
= taxonNode
;
116 private void initForTaxonNode(UUID taxonNodeUuid
) {
117 this.getCdmEntitySession().bind();
118 TaxonNode taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNodeUuid
, getTaxonNodePropertyPaths());
119 // TaxonNode taxonNode = getCdmEntitySession().remoteLoad(CdmStore.getService(ITaxonNodeService.class), taxonNodeUuid, getTaxonNodePropertyPaths());
120 if(taxonNode
== null){
121 MessagingUtils
.warningDialog(Messages
.TaxonEditorInput_NOT_IMPLEMENTED
, TaxonEditorInputE4
.class, Messages
.TaxonEditorInput_NOT_IMPLEMENTED_MESSAGE
);
124 setInitiallySelectedTaxonBase(getTaxon());
128 private void initForTaxonBase(UUID taxonBaseUuid
) {
129 this.getCdmEntitySession().bind();
130 // TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).load(taxonBaseUuid, getTaxonBasePropertyPaths());
131 TaxonBase taxonBase
= getCdmEntitySession().remoteLoad(CdmStore
.getService(ITaxonService
.class), taxonBaseUuid
, getTaxonBasePropertyPaths());
132 if (taxonBase
!= null){
133 if(taxonBase
.isInstanceOf(Taxon
.class)){
134 Taxon taxon
= CdmBase
.deproxy(taxonBase
, Taxon
.class);
135 setInitiallySelectedTaxonBase(taxon
);
137 if (taxon
.getTaxonNodes().size() == 0 && taxon
.isMisapplication()){
138 // TODO get accepted taxon
139 MessagingUtils
.info(Messages
.TaxonEditorInput_OPEN_MISSAPPLIED_NAME
);
141 Set
<Taxon
> acceptedTaxa
= new HashSet
<Taxon
>();
142 Set
<TaxonRelationship
> relations
= taxon
.getRelationsFromThisTaxon();
143 for(TaxonRelationship relation
: relations
){
144 if(relation
.getType().equals(TaxonRelationshipType
.MISAPPLIED_NAME_FOR())){
145 acceptedTaxa
.add(relation
.getToTaxon());
148 setInputForMultipleTaxa(conversation
, acceptedTaxa
);
151 setInputForMultipleNodes(conversation
, taxon
.getTaxonNodes());
153 }else if(taxonBase
instanceof Synonym
){
154 Synonym synonym
= (Synonym
) taxonBase
;
156 Set
<Taxon
> taxa
= new HashSet
<>();
157 Taxon taxon
= synonym
.getAcceptedTaxon();
161 setInputForMultipleTaxa(conversation
, taxa
);
162 setInitiallySelectedTaxonBase(synonym
);
168 private void initForParentTaxonNode(UUID parentNodeUuid
){
169 this.getCdmEntitySession().bind();
170 TaxonName name
= PreferencesUtil
.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
171 ITaxonTreeNode parentNode
= CdmStore
.getService(IClassificationService
.class).getTreeNodeByUuid(parentNodeUuid
);
173 Taxon newTaxon
= Taxon
.NewInstance(name
, parentNode
.getReference());
174 TaxonNode newTaxonNode
= parentNode
.addChildTaxon(newTaxon
, parentNode
.getReference(), parentNode
.getMicroReference());
176 // add the new taxon to the editors persistence context
177 UUID newTaxonNodeUuid
= CdmStore
.getService(ITaxonNodeService
.class).save(newTaxonNode
).getUuid();
179 initForTaxonNode(newTaxonNodeUuid
);
182 private void setInputForMultipleNodes(ConversationHolder conversation
, Set
<TaxonNode
> taxonNodes
){
183 if(taxonNodes
.size() == 1){
184 TaxonNode taxonNode
= taxonNodes
.iterator().next();
185 taxonNode
= getCdmEntitySession().remoteLoad(CdmStore
.getService(ITaxonNodeService
.class), taxonNode
.getUuid(), getTaxonNodePropertyPaths());
187 }else if(taxonNodes
.size() > 1){
188 TaxonNode taxonNode
= ChooseFromMultipleTaxonNodesDialog
.choose(taxonNodes
);
189 taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNode
.getUuid(), getTaxonNodePropertyPaths());
190 if(taxonNode
!= null){
193 } else if (taxonNodes
.size() == 0) {
194 // this is an undesired state
195 MessagingUtils
.warningDialog(INCORRECT_STATE
,TaxonEditorInputE4
.class,Messages
.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION
);
199 private void setInputForMultipleTaxa(ConversationHolder conversation
, Set
<Taxon
> taxa
){
200 if(taxa
.size() == 1){
201 Taxon taxon
= taxa
.iterator().next();
202 Set
<TaxonNode
> nodes
= taxon
.getTaxonNodes();
203 setInputForMultipleNodes(conversation
, nodes
);
204 }else if(taxa
.size() > 1){
205 Set
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
206 for ( Taxon taxon
: taxa
){
207 taxonNodes
.addAll(taxon
.getTaxonNodes());
209 setInputForMultipleNodes(conversation
, taxonNodes
);
210 }else if(taxa
.size() == 0){
211 // this is an undesired state
212 MessagingUtils
.warningDialog(INCORRECT_STATE
, TaxonEditorInputE4
.class, Messages
.TaxonEditorInput_NO_ACCEPTED_TAXON_PRESENT
);
216 public static TaxonEditorInputE4
NewInstance(UUID taxonNodeUuid
) {
217 return new TaxonEditorInputE4(taxonNodeUuid
, CdmType
.TAXON_NODE
);
221 public static TaxonEditorInputE4
NewInstanceFromTaxonBase(UUID taxonBaseUuid
){
222 return new TaxonEditorInputE4(taxonBaseUuid
, CdmType
.TAXON_BASE
);
226 public static TaxonEditorInputE4
NewEmptyInstance(UUID parentNodeUuid
){
227 return new TaxonEditorInputE4(parentNodeUuid
, CdmType
.PARENT_TAXON_NODE
);
230 public Object
getAdapter(Class adapter
) {
232 if (adapter
== Taxon
.class) {
236 if (adapter
== TaxonNode
.class) {
246 * Overrides equals to ensure that a taxon can only be edited by
247 * one editor at a time.
250 public boolean equals(Object obj
) {
251 if (TaxonEditorInputE4
.class.equals(obj
.getClass())
252 && getTaxon() != null
253 && getTaxon().equals(((TaxonEditorInputE4
) obj
).getTaxon())) {
254 if (((TaxonEditorInputE4
) obj
).getInitiallySelectedTaxonBase() != null){
255 setInitiallySelectedTaxonBase(((TaxonEditorInputE4
) obj
).getInitiallySelectedTaxonBase());
262 public Taxon
getTaxon(){
264 Taxon taxon
= CdmBase
.deproxy(taxonNode
.getTaxon(), Taxon
.class);
270 public TaxonNode
getTaxonNode() {
275 public ConversationHolder
getConversationHolder() {
281 public void update(CdmDataChangeMap events
) {
282 if(dataChangeBehavior
== null){
283 dataChangeBehavior
= new TaxonEditorInputDataChangeBehaviourE4(this);
286 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
289 public void setInitiallySelectedTaxonBase(TaxonBase taxonBase
) {
290 this.initiallySelectedTaxonBase
= taxonBase
;
293 public TaxonBase
getInitiallySelectedTaxonBase() {
294 return initiallySelectedTaxonBase
;
297 public Set
<AbstractPostTaxonOperation
> getOperations() {
301 public void setOperations(Set
<AbstractPostTaxonOperation
> operations
) {
302 this.operations
= operations
;
305 public void addOperation(AbstractPostTaxonOperation operation
) {
306 this.operations
.add(operation
);
309 public void addToSaveNewSynonym(Synonym toSaveNew
) {
310 this.toSaveNewSynonym
.add(toSaveNew
);
312 public void addToSaveNewConcept(Taxon toSaveNew
) {
313 this.toSaveNewConcept
.add(toSaveNew
);
317 public List
<TaxonNode
> getRootEntities() {
318 return Arrays
.asList(taxonNode
);
322 public void merge() {
323 if (!this.getCdmEntitySession().isActive()){
324 this.getCdmEntitySession().bind();
326 for(Entry
<TaxonBase
, TaxonBaseDeletionConfigurator
> entry
:toDelete
.entrySet()){
327 delete(entry
.getKey(), entry
.getValue());
330 for(AbstractPostOperation entry
:operations
){
331 EditorUtil
.executeOperation(entry
,sync
);
335 for (Synonym syn
: toSaveNewSynonym
){
336 for (HybridRelationship rel
: syn
.getName().getHybridChildRelations()){
337 if (!rel
.getParentName().isPersited()) {
338 toSaveNewName
.add(rel
.getParentName());
340 if (!rel
.getHybridName().isPersited()) {
341 toSaveNewName
.add(rel
.getHybridName());
347 for (TaxonBase concept
: toSaveNewConcept
){
348 for (HybridRelationship rel
: concept
.getName().getHybridChildRelations()){
349 if (!rel
.getParentName().isPersited()) {
350 toSaveNewName
.add(rel
.getParentName());
352 if (!rel
.getHybridName().isPersited()) {
353 toSaveNewName
.add(rel
.getHybridName());
358 CdmStore
.getService(INameService
.class).saveOrUpdate(toSaveNewName
);
359 CdmStore
.getService(ITaxonService
.class).saveOrUpdate(toSaveNewConcept
);
361 toSaveNewName
.clear();
362 toSaveNewConcept
.clear();
364 CdmStore
.getService(ITaxonNodeService
.class).merge(taxonNode
, true);
372 private void delete(TaxonBase key
, TaxonBaseDeletionConfigurator value
) {
373 if (key
instanceof Synonym
){
374 CdmStore
.getService(ITaxonService
.class).deleteSynonym(((Synonym
)key
).getUuid(), (SynonymDeletionConfigurator
) value
);
376 CdmStore
.getService(ITaxonService
.class).deleteTaxon(((Taxon
)key
).getUuid(), (TaxonDeletionConfigurator
) value
, null);
382 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
386 private List
<String
> getTaxonNodePropertyPaths() {
387 List
<String
> taxonNodePropertyPaths
= new ArrayList
<String
>();
388 for(String propertyPath
: getTaxonBasePropertyPaths()) {
389 taxonNodePropertyPaths
.add("taxon." + propertyPath
); //$NON-NLS-1$
391 return taxonNodePropertyPaths
;
394 private List
<String
> getTaxonBasePropertyPaths() {
395 List
<String
> taxonBasePropertyPaths
= Arrays
.asList(new String
[] {
397 "createdBy", //$NON-NLS-1$
398 "updatedBy", //$NON-NLS-1$
399 "annotations", //$NON-NLS-1$
400 "markers", //$NON-NLS-1$
401 "credits", //$NON-NLS-1$
402 "extensions", //$NON-NLS-1$
403 "rights", //$NON-NLS-1$
404 "sources", //$NON-NLS-1$
406 "descriptions", //$NON-NLS-1$
407 // "relationsToThisTaxon.relatedFrom.name.taxonBases.taxonNodes", //$NON-NLS-1$
408 // "relationsToThisTaxon.relatedFrom.name.taxonBases.relationsFromThisTaxon", //$NON-NLS-1$
409 // "relationsToThisTaxon.relatedFrom.name.taxonBases.relationsToThisTaxon", //$NON-NLS-1$
410 "relationsToThisTaxon.relatedFrom.name.typeDesignations.typeSpecimen.derivedFrom.originals",//$NON-NLS-1$
411 "relationsToThisTaxon.relatedFrom.name.typeDesignations.typeName",//$NON-NLS-1$
412 // "relationsFromThisTaxon.relatedTo.name.taxonBases.taxonNodes", //$NON-NLS-1$
413 // "relationsFromThisTaxon.relatedTo.name.taxonBases.relationsFromThisTaxon", //$NON-NLS-1$
414 // "relationsFromThisTaxon.relatedTo.name.taxonBases.relationsToThisTaxon",
415 "relationsFromThisTaxon.relatedTo.name.typeDesignations.typeSpecimen.derivedFrom.originals",//$NON-NLS-1$
416 "relationsFromThisTaxon.relatedTo.name.typeDesignations.typeName",//$NON-NLS-1$
417 "taxonNodes", //$NON-NLS-1$
418 "descriptions.descriptionElements.feature", //$NON-NLS-1$
419 "descriptions.descriptionElements.area", //$NON-NLS-1$
420 "descriptions.descriptionElements.status", //$NON-NLS-1$
421 "descriptions.markers", //$NON-NLS-1$
422 "name.descriptions", //$NON-NLS-1$
423 "name.typeDesignations.typeSpecimen.derivedFrom.originals", //$NON-NLS-1$
424 "name.status", //$NON-NLS-1$
425 "name.nomenclaturalReference.inReference", //$NON-NLS-1$
426 "name.taxonBases.taxonNodes", //$NON-NLS-1$
427 "name.relationsFromThisName", //$NON-NLS-1$
428 "name.relationsToThisName", //$NON-NLS-1$
429 "name.homotypicalGroup.typifiedNames.taxonBases.synonyms.synonym.name.status", //$NON-NLS-1$
430 "name.homotypicalGroup.typifiedNames.relationsToThisName.fromName", //$NON-NLS-1$
431 "synonyms.name.status.type", //$NON-NLS-1$
432 "synonyms.name.relationsToThisName.fromName", //$NON-NLS-1$
433 "synonyms.name.nomenclaturalReference.inReference.authorship", //$NON-NLS-1$
434 "synonyms.name.nomenclaturalReference.authorship", //$NON-NLS-1$
435 // "synonyms.name.homotypicalGroup", //$NON-NLS-1$
436 // "synonyms.synonym.name.homotypicalGroup.typifiedNames", //$NON-NLS-1$
437 "synonyms.name.homotypicalGroup.typifiedNames.taxonBases.synonyms" //$NON-NLS-1$
440 return taxonBasePropertyPaths
;
444 * @param selectedElement
445 * @param deleteConfig
447 public void addTaxonBaseToDelete(TaxonBase selectedElement
, TaxonBaseDeletionConfigurator deleteConfig
) {
448 this.toDelete
.put(selectedElement
, deleteConfig
);
455 public void addToSaveNewName(TaxonName newName
) {
456 this.toSaveNewName
.add(newName
);