Project

General

Profile

Download (5.47 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.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.model.common.IIdentifiableEntity;
21

    
22
/**
23
 * This class represents the result of a delete action.
24
 *
25
 * @author a.mueller
26
 * @date 04.01.2012
27
 *
28
 */
29
public class DeleteResult {
30
	@SuppressWarnings("unused")
31
	private static final Logger logger = Logger.getLogger(DeleteResult.class);
32

    
33
	private DeleteStatus status = DeleteStatus.OK;
34

    
35
	private final List<Exception> exceptions = new ArrayList<Exception>();
36

    
37
	private final Set<CdmBase> relatedObjects = new HashSet<CdmBase>();
38

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

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

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

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

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

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

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

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

    
106

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

    
127

    
128
//****************** CONVENIENCE *********************************************/
129

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

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

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

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

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

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

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

    
176

    
177

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

    
206

    
207
}
(13-13/84)