Project

General

Profile

Download (5.52 KB) Statistics
| Branch: | Tag: | Revision:
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.io.Serializable;
13
import java.util.ArrayList;
14
import java.util.HashSet;
15
import java.util.List;
16
import java.util.Set;
17

    
18
import org.apache.log4j.Logger;
19

    
20
import eu.etaxonomy.cdm.model.common.CdmBase;
21
import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
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 implements Serializable {
31

    
32
	@SuppressWarnings("unused")
33
	private static final Logger logger = Logger.getLogger(DeleteResult.class);
34

    
35
	private DeleteStatus status = DeleteStatus.OK;
36

    
37
	private final List<Exception> exceptions = new ArrayList<Exception>();
38

    
39
	private final Set<CdmBase> relatedObjects = new HashSet<CdmBase>();
40

    
41
//	private Set<PersistPair> objectsToDelete = new HashSet<PersistPair>();
42
//
43
//	private Set<PersistPair> objectsToSave = new HashSet<DeleteResult.PersistPair>();
44

    
45
//	protected class PersistPair{
46
//		protected CdmBase objectToPersist;
47
//		protected ICdmEntityDao<CdmBase> dao;
48
//	}
49

    
50
	public enum DeleteStatus {
51
		OK(0),
52
		ABORT(1),
53
		ERROR(3),
54
		;
55

    
56
		protected Integer severity;
57
		private DeleteStatus(int severity){
58
			this.severity = severity;
59
		}
60

    
61
		public int compareSeverity(DeleteStatus other){
62
			return this.severity.compareTo(other.severity);
63
		}
64
	}
65

    
66
//***************************** GETTER /SETTER /ADDER *************************/
67
	/**
68
	 * The resuting status of a delete action.
69
	 *
70
	 * @see DeleteStatus
71
	 * @return
72
	 */
73
	public DeleteStatus getStatus() {
74
		return status;
75
	}
76
	public void setStatus(DeleteStatus status) {
77
		this.status = status;
78
	}
79

    
80
	/**
81
	 * The highest exception that occurred during delete (if any).
82
	 * @return
83
	 */
84
	public List<Exception> getExceptions() {
85
		return exceptions;
86
	}
87
	public void addException(Exception exception) {
88
		this.exceptions.add(exception);
89
	}
90
	public void addExceptions(List<Exception> exceptions) {
91
		this.exceptions.addAll(exceptions);
92
	}
93

    
94
	/**
95
	 * Related objects that prevent the delete action to take place.
96
	 * @return
97
	 */
98
	public Set<CdmBase> getRelatedObjects() {
99
		return relatedObjects;
100
	}
101
	public void addRelatedObject(CdmBase relatedObject) {
102
		this.relatedObjects.add(relatedObject);
103
	}
104
	public void addRelatedObjects(Set<? extends CdmBase> relatedObjects) {
105
		this.relatedObjects.addAll(relatedObjects);
106
	}
107

    
108

    
109
//	/**
110
//	 * @return
111
//	 */
112
//	public Set<PersistPair> getObjectsToDelete() {
113
//		return objectsToDelete;
114
//	}
115
//	public void setObjectsToDelete(Set<PersistPair> objectsToDelete) {
116
//		this.objectsToDelete = objectsToDelete;
117
//	}
118
//
119
//	/**
120
//	 * @return
121
//	 */
122
//	public Set<PersistPair> getObjectsToSave() {
123
//		return objectsToSave;
124
//	}
125
//	public void setObjectsToSave(Set<PersistPair> objectsToSave) {
126
//		this.objectsToSave = objectsToSave;
127
//	}
128

    
129

    
130
//****************** CONVENIENCE *********************************************/
131

    
132
	/**
133
	 * Sets the status to {@link DeleteStatus#ERROR} if not yet set to a more serious
134
	 * status.
135
	 */
136
	public void setError(){
137
		setMaxStatus(DeleteStatus.ERROR);
138
	}
139

    
140
	/**
141
	 * Sets the status to {@link DeleteStatus#ABORT} if not yet set to a more serious
142
	 * status.
143
	 */
144
	public void setAbort(){
145
		setMaxStatus(DeleteStatus.ABORT);
146
	}
147

    
148
	/**
149
	 * Sets status to most severe status. If maxStatus is more severe then existing status
150
	 * existing status is set to maxStatus. Otherwise nothing changes.
151
	 * If minStatus is more severe then given status minStatus will be the new status.
152
	 * @param maxStatus
153
	 */
154
	public void setMaxStatus(DeleteStatus maxStatus) {
155
		if (this.status.compareSeverity(maxStatus) < 0){
156
			this.status = maxStatus;
157
		}
158
	}
159

    
160
	public void includeResult(DeleteResult includedResult){
161
		this.setMaxStatus(includedResult.getStatus());
162
		this.addExceptions(includedResult.getExceptions());
163
		this.addRelatedObjects(includedResult.getRelatedObjects());
164
	}
165

    
166
	public boolean isOk(){
167
		return this.status == DeleteStatus.OK;
168
	}
169

    
170
	public boolean isAbort(){
171
		return this.status == DeleteStatus.ABORT;
172
	}
173

    
174
	public boolean isError(){
175
		return this.status == DeleteStatus.ERROR;
176
	}
177

    
178

    
179

    
180
	@Override
181
	public String toString(){
182
	    String separator = ", ";
183
	    String exceptionString = "";
184
	    for (Exception exception : exceptions) {
185
            exceptionString += exception.getLocalizedMessage()+separator;
186
        }
187
	    if(exceptionString.endsWith(separator)){
188
	        exceptionString = exceptionString.substring(0, exceptionString.length()-separator.length());
189
	    }
190
	    String relatedObjectString = "";
191
	    for (CdmBase relatedObject: relatedObjects) {
192
	        if(relatedObject instanceof IIdentifiableEntity){
193
	            relatedObjectString += ((IIdentifiableEntity) relatedObject).getTitleCache()+separator;
194
	        }
195
	        else{
196
	            relatedObjectString += relatedObject.toString()+separator;
197
	        }
198
	    }
199
	    if(relatedObjectString.endsWith(separator)){
200
	        relatedObjectString = relatedObjectString.substring(0, relatedObjectString.length()-separator.length());
201
	    }
202
		return "[DeleteResult]\n" +
203
				"Status: " + status.toString()+"\n" +
204
						"Exceptions: " + exceptionString+"\n" +
205
								"Related Objects: "+relatedObjectString;
206
	}
207

    
208

    
209
}
(13-13/88)