3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.api
.service
;
12 import java
.util
.ArrayList
;
13 import java
.util
.HashSet
;
14 import java
.util
.List
;
17 import org
.apache
.log4j
.Logger
;
19 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
20 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ICdmEntityDao
;
21 import eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.common
.DaoBase
;
24 * This class represents the result of a delete action.
30 public class DeleteResult
{
31 @SuppressWarnings("unused")
32 private static final Logger logger
= Logger
.getLogger(DeleteResult
.class);
34 private DeleteStatus status
= DeleteStatus
.OK
;
36 private List
<Exception
> exceptions
= new ArrayList
<Exception
>();
38 private Set
<CdmBase
> relatedObjects
= new HashSet
<CdmBase
>();
40 private Set
<PersistPair
> objectsToDelete
= new HashSet
<PersistPair
>();
42 private Set
<PersistPair
> objectsToSave
= new HashSet
<DeleteResult
.PersistPair
>();
44 protected class PersistPair
{
45 protected CdmBase objectToPersist
;
46 protected ICdmEntityDao
<CdmBase
> dao
;
49 public enum DeleteStatus
{
55 protected Integer severity
;
56 private DeleteStatus(int severity
){
57 this.severity
= severity
;
60 public int compareSeverity(DeleteStatus other
){
61 return this.severity
.compareTo(other
.severity
);
65 //***************************** GETTER /SETTER /ADDER *************************/
67 * The resuting status of a delete action.
72 public DeleteStatus
getStatus() {
75 public void setStatus(DeleteStatus status
) {
80 * The highest exception that occurred during delete (if any).
83 public List
<Exception
> getExceptions() {
86 public void addException(Exception exception
) {
87 this.exceptions
.add(exception
);
89 public void addExceptions(List
<Exception
> exceptions
) {
90 this.exceptions
.addAll(exceptions
);
94 * Related objects that prevent the delete action to take place.
97 public Set
<CdmBase
> getRelatedObjects() {
98 return relatedObjects
;
100 public void addRelatedObject(CdmBase relatedObject
) {
101 this.relatedObjects
.add(relatedObject
);
103 public void addRelatedObjects(Set
<?
extends CdmBase
> relatedObjects
) {
104 this.relatedObjects
.addAll(relatedObjects
);
111 public Set
<PersistPair
> getObjectsToDelete() {
112 return objectsToDelete
;
114 public void setObjectsToDelete(Set
<PersistPair
> objectsToDelete
) {
115 this.objectsToDelete
= objectsToDelete
;
121 public Set
<PersistPair
> getObjectsToSave() {
122 return objectsToSave
;
124 public void setObjectsToSave(Set
<PersistPair
> objectsToSave
) {
125 this.objectsToSave
= objectsToSave
;
129 //****************** CONVENIENCE *********************************************/
132 * Sets the status to {@link DeleteStatus#ERROR} if not yet set to a more serious
135 public void setError(){
136 setMaxStatus(DeleteStatus
.ERROR
);
140 * Sets the status to {@link DeleteStatus#ABORT} if not yet set to a more serious
143 public void setAbort(){
144 setMaxStatus(DeleteStatus
.ABORT
);
148 * Sets status to most severe status. If maxStatus is more severe then existing status
149 * existing status is set to maxStatus. Otherwise nothing changes.
150 * If minStatus is more severe then given status minStatus will be the new status.
153 public void setMaxStatus(DeleteStatus maxStatus
) {
154 if (this.status
.compareSeverity(maxStatus
) < 0){
155 this.status
= maxStatus
;
159 public void includeResult(DeleteResult includedResult
){
160 this.setMaxStatus(includedResult
.getStatus());
161 this.addExceptions(includedResult
.getExceptions());
162 this.addRelatedObjects(includedResult
.getRelatedObjects());
165 public boolean isOk(){
166 return this.status
== DeleteStatus
.OK
;
169 public boolean isAbort(){
170 return this.status
== DeleteStatus
.ABORT
;
173 public boolean isError(){
174 return this.status
== DeleteStatus
.ERROR
;
180 public String
toString(){
181 return status
.toString();