Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / handler / ChangeSynonymToAcceptedTaxonHandler.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.taxeditor.editor.name.handler;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Set;
15 import java.util.UUID;
16
17 import org.apache.log4j.Logger;
18 import org.eclipse.core.commands.AbstractHandler;
19 import org.eclipse.core.commands.ExecutionEvent;
20 import org.eclipse.core.commands.ExecutionException;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.ui.IEditorInput;
24 import org.eclipse.ui.PartInitException;
25 import org.eclipse.ui.handlers.HandlerUtil;
26
27 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
28 import eu.etaxonomy.cdm.model.common.CdmBase;
29 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
30 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
31 import eu.etaxonomy.cdm.model.taxon.Synonym;
32 import eu.etaxonomy.cdm.model.taxon.Taxon;
33 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
34 import eu.etaxonomy.taxeditor.editor.EditorUtil;
35 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
36 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
37 import eu.etaxonomy.taxeditor.editor.name.operation.ChangeSynonymToAcceptedTaxonOperation;
38 import eu.etaxonomy.taxeditor.model.MessagingUtils;
39 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
40 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
41 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
42 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
43
44 /**
45 * <p>ChangeSynonymToAcceptedTaxonHandler class.</p>
46 *
47 * @author n.hoffmann
48 * @created 21.04.2009
49 * @version 1.0
50 */
51 public class ChangeSynonymToAcceptedTaxonHandler extends AbstractHandler implements IPostOperationEnabled {
52 private static final Logger logger = Logger
53 .getLogger(ChangeSynonymToAcceptedTaxonHandler.class);
54 private MultiPageTaxonEditor editor;
55
56 /** {@inheritDoc} */
57 @Override
58 public Object execute(ExecutionEvent event) throws ExecutionException {
59 editor = (MultiPageTaxonEditor) HandlerUtil.getActiveEditor(event);
60 Shell shell = HandlerUtil.getActiveShell(event);
61 IEditorInput input = editor.getEditorInput();
62
63 if (!(input instanceof TaxonEditorInput)) {
64 logger.error("Editor input is not TaxonEditorInput");
65 return null;
66 }
67
68 // Get synonym from selection
69 StructuredSelection selection = (StructuredSelection) HandlerUtil.getCurrentSelection(event);
70 if (!(selection.getFirstElement() instanceof Synonym)) {
71 logger.error("Selection does not contain a Synonym");
72 return null;
73 }
74
75 Synonym synonym = (Synonym) selection.getFirstElement();
76
77 // Force user to save taxon - not really necessary though, is it?
78 if (!EditorUtil.forceUserSave(editor, shell)) {
79 return null;
80 }
81
82 // Get taxon
83 Taxon taxon = ((TaxonEditorInput) input).getTaxon();
84
85 TaxonNode parentNode = (TaxonNode) HibernateProxyHelper.deproxy(((TaxonEditorInput) input).getTaxonNode().getParent());
86
87 List<UUID> excludeTaxa = new ArrayList<UUID>();
88 //excludeTaxa.add(taxon.getUuid());//there are some cases where the accepted taxon should be the parent of the new created accepted taxon
89
90 TaxonNode newParentNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event), editor.getConversationHolder(), "Select parent", null, null, ((TaxonEditorInput) input).getTaxonNode().getClassification());
91
92
93 if(newParentNode != null){
94
95 // TODO get synonyms from homotypical group and add them as homotypic synonyms to new accepted taxon
96 // apply confirmation dialog
97 HomotypicalGroup group = synonym.getHomotypicGroup();
98 Set<TaxonNameBase> namesInGroup = group.getTypifiedNames();
99 // FIXME with this implementation we can not create a taxonNode that is a direct child of the classification node
100 AbstractPostOperation operation = new ChangeSynonymToAcceptedTaxonOperation("Change synonym to accepted taxon", EditorUtil.getUndoContext(),
101 taxon, newParentNode, synonym, namesInGroup, this, editor, (ICdmEntitySessionEnabled)editor.getEditorInput()); //$NON-NLS-1$
102
103 EditorUtil.executeOperation(operation);
104 }
105
106 return null;
107 }
108
109 /* (non-Javadoc)
110 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
111 */
112 /** {@inheritDoc} */
113 @Override
114 public boolean postOperation(CdmBase objectAffectedByOperation) {
115
116 // Redraw existing editor
117 ((IPostOperationEnabled) editor).postOperation(null);
118
119 editor.doSave(EditorUtil.getMonitor());
120
121 if (objectAffectedByOperation instanceof TaxonNode) {
122
123 // Open new unsaved editor with existing taxon's parent as temporary parent
124 TaxonNode newNode = (TaxonNode) objectAffectedByOperation;
125 // TaxonNode newNode = parentNode.addChild(newTaxon);
126
127 try {
128 // TODO
129 /*
130 * This doesn't work b/c newNode hasn't been committed yet, and therefore
131 * CdmStore.getTaxonService().getTaxonNodeByUuid(taxonNodeUuid);
132 * doesn't work yet.
133 */
134 EditorUtil.openTaxonNode(newNode.getUuid());
135
136 } catch (PartInitException e) {
137 // TODO Auto-generated catch block
138 e.printStackTrace();
139 } catch (Exception e) {
140 MessagingUtils.warningDialog("Could not create Taxon", this, e.getMessage());
141 }
142 }
143 return true;
144 }
145
146 /**
147 * <p>onComplete</p>
148 *
149 * @return a boolean.
150 */
151 @Override
152 public boolean onComplete() {
153 // TODO Auto-generated method stub
154 return false;
155 }
156 }