Partial fix of Ticket #4728
[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.HashSet;
16 import java.util.List;
17 import java.util.Set;
18
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 private final List<Exception> exceptions = new ArrayList<Exception>();
42
43 private final Set<CdmBase> upatedObjects = new HashSet<CdmBase>();
44
45 private final Set<CdmBase> unchangedObjects = new HashSet<CdmBase>();
46
47 private CdmBase cdmEntity;
48
49 // private Set<PersistPair> objectsToDelete = new HashSet<PersistPair>();
50 //
51 // private Set<PersistPair> objectsToSave = new HashSet<DeleteResult.PersistPair>();
52
53 // protected class PersistPair{
54 // protected CdmBase objectToPersist;
55 // protected ICdmEntityDao<CdmBase> dao;
56 // }
57
58 public enum Status {
59 OK(0),
60 ABORT(1),
61 ERROR(3),
62 ;
63
64 protected Integer severity;
65 private Status(int severity){
66 this.severity = severity;
67 }
68
69 public int compareSeverity(Status other){
70 return this.severity.compareTo(other.severity);
71 }
72 }
73
74 //***************************** GETTER /SETTER /ADDER *************************/
75 /**
76 * The resulting status of an update action.
77 *
78 * @see UpdateStatus
79 * @return
80 */
81 public Status getStatus() {
82 return status;
83 }
84 public void setStatus(Status status) {
85 this.status = status;
86 }
87
88 /**
89 * The highest exception that occurred during delete (if any).
90 * @return
91 */
92 public List<Exception> getExceptions() {
93 return exceptions;
94 }
95 public void addException(Exception exception) {
96 this.exceptions.add(exception);
97 }
98 public void addExceptions(List<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<CdmBase> getUpdatedObjects() {
107 return upatedObjects;
108 }
109 public void addUpdatedObject(CdmBase relatedObject) {
110 this.upatedObjects.add(relatedObject);
111 }
112 public void addUpdatedObjects(Set<? extends CdmBase> updatedObjects) {
113 this.upatedObjects.addAll(updatedObjects);
114 }
115
116
117 // /**
118 // * @return
119 // */
120 // public Set<PersistPair> getObjectsToDelete() {
121 // return objectsToDelete;
122 // }
123 // public void setObjectsToDelete(Set<PersistPair> objectsToDelete) {
124 // this.objectsToDelete = objectsToDelete;
125 // }
126 //
127 // /**
128 // * @return
129 // */
130 // public Set<PersistPair> getObjectsToSave() {
131 // return objectsToSave;
132 // }
133 // public void setObjectsToSave(Set<PersistPair> objectsToSave) {
134 // this.objectsToSave = objectsToSave;
135 // }
136
137
138 //****************** CONVENIENCE *********************************************/
139
140 /**
141 * Sets the status to {@link DeleteStatus#ERROR} if not yet set to a more serious
142 * status.
143 */
144 public void setError(){
145 setMaxStatus(Status.ERROR);
146 }
147
148 /**
149 * Sets the status to {@link DeleteStatus#ABORT} if not yet set to a more serious
150 * status.
151 */
152 public void setAbort(){
153 setMaxStatus(Status.ABORT);
154 }
155
156 /**
157 * Sets status to most severe status. If maxStatus is more severe then existing status
158 * existing status is set to maxStatus. Otherwise nothing changes.
159 * If minStatus is more severe then given status minStatus will be the new status.
160 * @param maxStatus
161 */
162 public void setMaxStatus(Status maxStatus) {
163 if (this.status.compareSeverity(maxStatus) < 0){
164 this.status = maxStatus;
165 }
166 }
167
168 public void includeResult(UpdateResult includedResult){
169 this.setMaxStatus(includedResult.getStatus());
170 this.addExceptions(includedResult.getExceptions());
171 this.addUpdatedObjects(includedResult.getUpdatedObjects());
172 }
173
174 public boolean isOk(){
175 return this.status == Status.OK;
176 }
177
178 public boolean isAbort(){
179 return this.status == Status.ABORT;
180 }
181
182 public boolean isError(){
183 return this.status == Status.ERROR;
184 }
185
186
187
188 @Override
189 public String toString(){
190 String separator = ", ";
191 String exceptionString = "";
192 for (Exception exception : exceptions) {
193 exceptionString += exception.getLocalizedMessage()+separator;
194 }
195 if(exceptionString.endsWith(separator)){
196 exceptionString = exceptionString.substring(0, exceptionString.length()-separator.length());
197 }
198 String relatedObjectString = "";
199 for (CdmBase upatedObject: upatedObjects) {
200 if(upatedObject instanceof IIdentifiableEntity){
201 relatedObjectString += ((IIdentifiableEntity) upatedObject).getTitleCache()+separator;
202 }
203 else{
204 relatedObjectString += upatedObject.toString()+separator;
205 }
206 }
207 if(relatedObjectString.endsWith(separator)){
208 relatedObjectString = relatedObjectString.substring(0, relatedObjectString.length()-separator.length());
209 }
210 return "[DeleteResult]\n" +
211 "Status: " + status.toString()+"\n" +
212 "Exceptions: " + exceptionString+"\n" +
213 "Related Objects: "+relatedObjectString;
214 }
215 public void setCdmEntity(CdmBase cdmBase) {
216 this.cdmEntity = cdmBase;
217
218 }
219
220 public CdmBase getCdmEntity(){
221 return cdmEntity;
222 }
223
224 public Set<CdmBase> getUnchangedObjects() {
225 return unchangedObjects;
226 }
227
228 public void addUnchangedObjects(Set<? extends CdmBase> unchangedObjects) {
229 this.unchangedObjects.addAll(unchangedObjects);
230 }
231 public void addUnChangedObject(CdmBase unchangedObject) {
232 this.unchangedObjects.add(unchangedObject);
233 }
234
235
236 }