1 package eu
.etaxonomy
.taxeditor
.navigation
.navigator
.handler
;
3 import java
.util
.ArrayList
;
7 import org
.apache
.commons
.lang
.StringUtils
;
8 import org
.apache
.log4j
.Logger
;
9 import org
.eclipse
.core
.commands
.AbstractHandler
;
10 import org
.eclipse
.core
.commands
.ExecutionEvent
;
11 import org
.eclipse
.core
.commands
.ExecutionException
;
12 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
13 import org
.eclipse
.jface
.viewers
.ISelection
;
14 import org
.eclipse
.jface
.viewers
.TreeSelection
;
15 import org
.eclipse
.swt
.widgets
.Display
;
16 import org
.eclipse
.ui
.IWorkbenchPart
;
17 import org
.eclipse
.ui
.PartInitException
;
18 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
20 import eu
.etaxonomy
.cdm
.api
.service
.IDescriptionService
;
21 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonNodeService
;
22 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
23 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
24 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
25 import eu
.etaxonomy
.cdm
.model
.common
.AnnotationType
;
26 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
27 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
30 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
31 import eu
.etaxonomy
.taxeditor
.editor
.MultiPageTaxonEditor
;
32 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
33 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
34 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
35 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.TaxonNavigator
;
36 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
37 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.TaxonNodeSelectionDialog
;
39 public class MoveFactualDataHandler
extends AbstractHandler
{
41 @SuppressWarnings("unused")
42 private static final Logger logger
= Logger
.getLogger(MoveFactualDataHandler
.class);
45 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
46 ISelection currentSelection
= HandlerUtil
.getCurrentSelection(event
);
47 IWorkbenchPart activePart
= AbstractUtility
.getActivePart();
48 TaxonNavigator navigator
= null;
49 if(activePart
instanceof TaxonNavigator
){
50 navigator
= (TaxonNavigator
)activePart
;
52 if(navigator
!=null && currentSelection
instanceof TreeSelection
){
53 Object object
= ((TreeSelection
) currentSelection
).getFirstElement();
54 if(object
instanceof TaxonNode
){
55 TaxonNode taxonNode
= HibernateProxyHelper
.deproxy(object
, TaxonNode
.class);
56 final TaxonNode sourceTaxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNode
.getUuid());
57 if(NavigationUtil
.isDirty(sourceTaxonNode
)){
58 MessageDialog
.openWarning(HandlerUtil
.getActiveShell(event
), "Unsaved Source Taxon", "There are unsaved " +
59 "changes in the source taxon. Please save first.");
62 //reload to avoid session conflicts
63 Taxon taxon
= HibernateProxyHelper
.deproxy(CdmStore
.getService(ITaxonService
.class).load(sourceTaxonNode
.getTaxon().getUuid()), Taxon
.class);
65 // Choose the target taxon
66 List
<UUID
> excludeTaxa
= new ArrayList
<UUID
>();
67 excludeTaxa
.add(taxon
.getUuid());
68 TaxonNode dialogTaxonNode
= TaxonNodeSelectionDialog
.select(HandlerUtil
.getActiveShell(event
),
69 navigator
.getConversationHolder(),
70 "Choose the accepted taxon",
74 if (dialogTaxonNode
== null) {
77 //reload to avoid session conflicts
78 final TaxonNode targetTaxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(dialogTaxonNode
.getUuid());
79 if(NavigationUtil
.isDirty(targetTaxonNode
)){
80 MessageDialog
.openWarning(HandlerUtil
.getActiveShell(event
), "Unsaved Target Taxon", "There are unsaved " +
81 "changes in the target taxon. Please save first.");
84 for(TaxonDescription description
: taxon
.getDescriptions()){
85 //reload to avoid session conflicts
86 description
= HibernateProxyHelper
.deproxy(CdmStore
.getService(IDescriptionService
.class).load(description
.getUuid()), TaxonDescription
.class);
88 String moveMessage
= String
.format("Description moved from %s", taxon
);
89 if(description
.isProtectedTitleCache()){
90 String separator
= "";
91 if(!StringUtils
.isBlank(description
.getTitleCache())){
94 description
.setTitleCache(description
.getTitleCache() + separator
+ moveMessage
, true);
96 Annotation annotation
= Annotation
.NewInstance(moveMessage
, Language
.getDefaultLanguage());
97 annotation
.setAnnotationType(AnnotationType
.TECHNICAL());
98 description
.addAnnotation(annotation
);
99 targetTaxonNode
.getTaxon().addDescription(description
);
100 CdmStore
.getService(IDescriptionService
.class).saveOrUpdate(description
);
101 navigator
.getConversationHolder().bind();
102 navigator
.getConversationHolder().commit();
105 Display
.getDefault().asyncExec(new Runnable(){
110 //close and re-open to refresh factual data view
111 MultiPageTaxonEditor sourceEditor
= (MultiPageTaxonEditor
) EditorUtil
.findEditorByTaxonNodeUuid(sourceTaxonNode
.getUuid());
112 MultiPageTaxonEditor targetEditor
= (MultiPageTaxonEditor
) EditorUtil
.findEditorByTaxonNodeUuid(targetTaxonNode
.getUuid());
113 if(targetEditor
!= null){
114 AbstractUtility
.close(sourceEditor
);
115 AbstractUtility
.close(targetEditor
);
117 EditorUtil
.openTaxonNode(sourceTaxonNode
.getUuid());
118 EditorUtil
.openTaxonNode(targetTaxonNode
.getUuid());
119 } catch (PartInitException e
) {
120 MessagingUtils
.error(this.getClass(), e
);
121 throw new RuntimeException(e
);
122 } catch (Exception e
) {
123 MessagingUtils
.warningDialog("Could not create Taxon", this, e
.getMessage());