Project

General

Profile

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

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

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

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

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

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

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

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

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

    
116

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

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

    
147

    
148
    //****************** CONVENIENCE *********************************************/
149

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

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

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

    
178
    public void includeResult(UpdateResult includedResult){
179

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

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

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

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

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

    
233
    public CdmBase getCdmEntity(){
234
        return cdmEntity;
235
    }
236

    
237
    public Set<CdmBase> getUnchangedObjects() {
238
        return unchangedObjects;
239
    }
240

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