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
;
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 org
.eclipse
.jface
.resource
.ImageDescriptor
;
21 import org
.eclipse
.ui
.IEditorInput
;
22 import org
.eclipse
.ui
.IMemento
;
23 import org
.eclipse
.ui
.IPersistableElement
;
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
.ITaxonNodeService
;
29 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
30 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
31 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.ITaxonTreeNode
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
39 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
40 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
41 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
42 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
43 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
47 * <p>TaxonEditorInput class.</p>
53 public class TaxonEditorInput
extends CdmEntitySessionInput
implements IEditorInput
, IConversationEnabled
, IPersistableElement
{
55 private final ConversationHolder conversation
;
57 private TaxonNode taxonNode
;
59 private TaxonEditorInputDataChangeBehaviour dataChangeBehavior
;
61 private TaxonBase initiallySelectedTaxonBase
;
63 private enum CdmType
{
69 private TaxonEditorInput(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
;
92 * @param taxonNodeUuid a {@link java.util.UUID} object.
93 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
94 * @throws java.lang.Exception if any.
96 private void initForTaxonNode(UUID taxonNodeUuid
) {
99 TaxonNode taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNodeUuid
, getTaxonNodePropertyPaths());
101 if(taxonNode
== null){
102 MessagingUtils
.warningDialog("Not yet implemented", TaxonEditorInput
.class, "Selected element is not type TaxonBase.");
108 private void initForTaxonBase(UUID taxonBaseUuid
) {
109 TaxonBase taxonBase
= CdmStore
.getService(ITaxonService
.class).load(taxonBaseUuid
, getTaxonBasePropertyPaths());
110 if (taxonBase
!= null){
111 if(taxonBase
.isInstanceOf(Taxon
.class)){
112 Taxon taxon
= CdmBase
.deproxy(taxonBase
, Taxon
.class);
114 if (taxon
.getTaxonNodes().size() == 0 && taxon
.isMisapplication()){
115 // TODO get accepted taxon
116 MessagingUtils
.info("trying to open Mispplied Name ");
118 Set
<Taxon
> acceptedTaxa
= new HashSet
<Taxon
>();
119 Set
<TaxonRelationship
> relations
= taxon
.getRelationsFromThisTaxon();
120 for(TaxonRelationship relation
: relations
){
121 if(relation
.getType().equals(TaxonRelationshipType
.MISAPPLIED_NAME_FOR())){
122 acceptedTaxa
.add(relation
.getToTaxon());
125 setInputForMultipleTaxa(conversation
, acceptedTaxa
);
128 setInputForMultipleNodes(conversation
, taxon
.getTaxonNodes());
130 }else if(taxonBase
instanceof Synonym
){
131 Synonym synonym
= (Synonym
) taxonBase
;
133 Set
<Taxon
> taxa
= synonym
.getAcceptedTaxa();
134 setInputForMultipleTaxa(conversation
, taxa
);
141 * <p>NewEmptyInstance</p>
143 * @param parentNodeUuid a {@link java.util.UUID} object.
144 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
146 private void initForParentTaxonNode(UUID parentNodeUuid
){
149 TaxonNameBase
<?
, ?
> name
= PreferencesUtil
.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
150 ITaxonTreeNode parentNode
= CdmStore
.getService(IClassificationService
.class).getTreeNodeByUuid(parentNodeUuid
);
152 Taxon newTaxon
= Taxon
.NewInstance(name
, parentNode
.getReference());
153 TaxonNode newTaxonNode
= parentNode
.addChildTaxon(newTaxon
, parentNode
.getReference(), parentNode
.getMicroReference());
155 // add the new taxon to the editors persistence context
156 UUID newTaxonNodeUuid
= CdmStore
.getService(ITaxonNodeService
.class).save(newTaxonNode
).getUuid();
158 initForTaxonNode(newTaxonNodeUuid
);
164 private void setInputForMultipleNodes(ConversationHolder conversation
, Set
<TaxonNode
> taxonNodes
){
165 if(taxonNodes
.size() == 1){
166 TaxonNode taxonNode
= taxonNodes
.iterator().next();
168 }else if(taxonNodes
.size() > 1){
169 TaxonNode taxonNode
= ChooseFromMultipleTaxonNodesDialog
.choose(taxonNodes
);
170 if(taxonNode
!= null){
173 }else if(taxonNodes
.size() == 0){
174 // this is an undesired state
175 MessagingUtils
.warningDialog("Incorrect state", TaxonEditorInput
.class, "The accepted taxon is not part of any classification. This should not have happened.");
179 private void setInputForMultipleTaxa(ConversationHolder conversation
, Set
<Taxon
> taxa
){
180 if(taxa
.size() == 1){
181 Taxon taxon
= taxa
.iterator().next();
182 Set
<TaxonNode
> nodes
= taxon
.getTaxonNodes();
183 setInputForMultipleNodes(conversation
, nodes
);
184 }else if(taxa
.size() > 1){
185 Set
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
186 for ( Taxon taxon
: taxa
){
187 taxonNodes
.addAll(taxon
.getTaxonNodes());
189 setInputForMultipleNodes(conversation
, taxonNodes
);
190 }else if(taxa
.size() == 0){
191 // this is an undesired state
192 MessagingUtils
.warningDialog("Incorrect state", TaxonEditorInput
.class, "Trying to open accepted taxon for a synonym or misapplication but" +
193 " no accepted taxa are present. This should not have happened.");
200 * @param taxonNodeUuid a {@link java.util.UUID} object.
201 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
202 * @throws java.lang.Exception if any.
204 public static TaxonEditorInput
NewInstance(UUID taxonNodeUuid
) throws Exception
{
205 return new TaxonEditorInput(taxonNodeUuid
, CdmType
.TAXON_NODE
);
210 * <p>NewInstanceFromTaxonBase</p>
212 * @param taxonBaseUuid a {@link java.util.UUID} object.
213 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
215 public static TaxonEditorInput
NewInstanceFromTaxonBase(UUID taxonBaseUuid
){
216 return new TaxonEditorInput(taxonBaseUuid
, CdmType
.TAXON_BASE
);
221 * <p>NewEmptyInstance</p>
223 * @param parentNodeUuid a {@link java.util.UUID} object.
224 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
226 public static TaxonEditorInput
NewEmptyInstance(UUID parentNodeUuid
){
227 return new TaxonEditorInput(parentNodeUuid
, CdmType
.PARENT_TAXON_NODE
);
231 * @see org.eclipse.ui.IEditorInput#exists()
239 public boolean exists() {
240 return taxonNode
!= null;
244 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
247 * <p>getImageDescriptor</p>
249 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
252 public ImageDescriptor
getImageDescriptor() {
257 * @see org.eclipse.ui.IEditorInput#getName()
262 * @return a {@link java.lang.String} object.
265 public String
getName() {
266 if(getTaxon() == null){
269 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
270 if (name
== null || name
.getTitleCache() == null) {
273 return name
.getTitleCache();
278 * @see org.eclipse.ui.IEditorInput#getPersistable()
281 * <p>getPersistable</p>
283 * @return a {@link org.eclipse.ui.IPersistableElement} object.
286 public IPersistableElement
getPersistable() {
287 // if(CdmStore.isActive()){
288 // TaxonNode test = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid());
289 // boolean isPersistable = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid()) != null;
290 // if (isPersistable) {
298 * @see org.eclipse.ui.IEditorInput#getToolTipText()
301 * <p>getToolTipText</p>
303 * @return a {@link java.lang.String} object.
306 public String
getToolTipText() {
311 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
315 public Object
getAdapter(Class adapter
) {
317 if (adapter
== Taxon
.class) {
321 if (adapter
== TaxonNode
.class) {
331 * Overrides equals to ensure that a taxon can only be edited by
332 * one editor at a time.
335 public boolean equals(Object obj
) {
336 if (TaxonEditorInput
.class.equals(obj
.getClass())
337 && getTaxon() != null
338 && getTaxon().equals(((TaxonEditorInput
) obj
).getTaxon())){
339 if(((TaxonEditorInput
) obj
).getInitiallySelectedTaxonBase() != null){
340 setInitiallySelectedTaxonBase(((TaxonEditorInput
) obj
).getInitiallySelectedTaxonBase());
352 public Taxon
getTaxon(){
353 Taxon taxon
= CdmBase
.deproxy(taxonNode
.getTaxon(), Taxon
.class);
358 * <p>Getter for the field <code>taxonNode</code>.</p>
360 * @return the taxonNode
362 public TaxonNode
getTaxonNode() {
368 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
371 * <p>getConversationHolder</p>
373 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
376 public ConversationHolder
getConversationHolder() {
382 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
386 public void update(CdmDataChangeMap events
) {
387 if(dataChangeBehavior
== null){
388 dataChangeBehavior
= new TaxonEditorInputDataChangeBehaviour(this);
391 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
395 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
398 * <p>getFactoryId</p>
400 * @return a {@link java.lang.String} object.
403 public String
getFactoryId() {
404 return TaxonEditorInputFactory
.getFactoryId();
408 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
412 public void saveState(IMemento memento
) {
413 TaxonEditorInputFactory
.saveState(memento
, this);
418 * <p>Setter for the field <code>initiallySelectedTaxonBase</code>.</p>
420 * @param taxonBase a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
422 public void setInitiallySelectedTaxonBase(TaxonBase taxonBase
) {
423 this.initiallySelectedTaxonBase
= taxonBase
;
427 * <p>Getter for the field <code>initiallySelectedTaxonBase</code>.</p>
429 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
431 public TaxonBase
getInitiallySelectedTaxonBase() {
432 return initiallySelectedTaxonBase
;
436 // public String toString() {
437 // return String.format("%s[%s]", this.getClass().getSimpleName(), getTaxon());
442 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
445 public List
<TaxonNode
> getRootEntities() {
446 return Arrays
.asList(taxonNode
);
450 * @see eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput#merge()
453 public void merge() {
454 CdmStore
.getService(ITaxonNodeService
.class).merge(taxonNode
);
458 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
462 private List
<String
> getTaxonNodePropertyPaths() {
463 List
<String
> taxonNodePropertyPaths
= new ArrayList
<String
>();
464 for(String propertyPath
: getTaxonBasePropertyPaths()) {
465 taxonNodePropertyPaths
.add("taxon." + propertyPath
);
467 return taxonNodePropertyPaths
;
470 private List
<String
> getTaxonBasePropertyPaths() {
471 List
<String
> taxonBasePropertyPaths
= Arrays
.asList(new String
[] {
482 "relationsToThisTaxon",
483 "relationsFromThisTaxon",
485 "descriptions.descriptionElements.feature",
486 "descriptions.descriptionElements.area",
487 "descriptions.descriptionElements.status",
488 "descriptions.markers",
490 "name.typeDesignations",
492 "name.nomenclaturalReference.inReference",
493 "name.taxonBases.taxonNodes",
494 "name.relationsFromThisName",
495 "name.relationsToThisName",
496 "name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations.synonym.name.status",
497 "name.homotypicalGroup.typifiedNames.relationsToThisName.fromName",
498 "synonymRelations.synonym.name.status.type",
499 "synonymRelations.synonym.name.relationsToThisName.fromName",
500 "synonymRelations.synonym.name.nomenclaturalReference.inReference.authorship",
501 "synonymRelations.synonym.name.nomenclaturalReference.authorship",
502 "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations"
505 return taxonBasePropertyPaths
;