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
.HashSet
;
16 import java
.util
.List
;
19 import org
.apache
.log4j
.Logger
;
21 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
22 import eu
.etaxonomy
.cdm
.model
.common
.IIdentifiableEntity
;
23 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
26 * This class represents the result of an update action.
32 public class UpdateResult
implements Serializable
{
36 @SuppressWarnings("unused")
37 private static final Logger logger
= Logger
.getLogger(UpdateResult
.class);
39 private Status status
= Status
.OK
;
41 private final List
<Exception
> exceptions
= new ArrayList
<Exception
>();
43 private final Set
<CdmBase
> upatedObjects
= new HashSet
<CdmBase
>();
45 private CdmBase cdmEntity
;
47 // private Set<PersistPair> objectsToDelete = new HashSet<PersistPair>();
49 // private Set<PersistPair> objectsToSave = new HashSet<DeleteResult.PersistPair>();
51 // protected class PersistPair{
52 // protected CdmBase objectToPersist;
53 // protected ICdmEntityDao<CdmBase> dao;
62 protected Integer severity
;
63 private Status(int severity
){
64 this.severity
= severity
;
67 public int compareSeverity(Status other
){
68 return this.severity
.compareTo(other
.severity
);
72 //***************************** GETTER /SETTER /ADDER *************************/
74 * The resulting status of an update action.
79 public Status
getStatus() {
82 public void setStatus(Status status
) {
87 * The highest exception that occurred during delete (if any).
90 public List
<Exception
> getExceptions() {
93 public void addException(Exception exception
) {
94 this.exceptions
.add(exception
);
96 public void addExceptions(List
<Exception
> exceptions
) {
97 this.exceptions
.addAll(exceptions
);
101 * Related objects that prevent the delete action to take place.
104 public Set
<CdmBase
> getUpdatedObjects() {
105 return upatedObjects
;
107 public void addUpdatedObject(CdmBase relatedObject
) {
108 this.upatedObjects
.add(relatedObject
);
110 public void addUpdatedObjects(Set
<?
extends CdmBase
> updatedObjects
) {
111 this.upatedObjects
.addAll(updatedObjects
);
118 // public Set<PersistPair> getObjectsToDelete() {
119 // return objectsToDelete;
121 // public void setObjectsToDelete(Set<PersistPair> objectsToDelete) {
122 // this.objectsToDelete = objectsToDelete;
128 // public Set<PersistPair> getObjectsToSave() {
129 // return objectsToSave;
131 // public void setObjectsToSave(Set<PersistPair> objectsToSave) {
132 // this.objectsToSave = objectsToSave;
136 //****************** CONVENIENCE *********************************************/
139 * Sets the status to {@link DeleteStatus#ERROR} if not yet set to a more serious
142 public void setError(){
143 setMaxStatus(Status
.ERROR
);
147 * Sets the status to {@link DeleteStatus#ABORT} if not yet set to a more serious
150 public void setAbort(){
151 setMaxStatus(Status
.ABORT
);
155 * Sets status to most severe status. If maxStatus is more severe then existing status
156 * existing status is set to maxStatus. Otherwise nothing changes.
157 * If minStatus is more severe then given status minStatus will be the new status.
160 public void setMaxStatus(Status maxStatus
) {
161 if (this.status
.compareSeverity(maxStatus
) < 0){
162 this.status
= maxStatus
;
166 public void includeResult(UpdateResult includedResult
){
167 this.setMaxStatus(includedResult
.getStatus());
168 this.addExceptions(includedResult
.getExceptions());
169 this.addUpdatedObjects(includedResult
.getUpdatedObjects());
172 public boolean isOk(){
173 return this.status
== Status
.OK
;
176 public boolean isAbort(){
177 return this.status
== Status
.ABORT
;
180 public boolean isError(){
181 return this.status
== Status
.ERROR
;
187 public String
toString(){
188 String separator
= ", ";
189 String exceptionString
= "";
190 for (Exception exception
: exceptions
) {
191 exceptionString
+= exception
.getLocalizedMessage()+separator
;
193 if(exceptionString
.endsWith(separator
)){
194 exceptionString
= exceptionString
.substring(0, exceptionString
.length()-separator
.length());
196 String relatedObjectString
= "";
197 for (CdmBase upatedObject
: upatedObjects
) {
198 if(upatedObject
instanceof IIdentifiableEntity
){
199 relatedObjectString
+= ((IIdentifiableEntity
) upatedObject
).getTitleCache()+separator
;
202 relatedObjectString
+= upatedObject
.toString()+separator
;
205 if(relatedObjectString
.endsWith(separator
)){
206 relatedObjectString
= relatedObjectString
.substring(0, relatedObjectString
.length()-separator
.length());
208 return "[DeleteResult]\n" +
209 "Status: " + status
.toString()+"\n" +
210 "Exceptions: " + exceptionString
+"\n" +
211 "Related Objects: "+relatedObjectString
;
213 public void setCdmEntity(CdmBase cdmBase
) {
214 this.cdmEntity
= cdmBase
;