Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / handler / MoveDescriptionElementsHandler.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.view.descriptive.handler;
12
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.UUID;
16
17 import org.eclipse.core.commands.AbstractHandler;
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.core.commands.common.NotDefinedException;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.swt.widgets.Display;
24 import org.eclipse.ui.PartInitException;
25 import org.eclipse.ui.handlers.HandlerUtil;
26
27 import eu.etaxonomy.cdm.api.service.IDescriptionService;
28 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
29 import eu.etaxonomy.cdm.model.common.Annotation;
30 import eu.etaxonomy.cdm.model.common.AnnotationType;
31 import eu.etaxonomy.cdm.model.common.CdmBase;
32 import eu.etaxonomy.cdm.model.common.Language;
33 import eu.etaxonomy.cdm.model.description.DescriptionBase;
34 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
35 import eu.etaxonomy.cdm.model.description.TaxonDescription;
36 import eu.etaxonomy.cdm.model.taxon.Classification;
37 import eu.etaxonomy.cdm.model.taxon.Taxon;
38 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
39 import eu.etaxonomy.taxeditor.editor.EditorUtil;
40 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
41 import eu.etaxonomy.taxeditor.editor.Page;
42 import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
43 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.MoveDescriptionElementsOperation;
44 import eu.etaxonomy.taxeditor.model.AbstractUtility;
45 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
46 import eu.etaxonomy.taxeditor.model.MessagingUtils;
47 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
48 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
49 import eu.etaxonomy.taxeditor.store.CdmStore;
50 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
51
52
53 /**
54 * @author n.hoffmann
55 * @created Feb 8, 2011
56 * @version 1.0
57 */
58 public class MoveDescriptionElementsHandler extends AbstractHandler implements IPostOperationEnabled{
59 private UUID newAcceptedTaxonNodeUuid;
60 private TaxonNameEditor editor;
61 /* (non-Javadoc)
62 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
63 */
64 @Override
65 public Object execute(ExecutionEvent event) throws ExecutionException {
66
67 // ConversationHolder conversation = CdmStore.createConversation();
68 editor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
69 ISelection selection = HandlerUtil.getCurrentSelection(event);
70 Taxon actualTaxon= null;
71 if(selection instanceof IStructuredSelection){
72
73 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
74
75 List<DescriptionElementBase> elements = new ArrayList<DescriptionElementBase>();
76
77 for(Object element : structuredSelection.toArray()){
78 if(element instanceof DescriptionElementBase){
79 UUID uuid = ((DescriptionElementBase) element).getUuid();
80
81 elements.add(CdmStore.getService(IDescriptionService.class).loadDescriptionElement(uuid, null));
82 } else if(element instanceof FeatureNodeContainer){
83 for(DescriptionElementBase de : ((FeatureNodeContainer)element).getDescriptionElements()){
84 elements.add(
85 CdmStore.getService(IDescriptionService.class).loadDescriptionElement(de.getUuid(), null)
86 );
87 }
88 }
89 }
90
91 if(elements.size() == 0){
92 return null;
93 }
94 DescriptionBase description = elements.get(0).getInDescription();
95 List<UUID> excludeTaxa = new ArrayList<UUID>();
96 if (description.isInstanceOf(TaxonDescription.class)){
97 TaxonDescription taxonDescription = HibernateProxyHelper.deproxy(description, TaxonDescription.class);
98 actualTaxon = taxonDescription.getTaxon();
99 excludeTaxa.add(actualTaxon.getUuid());
100 }
101 Classification classification = null;
102 if (actualTaxon != null){
103 if (!actualTaxon.getTaxonNodes().isEmpty() && actualTaxon.getTaxonNodes().size() ==1){
104 classification = actualTaxon.getTaxonNodes().iterator().next().getClassification();
105 }
106 }
107 TaxonNode newAcceptedTaxonNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event),
108 editor.getConversationHolder(),
109 "Choose the accepted taxon",
110 excludeTaxa,
111 null,classification
112 );
113 if (newAcceptedTaxonNode != null){
114 Taxon targetTaxon = newAcceptedTaxonNode.getTaxon();
115
116 if(targetTaxon == null){
117 // canceled
118 return null;
119 }
120 newAcceptedTaxonNodeUuid = newAcceptedTaxonNode.getUuid();
121 TaxonDescription targetDescription = TaxonDescription.NewInstance(targetTaxon);
122 String moveMessage = String.format("Elements moved from %s", EditorUtil.getActiveMultiPageTaxonEditor().getTaxon());
123 targetDescription.setTitleCache(moveMessage, true);
124 Annotation annotation = Annotation.NewInstance(moveMessage, Language.getDefaultLanguage());
125 annotation.setAnnotationType(AnnotationType.TECHNICAL());
126 targetDescription.addAnnotation(annotation);
127
128 try {
129 AbstractPostOperation operation = new MoveDescriptionElementsOperation(
130 event.getCommand().getName(), EditorUtil.getUndoContext(),
131 targetDescription, elements, false, this);
132
133 EditorUtil.executeOperation(operation);
134
135 //CdmStore.getService(ITaxonService.class).saveOrUpdate(targetTaxon);
136
137 } catch (NotDefinedException e) {
138 MessagingUtils.error(getClass(), e);
139 }
140 }
141 }
142
143 return null;
144 }
145
146 /* (non-Javadoc)
147 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
148 */
149 /** {@inheritDoc} */
150 @Override
151 public boolean postOperation(CdmBase objectAffectedByOperation) {
152
153 editor.getConversationHolder().bind();
154 editor.getConversationHolder().commit(true);
155 Display.getDefault().asyncExec(new Runnable(){
156
157 @Override
158 public void run() {
159 //AbstractUtility.close(editor.getMultiPageTaxonEditor());
160
161 try {
162 MultiPageTaxonEditor possibleOpenEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(newAcceptedTaxonNodeUuid);
163 if(possibleOpenEditor != null){
164 AbstractUtility.close(possibleOpenEditor);
165 }
166 EditorUtil.openTaxonNode(newAcceptedTaxonNodeUuid);
167 } catch (PartInitException e) {
168 MessagingUtils.error(this.getClass(), e);
169 throw new RuntimeException(e);
170 } catch (Exception e) {
171 MessagingUtils.warningDialog("Could not create Taxon", this, e.getMessage());
172 }
173 }
174
175 });
176
177
178 return true;
179 }
180
181 @Override
182 public boolean onComplete() {
183 // TODO Auto-generated method stub
184 return false;
185 }
186
187 }