remove deprecated method
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / DeleteResult.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10 package eu.etaxonomy.cdm.api.service;
11
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Set;
16
17 import org.apache.log4j.Logger;
18
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;
22
23 /**
24 * This class represents the result of a delete action.
25 *
26 * @author a.mueller
27 * @date 04.01.2012
28 *
29 */
30 public class DeleteResult {
31 @SuppressWarnings("unused")
32 private static final Logger logger = Logger.getLogger(DeleteResult.class);
33
34 private DeleteStatus status = DeleteStatus.OK;
35
36 private List<Exception> exceptions = new ArrayList<Exception>();
37
38 private Set<CdmBase> relatedObjects = new HashSet<CdmBase>();
39
40 private Set<PersistPair> objectsToDelete = new HashSet<PersistPair>();
41
42 private Set<PersistPair> objectsToSave = new HashSet<DeleteResult.PersistPair>();
43
44 protected class PersistPair{
45 protected CdmBase objectToPersist;
46 protected ICdmEntityDao<CdmBase> dao;
47 }
48
49 public enum DeleteStatus {
50 OK(0),
51 ABORT(1),
52 ERROR(3),
53 ;
54
55 protected Integer severity;
56 private DeleteStatus(int severity){
57 this.severity = severity;
58 }
59
60 public int compareSeverity(DeleteStatus other){
61 return this.severity.compareTo(other.severity);
62 }
63 }
64
65 //***************************** GETTER /SETTER /ADDER *************************/
66 /**
67 * The resuting status of a delete action.
68 *
69 * @see DeleteStatus
70 * @return
71 */
72 public DeleteStatus getStatus() {
73 return status;
74 }
75 public void setStatus(DeleteStatus status) {
76 this.status = status;
77 }
78
79 /**
80 * The highest exception that occurred during delete (if any).
81 * @return
82 */
83 public List<Exception> getExceptions() {
84 return exceptions;
85 }
86 public void addException(Exception exception) {
87 this.exceptions.add(exception);
88 }
89 public void addExceptions(List<Exception> exceptions) {
90 this.exceptions.addAll(exceptions);
91 }
92
93 /**
94 * Related objects that prevent the delete action to take place.
95 * @return
96 */
97 public Set<CdmBase> getRelatedObjects() {
98 return relatedObjects;
99 }
100 public void addRelatedObject(CdmBase relatedObject) {
101 this.relatedObjects.add(relatedObject);
102 }
103 public void addRelatedObjects(Set<? extends CdmBase> relatedObjects) {
104 this.relatedObjects.addAll(relatedObjects);
105 }
106
107
108 /**
109 * @return
110 */
111 public Set<PersistPair> getObjectsToDelete() {
112 return objectsToDelete;
113 }
114 public void setObjectsToDelete(Set<PersistPair> objectsToDelete) {
115 this.objectsToDelete = objectsToDelete;
116 }
117
118 /**
119 * @return
120 */
121 public Set<PersistPair> getObjectsToSave() {
122 return objectsToSave;
123 }
124 public void setObjectsToSave(Set<PersistPair> objectsToSave) {
125 this.objectsToSave = objectsToSave;
126 }
127
128
129 //****************** CONVENIENCE *********************************************/
130
131 /**
132 * Sets the status to {@link DeleteStatus#ERROR} if not yet set to a more serious
133 * status.
134 */
135 public void setError(){
136 setMaxStatus(DeleteStatus.ERROR);
137 }
138
139 /**
140 * Sets the status to {@link DeleteStatus#ABORT} if not yet set to a more serious
141 * status.
142 */
143 public void setAbort(){
144 setMaxStatus(DeleteStatus.ABORT);
145 }
146
147 /**
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.
151 * @param maxStatus
152 */
153 public void setMaxStatus(DeleteStatus maxStatus) {
154 if (this.status.compareSeverity(maxStatus) < 0){
155 this.status = maxStatus;
156 }
157 }
158
159 public void includeResult(DeleteResult includedResult){
160 this.setMaxStatus(includedResult.getStatus());
161 this.addExceptions(includedResult.getExceptions());
162 this.addRelatedObjects(includedResult.getRelatedObjects());
163 }
164
165 public boolean isOk(){
166 return this.status == DeleteStatus.OK;
167 }
168
169 public boolean isAbort(){
170 return this.status == DeleteStatus.ABORT;
171 }
172
173 public boolean isError(){
174 return this.status == DeleteStatus.ERROR;
175 }
176
177
178
179 @Override
180 public String toString(){
181 return status.toString();
182 }
183
184
185 }