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