Project

General

Profile

Download (6.64 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2009 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.cdm.api.service;
10

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

    
16
import org.apache.commons.collections.buffer.CircularFifoBuffer;
17
import org.apache.log4j.Logger;
18

    
19
import eu.etaxonomy.cdm.api.service.dto.CdmEntityIdentifier;
20
import eu.etaxonomy.cdm.model.common.CdmBase;
21
import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
22

    
23
/**
24
 * This class represents the result of an update action.
25
 *
26
 * @author k.luther
27
 * @since 11.02.2015
28
 */
29
public class UpdateResult implements Serializable{
30

    
31
    private static final long serialVersionUID = -7040027587709706700L;
32

    
33
    @SuppressWarnings("unused")
34
    private static final Logger logger = Logger.getLogger(UpdateResult.class);
35

    
36
    private Status status = Status.OK;
37

    
38
    @SuppressWarnings("unchecked")
39
    private final Collection<Exception> exceptions = new CircularFifoBuffer(10);
40

    
41
    private Set<CdmBase> updatedObjects = new HashSet<>();
42

    
43
    private final Set<CdmEntityIdentifier> updatedCdmIds = new HashSet<>();
44

    
45
    private final Set<CdmBase> unchangedObjects = new HashSet<>();
46

    
47
    private CdmBase cdmEntity;
48

    
49
    public enum Status {
50
        OK(0),
51
        ABORT(1),
52
        ERROR(3),
53
        ;
54

    
55
        protected Integer severity;
56
        private Status(int severity){
57
            this.severity = severity;
58
        }
59

    
60
        public int compareSeverity(Status other){
61
            return this.severity.compareTo(other.severity);
62
        }
63
    }
64

    
65
    //***************************** GETTER /SETTER /ADDER *************************/
66

    
67
    /**
68
     * The resulting status of an update action.
69
     *
70
     * @see UpdateStatus
71
     * @return
72
     */
73
    public Status getStatus() {
74
        return status;
75
    }
76
    public void setStatus(Status status) {
77
        this.status = status;
78
    }
79

    
80
    /**
81
     * The highest exception that occurred during delete (if any).
82
     */
83
    public Collection<Exception> getExceptions() {
84
        return exceptions;
85
    }
86
    public void addException(Exception exception) {
87
        this.exceptions.add(exception);
88
    }
89
    public void addExceptions(Collection<Exception> exceptions) {
90
        this.exceptions.addAll(exceptions);
91
    }
92

    
93
    /**
94
     * Related objects that prevent the delete action to take place.
95
     * @return
96
     */
97
    public Set<CdmEntityIdentifier> getUpdatedCdmIds() {
98
        return updatedCdmIds;
99
    }
100
    public void addUpdatedCdmId(CdmEntityIdentifier cdmId) {
101
        this.updatedCdmIds.add(cdmId);
102
    }
103
    public void addUpdatedCdmIds(Set<CdmEntityIdentifier> updatedCdmIds) {
104
        this.updatedCdmIds.addAll(updatedCdmIds);
105
    }
106

    
107

    
108
    public Set<CdmBase> getUpdatedObjects() {
109
        return updatedObjects;
110
    }
111
    public void addUpdatedObject(CdmBase relatedObject) {
112
            this.updatedObjects.add(relatedObject);
113
        }
114
    public void addUpdatedObjects(Set<? extends CdmBase> updatedObjects) {
115
        this.updatedObjects.addAll(updatedObjects);
116
    }
117

    
118
    //****************** CONVENIENCE *********************************************/
119

    
120
    /**
121
     * Sets the status to {@link Status#ERROR} if not yet set to a more serious
122
     * status.
123
     */
124
    public void setError(){
125
        setMaxStatus(Status.ERROR);
126
    }
127

    
128
    /**
129
     * Sets the status to {@link Status#ABORT} if not yet set to a more serious
130
     * status.
131
     */
132
    public void setAbort(){
133
        setMaxStatus(Status.ABORT);
134
    }
135

    
136
    /**
137
     * Sets status to most severe status. If maxStatus is more severe then existing status
138
     * existing status is set to maxStatus. Otherwise nothing changes.
139
     * If minStatus is more severe then given status minStatus will be the new status.
140
     * @param maxStatus
141
     */
142
    public void setMaxStatus(Status maxStatus) {
143
        if (this.status.compareSeverity(maxStatus) < 0){
144
            this.status = maxStatus;
145
        }
146
    }
147

    
148
    public void includeResult(UpdateResult includedResult){
149

    
150
        this.setMaxStatus(includedResult.getStatus());
151
        this.addExceptions(includedResult.getExceptions());
152
        this.addUpdatedObjects(includedResult.getUpdatedObjects());
153
        this.addUpdatedCdmIds(includedResult.getUpdatedCdmIds());
154
        //also add cdm entity of included result to updated objects
155
        if(includedResult.getCdmEntity()!=null){
156
            this.getUpdatedObjects().add(includedResult.getCdmEntity());
157
        }
158
    }
159

    
160
    public boolean isOk(){
161
        return this.status == Status.OK;
162
    }
163

    
164
    public boolean isAbort(){
165
        return this.status == Status.ABORT;
166
    }
167

    
168
    public boolean isError(){
169
        return this.status == Status.ERROR;
170
    }
171

    
172
    @Override
173
    public String toString(){
174
        String separator = ", ";
175
        String exceptionString = "";
176
        for (Exception exception : exceptions) {
177
            exceptionString += exception.getLocalizedMessage()+separator;
178
        }
179
        if(exceptionString.endsWith(separator)){
180
            exceptionString = exceptionString.substring(0, exceptionString.length()-separator.length());
181
        }
182
        String relatedObjectString = "";
183
        for (CdmBase upatedObject: updatedObjects) {
184
            if(upatedObject instanceof IIdentifiableEntity){
185
                relatedObjectString += ((IIdentifiableEntity) upatedObject).getTitleCache()+separator;
186
            }
187
            else{
188
                relatedObjectString += upatedObject.toString()+separator;
189
            }
190
        }
191
        if(relatedObjectString.endsWith(separator)){
192
            relatedObjectString = relatedObjectString.substring(0, relatedObjectString.length()-separator.length());
193
        }
194
        return "[UpdateResult]\n" +
195
            "Status: " + status.toString()+"\n" +
196
            "Exceptions: " + exceptionString+"\n" +
197
            "Related Objects: "+relatedObjectString;
198
    }
199
    public void setCdmEntity(CdmBase cdmBase) {
200
        this.cdmEntity = cdmBase;
201
    }
202

    
203
    public CdmBase getCdmEntity(){
204
        return cdmEntity;
205
    }
206

    
207
    public Set<CdmBase> getUnchangedObjects() {
208
        return unchangedObjects;
209
    }
210

    
211
    public void addUnchangedObjects(Set<? extends CdmBase> unchangedObjects) {
212
        this.unchangedObjects.addAll(unchangedObjects);
213
    }
214
    public void addUnChangedObject(CdmBase unchangedObject) {
215
        this.unchangedObjects.add(unchangedObject);
216
    }
217
}
(94-94/97)