2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.cdm
.api
.service
;
11 import java
.io
.Serializable
;
12 import java
.util
.Collection
;
13 import java
.util
.HashSet
;
16 import org
.apache
.commons
.collections
.buffer
.CircularFifoBuffer
;
17 import org
.apache
.log4j
.Logger
;
19 import eu
.etaxonomy
.cdm
.api
.service
.dto
.CdmEntityIdentifier
;
20 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
21 import eu
.etaxonomy
.cdm
.model
.common
.IIdentifiableEntity
;
24 * This class represents the result of an update action.
29 public class UpdateResult
implements Serializable
{
31 private static final long serialVersionUID
= -7040027587709706700L;
33 @SuppressWarnings("unused")
34 private static final Logger logger
= Logger
.getLogger(UpdateResult
.class);
36 private Status status
= Status
.OK
;
38 @SuppressWarnings("unchecked")
39 private final Collection
<Exception
> exceptions
= new CircularFifoBuffer(10);
41 private Set
<CdmBase
> updatedObjects
= new HashSet
<>();
43 private final Set
<CdmEntityIdentifier
> updatedCdmIds
= new HashSet
<>();
45 private final Set
<CdmBase
> unchangedObjects
= new HashSet
<>();
47 private CdmBase cdmEntity
;
55 protected Integer severity
;
56 private Status(int severity
){
57 this.severity
= severity
;
60 public int compareSeverity(Status other
){
61 return this.severity
.compareTo(other
.severity
);
65 //***************************** GETTER /SETTER /ADDER *************************/
68 * The resulting status of an update action.
73 public Status
getStatus() {
76 public void setStatus(Status status
) {
81 * The highest exception that occurred during delete (if any).
83 public Collection
<Exception
> getExceptions() {
86 public void addException(Exception exception
) {
87 this.exceptions
.add(exception
);
89 public void addExceptions(Collection
<Exception
> exceptions
) {
90 this.exceptions
.addAll(exceptions
);
94 * Related objects that prevent the delete action to take place.
97 public Set
<CdmEntityIdentifier
> getUpdatedCdmIds() {
100 public void addUpdatedCdmId(CdmEntityIdentifier cdmId
) {
101 this.updatedCdmIds
.add(cdmId
);
103 public void addUpdatedCdmIds(Set
<CdmEntityIdentifier
> updatedCdmIds
) {
104 this.updatedCdmIds
.addAll(updatedCdmIds
);
108 public Set
<CdmBase
> getUpdatedObjects() {
109 return updatedObjects
;
111 public void addUpdatedObject(CdmBase relatedObject
) {
112 this.updatedObjects
.add(relatedObject
);
114 public void addUpdatedObjects(Set
<?
extends CdmBase
> updatedObjects
) {
115 this.updatedObjects
.addAll(updatedObjects
);
118 //****************** CONVENIENCE *********************************************/
121 * Sets the status to {@link Status#ERROR} if not yet set to a more serious
124 public void setError(){
125 setMaxStatus(Status
.ERROR
);
129 * Sets the status to {@link Status#ABORT} if not yet set to a more serious
132 public void setAbort(){
133 setMaxStatus(Status
.ABORT
);
137 * Sets status to most severe status. If maxStatus is more severe then existing status
138 * existing status is set to maxStatus. Otherwise nothing changes.
139 * If minStatus is more severe then given status minStatus will be the new status.
142 public void setMaxStatus(Status maxStatus
) {
143 if (this.status
.compareSeverity(maxStatus
) < 0){
144 this.status
= maxStatus
;
148 public void includeResult(UpdateResult includedResult
){
150 this.setMaxStatus(includedResult
.getStatus());
151 this.addExceptions(includedResult
.getExceptions());
152 this.addUpdatedObjects(includedResult
.getUpdatedObjects());
153 this.addUpdatedCdmIds(includedResult
.getUpdatedCdmIds());
154 //also add cdm entity of included result to updated objects
155 if(includedResult
.getCdmEntity()!=null){
156 this.getUpdatedObjects().add(includedResult
.getCdmEntity());
160 public boolean isOk(){
161 return this.status
== Status
.OK
;
164 public boolean isAbort(){
165 return this.status
== Status
.ABORT
;
168 public boolean isError(){
169 return this.status
== Status
.ERROR
;
173 public String
toString(){
174 String separator
= ", ";
175 String exceptionString
= "";
176 for (Exception exception
: exceptions
) {
177 exceptionString
+= exception
.getLocalizedMessage()+separator
;
179 if(exceptionString
.endsWith(separator
)){
180 exceptionString
= exceptionString
.substring(0, exceptionString
.length()-separator
.length());
182 String relatedObjectString
= "";
183 for (CdmBase upatedObject
: updatedObjects
) {
184 if(upatedObject
instanceof IIdentifiableEntity
){
185 relatedObjectString
+= ((IIdentifiableEntity
) upatedObject
).getTitleCache()+separator
;
188 relatedObjectString
+= upatedObject
.toString()+separator
;
191 if(relatedObjectString
.endsWith(separator
)){
192 relatedObjectString
= relatedObjectString
.substring(0, relatedObjectString
.length()-separator
.length());
194 return "[UpdateResult]\n" +
195 "Status: " + status
.toString()+"\n" +
196 "Exceptions: " + exceptionString
+"\n" +
197 "Related Objects: "+relatedObjectString
;
199 public void setCdmEntity(CdmBase cdmBase
) {
200 this.cdmEntity
= cdmBase
;
203 public CdmBase
getCdmEntity(){
207 public Set
<CdmBase
> getUnchangedObjects() {
208 return unchangedObjects
;
211 public void addUnchangedObjects(Set
<?
extends CdmBase
> unchangedObjects
) {
212 this.unchangedObjects
.addAll(unchangedObjects
);
214 public void addUnChangedObject(CdmBase unchangedObject
) {
215 this.unchangedObjects
.add(unchangedObject
);