merging trunk into 3.6 branche
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / UpdateResult.java
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.ArrayList;
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Set;
19
20 import org.apache.commons.collections.buffer.CircularFifoBuffer;
21 import org.apache.log4j.Logger;
22
23 import eu.etaxonomy.cdm.model.common.CdmBase;
24 import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
25
26 /**
27 * This class represents the result of an update action.
28 *
29 * @author k.luther
30 * @date 11.02.2015
31 *
32 */
33 public class UpdateResult implements Serializable{
34
35
36 private static final long serialVersionUID = 1L;
37
38 @SuppressWarnings("unused")
39 private static final Logger logger = Logger.getLogger(UpdateResult.class);
40
41 private Status status = Status.OK;
42
43 @SuppressWarnings("unchecked")
44 private final Collection<Exception> exceptions = new CircularFifoBuffer(10);
45
46 private final Set<CdmBase> upatedObjects = new HashSet<CdmBase>();
47
48 private final Set<CdmBase> unchangedObjects = new HashSet<CdmBase>();
49
50 private CdmBase cdmEntity;
51
52 // private Set<PersistPair> objectsToDelete = new HashSet<PersistPair>();
53 //
54 // private Set<PersistPair> objectsToSave = new HashSet<DeleteResult.PersistPair>();
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 * @return
94 */
95 public List<Exception> getExceptions() {
96 return new ArrayList<Exception>(exceptions);
97 }
98 public void addException(Exception exception) {
99 this.exceptions.add(exception);
100 }
101 public void addExceptions(Collection<Exception> exceptions) {
102 this.exceptions.addAll(exceptions);
103 }
104
105 /**
106 * Related objects that prevent the delete action to take place.
107 * @return
108 */
109 public Set<CdmBase> getUpdatedObjects() {
110 return upatedObjects;
111 }
112 public void addUpdatedObject(CdmBase relatedObject) {
113 this.upatedObjects.add(relatedObject);
114 }
115 public void addUpdatedObjects(Set<? extends CdmBase> updatedObjects) {
116 this.upatedObjects.addAll(updatedObjects);
117 }
118
119
120 // /**
121 // * @return
122 // */
123 // public Set<PersistPair> getObjectsToDelete() {
124 // return objectsToDelete;
125 // }
126 // public void setObjectsToDelete(Set<PersistPair> objectsToDelete) {
127 // this.objectsToDelete = objectsToDelete;
128 // }
129 //
130 // /**
131 // * @return
132 // */
133 // public Set<PersistPair> getObjectsToSave() {
134 // return objectsToSave;
135 // }
136 // public void setObjectsToSave(Set<PersistPair> objectsToSave) {
137 // this.objectsToSave = objectsToSave;
138 // }
139
140
141 //****************** CONVENIENCE *********************************************/
142
143 /**
144 * Sets the status to {@link DeleteStatus#ERROR} if not yet set to a more serious
145 * status.
146 */
147 public void setError(){
148 setMaxStatus(Status.ERROR);
149 }
150
151 /**
152 * Sets the status to {@link DeleteStatus#ABORT} if not yet set to a more serious
153 * status.
154 */
155 public void setAbort(){
156 setMaxStatus(Status.ABORT);
157 }
158
159 /**
160 * Sets status to most severe status. If maxStatus is more severe then existing status
161 * existing status is set to maxStatus. Otherwise nothing changes.
162 * If minStatus is more severe then given status minStatus will be the new status.
163 * @param maxStatus
164 */
165 public void setMaxStatus(Status maxStatus) {
166 if (this.status.compareSeverity(maxStatus) < 0){
167 this.status = maxStatus;
168 }
169 }
170
171 public void includeResult(UpdateResult includedResult){
172 this.setMaxStatus(includedResult.getStatus());
173 this.addExceptions(includedResult.getExceptions());
174 this.addUpdatedObjects(includedResult.getUpdatedObjects());
175 }
176
177 public boolean isOk(){
178 return this.status == Status.OK;
179 }
180
181 public boolean isAbort(){
182 return this.status == Status.ABORT;
183 }
184
185 public boolean isError(){
186 return this.status == Status.ERROR;
187 }
188
189
190
191 @Override
192 public String toString(){
193 String separator = ", ";
194 String exceptionString = "";
195 for (Exception exception : exceptions) {
196 exceptionString += exception.getLocalizedMessage()+separator;
197 }
198 if(exceptionString.endsWith(separator)){
199 exceptionString = exceptionString.substring(0, exceptionString.length()-separator.length());
200 }
201 String relatedObjectString = "";
202 for (CdmBase upatedObject: upatedObjects) {
203 if(upatedObject instanceof IIdentifiableEntity){
204 relatedObjectString += ((IIdentifiableEntity) upatedObject).getTitleCache()+separator;
205 }
206 else{
207 relatedObjectString += upatedObject.toString()+separator;
208 }
209 }
210 if(relatedObjectString.endsWith(separator)){
211 relatedObjectString = relatedObjectString.substring(0, relatedObjectString.length()-separator.length());
212 }
213 return "[DeleteResult]\n" +
214 "Status: " + status.toString()+"\n" +
215 "Exceptions: " + exceptionString+"\n" +
216 "Related Objects: "+relatedObjectString;
217 }
218 public void setCdmEntity(CdmBase cdmBase) {
219 this.cdmEntity = cdmBase;
220
221 }
222
223 public CdmBase getCdmEntity(){
224 return cdmEntity;
225 }
226
227 public Set<CdmBase> getUnchangedObjects() {
228 return unchangedObjects;
229 }
230
231 public void addUnchangedObjects(Set<? extends CdmBase> unchangedObjects) {
232 this.unchangedObjects.addAll(unchangedObjects);
233 }
234 public void addUnChangedObject(CdmBase unchangedObject) {
235 this.unchangedObjects.add(unchangedObject);
236 }
237
238
239 }