1
|
package eu.etaxonomy.taxeditor.navigation.navigator.handler;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.List;
|
5
|
import java.util.UUID;
|
6
|
|
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;
|
19
|
|
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;
|
38
|
|
39
|
public class MoveFactualDataHandler extends AbstractHandler {
|
40
|
|
41
|
@SuppressWarnings("unused")
|
42
|
private static final Logger logger = Logger.getLogger(MoveFactualDataHandler.class);
|
43
|
|
44
|
@Override
|
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;
|
51
|
}
|
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.");
|
60
|
return null;
|
61
|
}
|
62
|
//reload to avoid session conflicts
|
63
|
Taxon taxon = HibernateProxyHelper.deproxy(CdmStore.getService(ITaxonService.class).load(sourceTaxonNode.getTaxon().getUuid()), Taxon.class);
|
64
|
if(taxon!=null){
|
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",
|
71
|
excludeTaxa,
|
72
|
null,
|
73
|
null);
|
74
|
if (dialogTaxonNode == null) {
|
75
|
return null;
|
76
|
}
|
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.");
|
82
|
return null;
|
83
|
}
|
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);
|
87
|
|
88
|
String moveMessage = String.format("Description moved from %s", taxon);
|
89
|
if(description.isProtectedTitleCache()){
|
90
|
String separator = "";
|
91
|
if(!StringUtils.isBlank(description.getTitleCache())){
|
92
|
separator = " - ";
|
93
|
}
|
94
|
description.setTitleCache(description.getTitleCache() + separator + moveMessage, true);
|
95
|
}
|
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();
|
103
|
}
|
104
|
|
105
|
Display.getDefault().asyncExec(new Runnable(){
|
106
|
|
107
|
@Override
|
108
|
public void run() {
|
109
|
try {
|
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);
|
116
|
}
|
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());
|
124
|
}
|
125
|
}
|
126
|
|
127
|
});
|
128
|
}
|
129
|
}
|
130
|
}
|
131
|
return null;
|
132
|
}
|
133
|
|
134
|
}
|