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