Project

General

Profile

Download (7.49 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
 * @since 11.02.2015
30
 */
31
public class UpdateResult implements Serializable{
32

    
33
    private static final long serialVersionUID = -7040027587709706700L;
34

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

    
38
    private Status status = Status.OK;
39

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

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

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

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

    
49
    private CdmBase cdmEntity;
50

    
51

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

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

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

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

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

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

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

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

    
118

    
119
    public Set<CdmBase> getUpdatedObjects() {
120
        return updatedObjects;
121
    }
122
    public void addUpdatedObject(CdmBase relatedObject) {
123
            this.updatedObjects.add(relatedObject);
124
        }
125
    public void addUpdatedObjects(Set<? extends CdmBase> updatedObjects) {
126
        this.updatedObjects.addAll(updatedObjects);
127
    }
128

    
129
    //		/**
130
    //		 * @return
131
    //		 */
132
    //		public Set<PersistPair> getObjectsToDelete() {
133
    //			return objectsToDelete;
134
    //		}
135
    //		public void setObjectsToDelete(Set<PersistPair> objectsToDelete) {
136
    //			this.objectsToDelete = objectsToDelete;
137
    //		}
138
    //
139
    //		/**
140
    //		 * @return
141
    //		 */
142
    //		public Set<PersistPair> getObjectsToSave() {
143
    //			return objectsToSave;
144
    //		}
145
    //		public void setObjectsToSave(Set<PersistPair> objectsToSave) {
146
    //			this.objectsToSave = objectsToSave;
147
    //		}
148

    
149

    
150
    //****************** CONVENIENCE *********************************************/
151

    
152
    /**
153
     * Sets the status to {@link DeleteStatus#ERROR} if not yet set to a more serious
154
     * status.
155
     */
156
    public void setError(){
157
        setMaxStatus(Status.ERROR);
158
    }
159

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

    
168
    /**
169
     * Sets status to most severe status. If maxStatus is more severe then existing status
170
     * existing status is set to maxStatus. Otherwise nothing changes.
171
     * If minStatus is more severe then given status minStatus will be the new status.
172
     * @param maxStatus
173
     */
174
    public void setMaxStatus(Status maxStatus) {
175
        if (this.status.compareSeverity(maxStatus) < 0){
176
            this.status = maxStatus;
177
        }
178
    }
179

    
180
    public void includeResult(UpdateResult includedResult){
181

    
182
        this.setMaxStatus(includedResult.getStatus());
183
        this.addExceptions(includedResult.getExceptions());
184
        this.addUpdatedObjects(includedResult.getUpdatedObjects());
185
        this.addUpdatedCdmIds(includedResult.getUpdatedCdmIds());
186
        //also add cdm entity of included result to updated objects
187
        if(includedResult.getCdmEntity()!=null){
188
            this.getUpdatedObjects().add(includedResult.getCdmEntity());
189
        }
190
    }
191

    
192
    public boolean isOk(){
193
        return this.status == Status.OK;
194
    }
195

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

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

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

    
235
    public CdmBase getCdmEntity(){
236
        return cdmEntity;
237
    }
238

    
239
    public Set<CdmBase> getUnchangedObjects() {
240
        return unchangedObjects;
241
    }
242

    
243
    public void addUnchangedObjects(Set<? extends CdmBase> unchangedObjects) {
244
        this.unchangedObjects.addAll(unchangedObjects);
245
    }
246
    public void addUnChangedObject(CdmBase unchangedObject) {
247
        this.unchangedObjects.add(unchangedObject);
248
    }
249
}
(97-97/100)