Project

General

Profile

Download (4.47 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
 * @author cmathew
29
 * @date 16 Jun 2015
30
 */
31
public abstract class RemotingCdmUpdateOperation extends RemotingCdmOperation {
32

    
33
    protected UpdateResult updateResult;
34
    public static boolean throwExceptions = false;
35
    protected EPartService partService = null;
36

    
37
    protected MPart activePart = null;
38
    protected MApplication application = null;
39

    
40
    public RemotingCdmUpdateOperation(String label, Action action, Object source, boolean async,
41
    		EPartService partService,
42
    		MPart activePart,
43
    		MApplication application) {
44
        super(label, action, source, async);
45
        this.partService= partService;
46
        this.activePart = activePart;
47
        this.application = application;
48
    }
49

    
50
    public RemotingCdmUpdateOperation(String label, Action action, Object source, boolean async){
51
        super(label, action, source, async);
52

    
53
    }
54

    
55
    @Override
56
    protected boolean doExecute(IProgressMonitor monitor, IAdaptable info) {
57
        updateResult = new UpdateResult();
58
        try {
59
            updateResult = doUpdateExecute(monitor, info);
60
        } catch (Exception e) {
61
            if(throwExceptions) {
62
                throw new RuntimeException(e);
63
            } else {
64
                UpdateResult exceptionResult = new UpdateResult();
65
                exceptionResult.addException(e);
66
                exceptionResult.setAbort();
67
                updateResult = exceptionResult;
68
            }
69
        }
70
        return updateResult.isOk();
71
    }
72

    
73
    protected abstract UpdateResult doUpdateExecute(IProgressMonitor monitor, IAdaptable info) throws Exception;
74

    
75
    @Override
76
    protected void postExecute(boolean success) {
77
        if(success && updateResult != null) {
78
            fireDataChangeEvent(updateResult);
79
        }
80
    }
81

    
82
    @Override
83
    protected IStatus onComplete(boolean success) {
84

    
85
        if(updateResult != null) {
86
            int statusFlag = IStatus.OK;
87

    
88
            Collection<Exception> exceptions = updateResult.getExceptions();
89
            StringBuffer statusMsg = new StringBuffer();
90
            if(success && updateResult.isOk()) {
91
                if(exceptions.isEmpty()) {
92
                    return Status.OK_STATUS;
93
                } else {
94
                    statusFlag = IStatus.WARNING;
95
                    statusMsg.append(getLabel() + " executed sucessfully but with warnings." + System.lineSeparator());
96
                }
97
            } else if (updateResult.isError()) {
98
                statusFlag = IStatus.ERROR;
99
                statusMsg.append(getLabel() + " failed." + System.lineSeparator());
100
            } else if (updateResult.isAbort()) {
101
                statusFlag = IStatus.ERROR;
102
                statusMsg.append(getLabel() + " aborted." + System.lineSeparator());
103
            }
104

    
105
            Status[] childStatus = new Status[exceptions.size()];
106
            int count = 0;
107
            Set<String> messages = new HashSet<String>();
108
            for(Exception ex : exceptions) {
109
                Status status = new Status(statusFlag,
110
                        "unknown",
111
                        statusFlag,
112
                        ex.getLocalizedMessage(),
113
                        ex);
114
                messages.add(ex.getLocalizedMessage());
115
                childStatus[count] = status;
116
                count++;
117
            }
118

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

    
121
            MultiStatus multiStatus = new MultiStatus("unknown",
122
                    statusFlag,
123
                    childStatus,
124
                    statusMsg.toString(),
125
                    null);
126
            return multiStatus;
127
        }
128
        return null;
129
    }
130
}
(11-11/11)