1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.taxeditor.editor.name.operation;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.HashSet;
|
13
|
import java.util.Iterator;
|
14
|
import java.util.List;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import org.eclipse.core.commands.ExecutionException;
|
18
|
import org.eclipse.core.commands.operations.IUndoContext;
|
19
|
import org.eclipse.core.runtime.IAdaptable;
|
20
|
import org.eclipse.core.runtime.IProgressMonitor;
|
21
|
import org.eclipse.core.runtime.IStatus;
|
22
|
|
23
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
24
|
import eu.etaxonomy.cdm.api.service.UpdateResult;
|
25
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
26
|
import eu.etaxonomy.cdm.model.media.ExternalLink;
|
27
|
import eu.etaxonomy.cdm.model.media.ExternalLinkType;
|
28
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
29
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
30
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
31
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
32
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
33
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
34
|
import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
|
35
|
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
|
36
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
|
37
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
38
|
|
39
|
/**
|
40
|
* <p>SwapSynonymAndAcceptedOperation class.</p>
|
41
|
*
|
42
|
* @author n.hoffmann
|
43
|
* @created 23.04.2009
|
44
|
*/
|
45
|
public class SwapSynonymAndAcceptedOperation extends AbstractPostTaxonOperation {
|
46
|
|
47
|
private final Synonym synonym;
|
48
|
|
49
|
private boolean setNameInSource;
|
50
|
|
51
|
/**
|
52
|
* <p>Constructor for SwapSynonymAndAcceptedOperation.</p>
|
53
|
*
|
54
|
* @param label a {@link java.lang.String} object.
|
55
|
* @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
|
56
|
* @param taxon a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
|
57
|
* @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
|
58
|
* @param synonym a {@link eu.etaxonomy.cdm.model.taxon.Synonym} object.
|
59
|
*/
|
60
|
public SwapSynonymAndAcceptedOperation(String label, IUndoContext undoContext,
|
61
|
Taxon taxon, Synonym synonym, IPostOperationEnabled postOperationEnabled,
|
62
|
ICdmEntitySessionEnabled cdmEntitySessionEnabled, boolean setNameInSource) {
|
63
|
super(Messages.SwapSynonymAndAcceptedOperation_SWAP_SYN_ACC_TAXON, undoContext, taxon, postOperationEnabled, cdmEntitySessionEnabled);
|
64
|
this.setNameInSource = setNameInSource;
|
65
|
this.synonym = synonym;
|
66
|
}
|
67
|
|
68
|
@Override
|
69
|
public IStatus execute(IProgressMonitor monitor, IAdaptable info)
|
70
|
throws ExecutionException {
|
71
|
|
72
|
monitor.worked(20);
|
73
|
UpdateResult result = CdmStore.getService(ITaxonService.class).swapSynonymAndAcceptedTaxon(synonym.getUuid(), element.getUuid(), setNameInSource, false);//TODO
|
74
|
// element = (Taxon) result.getCdmEntity();
|
75
|
element = (Taxon)CdmStore.getService(ITaxonService.class).load(result.getCdmEntity().getUuid());
|
76
|
Synonym newSyn = null;
|
77
|
if (!result.getUpdatedObjects().isEmpty()){
|
78
|
Iterator<CdmBase> it = result.getUpdatedObjects().iterator();
|
79
|
while(it.hasNext()){
|
80
|
CdmBase tb = it.next();
|
81
|
if (tb instanceof Synonym){
|
82
|
newSyn = (Synonym)tb;
|
83
|
}
|
84
|
}
|
85
|
newSyn = (Synonym)CdmStore.getService(ITaxonService.class).load(result.getUpdatedObjects().iterator().next().getUuid());
|
86
|
}
|
87
|
Reference accRef = element.getSec();
|
88
|
String accMicroRef = element.getSecMicroReference();
|
89
|
Set<ExternalLink> accLinks = new HashSet<>();
|
90
|
|
91
|
for (ExternalLink link: element.getSecSource().getLinks()){
|
92
|
accLinks.add(ExternalLink.NewInstance(ExternalLinkType.Unknown, link.getUri()));
|
93
|
}
|
94
|
if (newSyn != null ){
|
95
|
element.setSec(newSyn.getSec());
|
96
|
element.setSecMicroReference(newSyn.getSecMicroReference());
|
97
|
element.getSecSource().getLinks().clear();
|
98
|
for (ExternalLink link: newSyn.getSecSource().getLinks()){
|
99
|
element.getSecSource().addLink(ExternalLink.NewInstance(ExternalLinkType.Unknown, link.getUri()));
|
100
|
}
|
101
|
newSyn.setSec(accRef);
|
102
|
newSyn.setSecMicroReference(accMicroRef);
|
103
|
newSyn.getSecSource().getLinks().clear();
|
104
|
for (ExternalLink link: accLinks){
|
105
|
newSyn.getSecSource().addLink(link);
|
106
|
}
|
107
|
}
|
108
|
|
109
|
|
110
|
List<TaxonBase> mergeList = new ArrayList<>();
|
111
|
mergeList.add(element);
|
112
|
mergeList.add(newSyn);
|
113
|
|
114
|
CdmStore.getService(ITaxonService.class).merge(mergeList, true);
|
115
|
monitor.worked(40);
|
116
|
|
117
|
return postExecute(element);
|
118
|
}
|
119
|
|
120
|
@Override
|
121
|
public IStatus redo(IProgressMonitor monitor, IAdaptable info)
|
122
|
throws ExecutionException {
|
123
|
return execute(monitor, info);
|
124
|
}
|
125
|
|
126
|
@Override
|
127
|
public IStatus undo(IProgressMonitor monitor, IAdaptable info)
|
128
|
throws ExecutionException {
|
129
|
MessagingUtils.error(this.getClass(), Messages.SwapSynonymAndAcceptedOperation_NOT_IMPLEMENTED, null);
|
130
|
return null;
|
131
|
}
|
132
|
}
|