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
;
25 * This class represents the result of an update action.
31 public class UpdateResult
implements Serializable
{
35 @SuppressWarnings("unused")
36 private static final Logger logger
= Logger
.getLogger(UpdateResult
.class);
38 private Status status
= Status
.OK
;
40 private final List
<Exception
> exceptions
= new ArrayList
<Exception
>();
42 private final Set
<CdmBase
> upatedObjects
= new HashSet
<CdmBase
>();
44 private CdmBase cdmEntity
;
46 // private Set<PersistPair> objectsToDelete = new HashSet<PersistPair>();
48 // private Set<PersistPair> objectsToSave = new HashSet<DeleteResult.PersistPair>();
50 // protected class PersistPair{
51 // protected CdmBase objectToPersist;
52 // protected ICdmEntityDao<CdmBase> dao;
61 protected Integer severity
;
62 private Status(int severity
){
63 this.severity
= severity
;
66 public int compareSeverity(Status other
){
67 return this.severity
.compareTo(other
.severity
);
71 //***************************** GETTER /SETTER /ADDER *************************/
73 * The resulting status of an update action.
78 public Status
getStatus() {
81 public void setStatus(Status status
) {
86 * The highest exception that occurred during delete (if any).
89 public List
<Exception
> getExceptions() {
92 public void addException(Exception exception
) {
93 this.exceptions
.add(exception
);
95 public void addExceptions(List
<Exception
> exceptions
) {
96 this.exceptions
.addAll(exceptions
);
100 * Related objects that prevent the delete action to take place.
103 public Set
<CdmBase
> getUpdatedObjects() {
104 return upatedObjects
;
106 public void addUpdatedObject(CdmBase relatedObject
) {
107 this.upatedObjects
.add(relatedObject
);
109 public void addUpdatedObjects(Set
<?
extends CdmBase
> updatedObjects
) {
110 this.upatedObjects
.addAll(updatedObjects
);
117 // public Set<PersistPair> getObjectsToDelete() {
118 // return objectsToDelete;
120 // public void setObjectsToDelete(Set<PersistPair> objectsToDelete) {
121 // this.objectsToDelete = objectsToDelete;
127 // public Set<PersistPair> getObjectsToSave() {
128 // return objectsToSave;
130 // public void setObjectsToSave(Set<PersistPair> objectsToSave) {
131 // this.objectsToSave = objectsToSave;
135 //****************** CONVENIENCE *********************************************/
138 * Sets the status to {@link DeleteStatus#ERROR} if not yet set to a more serious
141 public void setError(){
142 setMaxStatus(Status
.ERROR
);
146 * Sets the status to {@link DeleteStatus#ABORT} if not yet set to a more serious
149 public void setAbort(){
150 setMaxStatus(Status
.ABORT
);
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.
159 public void setMaxStatus(Status maxStatus
) {
160 if (this.status
.compareSeverity(maxStatus
) < 0){
161 this.status
= maxStatus
;
165 public void includeResult(UpdateResult includedResult
){
166 this.setMaxStatus(includedResult
.getStatus());
167 this.addExceptions(includedResult
.getExceptions());
168 this.addUpdatedObjects(includedResult
.getUpdatedObjects());
171 public boolean isOk(){
172 return this.status
== Status
.OK
;
175 public boolean isAbort(){
176 return this.status
== Status
.ABORT
;
179 public boolean isError(){
180 return this.status
== Status
.ERROR
;
186 public String
toString(){
187 String separator
= ", ";
188 String exceptionString
= "";
189 for (Exception exception
: exceptions
) {
190 exceptionString
+= exception
.getLocalizedMessage()+separator
;
192 if(exceptionString
.endsWith(separator
)){
193 exceptionString
= exceptionString
.substring(0, exceptionString
.length()-separator
.length());
195 String relatedObjectString
= "";
196 for (CdmBase upatedObject
: upatedObjects
) {
197 if(upatedObject
instanceof IIdentifiableEntity
){
198 relatedObjectString
+= ((IIdentifiableEntity
) upatedObject
).getTitleCache()+separator
;
201 relatedObjectString
+= upatedObject
.toString()+separator
;
204 if(relatedObjectString
.endsWith(separator
)){
205 relatedObjectString
= relatedObjectString
.substring(0, relatedObjectString
.length()-separator
.length());
207 return "[DeleteResult]\n" +
208 "Status: " + status
.toString()+"\n" +
209 "Exceptions: " + exceptionString
+"\n" +
210 "Related Objects: "+relatedObjectString
;
213 public CdmBase
getCdmEntity() {
216 public void setCdmEntity(CdmBase cdmEntity
) {
217 this.cdmEntity
= cdmEntity
;