Project

General

Profile

Download (4.77 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
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
        try {
66
            updateResult = doUpdateExecute(monitor, info);
67
        } catch (Exception e) {
68
            if(throwExceptions) {
69
                throw new RuntimeException(e);
70
            } else {
71
                UpdateResult exceptionResult = new UpdateResult();
72
                exceptionResult.addException(e);
73
                exceptionResult.setAbort();
74
                updateResult = exceptionResult;
75
            }
76
        }
77
        return updateResult.isOk();
78
    }
79

    
80
    protected abstract UpdateResult doUpdateExecute(IProgressMonitor monitor, IAdaptable info) throws Exception;
81

    
82
    @Override
83
    protected void postExecute(boolean success) {
84
        if(success && updateResult != null) {
85
            fireDataChangeEvent(updateResult);
86
        }
87

    
88
    }
89

    
90
    /* (non-Javadoc)
91
     * @see eu.etaxonomy.taxeditor.operation.RemotingCdmOperation#onComplete(boolean)
92
     */
93
    @Override
94
    protected IStatus onComplete(boolean success) {
95

    
96
        if(updateResult != null) {
97
            int statusFlag = IStatus.OK;
98

    
99
            Collection<Exception> exceptions = updateResult.getExceptions();
100
            StringBuffer statusMsg = new StringBuffer();
101
            if(success && updateResult.isOk()) {
102
                if(exceptions.isEmpty()) {
103
                    return Status.OK_STATUS;
104
                } else {
105
                    statusFlag = IStatus.WARNING;
106
                    statusMsg.append(getLabel() + " executed sucessfully but with warnings." + System.lineSeparator());
107
                }
108
            } else if (updateResult.isError()) {
109
                statusFlag = IStatus.ERROR;
110
                statusMsg.append(getLabel() + " failed." + System.lineSeparator());
111
            } else if (updateResult.isAbort()) {
112
                statusFlag = IStatus.ERROR;
113
                statusMsg.append(getLabel() + " aborted." + System.lineSeparator());
114
            }
115

    
116
            Status[] childStatus = new Status[exceptions.size()];
117
            int count = 0;
118
            Set<String> messages = new HashSet<String>();
119
            for(Exception ex : exceptions) {
120
                Status status = new Status(statusFlag,
121
                        "unknown",
122
                        statusFlag,
123
                        ex.getLocalizedMessage(),
124
                        ex);
125
                messages.add(ex.getLocalizedMessage());
126
                childStatus[count] = status;
127
                count++;
128
            }
129

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

    
132
            MultiStatus multiStatus = new MultiStatus("unknown",
133
                    statusFlag,
134
                    childStatus,
135
                    statusMsg.toString(),
136
                    null);
137
            return multiStatus;
138
        }
139
        return null;
140
    }
141

    
142

    
143
}
(11-11/11)