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
) {
71 this.conversation
= CdmStore
.createConversation();
73 case PARENT_TAXON_NODE
:
74 initForParentTaxonNode(uuid
);
77 initForTaxonBase(uuid
);
80 initForTaxonNode(uuid
);
85 private void init(TaxonNode taxonNode
) {
86 this.taxonNode
= taxonNode
;
93 * @param taxonNodeUuid a {@link java.util.UUID} object.
94 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
95 * @throws java.lang.Exception if any.
97 private void initForTaxonNode(UUID taxonNodeUuid
) {
100 TaxonNode taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNodeUuid
, getTaxonNodePropertyPaths());
102 if(taxonNode
== null){
103 MessagingUtils
.warningDialog("Not yet implemented", TaxonEditorInput
.class, "Selected element is not type TaxonBase.");
109 private void initForTaxonBase(UUID taxonBaseUuid
) {
110 TaxonBase taxonBase
= CdmStore
.getService(ITaxonService
.class).load(taxonBaseUuid
, getTaxonBasePropertyPaths());
111 if (taxonBase
!= null){
112 if(taxonBase
.isInstanceOf(Taxon
.class)){
113 Taxon taxon
= CdmBase
.deproxy(taxonBase
, Taxon
.class);
115 if (taxon
.getTaxonNodes().size() == 0 && taxon
.isMisapplication()){
116 // TODO get accepted taxon
117 MessagingUtils
.info("trying to open Mispplied Name ");
119 Set
<Taxon
> acceptedTaxa
= new HashSet
<Taxon
>();
120 Set
<TaxonRelationship
> relations
= taxon
.getRelationsFromThisTaxon();
121 for(TaxonRelationship relation
: relations
){
122 if(relation
.getType().equals(TaxonRelationshipType
.MISAPPLIED_NAME_FOR())){
123 acceptedTaxa
.add(relation
.getToTaxon());
126 setInputForMultipleTaxa(conversation
, acceptedTaxa
);
129 setInputForMultipleNodes(conversation
, taxon
.getTaxonNodes());
131 }else if(taxonBase
instanceof Synonym
){
132 Synonym synonym
= (Synonym
) taxonBase
;
134 Set
<Taxon
> taxa
= new HashSet
<>();
135 Taxon taxon
= synonym
.getAcceptedTaxon();
139 setInputForMultipleTaxa(conversation
, taxa
);
146 * <p>NewEmptyInstance</p>
148 * @param parentNodeUuid a {@link java.util.UUID} object.
149 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
151 private void initForParentTaxonNode(UUID parentNodeUuid
){
154 TaxonNameBase
<?
, ?
> name
= PreferencesUtil
.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
155 ITaxonTreeNode parentNode
= CdmStore
.getService(IClassificationService
.class).getTreeNodeByUuid(parentNodeUuid
);
157 Taxon newTaxon
= Taxon
.NewInstance(name
, parentNode
.getReference());
158 TaxonNode newTaxonNode
= parentNode
.addChildTaxon(newTaxon
, parentNode
.getReference(), parentNode
.getMicroReference());
160 // add the new taxon to the editors persistence context
161 UUID newTaxonNodeUuid
= CdmStore
.getService(ITaxonNodeService
.class).save(newTaxonNode
).getUuid();
163 initForTaxonNode(newTaxonNodeUuid
);
169 private void setInputForMultipleNodes(ConversationHolder conversation
, Set
<TaxonNode
> taxonNodes
){
170 if(taxonNodes
.size() == 1){
171 TaxonNode taxonNode
= taxonNodes
.iterator().next();
173 }else if(taxonNodes
.size() > 1){
174 TaxonNode taxonNode
= ChooseFromMultipleTaxonNodesDialog
.choose(taxonNodes
);
175 if(taxonNode
!= null){
178 } else if (taxonNodes
.size() == 0) {
179 // this is an undesired state
180 MessagingUtils
.warningDialog("Incorrect state",TaxonEditorInput
.class,"The accepted "
181 + "taxon is not part of any classification. Editing with the "
182 + "name editor is currently not implemented. Try to edit the taxon with the bulk editor.");
186 private void setInputForMultipleTaxa(ConversationHolder conversation
, Set
<Taxon
> taxa
){
187 if(taxa
.size() == 1){
188 Taxon taxon
= taxa
.iterator().next();
189 Set
<TaxonNode
> nodes
= taxon
.getTaxonNodes();
190 setInputForMultipleNodes(conversation
, nodes
);
191 }else if(taxa
.size() > 1){
192 Set
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
193 for ( Taxon taxon
: taxa
){
194 taxonNodes
.addAll(taxon
.getTaxonNodes());
196 setInputForMultipleNodes(conversation
, taxonNodes
);
197 }else if(taxa
.size() == 0){
198 // this is an undesired state
199 MessagingUtils
.warningDialog("Incorrect state", TaxonEditorInput
.class, "Trying to open accepted taxon for a synonym or misapplication but" +
200 " no accepted taxa are present. This should not have happened.");
207 * @param taxonNodeUuid a {@link java.util.UUID} object.
208 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
209 * @throws java.lang.Exception if any.
211 public static TaxonEditorInput
NewInstance(UUID taxonNodeUuid
) throws Exception
{
212 return new TaxonEditorInput(taxonNodeUuid
, CdmType
.TAXON_NODE
);
217 * <p>NewInstanceFromTaxonBase</p>
219 * @param taxonBaseUuid a {@link java.util.UUID} object.
220 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
222 public static TaxonEditorInput
NewInstanceFromTaxonBase(UUID taxonBaseUuid
){
223 return new TaxonEditorInput(taxonBaseUuid
, CdmType
.TAXON_BASE
);
228 * <p>NewEmptyInstance</p>
230 * @param parentNodeUuid a {@link java.util.UUID} object.
231 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
233 public static TaxonEditorInput
NewEmptyInstance(UUID parentNodeUuid
){
234 return new TaxonEditorInput(parentNodeUuid
, CdmType
.PARENT_TAXON_NODE
);
238 * @see org.eclipse.ui.IEditorInput#exists()
246 public boolean exists() {
247 return taxonNode
!= null;
251 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
254 * <p>getImageDescriptor</p>
256 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
259 public ImageDescriptor
getImageDescriptor() {
264 * @see org.eclipse.ui.IEditorInput#getName()
269 * @return a {@link java.lang.String} object.
272 public String
getName() {
273 if(getTaxon() == null){
276 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
277 if (name
== null || name
.getTitleCache() == null) {
280 return name
.getTitleCache();
285 * @see org.eclipse.ui.IEditorInput#getPersistable()
288 * <p>getPersistable</p>
290 * @return a {@link org.eclipse.ui.IPersistableElement} object.
293 public IPersistableElement
getPersistable() {
294 // if(CdmStore.isActive()){
295 // TaxonNode test = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid());
296 // boolean isPersistable = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid()) != null;
297 // if (isPersistable) {
305 * @see org.eclipse.ui.IEditorInput#getToolTipText()
308 * <p>getToolTipText</p>
310 * @return a {@link java.lang.String} object.
313 public String
getToolTipText() {
318 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
322 public Object
getAdapter(Class adapter
) {
324 if (adapter
== Taxon
.class) {
328 if (adapter
== TaxonNode
.class) {
338 * Overrides equals to ensure that a taxon can only be edited by
339 * one editor at a time.
342 public boolean equals(Object obj
) {
343 if (TaxonEditorInput
.class.equals(obj
.getClass())
344 && getTaxon() != null
345 && getTaxon().equals(((TaxonEditorInput
) obj
).getTaxon())) {
346 if (((TaxonEditorInput
) obj
).getInitiallySelectedTaxonBase() != null){
347 setInitiallySelectedTaxonBase(((TaxonEditorInput
) obj
).getInitiallySelectedTaxonBase());
359 public Taxon
getTaxon(){
360 Taxon taxon
= CdmBase
.deproxy(taxonNode
.getTaxon(), Taxon
.class);
365 * <p>Getter for the field <code>taxonNode</code>.</p>
367 * @return the taxonNode
369 public TaxonNode
getTaxonNode() {
375 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
378 * <p>getConversationHolder</p>
380 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
383 public ConversationHolder
getConversationHolder() {
389 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
393 public void update(CdmDataChangeMap events
) {
394 if(dataChangeBehavior
== null){
395 dataChangeBehavior
= new TaxonEditorInputDataChangeBehaviour(this);
398 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
402 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
405 * <p>getFactoryId</p>
407 * @return a {@link java.lang.String} object.
410 public String
getFactoryId() {
411 return TaxonEditorInputFactory
.getFactoryId();
415 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
419 public void saveState(IMemento memento
) {
420 TaxonEditorInputFactory
.saveState(memento
, this);
425 * <p>Setter for the field <code>initiallySelectedTaxonBase</code>.</p>
427 * @param taxonBase a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
429 public void setInitiallySelectedTaxonBase(TaxonBase taxonBase
) {
430 this.initiallySelectedTaxonBase
= taxonBase
;
434 * <p>Getter for the field <code>initiallySelectedTaxonBase</code>.</p>
436 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
438 public TaxonBase
getInitiallySelectedTaxonBase() {
439 return initiallySelectedTaxonBase
;
443 // public String toString() {
444 // return String.format("%s[%s]", this.getClass().getSimpleName(), getTaxon());
449 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
452 public List
<TaxonNode
> getRootEntities() {
453 return Arrays
.asList(taxonNode
);
457 * @see eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput#merge()
460 public void merge() {
461 CdmStore
.getService(ITaxonNodeService
.class).merge(taxonNode
, true);
466 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
470 private List
<String
> getTaxonNodePropertyPaths() {
471 List
<String
> taxonNodePropertyPaths
= new ArrayList
<String
>();
472 for(String propertyPath
: getTaxonBasePropertyPaths()) {
473 taxonNodePropertyPaths
.add("taxon." + propertyPath
);
475 return taxonNodePropertyPaths
;
478 private List
<String
> getTaxonBasePropertyPaths() {
479 List
<String
> taxonBasePropertyPaths
= Arrays
.asList(new String
[] {
490 "relationsToThisTaxon",
491 "relationsFromThisTaxon",
493 "descriptions.descriptionElements.feature",
494 "descriptions.descriptionElements.area",
495 "descriptions.descriptionElements.status",
496 "descriptions.markers",
498 "name.typeDesignations",
500 "name.nomenclaturalReference.inReference",
501 "name.taxonBases.taxonNodes",
502 "name.relationsFromThisName",
503 "name.relationsToThisName",
504 "name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations.synonym.name.status",
505 "name.homotypicalGroup.typifiedNames.relationsToThisName.fromName",
506 "synonymRelations.synonym.name.status.type",
507 "synonymRelations.synonym.name.relationsToThisName.fromName",
508 "synonymRelations.synonym.name.nomenclaturalReference.inReference.authorship",
509 "synonymRelations.synonym.name.nomenclaturalReference.authorship",
510 "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations"
513 return taxonBasePropertyPaths
;