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
.HashMap
;
14 import java
.util
.HashSet
;
17 import java
.util
.UUID
;
19 import org
.apache
.commons
.collections
.buffer
.CircularFifoBuffer
;
20 import org
.apache
.log4j
.Logger
;
22 import eu
.etaxonomy
.cdm
.api
.service
.dto
.CdmEntityIdentifier
;
23 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
24 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
25 import eu
.etaxonomy
.cdm
.model
.common
.IIdentifiableEntity
;
28 * This class represents the result of an update action.
33 public class UpdateResult
implements Serializable
{
35 private static final long serialVersionUID
= -7040027587709706700L;
37 @SuppressWarnings("unused")
38 private static final Logger logger
= Logger
.getLogger(UpdateResult
.class);
40 private Status status
= Status
.OK
;
42 @SuppressWarnings("unchecked")
43 private final Collection
<Exception
> exceptions
= new CircularFifoBuffer(10);
45 private Set
<CdmBase
> updatedObjects
= new HashSet
<>();
47 private final Set
<CdmEntityIdentifier
> updatedCdmIds
= new HashSet
<>();
49 private final Set
<CdmBase
> unchangedObjects
= new HashSet
<>();
51 private CdmBase cdmEntity
;
53 private Map
<Class
<?
extends CdmBase
>, Set
<UUID
>> insertedUuids
= new HashMap
<>();
54 private Map
<Class
<?
extends CdmBase
>, Set
<UUID
>> updatedUuids
= new HashMap
<>();
62 protected Integer severity
;
63 private Status(int severity
){
64 this.severity
= severity
;
67 public int compareSeverity(Status other
){
68 return this.severity
.compareTo(other
.severity
);
72 //***************************** GETTER /SETTER /ADDER *************************/
75 * The resulting status of an update action.
80 public Status
getStatus() {
83 public void setStatus(Status status
) {
88 * The highest exception that occurred during delete (if any).
90 public Collection
<Exception
> getExceptions() {
93 public void addException(Exception exception
) {
94 this.exceptions
.add(exception
);
96 public void addExceptions(Collection
<Exception
> exceptions
) {
97 this.exceptions
.addAll(exceptions
);
100 //inserted object UUIDs
101 public Map
<Class
<?
extends CdmBase
>, Set
<UUID
>> getInsertedUuids(){
102 return this.insertedUuids
;
104 public void addInsertedUuid(CdmBase cdmBase
) {
105 Class
<?
extends CdmBase
> clazz
= CdmBase
.deproxy(cdmBase
).getClass();
106 initClassRecord(insertedUuids
, clazz
);
107 this.insertedUuids
.get(clazz
).add(cdmBase
.getUuid());
109 public Set
<UUID
> getInsertedUuids(Class
<?
extends CdmBase
> clazz
){
110 return byMapKey(this.insertedUuids
, clazz
);
113 //updated object UUIDs
114 public Map
<Class
<?
extends CdmBase
>, Set
<UUID
>> getUpdatedUuids(){
115 return this.updatedUuids
;
117 public void addUpdatedUuid(CdmBase cdmBase
) {
118 Class
<?
extends CdmBase
> clazz
= CdmBase
.deproxy(cdmBase
).getClass();
119 initClassRecord(updatedUuids
, clazz
);
120 this.updatedUuids
.get(clazz
).add(cdmBase
.getUuid());
122 public Set
<UUID
> getUpdatedUuids(Class
<?
extends CdmBase
> clazz
){
123 return byMapKey(this.updatedUuids
, clazz
);
126 public Set
<UUID
> getInsertedOrUpdatedUuids(Class
<?
extends CdmBase
> clazz
){
127 HashSet
<UUID
> result
= new HashSet
<UUID
>(getUpdatedUuids(clazz
));
128 result
.addAll(getInsertedUuids(clazz
));
132 private void initClassRecord(Map
<Class
<?
extends CdmBase
>, Set
<UUID
>> map
, Class
<?
extends CdmBase
> clazz
){
133 if (map
.get(clazz
) == null){
134 map
.put(clazz
, new HashSet
<>());
137 private Set
<UUID
> byMapKey(Map
<Class
<?
extends CdmBase
>, Set
<UUID
>> map
, Class
<?
extends CdmBase
> clazz
){
138 return map
.get(clazz
) == null ?
new HashSet
<>() : map
.get(clazz
);
142 public Set
<CdmEntityIdentifier
> getUpdatedCdmIds() {
143 return updatedCdmIds
;
145 public void addUpdatedCdmId(CdmEntityIdentifier cdmId
) {
146 this.updatedCdmIds
.add(cdmId
);
148 public void addUpdatedCdmIds(Set
<CdmEntityIdentifier
> updatedCdmIds
) {
149 this.updatedCdmIds
.addAll(updatedCdmIds
);
151 public void addUpdatedCdmId(CdmBase updatedObject
) {
152 this.updatedCdmIds
.add(CdmEntityIdentifier
.NewInstance(updatedObject
));
156 public Set
<CdmBase
> getUpdatedObjects() {
157 return updatedObjects
;
159 public void addUpdatedObject(CdmBase relatedObject
) {
160 this.updatedObjects
.add(relatedObject
);
162 public void addUpdatedObjects(Set
<?
extends CdmBase
> updatedObjects
) {
163 this.updatedObjects
.addAll(updatedObjects
);
168 public void setCdmEntity(CdmBase cdmBase
) {
169 this.cdmEntity
= cdmBase
;
171 public CdmBase
getCdmEntity(){
176 public Set
<CdmBase
> getUnchangedObjects() {
177 return unchangedObjects
;
179 public void addUnchangedObjects(Set
<?
extends CdmBase
> unchangedObjects
) {
180 this.unchangedObjects
.addAll(unchangedObjects
);
182 public void addUnChangedObject(CdmBase unchangedObject
) {
183 this.unchangedObjects
.add(unchangedObject
);
186 //****************** CONVENIENCE *********************************************/
189 * Sets the status to {@link Status#ERROR} if not yet set to a more serious
192 public void setError(){
193 setMaxStatus(Status
.ERROR
);
197 * Sets the status to {@link Status#ABORT} if not yet set to a more serious
200 public void setAbort(){
201 setMaxStatus(Status
.ABORT
);
205 * Sets status to most severe status. If maxStatus is more severe then existing status
206 * existing status is set to maxStatus. Otherwise nothing changes.
207 * If minStatus is more severe then given status minStatus will be the new status.
210 public void setMaxStatus(Status maxStatus
) {
211 if (this.status
.compareSeverity(maxStatus
) < 0){
212 this.status
= maxStatus
;
216 public void includeResult(UpdateResult includedResult
){
217 includeResult(includedResult
, false);
220 public void includeResult(UpdateResult includedResult
, boolean excludeStatusAndException
){
222 if (!excludeStatusAndException
){
223 this.setMaxStatus(includedResult
.getStatus());
224 this.addExceptions(includedResult
.getExceptions());
226 this.addUpdatedObjects(includedResult
.getUpdatedObjects());
227 this.addUpdatedCdmIds(includedResult
.getUpdatedCdmIds());
228 //also add cdm entity of included result to updated objects
229 if(includedResult
.getCdmEntity()!=null){
230 this.getUpdatedObjects().add(includedResult
.getCdmEntity());
234 public boolean isOk(){
235 return this.status
== Status
.OK
;
238 public boolean isAbort(){
239 return this.status
== Status
.ABORT
;
242 public boolean isError(){
243 return this.status
== Status
.ERROR
;
246 // *********************************** TO STRING ***************************************/
248 public String
toString(){
249 String separator
= ", ";
250 String exceptionString
= "";
251 for (Exception exception
: exceptions
) {
252 exceptionString
+= exception
.getLocalizedMessage()+separator
;
254 if(exceptionString
.endsWith(separator
)){
255 exceptionString
= exceptionString
.substring(0, exceptionString
.length()-separator
.length());
257 String updatedObjectString
= toStringObjectsString(separator
, updatedObjects
);
258 String unchangedObjectString
= toStringObjectsString(separator
, unchangedObjects
);
259 return "[UpdateResult]\n" +
260 "Status: " + status
.toString()+"\n" +
261 "Exceptions: " + exceptionString
+"\n" +
262 "Updated objects: " + updatedObjectString
+"\n" +
263 "Updated objects IDs: " + toStringIdsString(separator
, updatedCdmIds
)+"\n" +
264 "Unchanged objects: " + unchangedObjectString
268 private String
toStringIdsString(String separator
, Set
<CdmEntityIdentifier
> cdmIds
) {
270 for (CdmEntityIdentifier id
: cdmIds
){
271 result
= CdmUtils
.concat(separator
, id
.toString());
276 * Serializes the CdmBase collection
278 protected static String
toStringObjectsString(String separator
, Set
<CdmBase
> cdmBases
) {
279 String cdmBasesString
= "";
280 for (CdmBase cdmBase
: cdmBases
) {
281 if(cdmBase
instanceof IIdentifiableEntity
){
282 cdmBasesString
+= ((IIdentifiableEntity
) cdmBase
).getTitleCache()+separator
;
285 cdmBasesString
+= cdmBase
.toString()+separator
;
288 if(cdmBasesString
.endsWith(separator
)){
289 cdmBasesString
= cdmBasesString
.substring(0, cdmBasesString
.length()-separator
.length());
291 return cdmBasesString
;