Project

General

Profile

Download (4.11 KB) Statistics
| Branch: | Tag: | Revision:
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

    
21
import eu.etaxonomy.cdm.api.application.CdmChangeEvent.Action;
22
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
    public static boolean throwExceptions = false;
33

    
34
    /**
35
     * @param label
36
     */
37
    public RemotingCdmUpdateOperation(String label, Action action, Object source, boolean async) {
38
        super(label, action, source, async);
39
    }
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
    protected boolean doExecute(IProgressMonitor monitor, IAdaptable info) {
46
        try {
47
            updateResult = doUpdateExecute(monitor, info);
48
        } catch (Exception e) {
49
            if(throwExceptions) {
50
                throw new RuntimeException(e);
51
            } else {
52
                UpdateResult exceptionResult = new UpdateResult();
53
                exceptionResult.addException(e);
54
                exceptionResult.setAbort();
55
                updateResult = exceptionResult;
56
            }
57
        }
58
        return updateResult.isOk();
59
    }
60

    
61
    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

    
71
    /* (non-Javadoc)
72
     * @see eu.etaxonomy.taxeditor.operation.RemotingCdmOperation#onComplete(boolean)
73
     */
74
    @Override
75
    protected IStatus onComplete(boolean success) {
76

    
77
        if(updateResult != null) {
78
            int statusFlag = IStatus.OK;
79

    
80
            Collection<Exception> exceptions = updateResult.getExceptions();
81
            StringBuffer statusMsg = new StringBuffer();
82
            if(success && updateResult.isOk()) {
83
                if(exceptions.isEmpty()) {
84
                    return Status.OK_STATUS;
85
                } else {
86
                    statusFlag = IStatus.WARNING;
87
                    statusMsg.append(getLabel() + " executed sucessfully but with warnings." + System.lineSeparator());
88
                }
89
            } else if (updateResult.isError()) {
90
                statusFlag = IStatus.ERROR;
91
                statusMsg.append(getLabel() + " failed." + System.lineSeparator());
92
            } else if (updateResult.isAbort()) {
93
                statusFlag = IStatus.ERROR;
94
                statusMsg.append(getLabel() + " aborted." + System.lineSeparator());
95
            }
96

    
97
            Status[] childStatus = new Status[exceptions.size()];
98
            int count = 0;
99
            Set<String> messages = new HashSet<String>();
100
            for(Exception ex : exceptions) {
101
                Status status = new Status(statusFlag,
102
                        "unknown",
103
                        statusFlag,
104
                        ex.getLocalizedMessage(),
105
                        ex);
106
                messages.add(ex.getLocalizedMessage());
107
                childStatus[count] = status;
108
                count++;
109
            }
110

    
111
            statusMsg.append("Please click on the 'Details' button to see all the errors / warnings");
112

    
113
            MultiStatus multiStatus = new MultiStatus("unknown",
114
                    statusFlag,
115
                    childStatus,
116
                    statusMsg.toString(),
117
                    null);
118
            return multiStatus;
119
        }
120
        return null;
121
    }
122
}
(12-12/12)