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