4 * Copyright (C) 2009 EDIT
5 * European Distributed Institute of Taxonomy
6 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.cdm
.api
.service
;
13 import java
.io
.Serializable
;
14 import java
.util
.Collection
;
15 import java
.util
.HashSet
;
18 import org
.apache
.commons
.collections
.buffer
.CircularFifoBuffer
;
19 import org
.apache
.log4j
.Logger
;
21 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
22 import eu
.etaxonomy
.cdm
.model
.common
.IIdentifiableEntity
;
25 * This class represents the result of an update action.
31 public class UpdateResult
implements Serializable
{
34 private static final long serialVersionUID
= 1L;
36 @SuppressWarnings("unused")
37 private static final Logger logger
= Logger
.getLogger(UpdateResult
.class);
39 private Status status
= Status
.OK
;
41 private final Collection
<Exception
> exceptions
= new CircularFifoBuffer(10);
43 private final Set
<CdmBase
> upatedObjects
= new HashSet
<CdmBase
>();
45 private final Set
<CdmBase
> unchangedObjects
= new HashSet
<CdmBase
>();
47 private CdmBase cdmEntity
;
49 // private Set<PersistPair> objectsToDelete = new HashSet<PersistPair>();
51 // private Set<PersistPair> objectsToSave = new HashSet<DeleteResult.PersistPair>();
53 // protected class PersistPair{
54 // protected CdmBase objectToPersist;
55 // protected ICdmEntityDao<CdmBase> dao;
64 protected Integer severity
;
65 private Status(int severity
){
66 this.severity
= severity
;
69 public int compareSeverity(Status other
){
70 return this.severity
.compareTo(other
.severity
);
74 //***************************** GETTER /SETTER /ADDER *************************/
76 * The resulting status of an update action.
81 public Status
getStatus() {
84 public void setStatus(Status status
) {
89 * The highest exception that occurred during delete (if any).
92 public Collection
<Exception
> getExceptions() {
95 public void addException(Exception exception
) {
96 this.exceptions
.add(exception
);
98 public void addExceptions(Collection
<Exception
> exceptions
) {
99 this.exceptions
.addAll(exceptions
);
103 * Related objects that prevent the delete action to take place.
106 public Set
<CdmBase
> getUpdatedObjects() {
107 return upatedObjects
;
109 public void addUpdatedObject(CdmBase relatedObject
) {
110 this.upatedObjects
.add(relatedObject
);
112 public void addUpdatedObjects(Set
<?
extends CdmBase
> updatedObjects
) {
113 this.upatedObjects
.addAll(updatedObjects
);
120 // public Set<PersistPair> getObjectsToDelete() {
121 // return objectsToDelete;
123 // public void setObjectsToDelete(Set<PersistPair> objectsToDelete) {
124 // this.objectsToDelete = objectsToDelete;
130 // public Set<PersistPair> getObjectsToSave() {
131 // return objectsToSave;
133 // public void setObjectsToSave(Set<PersistPair> objectsToSave) {
134 // this.objectsToSave = objectsToSave;
138 //****************** CONVENIENCE *********************************************/
141 * Sets the status to {@link DeleteStatus#ERROR} if not yet set to a more serious
144 public void setError(){
145 setMaxStatus(Status
.ERROR
);
149 * Sets the status to {@link DeleteStatus#ABORT} if not yet set to a more serious
152 public void setAbort(){
153 setMaxStatus(Status
.ABORT
);
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.
162 public void setMaxStatus(Status maxStatus
) {
163 if (this.status
.compareSeverity(maxStatus
) < 0){
164 this.status
= maxStatus
;
168 public void includeResult(UpdateResult includedResult
){
169 this.setMaxStatus(includedResult
.getStatus());
170 this.addExceptions(includedResult
.getExceptions());
171 this.addUpdatedObjects(includedResult
.getUpdatedObjects());
174 public boolean isOk(){
175 return this.status
== Status
.OK
;
178 public boolean isAbort(){
179 return this.status
== Status
.ABORT
;
182 public boolean isError(){
183 return this.status
== Status
.ERROR
;
189 public String
toString(){
190 String separator
= ", ";
191 String exceptionString
= "";
192 for (Exception exception
: exceptions
) {
193 exceptionString
+= exception
.getLocalizedMessage()+separator
;
195 if(exceptionString
.endsWith(separator
)){
196 exceptionString
= exceptionString
.substring(0, exceptionString
.length()-separator
.length());
198 String relatedObjectString
= "";
199 for (CdmBase upatedObject
: upatedObjects
) {
200 if(upatedObject
instanceof IIdentifiableEntity
){
201 relatedObjectString
+= ((IIdentifiableEntity
) upatedObject
).getTitleCache()+separator
;
204 relatedObjectString
+= upatedObject
.toString()+separator
;
207 if(relatedObjectString
.endsWith(separator
)){
208 relatedObjectString
= relatedObjectString
.substring(0, relatedObjectString
.length()-separator
.length());
210 return "[DeleteResult]\n" +
211 "Status: " + status
.toString()+"\n" +
212 "Exceptions: " + exceptionString
+"\n" +
213 "Related Objects: "+relatedObjectString
;
215 public void setCdmEntity(CdmBase cdmBase
) {
216 this.cdmEntity
= cdmBase
;
220 public CdmBase
getCdmEntity(){
224 public Set
<CdmBase
> getUnchangedObjects() {
225 return unchangedObjects
;
228 public void addUnchangedObjects(Set
<?
extends CdmBase
> unchangedObjects
) {
229 this.unchangedObjects
.addAll(unchangedObjects
);
231 public void addUnChangedObject(CdmBase unchangedObject
) {
232 this.unchangedObjects
.add(unchangedObject
);