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