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
.Collection
;
15 import java
.util
.HashSet
;
18 import org
.apache
.commons
.collections
.buffer
.CircularFifoBuffer
;
19 import org
.apache
.log4j
.Logger
;
21 import eu
.etaxonomy
.cdm
.api
.service
.dto
.CdmEntityIdentifier
;
22 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
23 import eu
.etaxonomy
.cdm
.model
.common
.IIdentifiableEntity
;
26 * This class represents the result of an update action.
32 public class UpdateResult
implements Serializable
{
34 private static final long serialVersionUID
= -7040027587709706700L;
36 @SuppressWarnings("unused")
37 private static final Logger logger
= Logger
.getLogger(UpdateResult
.class);
39 private Status status
= Status
.OK
;
41 @SuppressWarnings("unchecked")
42 private final Collection
<Exception
> exceptions
= new CircularFifoBuffer(10);
44 private Set
<CdmBase
> updatedObjects
= new HashSet
<>();
46 private final Set
<CdmEntityIdentifier
> updatedCdmIds
= new HashSet
<>();
48 private final Set
<CdmBase
> unchangedObjects
= new HashSet
<>();
50 private CdmBase cdmEntity
;
53 // private Set<PersistPair> objectsToDelete = new HashSet<>();
55 // private Set<PersistPair> objectsToSave = new HashSet<>();
57 // protected class PersistPair{
58 // protected CdmBase objectToPersist;
59 // protected ICdmEntityDao<CdmBase> dao;
68 protected Integer severity
;
69 private Status(int severity
){
70 this.severity
= severity
;
73 public int compareSeverity(Status other
){
74 return this.severity
.compareTo(other
.severity
);
78 //***************************** GETTER /SETTER /ADDER *************************/
80 * The resulting status of an update action.
85 public Status
getStatus() {
88 public void setStatus(Status status
) {
93 * The highest exception that occurred during delete (if any).
96 public Collection
<Exception
> getExceptions() {
99 public void addException(Exception exception
) {
100 this.exceptions
.add(exception
);
102 public void addExceptions(Collection
<Exception
> exceptions
) {
103 this.exceptions
.addAll(exceptions
);
107 * Related objects that prevent the delete action to take place.
110 public Set
<CdmEntityIdentifier
> getUpdatedCdmIds() {
111 return updatedCdmIds
;
113 public void addUpdatedCdmId(CdmEntityIdentifier cdmId
) {
114 this.updatedCdmIds
.add(cdmId
);
116 public void addUpdatedCdmIds(Set
<CdmEntityIdentifier
> updatedCdmIds
) {
117 this.updatedCdmIds
.addAll(updatedCdmIds
);
121 public Set
<CdmBase
> getUpdatedObjects() {
122 return updatedObjects
;
124 public void addUpdatedObject(CdmBase relatedObject
) {
125 this.updatedObjects
.add(relatedObject
);
127 public void addUpdatedObjects(Set
<?
extends CdmBase
> updatedObjects
) {
128 this.updatedObjects
.addAll(updatedObjects
);
134 // public Set<PersistPair> getObjectsToDelete() {
135 // return objectsToDelete;
137 // public void setObjectsToDelete(Set<PersistPair> objectsToDelete) {
138 // this.objectsToDelete = objectsToDelete;
144 // public Set<PersistPair> getObjectsToSave() {
145 // return objectsToSave;
147 // public void setObjectsToSave(Set<PersistPair> objectsToSave) {
148 // this.objectsToSave = objectsToSave;
152 //****************** CONVENIENCE *********************************************/
155 * Sets the status to {@link DeleteStatus#ERROR} if not yet set to a more serious
158 public void setError(){
159 setMaxStatus(Status
.ERROR
);
163 * Sets the status to {@link DeleteStatus#ABORT} if not yet set to a more serious
166 public void setAbort(){
167 setMaxStatus(Status
.ABORT
);
171 * Sets status to most severe status. If maxStatus is more severe then existing status
172 * existing status is set to maxStatus. Otherwise nothing changes.
173 * If minStatus is more severe then given status minStatus will be the new status.
176 public void setMaxStatus(Status maxStatus
) {
177 if (this.status
.compareSeverity(maxStatus
) < 0){
178 this.status
= maxStatus
;
182 public void includeResult(UpdateResult includedResult
){
184 this.setMaxStatus(includedResult
.getStatus());
185 this.addExceptions(includedResult
.getExceptions());
186 this.addUpdatedObjects(includedResult
.getUpdatedObjects());
187 this.addUpdatedCdmIds(includedResult
.getUpdatedCdmIds());
188 //also add cdm entity of included result to updated objects
189 if(includedResult
.getCdmEntity()!=null){
190 this.getUpdatedObjects().add(includedResult
.getCdmEntity());
194 public boolean isOk(){
195 return this.status
== Status
.OK
;
198 public boolean isAbort(){
199 return this.status
== Status
.ABORT
;
202 public boolean isError(){
203 return this.status
== Status
.ERROR
;
209 public String
toString(){
210 String separator
= ", ";
211 String exceptionString
= "";
212 for (Exception exception
: exceptions
) {
213 exceptionString
+= exception
.getLocalizedMessage()+separator
;
215 if(exceptionString
.endsWith(separator
)){
216 exceptionString
= exceptionString
.substring(0, exceptionString
.length()-separator
.length());
218 String relatedObjectString
= "";
219 for (CdmBase upatedObject
: updatedObjects
) {
220 if(upatedObject
instanceof IIdentifiableEntity
){
221 relatedObjectString
+= ((IIdentifiableEntity
) upatedObject
).getTitleCache()+separator
;
224 relatedObjectString
+= upatedObject
.toString()+separator
;
227 if(relatedObjectString
.endsWith(separator
)){
228 relatedObjectString
= relatedObjectString
.substring(0, relatedObjectString
.length()-separator
.length());
230 return "[UpdateResult]\n" +
231 "Status: " + status
.toString()+"\n" +
232 "Exceptions: " + exceptionString
+"\n" +
233 "Related Objects: "+relatedObjectString
;
235 public void setCdmEntity(CdmBase cdmBase
) {
236 this.cdmEntity
= cdmBase
;
241 public CdmBase
getCdmEntity(){
245 public Set
<CdmBase
> getUnchangedObjects() {
246 return unchangedObjects
;
249 public void addUnchangedObjects(Set
<?
extends CdmBase
> unchangedObjects
) {
250 this.unchangedObjects
.addAll(unchangedObjects
);
252 public void addUnChangedObject(CdmBase unchangedObject
) {
253 this.unchangedObjects
.add(unchangedObject
);