1
|
/**
|
2
|
* Copyright (C) 2015 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.operation;
|
10
|
|
11
|
import java.util.Collection;
|
12
|
import java.util.HashSet;
|
13
|
import java.util.Set;
|
14
|
|
15
|
import org.eclipse.core.runtime.IAdaptable;
|
16
|
import org.eclipse.core.runtime.IProgressMonitor;
|
17
|
import org.eclipse.core.runtime.IStatus;
|
18
|
import org.eclipse.core.runtime.MultiStatus;
|
19
|
import org.eclipse.core.runtime.Status;
|
20
|
import org.eclipse.e4.ui.model.application.MApplication;
|
21
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
22
|
import org.eclipse.e4.ui.workbench.modeling.EPartService;
|
23
|
|
24
|
import eu.etaxonomy.cdm.api.application.CdmChangeEvent.Action;
|
25
|
import eu.etaxonomy.cdm.api.service.UpdateResult;
|
26
|
|
27
|
|
28
|
/**
|
29
|
* @author cmathew
|
30
|
* @date 16 Jun 2015
|
31
|
*
|
32
|
*/
|
33
|
public abstract class RemotingCdmUpdateOperation extends RemotingCdmOperation {
|
34
|
|
35
|
protected UpdateResult updateResult;
|
36
|
public static boolean throwExceptions = false;
|
37
|
protected EPartService partService = null;
|
38
|
|
39
|
protected MPart activePart = null;
|
40
|
protected MApplication application = null;
|
41
|
|
42
|
/**
|
43
|
* @param label
|
44
|
*/
|
45
|
public RemotingCdmUpdateOperation(String label, Action action, Object source, boolean async,
|
46
|
EPartService partService,
|
47
|
MPart activePart,
|
48
|
MApplication application) {
|
49
|
super(label, action, source, async);
|
50
|
this.partService= partService;
|
51
|
this.activePart = activePart;
|
52
|
this.application = application;
|
53
|
}
|
54
|
|
55
|
public RemotingCdmUpdateOperation(String label, Action action, Object source, boolean async){
|
56
|
super(label, action, source, async);
|
57
|
|
58
|
}
|
59
|
|
60
|
/* (non-Javadoc)
|
61
|
* @see eu.etaxonomy.taxeditor.operation.RemotingCdmOperation#doExecute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
|
62
|
*/
|
63
|
@Override
|
64
|
protected boolean doExecute(IProgressMonitor monitor, IAdaptable info) {
|
65
|
updateResult = new UpdateResult();
|
66
|
try {
|
67
|
updateResult = doUpdateExecute(monitor, info);
|
68
|
} catch (Exception e) {
|
69
|
if(throwExceptions) {
|
70
|
throw new RuntimeException(e);
|
71
|
} else {
|
72
|
UpdateResult exceptionResult = new UpdateResult();
|
73
|
exceptionResult.addException(e);
|
74
|
exceptionResult.setAbort();
|
75
|
updateResult = exceptionResult;
|
76
|
}
|
77
|
}
|
78
|
return updateResult.isOk();
|
79
|
}
|
80
|
|
81
|
protected abstract UpdateResult doUpdateExecute(IProgressMonitor monitor, IAdaptable info) throws Exception;
|
82
|
|
83
|
@Override
|
84
|
protected void postExecute(boolean success) {
|
85
|
if(success && updateResult != null) {
|
86
|
fireDataChangeEvent(updateResult);
|
87
|
}
|
88
|
|
89
|
}
|
90
|
|
91
|
/* (non-Javadoc)
|
92
|
* @see eu.etaxonomy.taxeditor.operation.RemotingCdmOperation#onComplete(boolean)
|
93
|
*/
|
94
|
@Override
|
95
|
protected IStatus onComplete(boolean success) {
|
96
|
|
97
|
if(updateResult != null) {
|
98
|
int statusFlag = IStatus.OK;
|
99
|
|
100
|
Collection<Exception> exceptions = updateResult.getExceptions();
|
101
|
StringBuffer statusMsg = new StringBuffer();
|
102
|
if(success && updateResult.isOk()) {
|
103
|
if(exceptions.isEmpty()) {
|
104
|
return Status.OK_STATUS;
|
105
|
} else {
|
106
|
statusFlag = IStatus.WARNING;
|
107
|
statusMsg.append(getLabel() + " executed sucessfully but with warnings." + System.lineSeparator());
|
108
|
}
|
109
|
} else if (updateResult.isError()) {
|
110
|
statusFlag = IStatus.ERROR;
|
111
|
statusMsg.append(getLabel() + " failed." + System.lineSeparator());
|
112
|
} else if (updateResult.isAbort()) {
|
113
|
statusFlag = IStatus.ERROR;
|
114
|
statusMsg.append(getLabel() + " aborted." + System.lineSeparator());
|
115
|
}
|
116
|
|
117
|
Status[] childStatus = new Status[exceptions.size()];
|
118
|
int count = 0;
|
119
|
Set<String> messages = new HashSet<String>();
|
120
|
for(Exception ex : exceptions) {
|
121
|
Status status = new Status(statusFlag,
|
122
|
"unknown",
|
123
|
statusFlag,
|
124
|
ex.getLocalizedMessage(),
|
125
|
ex);
|
126
|
messages.add(ex.getLocalizedMessage());
|
127
|
childStatus[count] = status;
|
128
|
count++;
|
129
|
}
|
130
|
|
131
|
statusMsg.append("Please click on the 'Details' button to see all the errors / warnings");
|
132
|
|
133
|
MultiStatus multiStatus = new MultiStatus("unknown",
|
134
|
statusFlag,
|
135
|
childStatus,
|
136
|
statusMsg.toString(),
|
137
|
null);
|
138
|
return multiStatus;
|
139
|
}
|
140
|
return null;
|
141
|
}
|
142
|
|
143
|
|
144
|
}
|