Project

General

Profile

Download (7.41 KB) Statistics
| Branch: | Tag: | Revision:
1

    
2
//$Id$
3
/**
4
 * Copyright (C) 2009 EDIT
5
 * European Distributed Institute of Taxonomy
6
 * http://www.e-taxonomy.eu
7
 *
8
 * The contents of this file are subject to the Mozilla Public License Version 1.1
9
 * See LICENSE.TXT at the top of this package for the full license terms.
10
 */
11
package eu.etaxonomy.cdm.api.service;
12

    
13
import java.io.Serializable;
14
import java.util.Collection;
15
import java.util.HashSet;
16
import java.util.Set;
17

    
18
import org.apache.commons.collections.buffer.CircularFifoBuffer;
19
import org.apache.log4j.Logger;
20

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

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

    
34
    private static final long serialVersionUID = 1L;
35

    
36
    @SuppressWarnings("unused")
37
    private static final Logger logger = Logger.getLogger(UpdateResult.class);
38

    
39
    private Status status = Status.OK;
40

    
41
    @SuppressWarnings("unchecked")
42
    private final Collection<Exception> exceptions = new CircularFifoBuffer(10);
43

    
44
    private final Set<CdmBase> updatedObjects = new HashSet<CdmBase>();
45

    
46
    private final Set<CdmEntityIdentifier> updatedCdmIds = new HashSet<CdmEntityIdentifier>();
47

    
48
    private final Set<CdmBase> unchangedObjects = new HashSet<CdmBase>();
49

    
50
    private CdmBase cdmEntity;
51

    
52

    
53
    //		private Set<PersistPair> objectsToDelete = new HashSet<PersistPair>();
54
    //
55
    //		private Set<PersistPair> objectsToSave = new HashSet<DeleteResult.PersistPair>();
56

    
57
    //		protected class PersistPair{
58
    //			protected CdmBase objectToPersist;
59
    //			protected ICdmEntityDao<CdmBase> dao;
60
    //		}
61

    
62
    public enum Status {
63
        OK(0),
64
        ABORT(1),
65
        ERROR(3),
66
        ;
67

    
68
        protected Integer severity;
69
        private Status(int severity){
70
            this.severity = severity;
71
        }
72

    
73
        public int compareSeverity(Status other){
74
            return this.severity.compareTo(other.severity);
75
        }
76
    }
77

    
78
    //***************************** GETTER /SETTER /ADDER *************************/
79
    /**
80
     * The resulting status of an update action.
81
     *
82
     * @see UpdateStatus
83
     * @return
84
     */
85
    public Status getStatus() {
86
        return status;
87
    }
88
    public void setStatus(Status status) {
89
        this.status = status;
90
    }
91

    
92
    /**
93
     * The highest exception that occurred during delete (if any).
94
     * @return
95
     */
96
    public Collection<Exception> getExceptions() {
97
        return exceptions;
98
    }
99
    public void addException(Exception exception) {
100
        this.exceptions.add(exception);
101
    }
102
    public void addExceptions(Collection<Exception> exceptions) {
103
        this.exceptions.addAll(exceptions);
104
    }
105

    
106
    /**
107
     * Related objects that prevent the delete action to take place.
108
     * @return
109
     */
110
    public Set<CdmEntityIdentifier> getUpdatedCdmIds() {
111
        return updatedCdmIds;
112
    }
113
    public void addUpdatedCdmId(CdmEntityIdentifier cdmId) {
114
        this.updatedCdmIds.add(cdmId);
115
    }
116
    public void addUpdatedCdmIds(Set<CdmEntityIdentifier> updatedCdmIds) {
117
        this.updatedCdmIds.addAll(updatedCdmIds);
118
    }
119

    
120

    
121
    public Set<CdmBase> getUpdatedObjects() {
122
        return updatedObjects;
123
    }
124
    public void addUpdatedObject(CdmBase relatedObject) {
125
        if (relatedObject != null){
126
            this.updatedObjects.add(relatedObject);
127
        }
128
    }
129
    public void addUpdatedObjects(Set<? extends CdmBase> updatedObjects) {
130
        if (updatedObjects.size() >0){
131
            this.updatedObjects.addAll(updatedObjects);
132
        }
133

    
134
    }
135

    
136
    //		/**
