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
.ArrayList
;
15 import java
.util
.Collection
;
16 import java
.util
.HashSet
;
17 import java
.util
.List
;
20 import org
.apache
.commons
.collections
.buffer
.CircularFifoBuffer
;
21 import org
.apache
.log4j
.Logger
;
23 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
24 import eu
.etaxonomy
.cdm
.model
.common
.IIdentifiableEntity
;
27 * This class represents the result of an update action.
33 public class UpdateResult
implements Serializable
{
36 private static final long serialVersionUID
= 1L;
38 @SuppressWarnings("unused")
39 private static final Logger logger
= Logger
.getLogger(UpdateResult
.class);
41 private Status status
= Status
.OK
;
43 @SuppressWarnings("unchecked")
44 private final Collection
<Exception
> exceptions
= new CircularFifoBuffer(10);
46 private final Set
<CdmBase
> upatedObjects
= new HashSet
<CdmBase
>();
48 private final Set
<CdmBase
> unchangedObjects
= new HashSet
<CdmBase
>();
50 private CdmBase cdmEntity
;
52 // private Set<PersistPair> objectsToDelete = new HashSet<PersistPair>();
54 // private Set<PersistPair> objectsToSave = new HashSet<DeleteResult.PersistPair>();
56 // protected class PersistPair{
57 // protected CdmBase objectToPersist;
58 // protected ICdmEntityDao<CdmBase> dao;
67 protected Integer severity
;
68 private Status(int severity
){
69 this.severity
= severity
;
72 public int compareSeverity(Status other
){
73 return this.severity
.compareTo(other
.severity
);
77 //***************************** GETTER /SETTER /ADDER *************************/
79 * The resulting status of an update action.
84 public Status
getStatus() {
87 public void setStatus(Status status
) {
92 * The highest exception that occurred during delete (if any).
95 public List
<Exception
> getExceptions() {
96 return new ArrayList
<Exception
>(exceptions
);
98 public void addException(Exception exception
) {
99 this.exceptions
.add(exception
);
101 public void addExceptions(Collection
<Exception
> exceptions
) {
102 this.exceptions
.addAll(exceptions
);
106 * Related objects that prevent the delete action to take place.
109 public Set
<CdmBase
> getUpdatedObjects() {
110 return upatedObjects
;
112 public void addUpdatedObject(CdmBase relatedObject
) {
113 this.upatedObjects
.add(relatedObject
);
115 public void addUpdatedObjects(Set
<?
extends CdmBase
> updatedObjects
) {
116 this.upatedObjects
.addAll(updatedObjects
);
123 // public Set<PersistPair> getObjectsToDelete() {
124 // return objectsToDelete;
126 // public void setObjectsToDelete(Set<PersistPair> objectsToDelete) {
127 // this.objectsToDelete = objectsToDelete;
133 // public Set<PersistPair> getObjectsToSave() {
134 // return objectsToSave;
136 // public void setObjectsToSave(Set<PersistPair> objectsToSave) {
137 // this.objectsToSave = objectsToSave;
141 //****************** CONVENIENCE *********************************************/
144 * Sets the status to {@link DeleteStatus#ERROR} if not yet set to a more serious
147 public void setError(){
148 setMaxStatus(Status
.ERROR
);
152 * Sets the status to {@link DeleteStatus#ABORT} if not yet set to a more serious
155 public void setAbort(){
156 setMaxStatus(Status
.ABORT
);
160 * Sets status to most severe status. If maxStatus is more severe then existing status
161 * existing status is set to maxStatus. Otherwise nothing changes.
162 * If minStatus is more severe then given status minStatus will be the new status.
165 public void setMaxStatus(Status maxStatus
) {
166 if (this.status
.compareSeverity(maxStatus
) < 0){
167 this.status
= maxStatus
;
171 public void includeResult(UpdateResult includedResult
){
172 this.setMaxStatus(includedResult
.getStatus());
173 this.addExceptions(includedResult
.getExceptions());
174 this.addUpdatedObjects(includedResult
.getUpdatedObjects());
177 public boolean isOk(){
178 return this.status
== Status
.OK
;
181 public boolean isAbort(){
182 return this.status
== Status
.ABORT
;
185 public boolean isError(){
186 return this.status
== Status
.ERROR
;
192 public String
toString(){
193 String separator
= ", ";
194 String exceptionString
= "";
195 for (Exception exception
: exceptions
) {
196 exceptionString
+= exception
.getLocalizedMessage()+separator
;
198 if(exceptionString
.endsWith(separator
)){
199 exceptionString
= exceptionString
.substring(0, exceptionString
.length()-separator
.length());
201 String relatedObjectString
= "";
202 for (CdmBase upatedObject
: upatedObjects
) {
203 if(upatedObject
instanceof IIdentifiableEntity
){
204 relatedObjectString
+= ((IIdentifiableEntity
) upatedObject
).getTitleCache()+separator
;
207 relatedObjectString
+= upatedObject
.toString()+separator
;
210 if(relatedObjectString
.endsWith(separator
)){
211 relatedObjectString
= relatedObjectString
.substring(0, relatedObjectString
.length()-separator
.length());
213 return "[DeleteResult]\n" +
214 "Status: " + status
.toString()+"\n" +
215 "Exceptions: " + exceptionString
+"\n" +
216 "Related Objects: "+relatedObjectString
;
218 public void setCdmEntity(CdmBase cdmBase
) {
219 this.cdmEntity
= cdmBase
;
223 public CdmBase
getCdmEntity(){
227 public Set
<CdmBase
> getUnchangedObjects() {
228 return unchangedObjects
;
231 public void addUnchangedObjects(Set
<?
extends CdmBase
> unchangedObjects
) {
232 this.unchangedObjects
.addAll(unchangedObjects
);
234 public void addUnChangedObject(CdmBase unchangedObject
) {
235 this.unchangedObjects
.add(unchangedObject
);