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
.HashSet
;
15 import java
.util
.List
;
18 import java
.util
.UUID
;
20 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
21 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
22 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
23 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonNodeService
;
24 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
25 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
26 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.ITaxonTreeNode
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
34 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
35 import eu
.etaxonomy
.taxeditor
.editor
.CdmEntitySessionInput
;
36 import eu
.etaxonomy
.taxeditor
.editor
.ChooseFromMultipleTaxonNodesDialog
;
37 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
38 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
39 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
40 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
41 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
50 public class TaxonEditorInputE4
extends CdmEntitySessionInput
implements IConversationEnabled
{
52 private static final String INCORRECT_STATE
= Messages
.TaxonEditorInput_INCORRECT_STATE
;
54 private final ConversationHolder conversation
;
56 private TaxonNode taxonNode
;
58 private TaxonEditorInputDataChangeBehaviourE4 dataChangeBehavior
;
60 private TaxonBase initiallySelectedTaxonBase
;
62 private enum CdmType
{
68 private TaxonEditorInputE4(UUID uuid
, CdmType type
) {
70 this.conversation
= CdmStore
.createConversation();
72 case PARENT_TAXON_NODE
:
73 initForParentTaxonNode(uuid
);
76 initForTaxonBase(uuid
);
79 initForTaxonNode(uuid
);
84 private void init(TaxonNode taxonNode
) {
85 this.taxonNode
= taxonNode
;
89 private void initForTaxonNode(UUID taxonNodeUuid
) {
92 TaxonNode taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNodeUuid
, getTaxonNodePropertyPaths());
94 if(taxonNode
== null){
95 MessagingUtils
.warningDialog(Messages
.TaxonEditorInput_NOT_IMPLEMENTED
, TaxonEditorInputE4
.class, Messages
.TaxonEditorInput_NOT_IMPLEMENTED_MESSAGE
);
98 setInitiallySelectedTaxonBase(getTaxon());
102 private void initForTaxonBase(UUID taxonBaseUuid
) {
103 TaxonBase taxonBase
= CdmStore
.getService(ITaxonService
.class).load(taxonBaseUuid
, getTaxonBasePropertyPaths());
104 if (taxonBase
!= null){
105 if(taxonBase
.isInstanceOf(Taxon
.class)){
106 Taxon taxon
= CdmBase
.deproxy(taxonBase
, Taxon
.class);
107 setInitiallySelectedTaxonBase(taxon
);
109 if (taxon
.getTaxonNodes().size() == 0 && taxon
.isMisapplication()){
110 // TODO get accepted taxon
111 MessagingUtils
.info(Messages
.TaxonEditorInput_OPEN_MISSAPPLIED_NAME
);
113 Set
<Taxon
> acceptedTaxa
= new HashSet
<Taxon
>();
114 Set
<TaxonRelationship
> relations
= taxon
.getRelationsFromThisTaxon();
115 for(TaxonRelationship relation
: relations
){
116 if(relation
.getType().equals(TaxonRelationshipType
.MISAPPLIED_NAME_FOR())){
117 acceptedTaxa
.add(relation
.getToTaxon());
120 setInputForMultipleTaxa(conversation
, acceptedTaxa
);
123 setInputForMultipleNodes(conversation
, taxon
.getTaxonNodes());
125 }else if(taxonBase
instanceof Synonym
){
126 Synonym synonym
= (Synonym
) taxonBase
;
128 Set
<Taxon
> taxa
= new HashSet
<>();
129 Taxon taxon
= synonym
.getAcceptedTaxon();
133 setInputForMultipleTaxa(conversation
, taxa
);
134 setInitiallySelectedTaxonBase(synonym
);
140 private void initForParentTaxonNode(UUID parentNodeUuid
){
143 TaxonName name
= PreferencesUtil
.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
144 ITaxonTreeNode parentNode
= CdmStore
.getService(IClassificationService
.class).getTreeNodeByUuid(parentNodeUuid
);
146 Taxon newTaxon
= Taxon
.NewInstance(name
, parentNode
.getReference());
147 TaxonNode newTaxonNode
= parentNode
.addChildTaxon(newTaxon
, parentNode
.getReference(), parentNode
.getMicroReference());
149 // add the new taxon to the editors persistence context
150 UUID newTaxonNodeUuid
= CdmStore
.getService(ITaxonNodeService
.class).save(newTaxonNode
).getUuid();
152 initForTaxonNode(newTaxonNodeUuid
);
155 private void setInputForMultipleNodes(ConversationHolder conversation
, Set
<TaxonNode
> taxonNodes
){
156 if(taxonNodes
.size() == 1){
157 TaxonNode taxonNode
= taxonNodes
.iterator().next();
159 }else if(taxonNodes
.size() > 1){
160 TaxonNode taxonNode
= ChooseFromMultipleTaxonNodesDialog
.choose(taxonNodes
);
161 if(taxonNode
!= null){
164 } else if (taxonNodes
.size() == 0) {
165 // this is an undesired state
166 MessagingUtils
.warningDialog(INCORRECT_STATE
,TaxonEditorInputE4
.class,Messages
.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION
);
170 private void setInputForMultipleTaxa(ConversationHolder conversation
, Set
<Taxon
> taxa
){
171 if(taxa
.size() == 1){
172 Taxon taxon
= taxa
.iterator().next();
173 Set
<TaxonNode
> nodes
= taxon
.getTaxonNodes();
174 setInputForMultipleNodes(conversation
, nodes
);
175 }else if(taxa
.size() > 1){
176 Set
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
177 for ( Taxon taxon
: taxa
){
178 taxonNodes
.addAll(taxon
.getTaxonNodes());
180 setInputForMultipleNodes(conversation
, taxonNodes
);
181 }else if(taxa
.size() == 0){
182 // this is an undesired state
183 MessagingUtils
.warningDialog(INCORRECT_STATE
, TaxonEditorInputE4
.class, Messages
.TaxonEditorInput_NO_ACCEPTED_TAXON_PRESENT
);
187 public static TaxonEditorInputE4
NewInstance(UUID taxonNodeUuid
) {
188 return new TaxonEditorInputE4(taxonNodeUuid
, CdmType
.TAXON_NODE
);
192 public static TaxonEditorInputE4
NewInstanceFromTaxonBase(UUID taxonBaseUuid
){
193 return new TaxonEditorInputE4(taxonBaseUuid
, CdmType
.TAXON_BASE
);
197 public static TaxonEditorInputE4
NewEmptyInstance(UUID parentNodeUuid
){
198 return new TaxonEditorInputE4(parentNodeUuid
, CdmType
.PARENT_TAXON_NODE
);
201 public Object
getAdapter(Class adapter
) {
203 if (adapter
== Taxon
.class) {
207 if (adapter
== TaxonNode
.class) {
217 * Overrides equals to ensure that a taxon can only be edited by
218 * one editor at a time.
221 public boolean equals(Object obj
) {
222 if (TaxonEditorInputE4
.class.equals(obj
.getClass())
223 && getTaxon() != null
224 && getTaxon().equals(((TaxonEditorInputE4
) obj
).getTaxon())) {
225 if (((TaxonEditorInputE4
) obj
).getInitiallySelectedTaxonBase() != null){
226 setInitiallySelectedTaxonBase(((TaxonEditorInputE4
) obj
).getInitiallySelectedTaxonBase());
233 public Taxon
getTaxon(){
235 Taxon taxon
= CdmBase
.deproxy(taxonNode
.getTaxon(), Taxon
.class);
241 public TaxonNode
getTaxonNode() {
246 public ConversationHolder
getConversationHolder() {
252 public void update(CdmDataChangeMap events
) {
253 if(dataChangeBehavior
== null){
254 dataChangeBehavior
= new TaxonEditorInputDataChangeBehaviourE4(this);
257 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
260 public void setInitiallySelectedTaxonBase(TaxonBase taxonBase
) {
261 this.initiallySelectedTaxonBase
= taxonBase
;
264 public TaxonBase
getInitiallySelectedTaxonBase() {
265 return initiallySelectedTaxonBase
;
269 public List
<TaxonNode
> getRootEntities() {
270 return Arrays
.asList(taxonNode
);
274 public void merge() {
275 CdmStore
.getService(ITaxonNodeService
.class).merge(taxonNode
, true);
280 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
284 private List
<String
> getTaxonNodePropertyPaths() {
285 List
<String
> taxonNodePropertyPaths
= new ArrayList
<String
>();
286 for(String propertyPath
: getTaxonBasePropertyPaths()) {
287 taxonNodePropertyPaths
.add("taxon." + propertyPath
); //$NON-NLS-1$
289 return taxonNodePropertyPaths
;
292 private List
<String
> getTaxonBasePropertyPaths() {
293 List
<String
> taxonBasePropertyPaths
= Arrays
.asList(new String
[] {
295 "createdBy", //$NON-NLS-1$
296 "updatedBy", //$NON-NLS-1$
297 "annotations", //$NON-NLS-1$
298 "markers", //$NON-NLS-1$
299 "credits", //$NON-NLS-1$
300 "extensions", //$NON-NLS-1$
301 "rights", //$NON-NLS-1$
302 "sources", //$NON-NLS-1$
303 "descriptions", //$NON-NLS-1$
304 "relationsToThisTaxon", //$NON-NLS-1$
305 "relationsFromThisTaxon", //$NON-NLS-1$
306 "taxonNodes", //$NON-NLS-1$
307 "descriptions.descriptionElements.feature", //$NON-NLS-1$
308 "descriptions.descriptionElements.area", //$NON-NLS-1$
309 "descriptions.descriptionElements.status", //$NON-NLS-1$
310 "descriptions.markers", //$NON-NLS-1$
311 "name.descriptions", //$NON-NLS-1$
312 "name.typeDesignations", //$NON-NLS-1$
313 "name.status", //$NON-NLS-1$
314 "name.nomenclaturalReference.inReference", //$NON-NLS-1$
315 "name.taxonBases.taxonNodes", //$NON-NLS-1$
316 "name.relationsFromThisName", //$NON-NLS-1$
317 "name.relationsToThisName", //$NON-NLS-1$
318 "name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations.synonym.name.status", //$NON-NLS-1$
319 "name.homotypicalGroup.typifiedNames.relationsToThisName.fromName", //$NON-NLS-1$
320 "synonymRelations.synonym.name.status.type", //$NON-NLS-1$
321 "synonymRelations.synonym.name.relationsToThisName.fromName", //$NON-NLS-1$
322 "synonymRelations.synonym.name.nomenclaturalReference.inReference.authorship", //$NON-NLS-1$
323 "synonymRelations.synonym.name.nomenclaturalReference.authorship", //$NON-NLS-1$
324 "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations" //$NON-NLS-1$
327 return taxonBasePropertyPaths
;