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 @SuppressWarnings("unchecked")
42 private final Collection
<Exception
> exceptions
= new CircularFifoBuffer(10);
44 private final Set
<CdmBase
> upatedObjects
= new HashSet
<CdmBase
>();
46 private final Set
<CdmBase
> unchangedObjects
= new HashSet
<CdmBase
>();
48 private CdmBase cdmEntity
;
50 // private Set<PersistPair> objectsToDelete = new HashSet<PersistPair>();
52 // private Set<PersistPair> objectsToSave = new HashSet<DeleteResult.PersistPair>();
54 // protected class PersistPair{
55 // protected CdmBase objectToPersist;
56 // protected ICdmEntityDao<CdmBase> dao;
65 protected Integer severity
;
66 private Status(int severity
){
67 this.severity
= severity
;
70 public int compareSeverity(Status other
){
71 return this.severity
.compareTo(other
.severity
);
75 //***************************** GETTER /SETTER /ADDER *************************/
77 * The resulting status of an update action.
82 public Status
getStatus() {
85 public void setStatus(Status status
) {
90 * The highest exception that occurred during delete (if any).
93 public Collection
<Exception
> getExceptions() {
96 public void addException(Exception exception
) {
97 this.exceptions
.add(exception
);
99 public void addExceptions(Collection
<Exception
> exceptions
) {
100 this.exceptions
.addAll(exceptions
);
104 * Related objects that prevent the delete action to take place.
107 public Set
<CdmBase
> getUpdatedObjects() {
108 return upatedObjects
;
110 public void addUpdatedObject(CdmBase relatedObject
) {
111 this.upatedObjects
.add(relatedObject
);
113 public void addUpdatedObjects(Set
<?
extends CdmBase
> updatedObjects
) {
114 this.upatedObjects
.addAll(updatedObjects
);
121 // public Set<PersistPair> getObjectsToDelete() {
122 // return objectsToDelete;
124 // public void setObjectsToDelete(Set<PersistPair> objectsToDelete) {
125 // this.objectsToDelete = objectsToDelete;
131 // public Set<PersistPair> getObjectsToSave() {
132 // return objectsToSave;
134 // public void setObjectsToSave(Set<PersistPair> objectsToSave) {
135 // this.objectsToSave = objectsToSave;
139 //****************** CONVENIENCE *********************************************/
142 * Sets the status to {@link DeleteStatus#ERROR} if not yet set to a more serious
145 public void setError(){
146 setMaxStatus(Status
.ERROR
);
150 * Sets the status to {@link DeleteStatus#ABORT} if not yet set to a more serious
153 public void setAbort(){
154 setMaxStatus(Status
.ABORT
);
158 * Sets status to most severe status. If maxStatus is more severe then existing status
159 * existing status is set to maxStatus. Otherwise nothing changes.
160 * If minStatus is more severe then given status minStatus will be the new status.
163 public void setMaxStatus(Status maxStatus
) {
164 if (this.status
.compareSeverity(maxStatus
) < 0){
165 this.status
= maxStatus
;
169 public void includeResult(UpdateResult includedResult
){
170 this.setMaxStatus(includedResult
.getStatus());
171 this.addExceptions(includedResult
.getExceptions());
172 this.addUpdatedObjects(includedResult
.getUpdatedObjects());
175 public boolean isOk(){
176 return this.status
== Status
.OK
;
179 public boolean isAbort(){
180 return this.status
== Status
.ABORT
;
183 public boolean isError(){
184 return this.status
== Status
.ERROR
;
190 public String
toString(){
191 String separator
= ", ";
192 String exceptionString
= "";
193 for (Exception exception
: exceptions
) {
194 exceptionString
+= exception
.getLocalizedMessage()+separator
;
196 if(exceptionString
.endsWith(separator
)){
197 exceptionString
= exceptionString
.substring(0, exceptionString
.length()-separator
.length());
199 String relatedObjectString
= "";
200 for (CdmBase upatedObject
: upatedObjects
) {
201 if(upatedObject
instanceof IIdentifiableEntity
){
202 relatedObjectString
+= ((IIdentifiableEntity
) upatedObject
).getTitleCache()+separator
;
205 relatedObjectString
+= upatedObject
.toString()+separator
;
208 if(relatedObjectString
.endsWith(separator
)){
209 relatedObjectString
= relatedObjectString
.substring(0, relatedObjectString
.length()-separator
.length());
211 return "[DeleteResult]\n" +
212 "Status: " + status
.toString()+"\n" +
213 "Exceptions: " + exceptionString
+"\n" +
214 "Related Objects: "+relatedObjectString
;
216 public void setCdmEntity(CdmBase cdmBase
) {
217 this.cdmEntity
= cdmBase
;
221 public CdmBase
getCdmEntity(){
225 public Set
<CdmBase
> getUnchangedObjects() {
226 return unchangedObjects
;
229 public void addUnchangedObjects(Set
<?
extends CdmBase
> unchangedObjects
) {
230 this.unchangedObjects
.addAll(unchangedObjects
);
232 public void addUnChangedObject(CdmBase unchangedObject
) {
233 this.unchangedObjects
.add(unchangedObject
);