Project

General

Profile

Download (5.05 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
import org.eclipse.swt.widgets.Display;
24

    
25
import eu.etaxonomy.cdm.api.application.CdmChangeEvent.Action;
26
import eu.etaxonomy.cdm.api.service.UpdateResult;
27
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
28
import eu.etaxonomy.cdm.model.common.CdmBase;
29
import eu.etaxonomy.cdm.model.taxon.Synonym;
30
import eu.etaxonomy.cdm.model.taxon.Taxon;
31
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
32

    
33

    
34
/**
35
 * @author cmathew
36
 * @date 16 Jun 2015
37
 *
38
 */
39
public abstract class RemotingCdmUpdateOperation extends RemotingCdmOperation {
40

    
41
    protected UpdateResult updateResult;
42
    public static boolean throwExceptions = false;
43
    protected EPartService partService = null;
44
    protected MPart activePart = null;
45
    protected MApplication application = null;
46

    
47
    /**
48
     * @param label
49
     */
50
    public RemotingCdmUpdateOperation(String label, Action action, Object source, boolean async, 
51
    		EPartService partService,
52
    		MPart activePart,
53
    		MApplication application) {
54
        super(label, action, source, async);
55
        this.partService= partService;
56
        this.activePart = activePart;
57
        this.application = application;
58
    }
59
    
60
    public RemotingCdmUpdateOperation(String label, Action action, Object source, boolean async){
61
        super(label, action, source, async);
62
       
63
    }
64

    
65
    /* (non-Javadoc)
66
     * @see eu.etaxonomy.taxeditor.operation.RemotingCdmOperation#doExecute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
67
     */
68
    @Override
69
    protected boolean doExecute(IProgressMonitor monitor, IAdaptable info) {
70
        try {
71
            updateResult = doUpdateExecute(monitor, info);
72
        } catch (Exception e) {
73
            if(throwExceptions) {
74
                throw new RuntimeException(e);
75
            } else {
76
                UpdateResult exceptionResult = new UpdateResult();
77
                exceptionResult.addException(e);
78
                exceptionResult.setAbort();
79
                updateResult = exceptionResult;
80
            }
81
        }
82
        return updateResult.isOk();
83
    }
84

    
85
    protected abstract UpdateResult doUpdateExecute(IProgressMonitor monitor, IAdaptable info) throws Exception;
86

    
87
    @Override
88
    protected void postExecute(boolean success) {
89
        if(success && updateResult != null) {
90
            fireDataChangeEvent(updateResult);
91
        }
92

    
93
    }
94

    
95
    /* (non-Javadoc)
96
     * @see eu.etaxonomy.taxeditor.operation.RemotingCdmOperation#onComplete(boolean)
97
     */
98
    @Override
99
    protected IStatus onComplete(boolean success) {
100

    
101
        if(updateResult != null) {
102
            int statusFlag = IStatus.OK;
103

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

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

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

    
137
            MultiStatus multiStatus = new MultiStatus("unknown",
138
                    statusFlag,
139
                    childStatus,
140
                    statusMsg.toString(),
141
                    null);
142
            return multiStatus;
143
        }
144
        return null;
145
    }
146
    
147
  
148
}
(12-12/12)