Project

General

Profile

Download (4.12 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2015 EDIT
4
 * European Distributed Institute of Taxonomy
5
 * http://www.e-taxonomy.eu
6
 *
7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 * See LICENSE.TXT at the top of this package for the full license terms.
9
 */
10
package eu.etaxonomy.taxeditor.operation;
11

    
12
import java.util.Collection;
13
import java.util.HashSet;
14
import java.util.Set;
15

    
16
import org.eclipse.core.runtime.IAdaptable;
17
import org.eclipse.core.runtime.IProgressMonitor;
18
import org.eclipse.core.runtime.IStatus;
19
import org.eclipse.core.runtime.MultiStatus;
20
import org.eclipse.core.runtime.Status;
21

    
22
import eu.etaxonomy.cdm.api.application.CdmChangeEvent.Action;
23
import eu.etaxonomy.cdm.api.service.UpdateResult;
24

    
25
/**
26
 * @author cmathew
27
 * @date 16 Jun 2015
28
 *
29
 */
30
public abstract class RemotingCdmUpdateOperation extends RemotingCdmOperation {
31

    
32
    private UpdateResult updateResult;
33
    public static boolean throwExceptions = false;
34

    
35
    /**
36
     * @param label
37
     */
38
    public RemotingCdmUpdateOperation(String label, Action action, Object source, boolean async) {
39
        super(label, action, source, async);
40
    }
41

    
42
    /* (non-Javadoc)
43
     * @see eu.etaxonomy.taxeditor.operation.RemotingCdmOperation#doExecute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
44
     */
45
    @Override
46
    protected boolean doExecute(IProgressMonitor monitor, IAdaptable info) {
47
        try {
48
            updateResult = doUpdateExecute(monitor, info);
49
        } catch (Exception e) {
50
            if(throwExceptions) {
51
                throw new RuntimeException(e);
52
            } else {
53
                UpdateResult exceptionResult = new UpdateResult();
54
                exceptionResult.addException(e);
55
                exceptionResult.setAbort();
56
                updateResult = exceptionResult;
57
            }
58
        }
59
        return updateResult.isOk();
60
    }
61

    
62
    protected abstract UpdateResult doUpdateExecute(IProgressMonitor monitor, IAdaptable info) throws Exception;
63

    
64
    @Override
65
    protected void postExecute(boolean success) {
66
        if(success && updateResult != null) {
67
            fireDataChangeEvent(updateResult);
68
        }
69

    
70
    }
71

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

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

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

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

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

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