Moved all logging and dialog functionality to the new class MessagingUtils.
[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.ui.handlers.HandlerUtil;
24
25 import eu.etaxonomy.cdm.api.service.IDescriptionService;
26 import eu.etaxonomy.cdm.api.service.ITaxonService;
27 import eu.etaxonomy.cdm.model.common.Annotation;
28 import eu.etaxonomy.cdm.model.common.AnnotationType;
29 import eu.etaxonomy.cdm.model.common.Language;
30 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
31 import eu.etaxonomy.cdm.model.description.TaxonDescription;
32 import eu.etaxonomy.cdm.model.taxon.Taxon;
33 import eu.etaxonomy.taxeditor.editor.EditorUtil;
34 import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveViewPart;
35 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.MoveDescriptionElementsOperation;
36 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
37 import eu.etaxonomy.taxeditor.model.MessagingUtils;
38 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
39 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
40 import eu.etaxonomy.taxeditor.store.CdmStore;
41 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonBaseSelectionDialog;
42
43
44 /**
45 * @author n.hoffmann
46 * @created Feb 8, 2011
47 * @version 1.0
48 */
49 public class MoveDescriptionElementsHandler extends AbstractHandler {
50
51 /* (non-Javadoc)
52 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
53 */
54 @Override
55 public Object execute(ExecutionEvent event) throws ExecutionException {
56
57 // ConversationHolder conversation = CdmStore.createConversation();
58
59 ISelection selection = HandlerUtil.getCurrentSelection(event);
60
61 if(selection instanceof IStructuredSelection){
62
63 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
64
65 List<DescriptionElementBase> elements = new ArrayList<DescriptionElementBase>();
66
67 for(Object element : structuredSelection.toArray()){
68 if(element instanceof DescriptionElementBase){
69 UUID uuid = ((DescriptionElementBase) element).getUuid();
70
71 elements.add(CdmStore.getService(IDescriptionService.class).loadDescriptionElement(uuid, null));
72 } else if(element instanceof FeatureNodeContainer){
73 for(DescriptionElementBase de : ((FeatureNodeContainer)element).getDescriptionElements()){
74 elements.add(
75 (DescriptionElementBase)CdmStore.getService(IDescriptionService.class).loadDescriptionElement(de.getUuid(), null)
76 );
77 }
78 }
79 }
80
81 if(elements.size() == 0){
82 return null;
83 }
84
85 Taxon targetTaxon = TaxonBaseSelectionDialog.selectTaxon(HandlerUtil.getActiveShell(event), EditorUtil.getActiveMultiPageTaxonEditor().getConversationHolder(), null);
86
87 if(targetTaxon == null){
88 // canceled
89 return null;
90 }
91
92 TaxonDescription targetDescription = TaxonDescription.NewInstance(targetTaxon);
93 String moveMessage = String.format("Elements moved from %s", EditorUtil.getActiveMultiPageTaxonEditor().getTaxon());
94 targetDescription.setTitleCache(moveMessage, true);
95 Annotation annotation = Annotation.NewInstance(moveMessage, Language.getDefaultLanguage());
96 annotation.setAnnotationType(AnnotationType.TECHNICAL());
97 targetDescription.addAnnotation(annotation);
98
99 try {
100 AbstractPostOperation operation = new MoveDescriptionElementsOperation(
101 event.getCommand().getName(), EditorUtil.getUndoContext(),
102 targetDescription, elements, false, (IPostOperationEnabled) EditorUtil.getView(DescriptiveViewPart.ID, true));
103 EditorUtil.executeOperation(operation);
104 // conversation.commit(true);
105 CdmStore.getService(ITaxonService.class).saveOrUpdate(targetTaxon);
106
107 } catch (NotDefinedException e) {
108 MessagingUtils.error(getClass(), e);
109 }
110 }
111
112 return null;
113 }
114
115 }