137
    //		 * @return
138
    //		 */
139
    //		public Set<PersistPair> getObjectsToDelete() {
140
    //			return objectsToDelete;
141
    //		}
142
    //		public void setObjectsToDelete(Set<PersistPair> objectsToDelete) {
143
    //			this.objectsToDelete = objectsToDelete;
144
    //		}
145
    //
146
    //		/**
147
    //		 * @return
148
    //		 */
149
    //		public Set<PersistPair> getObjectsToSave() {
150
    //			return objectsToSave;
151
    //		}
152
    //		public void setObjectsToSave(Set<PersistPair> objectsToSave) {
153
    //			this.objectsToSave = objectsToSave;
154
    //		}
155

    
156

    
157
    //****************** CONVENIENCE *********************************************/
158

    
159
    /**
160
     * Sets the status to {@link DeleteStatus#ERROR} if not yet set to a more serious
161
     * status.
162
     */
163
    public void setError(){
164
        setMaxStatus(Status.ERROR);
165
    }
166

    
167
    /**
168
     * Sets the status to {@link DeleteStatus#ABORT} if not yet set to a more serious
169
     * status.
170
     */
171
    public void setAbort(){
172
        setMaxStatus(Status.ABORT);
173
    }
174

    
175
    /**
176
     * Sets status to most severe status. If maxStatus is more severe then existing status
177
     * existing status is set to maxStatus. Otherwise nothing changes.
178
     * If minStatus is more severe then given status minStatus will be the new status.
179
     * @param maxStatus
180
     */
181
    public void setMaxStatus(Status maxStatus) {
182
        if (this.status.compareSeverity(maxStatus) < 0){
183
            this.status = maxStatus;
184
        }
185
    }
186

    
187
    public void includeResult(UpdateResult includedResult){
188
        this.setMaxStatus(includedResult.getStatus());
189
        this.addExceptions(includedResult.getExceptions());
190

    
191
        this.addUpdatedObjects(includedResult.getUpdatedObjects());
192

    
193

    
194
    }
195

    
196
    public boolean isOk(){
197
        return this.status == Status.OK;
198
    }
199

    
200
    public boolean isAbort(){
201
        return this.status == Status.ABORT;
202
    }
203

    
204
    public boolean isError(){
205
        return this.status == Status.ERROR;
206
    }
207

    
208

    
209

    
210
    @Override
211
    public String toString(){
212
        String separator = ", ";
213
        String exceptionString = "";
214
        for (Exception exception : exceptions) {
215
            exceptionString += exception.getLocalizedMessage()+separator;
216
        }
217
        if(exceptionString.endsWith(separator)){
218
            exceptionString = exceptionString.substring(0, exceptionString.length()-separator.length());
219
        }
220
        String relatedObjectString = "";
221
        for (CdmBase upatedObject: updatedObjects) {
222
            if(upatedObject instanceof IIdentifiableEntity){
223
                relatedObjectString += ((IIdentifiableEntity) upatedObject).getTitleCache()+separator;
224
            }
225
            else{
226
                relatedObjectString += upatedObject.toString()+separator;
227
            }
228
        }
229
        if(relatedObjectString.endsWith(separator)){
230
            relatedObjectString = relatedObjectString.substring(0, relatedObjectString.length()-separator.length());
231
        }
232
        return "[DeleteResult]\n" +
233
        "Status: " + status.toString()+"\n" +
234
        "Exceptions: " + exceptionString+"\n" +
235
        "Related Objects: "+relatedObjectString;
236
    }
237
    public void setCdmEntity(CdmBase cdmBase) {
238
        this.cdmEntity = cdmBase;
239

    
240
    }
241

    
242

    
243
    public CdmBase getCdmEntity(){
244
        return cdmEntity;
245
    }
246

    
247
    public Set<CdmBase> getUnchangedObjects() {
248
        return unchangedObjects;
249
    }
250

    
251
    public void addUnchangedObjects(Set<? extends CdmBase> unchangedObjects) {
252
        this.unchangedObjects.addAll(unchangedObjects);
253
    }
254
    public void addUnChangedObject(CdmBase unchangedObject) {
255
        this.unchangedObjects.add(unchangedObject);
256
    }
257

    
258

    
259

    
260
}
(88-88/92)