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