1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
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.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.model.validation;
|
10
|
|
11
|
import java.util.HashSet;
|
12
|
import java.util.Set;
|
13
|
import java.util.UUID;
|
14
|
|
15
|
import javax.persistence.Column;
|
16
|
import javax.persistence.Entity;
|
17
|
import javax.persistence.EnumType;
|
18
|
import javax.persistence.Enumerated;
|
19
|
import javax.persistence.OneToMany;
|
20
|
import javax.validation.Validator;
|
21
|
import javax.xml.bind.annotation.XmlAccessType;
|
22
|
import javax.xml.bind.annotation.XmlAccessorType;
|
23
|
import javax.xml.bind.annotation.XmlElement;
|
24
|
import javax.xml.bind.annotation.XmlElementWrapper;
|
25
|
import javax.xml.bind.annotation.XmlRootElement;
|
26
|
import javax.xml.bind.annotation.XmlType;
|
27
|
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
|
28
|
|
29
|
import org.apache.log4j.Logger;
|
30
|
import org.hibernate.annotations.Cascade;
|
31
|
import org.hibernate.annotations.CascadeType;
|
32
|
import org.hibernate.annotations.Fetch;
|
33
|
import org.hibernate.annotations.FetchMode;
|
34
|
import org.hibernate.annotations.Type;
|
35
|
import org.hibernate.search.annotations.Analyze;
|
36
|
import org.hibernate.search.annotations.Field;
|
37
|
import org.hibernate.search.annotations.FieldBridge;
|
38
|
import org.joda.time.DateTime;
|
39
|
|
40
|
import eu.etaxonomy.cdm.hibernate.search.DateTimeBridge;
|
41
|
import eu.etaxonomy.cdm.hibernate.search.UuidBridge;
|
42
|
import eu.etaxonomy.cdm.jaxb.DateTimeAdapter;
|
43
|
import eu.etaxonomy.cdm.jaxb.UUIDAdapter;
|
44
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
45
|
import eu.etaxonomy.cdm.model.common.ICdmBase;
|
46
|
import eu.etaxonomy.cdm.model.common.ISelfDescriptive;
|
47
|
|
48
|
/**
|
49
|
* An {@code EntityValidation} models the result of validating one entity, that
|
50
|
* is, the outcome of calling {@link Validator#validate(Object, Class...)}. More
|
51
|
* than one constraint {@link EntityConstraintViolation} may be violated while
|
52
|
* validating the entity.
|
53
|
*
|
54
|
* @see EntityValidation
|
55
|
*
|
56
|
* @author ayco_holleman
|
57
|
*
|
58
|
*/
|
59
|
|
60
|
@XmlAccessorType(XmlAccessType.FIELD)
|
61
|
// @formatter:off
|
62
|
@XmlType(name = "EntityValidation", propOrder = { "ValidatedEntityId", "ValidatedEntityUuid", "ValidatedEntityClass",
|
63
|
"ValidationCount", "Updated", "UserFriendlyDescription", "UserFriendlyTypeName", "CrudEventType",
|
64
|
"ConstraintViolations" })
|
65
|
// @formatter:on
|
66
|
@XmlRootElement(name = "EntityValidation")
|
67
|
@Entity
|
68
|
public class EntityValidation extends CdmBase {
|
69
|
|
70
|
private static final long serialVersionUID = 9120571815593117363L;
|
71
|
|
72
|
@SuppressWarnings("unused")
|
73
|
private static final Logger logger = Logger.getLogger(EntityValidation.class);
|
74
|
|
75
|
public static EntityValidation newInstance() {
|
76
|
return new EntityValidation();
|
77
|
}
|
78
|
|
79
|
/**
|
80
|
* Create a new {@code EntityValidation} instance.
|
81
|
* @param validatedEntity
|
82
|
* @param crudEventType
|
83
|
* @return
|
84
|
*/
|
85
|
public static EntityValidation newInstance(ICdmBase validatedEntity, CRUDEventType crudEventType) {
|
86
|
EntityValidation result = newInstance();
|
87
|
result.setValidatedEntity(validatedEntity);
|
88
|
result.setCrudEventType(crudEventType);
|
89
|
result.setValidationCount(1);
|
90
|
result.setStatus(EntityValidationStatus.IN_PROGRESS);
|
91
|
result.setUpdated(result.getCreated());
|
92
|
return result;
|
93
|
}
|
94
|
|
95
|
@XmlElement(name = "ValidatedEntityId")
|
96
|
private int validatedEntityId;
|
97
|
|
98
|
@XmlElement(name = "ValidatedEntityUuid")
|
99
|
@XmlJavaTypeAdapter(UUIDAdapter.class)
|
100
|
@Type(type = "uuidUserType")
|
101
|
@Column(length = 36)
|
102
|
// TODO needed? Type UUID will always assure that is exactly 36
|
103
|
@FieldBridge(impl = UuidBridge.class)
|
104
|
// TODO required?
|
105
|
private UUID validatedEntityUuid;
|
106
|
|
107
|
@XmlElement(name = "ValidatedEntityClass")
|
108
|
private String validatedEntityClass;
|
109
|
|
110
|
@XmlElement(name = "ValidationCount")
|
111
|
private int validationCount;
|
112
|
|
113
|
@XmlElement(name = "Updated", type = String.class)
|
114
|
@XmlJavaTypeAdapter(DateTimeAdapter.class)
|
115
|
@Type(type = "dateTimeUserType")
|
116
|
@Field(analyze = Analyze.NO)
|
117
|
@FieldBridge(impl = DateTimeBridge.class)
|
118
|
private DateTime updated;
|
119
|
|
120
|
@XmlElement(name = "UserFriendlyDescription")
|
121
|
private String userFriendlyDescription;
|
122
|
|
123
|
@XmlElement(name = "UserFriendlyTypeName")
|
124
|
private String userFriendlyTypeName;
|
125
|
|
126
|
@XmlElement(name = "CrudEventType")
|
127
|
@Enumerated(EnumType.STRING)
|
128
|
private CRUDEventType crudEventType;
|
129
|
|
130
|
@XmlElement(name = "Status")
|
131
|
@Enumerated(EnumType.STRING)
|
132
|
private EntityValidationStatus status;
|
133
|
|
134
|
@XmlElementWrapper(name = "EntityConstraintViolations")
|
135
|
@OneToMany(mappedBy = "entityValidation", orphanRemoval = true)
|
136
|
@Cascade({ CascadeType.ALL })
|
137
|
@Fetch(value = FetchMode.JOIN)
|
138
|
private Set<EntityConstraintViolation> entityConstraintViolations = new HashSet<EntityConstraintViolation>();
|
139
|
|
140
|
protected EntityValidation() {
|
141
|
super();
|
142
|
}
|
143
|
|
144
|
public void setValidatedEntity(ICdmBase entity) {
|
145
|
setValidatedEntityClass(entity.getClass().getName());
|
146
|
setValidatedEntityId(entity.getId());
|
147
|
setValidatedEntityUuid(entity.getUuid());
|
148
|
if (entity instanceof ISelfDescriptive) {
|
149
|
ISelfDescriptive isd = (ISelfDescriptive) entity;
|
150
|
setUserFriendlyTypeName(isd.getUserFriendlyTypeName());
|
151
|
setUserFriendlyDescription(isd.getUserFriendlyDescription());
|
152
|
} else {
|
153
|
setUserFriendlyTypeName(entity.getClass().getSimpleName());
|
154
|
setUserFriendlyDescription(entity.toString());
|
155
|
}
|
156
|
}
|
157
|
|
158
|
public int getValidatedEntityId() {
|
159
|
return validatedEntityId;
|
160
|
}
|
161
|
|
162
|
public void setValidatedEntityId(int validatedEntityId) {
|
163
|
this.validatedEntityId = validatedEntityId;
|
164
|
}
|
165
|
|
166
|
public UUID getValidatedEntityUuid() {
|
167
|
return validatedEntityUuid;
|
168
|
}
|
169
|
|
170
|
public void setValidatedEntityUuid(UUID validatedEntityUuid) {
|
171
|
this.validatedEntityUuid = validatedEntityUuid;
|
172
|
}
|
173
|
|
174
|
public String getValidatedEntityClass() {
|
175
|
return validatedEntityClass;
|
176
|
}
|
177
|
|
178
|
public void setValidatedEntityClass(String validatedEntityClass) {
|
179
|
this.validatedEntityClass = validatedEntityClass;
|
180
|
}
|
181
|
|
182
|
public int getValidationCount() {
|
183
|
return validationCount;
|
184
|
}
|
185
|
|
186
|
public void setValidationCount(int validationCount) {
|
187
|
this.validationCount = validationCount;
|
188
|
}
|
189
|
|
190
|
public DateTime getUpdated() {
|
191
|
return updated;
|
192
|
}
|
193
|
|
194
|
public void setUpdated(DateTime updated) {
|
195
|
this.updated = updated;
|
196
|
}
|
197
|
|
198
|
@Override
|
199
|
public String getUserFriendlyTypeName() {
|
200
|
return userFriendlyTypeName;
|
201
|
}
|
202
|
|
203
|
public void setUserFriendlyTypeName(String userFriendlyTypeName) {
|
204
|
this.userFriendlyTypeName = userFriendlyTypeName;
|
205
|
}
|
206
|
|
207
|
public CRUDEventType getCrudEventType() {
|
208
|
return crudEventType;
|
209
|
}
|
210
|
|
211
|
public void setCrudEventType(CRUDEventType crudEventType) {
|
212
|
this.crudEventType = crudEventType;
|
213
|
}
|
214
|
|
215
|
public EntityValidationStatus getStatus() {
|
216
|
return status;
|
217
|
}
|
218
|
|
219
|
public void setStatus(EntityValidationStatus status) {
|
220
|
this.status = status;
|
221
|
}
|
222
|
|
223
|
@Override
|
224
|
public String getUserFriendlyDescription() {
|
225
|
return userFriendlyDescription;
|
226
|
}
|
227
|
|
228
|
public void setUserFriendlyDescription(String userFriendlyDescription) {
|
229
|
this.userFriendlyDescription = userFriendlyDescription;
|
230
|
}
|
231
|
|
232
|
public Set<EntityConstraintViolation> getEntityConstraintViolations() {
|
233
|
if (entityConstraintViolations == null) {
|
234
|
entityConstraintViolations = new HashSet<EntityConstraintViolation>();
|
235
|
}
|
236
|
return entityConstraintViolations;
|
237
|
}
|
238
|
|
239
|
public void addEntityConstraintViolation(EntityConstraintViolation ecv) {
|
240
|
if (ecv != null) {
|
241
|
getEntityConstraintViolations().add(ecv);
|
242
|
}
|
243
|
}
|
244
|
|
245
|
public void removeEntityConstraintViolation(EntityConstraintViolation ecv) {
|
246
|
if (ecv != null) {
|
247
|
getEntityConstraintViolations().remove(ecv);
|
248
|
ecv.setEntityValidation(null);
|
249
|
}
|
250
|
}
|
251
|
|
252
|
public void setEntityConstraintViolations(Set<EntityConstraintViolation> errors) {
|
253
|
this.entityConstraintViolations = errors;
|
254
|
}
|
255
|
|
256
|
}
|