Project

General

Profile

Download (3.59 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

    
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
            UpdateResult exceptionResult = new UpdateResult();
50
            exceptionResult.addException(e);
51
            exceptionResult.setError();
52
            updateResult = exceptionResult;
53
        }
54
        return updateResult.isOk();
55
    }
56

    
57
    protected abstract UpdateResult doUpdateExecute(IProgressMonitor monitor, IAdaptable info) throws Exception;
58

    
59
    @Override
60
    protected void postExecute(boolean success) {
61
        if(success && updateResult != null) {
62
            fireDataChangeEvent(updateResult);
63
        }
64

    
65
    }
66

    
67
    /* (non-Javadoc)
68
     * @see eu.etaxonomy.taxeditor.operation.RemotingCdmOperation#onComplete(boolean)
69
     */
70
    @Override
71
    protected IStatus onComplete(boolean success) {
72

    
73
        if(updateResult != null) {
74
            int statusFlag = IStatus.OK;
75

    
76
            Collection<Exception> exceptions = updateResult.getExceptions();
77
            if(success && updateResult.isOk()) {
78
                if(exceptions.isEmpty()) {
79
                    return Status.OK_STATUS;
80
                } else {
81
                    statusFlag = IStatus.WARNING;
82
                }
83
            } else {
84
                statusFlag = IStatus.ERROR;
85
            }
86

    
87
            Status[] childStatus = new Status[exceptions.size()];
88
            int count = 0;
89
            Set<String> messages = new HashSet<String>();
90
            for(Exception ex : exceptions) {
91
                Status status = new Status(statusFlag,
92
                        "unknown",
93
                        statusFlag,
94
                        ex.getLocalizedMessage(),
95
                        ex);
96
                messages.add(ex.getLocalizedMessage());
97
                childStatus[count] = status;
98
                count++;
99
            }
100
            StringBuffer statusMsg = new StringBuffer();
101
            for(String message : messages) {
102
                statusMsg.append(message);
103
                statusMsg.append(System.lineSeparator());
104
            }
105
            MultiStatus multiStatus = new MultiStatus("unknown",
106
                    statusFlag,
107
                    childStatus,
108
                    statusMsg.toString(),
109
                    null);
110
            return multiStatus;
111
        }
112
        return null;
113
    }
114
}
(8-8/8)