-public class TaxonEditorInput implements IEditorInput, IConversationEnabled, IPersistableElement {
-
- private ConversationHolder conversation;
-
- private TaxonNode taxonNode;
-
- private TaxonEditorInputDataChangeBehaviour dataChangeBehavior;
-
- private TaxonBase initiallySelectedTaxonBase;
-
- private TaxonEditorInput(TaxonNode taxonNode, ConversationHolder conversation){
- this.conversation = conversation;
- this.taxonNode = taxonNode;
- }
-
-
-
- /**
- * <p>NewInstance</p>
- *
- * @param taxonNodeUuid a {@link java.util.UUID} object.
- * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
- * @throws java.lang.Exception if any.
- */
- public static TaxonEditorInput NewInstance(UUID taxonNodeUuid) throws Exception{
- try{
- ConversationHolder conversation = CdmStore.createConversation();
- return NewInstance(taxonNodeUuid, conversation);
- }catch(Exception e){
- throw e;
- }
- }
-
+public class TaxonEditorInput extends CdmEntitySessionInput implements IEditorInput, IConversationEnabled, IPersistableElement {
+
+ private static final String INCORRECT_STATE = Messages.TaxonEditorInput_INCORRECT_STATE;
+
+ private final ConversationHolder conversation;
+
+ private TaxonNode taxonNode;
+
+ private TaxonEditorInputDataChangeBehaviour dataChangeBehavior;
+
+ private TaxonBase initiallySelectedTaxonBase;
+
+ private enum CdmType {
+ TAXON_NODE,
+ TAXON_BASE,
+ PARENT_TAXON_NODE
+ }
+
+ private TaxonEditorInput(UUID uuid, CdmType type) {
+ super(true);
+ this.conversation = CdmStore.createConversation();
+ switch(type) {
+ case PARENT_TAXON_NODE:
+ initForParentTaxonNode(uuid);
+ break;
+ case TAXON_BASE:
+ initForTaxonBase(uuid);
+ break;
+ case TAXON_NODE:
+ initForTaxonNode(uuid);
+ break;
+ }
+ }
+
+ private void init(TaxonNode taxonNode) {
+ this.taxonNode = taxonNode;
+ }
+
+
+ /**
+ * <p>NewInstance</p>
+ *
+ * @param taxonNodeUuid a {@link java.util.UUID} object.
+ * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
+ * @throws java.lang.Exception if any.
+ */
+ private void initForTaxonNode(UUID taxonNodeUuid) {
+
+
+ TaxonNode taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNodeUuid, getTaxonNodePropertyPaths());
+
+ if(taxonNode == null){
+ MessagingUtils.warningDialog(Messages.TaxonEditorInput_NOT_IMPLEMENTED, TaxonEditorInput.class, Messages.TaxonEditorInput_NOT_IMPLEMENTED_MESSAGE);
+ }
+ init(taxonNode);
+
+ }
+
+ private void initForTaxonBase(UUID taxonBaseUuid) {
+ TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).load(taxonBaseUuid, getTaxonBasePropertyPaths());
+ if (taxonBase != null){
+ if(taxonBase.isInstanceOf(Taxon.class)){
+ Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
+
+ if (taxon.getTaxonNodes().size() == 0 && taxon.isMisapplication()){
+ // TODO get accepted taxon
+ MessagingUtils.info(Messages.TaxonEditorInput_OPEN_MISSAPPLIED_NAME);
+
+ Set<Taxon> acceptedTaxa = new HashSet<Taxon>();
+ Set<TaxonRelationship> relations = taxon.getRelationsFromThisTaxon();
+ for(TaxonRelationship relation : relations){
+ if(relation.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
+ acceptedTaxa.add(relation.getToTaxon());
+ }
+ }
+ setInputForMultipleTaxa(conversation, acceptedTaxa);
+
+ }else{
+ setInputForMultipleNodes(conversation, taxon.getTaxonNodes());
+ }
+ }else if(taxonBase instanceof Synonym){
+ Synonym synonym = (Synonym) taxonBase;
+
+ Set<Taxon> taxa = new HashSet<>();
+ Taxon taxon = synonym.getAcceptedTaxon();
+ if (taxon != null){
+ taxa.add(taxon);
+ }
+ setInputForMultipleTaxa(conversation, taxa);
+ }
+ }
+ }
+
+
+ /**
+ * <p>NewEmptyInstance</p>
+ *
+ * @param parentNodeUuid a {@link java.util.UUID} object.
+ * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
+ */
+ private void initForParentTaxonNode(UUID parentNodeUuid){
+
+
+ TaxonName name = PreferencesUtil.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
+ ITaxonTreeNode parentNode = CdmStore.getService(IClassificationService.class).getTreeNodeByUuid(parentNodeUuid);
+
+ Taxon newTaxon = Taxon.NewInstance(name, parentNode.getReference());
+ TaxonNode newTaxonNode = parentNode.addChildTaxon(newTaxon, parentNode.getReference(), parentNode.getMicroReference());
+
+ // add the new taxon to the editors persistence context
+ UUID newTaxonNodeUuid = CdmStore.getService(ITaxonNodeService.class).save(newTaxonNode).getUuid();
+
+ initForTaxonNode(newTaxonNodeUuid);
+ }
+
+
+
+
+ private void setInputForMultipleNodes(ConversationHolder conversation, Set<TaxonNode> taxonNodes){
+ if(taxonNodes.size() == 1){
+ TaxonNode taxonNode = taxonNodes.iterator().next();
+ init(taxonNode);
+ }else if(taxonNodes.size() > 1){
+ TaxonNode taxonNode = ChooseFromMultipleTaxonNodesDialog.choose(taxonNodes);
+ if(taxonNode != null){
+ init(taxonNode);
+ }
+ } else if (taxonNodes.size() == 0) {
+ // this is an undesired state
+ MessagingUtils.warningDialog(INCORRECT_STATE,TaxonEditorInput.class,Messages.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION);
+ }
+ }
+
+ private void setInputForMultipleTaxa(ConversationHolder conversation, Set<Taxon> taxa){
+ if(taxa.size() == 1){
+ Taxon taxon = taxa.iterator().next();
+ Set<TaxonNode> nodes = taxon.getTaxonNodes();
+ setInputForMultipleNodes(conversation, nodes);
+ }else if(taxa.size() > 1){
+ Set<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
+ for ( Taxon taxon : taxa ){
+ taxonNodes.addAll(taxon.getTaxonNodes());
+ }
+ setInputForMultipleNodes(conversation, taxonNodes);
+ }else if(taxa.size() == 0){
+ // this is an undesired state
+ MessagingUtils.warningDialog(INCORRECT_STATE, TaxonEditorInput.class, Messages.TaxonEditorInput_NO_ACCEPTED_TAXON_PRESENT);
+ }
+ }
+