1 | package eu.clarin.cmdi.virtualcollectionregistry.model; |
---|
2 | |
---|
3 | import eu.clarin.cmdi.virtualcollectionregistry.pid.PersistentIdentifier; |
---|
4 | import java.io.Serializable; |
---|
5 | import java.util.ArrayList; |
---|
6 | import java.util.Date; |
---|
7 | import java.util.HashSet; |
---|
8 | import java.util.List; |
---|
9 | import java.util.Set; |
---|
10 | |
---|
11 | import javax.persistence.Basic; |
---|
12 | import javax.persistence.CascadeType; |
---|
13 | import javax.persistence.CollectionTable; |
---|
14 | import javax.persistence.Column; |
---|
15 | import javax.persistence.ElementCollection; |
---|
16 | import javax.persistence.Embedded; |
---|
17 | import javax.persistence.Entity; |
---|
18 | import javax.persistence.EnumType; |
---|
19 | import javax.persistence.Enumerated; |
---|
20 | import javax.persistence.FetchType; |
---|
21 | import javax.persistence.GeneratedValue; |
---|
22 | import javax.persistence.GenerationType; |
---|
23 | import javax.persistence.Id; |
---|
24 | import javax.persistence.JoinColumn; |
---|
25 | import javax.persistence.Lob; |
---|
26 | import javax.persistence.ManyToOne; |
---|
27 | import javax.persistence.NamedQueries; |
---|
28 | import javax.persistence.NamedQuery; |
---|
29 | import javax.persistence.OneToMany; |
---|
30 | import javax.persistence.OneToOne; |
---|
31 | import javax.persistence.OrderBy; |
---|
32 | import javax.persistence.OrderColumn; |
---|
33 | import javax.persistence.Table; |
---|
34 | import javax.persistence.Temporal; |
---|
35 | import javax.persistence.TemporalType; |
---|
36 | import javax.persistence.Version; |
---|
37 | |
---|
38 | import org.apache.commons.lang.builder.EqualsBuilder; |
---|
39 | import org.apache.commons.lang.builder.HashCodeBuilder; |
---|
40 | |
---|
41 | @Entity |
---|
42 | @Table(name = "virtualcollection") |
---|
43 | @NamedQueries({ |
---|
44 | @NamedQuery(name = "VirtualCollection.findAllPublic", |
---|
45 | query = "SELECT c FROM VirtualCollection c " + |
---|
46 | "WHERE c.state = eu.clarin.cmdi." + |
---|
47 | "virtualcollectionregistry.model." + |
---|
48 | "VirtualCollection$State.PUBLIC " + |
---|
49 | "ORDER BY c.id"), |
---|
50 | @NamedQuery(name = "VirtualCollection.countAllPublic", |
---|
51 | query = "SELECT COUNT(c) FROM VirtualCollection c " + |
---|
52 | "WHERE c.state = eu.clarin.cmdi." + |
---|
53 | "virtualcollectionregistry.model." + |
---|
54 | "VirtualCollection$State.PUBLIC"), |
---|
55 | @NamedQuery(name = "VirtualCollection.findByOwner", |
---|
56 | query = "SELECT c FROM VirtualCollection c " + |
---|
57 | "WHERE c.owner = :owner ORDER BY c.id"), |
---|
58 | @NamedQuery(name = "VirtualCollection.countByOwner", |
---|
59 | query = "SELECT COUNT(c) FROM VirtualCollection c " + |
---|
60 | "WHERE c.owner = :owner"), |
---|
61 | @NamedQuery(name = "VirtualCollection.findAllByState", |
---|
62 | query = "SELECT c FROM VirtualCollection c " + |
---|
63 | "WHERE c.state = :state AND c.dateModified < :date") |
---|
64 | }) |
---|
65 | public class VirtualCollection implements Serializable, IdentifiedEntity { |
---|
66 | private static final long serialVersionUID = 1L; |
---|
67 | |
---|
68 | public static enum State { |
---|
69 | PRIVATE, |
---|
70 | PUBLIC_PENDING, |
---|
71 | PUBLIC, |
---|
72 | DELETED, |
---|
73 | DEAD |
---|
74 | } // enum VirtualCollection.State |
---|
75 | |
---|
76 | public static enum Type { |
---|
77 | EXTENSIONAL, |
---|
78 | INTENSIONAL |
---|
79 | } // enum VirtualCollecion.Type |
---|
80 | |
---|
81 | public static enum Purpose { |
---|
82 | RESEARCH, |
---|
83 | REFERENCE, |
---|
84 | SAMPLE, |
---|
85 | FUTURE_USE |
---|
86 | } // enum VirtualCollecion.Purpose |
---|
87 | |
---|
88 | public static enum Reproducibility { |
---|
89 | INTENDED, |
---|
90 | FLUCTUATING, |
---|
91 | UNTENDED |
---|
92 | } // enum VirtualCollecion.Reproducibility |
---|
93 | |
---|
94 | @Id |
---|
95 | @GeneratedValue(strategy = GenerationType.AUTO) |
---|
96 | @Column(name = "id", nullable = false, updatable = false) |
---|
97 | private Long id; |
---|
98 | |
---|
99 | @ManyToOne(cascade = { CascadeType.PERSIST, |
---|
100 | CascadeType.REFRESH, |
---|
101 | CascadeType.MERGE, |
---|
102 | CascadeType.DETACH |
---|
103 | }, |
---|
104 | fetch = FetchType.EAGER) |
---|
105 | @JoinColumn(name = "owner_id", |
---|
106 | nullable = false) |
---|
107 | private User owner; |
---|
108 | |
---|
109 | @OneToOne(cascade = CascadeType.ALL, |
---|
110 | fetch = FetchType.EAGER, |
---|
111 | mappedBy = "vc", |
---|
112 | optional = true) |
---|
113 | private PersistentIdentifier persistentId = null; |
---|
114 | |
---|
115 | @Column(name = "state", nullable = false) |
---|
116 | private VirtualCollection.State state; |
---|
117 | |
---|
118 | @Enumerated(EnumType.ORDINAL) |
---|
119 | @Column(name = "type", nullable = false) |
---|
120 | private VirtualCollection.Type type; |
---|
121 | |
---|
122 | @Column(name = "name", nullable = false, length = 255) |
---|
123 | private String name; |
---|
124 | |
---|
125 | @Lob |
---|
126 | @Basic(fetch = FetchType.EAGER) |
---|
127 | @Column(name = "description", length = 8192) |
---|
128 | private String description; |
---|
129 | |
---|
130 | @Temporal(TemporalType.DATE) |
---|
131 | @Column(name = "creation_date") |
---|
132 | private Date creationDate; |
---|
133 | |
---|
134 | @OneToMany(cascade = CascadeType.ALL, |
---|
135 | fetch = FetchType.LAZY, |
---|
136 | orphanRemoval = true) |
---|
137 | @JoinColumn(name = "vc_id", nullable = false) |
---|
138 | @OrderBy("id") |
---|
139 | private List<Creator> creators; |
---|
140 | |
---|
141 | @Enumerated(EnumType.ORDINAL) |
---|
142 | @Column(name = "purpose") |
---|
143 | private VirtualCollection.Purpose purpose; |
---|
144 | |
---|
145 | @Enumerated(EnumType.ORDINAL) |
---|
146 | @Column(name = "reproducibility") |
---|
147 | private VirtualCollection.Reproducibility reproducibility; |
---|
148 | |
---|
149 | @Lob |
---|
150 | @Basic(fetch = FetchType.EAGER) |
---|
151 | @Column(name = "reproducibility_notice", length = 8192) |
---|
152 | private String reproducibilityNotice; |
---|
153 | |
---|
154 | @ElementCollection |
---|
155 | @CollectionTable(name = "keyword", |
---|
156 | joinColumns = @JoinColumn(name="vc_id")) |
---|
157 | private List<String> keywords; |
---|
158 | |
---|
159 | @OneToMany(cascade = CascadeType.ALL, |
---|
160 | fetch = FetchType.LAZY, |
---|
161 | orphanRemoval = true) |
---|
162 | @JoinColumn(name = "vc_id", nullable = false) |
---|
163 | @OrderColumn(nullable = false) |
---|
164 | private List<Resource> resources; |
---|
165 | |
---|
166 | @Embedded |
---|
167 | private GeneratedBy generatedBy; |
---|
168 | |
---|
169 | @Temporal(TemporalType.TIMESTAMP) |
---|
170 | @Column(name = "created", nullable = false, updatable = false) |
---|
171 | private Date dateCreated = new Date(); |
---|
172 | |
---|
173 | @Temporal(TemporalType.TIMESTAMP) |
---|
174 | @Version |
---|
175 | @Column(name = "modified", nullable = false) |
---|
176 | private Date dateModified; |
---|
177 | |
---|
178 | public VirtualCollection() { |
---|
179 | super(); |
---|
180 | this.setState(VirtualCollection.State.PRIVATE); |
---|
181 | } |
---|
182 | |
---|
183 | public Long getId() { |
---|
184 | return id; |
---|
185 | } |
---|
186 | |
---|
187 | public void setId(Long id) { |
---|
188 | this.id = id; |
---|
189 | } |
---|
190 | |
---|
191 | public User getOwner() { |
---|
192 | return owner; |
---|
193 | } |
---|
194 | |
---|
195 | public void setOwner(User owner) { |
---|
196 | if (owner == null) { |
---|
197 | throw new NullPointerException("owner == null"); |
---|
198 | } |
---|
199 | if (this.owner != null) { |
---|
200 | this.owner.getVirtualCollections().remove(this); |
---|
201 | } |
---|
202 | this.owner = owner; |
---|
203 | this.owner.getVirtualCollections().add(this); |
---|
204 | } |
---|
205 | |
---|
206 | public PersistentIdentifier getPersistentIdentifier() { |
---|
207 | return persistentId; |
---|
208 | } |
---|
209 | |
---|
210 | public void setPersistentIdentifier(PersistentIdentifier persistentId) { |
---|
211 | if (persistentId == null) { |
---|
212 | throw new NullPointerException("pid == null"); |
---|
213 | } |
---|
214 | if ((this.persistentId != null) || (state != State.PUBLIC_PENDING)) { |
---|
215 | throw new IllegalStateException("illegal state"); |
---|
216 | } |
---|
217 | this.persistentId = persistentId; |
---|
218 | this.state = State.PUBLIC; |
---|
219 | } |
---|
220 | |
---|
221 | public State getState() { |
---|
222 | return state; |
---|
223 | } |
---|
224 | |
---|
225 | public void setState(State state) { |
---|
226 | if (state == null) { |
---|
227 | throw new NullPointerException("state == null"); |
---|
228 | } |
---|
229 | this.state = state; |
---|
230 | } |
---|
231 | |
---|
232 | public boolean isPrivate() { |
---|
233 | return state == State.PRIVATE; |
---|
234 | } |
---|
235 | |
---|
236 | public boolean isPublic() { |
---|
237 | return (state == State.PUBLIC_PENDING) || (state == State.PUBLIC); |
---|
238 | } |
---|
239 | |
---|
240 | public boolean isDeleted() { |
---|
241 | return (state == State.DELETED) || (state == State.DEAD); |
---|
242 | } |
---|
243 | |
---|
244 | public VirtualCollection.Type getType() { |
---|
245 | return type; |
---|
246 | } |
---|
247 | |
---|
248 | public void setType(VirtualCollection.Type type) { |
---|
249 | this.type = type; |
---|
250 | } |
---|
251 | |
---|
252 | public String getName() { |
---|
253 | return name; |
---|
254 | } |
---|
255 | |
---|
256 | public void setName(String name) { |
---|
257 | if (name == null) { |
---|
258 | throw new NullPointerException("name == null"); |
---|
259 | } |
---|
260 | name = name.trim(); |
---|
261 | if (name.isEmpty()) { |
---|
262 | throw new IllegalArgumentException("name is empty"); |
---|
263 | } |
---|
264 | this.name = name; |
---|
265 | } |
---|
266 | |
---|
267 | public String getDescription() { |
---|
268 | return description; |
---|
269 | } |
---|
270 | |
---|
271 | public void setDescription(String description) { |
---|
272 | if (description != null) { |
---|
273 | description = description.trim(); |
---|
274 | if (description.isEmpty()) { |
---|
275 | description = null; |
---|
276 | } |
---|
277 | } |
---|
278 | this.description = description; |
---|
279 | } |
---|
280 | |
---|
281 | public Date getCreationDate() { |
---|
282 | return creationDate; |
---|
283 | } |
---|
284 | |
---|
285 | public void setCreationDate(Date creationDate) { |
---|
286 | this.creationDate = creationDate; |
---|
287 | } |
---|
288 | |
---|
289 | public List<Creator> getCreators() { |
---|
290 | if (creators == null) { |
---|
291 | this.creators = new ArrayList<Creator>(); |
---|
292 | } |
---|
293 | return creators; |
---|
294 | } |
---|
295 | |
---|
296 | public VirtualCollection.Purpose getPurpose() { |
---|
297 | return purpose; |
---|
298 | } |
---|
299 | |
---|
300 | public void setPurpose(VirtualCollection.Purpose purpose) { |
---|
301 | this.purpose = purpose; |
---|
302 | } |
---|
303 | |
---|
304 | public VirtualCollection.Reproducibility getReproducibility() { |
---|
305 | return reproducibility; |
---|
306 | } |
---|
307 | |
---|
308 | public void setReproducibility( |
---|
309 | VirtualCollection.Reproducibility reproducibility) { |
---|
310 | this.reproducibility = reproducibility; |
---|
311 | } |
---|
312 | |
---|
313 | public String getReproducibilityNotice() { |
---|
314 | return reproducibilityNotice; |
---|
315 | } |
---|
316 | |
---|
317 | public void setReproducibilityNotice(String reproducibilityNotice) { |
---|
318 | if (reproducibilityNotice != null) { |
---|
319 | reproducibilityNotice = reproducibilityNotice.trim(); |
---|
320 | if (reproducibilityNotice.isEmpty()) { |
---|
321 | reproducibilityNotice = null; |
---|
322 | } |
---|
323 | } |
---|
324 | this.reproducibilityNotice = reproducibilityNotice; |
---|
325 | } |
---|
326 | |
---|
327 | public List<String> getKeywords() { |
---|
328 | if (keywords == null) { |
---|
329 | keywords = new ArrayList<String>(); |
---|
330 | } |
---|
331 | return keywords; |
---|
332 | } |
---|
333 | |
---|
334 | public List<Resource> getResources() { |
---|
335 | if (resources == null) { |
---|
336 | resources = new ArrayList<Resource>(); |
---|
337 | } |
---|
338 | return resources; |
---|
339 | } |
---|
340 | |
---|
341 | public GeneratedBy getGeneratedBy() { |
---|
342 | return generatedBy; |
---|
343 | } |
---|
344 | |
---|
345 | public void setGeneratedBy(GeneratedBy generatedBy) { |
---|
346 | this.generatedBy = generatedBy; |
---|
347 | } |
---|
348 | |
---|
349 | public Date getDateCreated() { |
---|
350 | return dateCreated; |
---|
351 | } |
---|
352 | |
---|
353 | public Date getDateModified() { |
---|
354 | return dateModified; |
---|
355 | } |
---|
356 | |
---|
357 | public void setDateModified(Date dateModified) { |
---|
358 | if (dateModified == null) { |
---|
359 | throw new NullPointerException("dateModified == null"); |
---|
360 | } |
---|
361 | this.dateModified = dateModified; |
---|
362 | } |
---|
363 | |
---|
364 | public void updateFrom(VirtualCollection vc) { |
---|
365 | if (this == vc) { |
---|
366 | return; |
---|
367 | } |
---|
368 | if (vc.getPersistentIdentifier() != null) { |
---|
369 | this.setPersistentIdentifier(vc.getPersistentIdentifier()); |
---|
370 | } |
---|
371 | this.setState(state); |
---|
372 | this.setType(vc.getType()); |
---|
373 | this.setName(vc.getName()); |
---|
374 | this.setDescription(vc.getDescription()); |
---|
375 | this.setCreationDate(vc.getCreationDate()); |
---|
376 | |
---|
377 | // Creators |
---|
378 | Set<Creator> obsolete_creators = |
---|
379 | new HashSet<Creator>(this.getCreators()); |
---|
380 | for (Creator creator : vc.getCreators()) { |
---|
381 | if (!obsolete_creators.contains(creator)) { |
---|
382 | this.getCreators().add(creator); |
---|
383 | } |
---|
384 | obsolete_creators.remove(creator); |
---|
385 | } |
---|
386 | if (!obsolete_creators.isEmpty()) { |
---|
387 | for (Creator creator : obsolete_creators) { |
---|
388 | this.getCreators().remove(creator); |
---|
389 | } |
---|
390 | obsolete_creators = null; |
---|
391 | } |
---|
392 | |
---|
393 | this.setPurpose(vc.getPurpose()); |
---|
394 | this.setReproducibility(vc.getReproducibility()); |
---|
395 | this.setReproducibilityNotice(vc.getReproducibilityNotice()); |
---|
396 | |
---|
397 | // Keywords |
---|
398 | Set<String> obsolete_keywords = |
---|
399 | new HashSet<String>(this.getKeywords()); |
---|
400 | for (String keyword : vc.getKeywords()) { |
---|
401 | if (!obsolete_keywords.contains(keyword)) { |
---|
402 | this.getKeywords().add(keyword); |
---|
403 | } |
---|
404 | obsolete_keywords.remove(keyword); |
---|
405 | } |
---|
406 | if (!obsolete_keywords.isEmpty()) { |
---|
407 | for (String keyword : obsolete_keywords) { |
---|
408 | this.getKeywords().remove(keyword); |
---|
409 | } |
---|
410 | obsolete_keywords = null; |
---|
411 | } |
---|
412 | |
---|
413 | // Resources |
---|
414 | Set<Resource> obsolete_resources = |
---|
415 | new HashSet<Resource>(this.getResources()); |
---|
416 | for (Resource resource : vc.getResources()) { |
---|
417 | if (!obsolete_resources.contains(resource)) { |
---|
418 | this.getResources().add(resource); |
---|
419 | } |
---|
420 | obsolete_resources.remove(resource); |
---|
421 | } |
---|
422 | if (!obsolete_resources.isEmpty()) { |
---|
423 | for (Resource resource : obsolete_resources) { |
---|
424 | this.getResources().remove(resource); |
---|
425 | } |
---|
426 | obsolete_resources = null; |
---|
427 | } |
---|
428 | |
---|
429 | if (vc.generatedBy != null) { |
---|
430 | final GeneratedBy genBy = vc.generatedBy; |
---|
431 | if (this.generatedBy == null) { |
---|
432 | this.generatedBy = new GeneratedBy(genBy.getDescription()); |
---|
433 | } else { |
---|
434 | this.generatedBy.setDescription(genBy.getDescription()); |
---|
435 | } |
---|
436 | this.generatedBy.setURI(genBy.getURI()); |
---|
437 | if (genBy.getQuery() != null) { |
---|
438 | final GeneratedByQuery q = genBy.getQuery(); |
---|
439 | GeneratedByQuery query = |
---|
440 | new GeneratedByQuery(q.getProfile(), q.getValue()); |
---|
441 | this.generatedBy.setQuery(query); |
---|
442 | } |
---|
443 | } |
---|
444 | } |
---|
445 | |
---|
446 | @Override |
---|
447 | public boolean equals(Object obj) { |
---|
448 | if (obj == null) { |
---|
449 | return false; |
---|
450 | } |
---|
451 | if (obj == this) { |
---|
452 | return true; |
---|
453 | } |
---|
454 | if (obj instanceof VirtualCollection) { |
---|
455 | final VirtualCollection rhs = (VirtualCollection) obj; |
---|
456 | return new EqualsBuilder() |
---|
457 | .append(this.getOwner(), rhs.getOwner()) |
---|
458 | .append(this.getPersistentIdentifier(), |
---|
459 | rhs.getPersistentIdentifier()) |
---|
460 | .append(this.getState(), rhs.getState()) |
---|
461 | .append(this.getType(), rhs.getType()) |
---|
462 | .append(this.getName(), rhs.getName()) |
---|
463 | .append(this.getDescription(), rhs.getDescription()) |
---|
464 | .append(this.getCreationDate(), rhs.getCreationDate()) |
---|
465 | .append(this.getCreators(), rhs.getCreators()) |
---|
466 | .append(this.getPurpose(), rhs.getPurpose()) |
---|
467 | .append(this.getReproducibility(), rhs.getReproducibility()) |
---|
468 | .append(this.getReproducibilityNotice(), |
---|
469 | rhs.getReproducibilityNotice()) |
---|
470 | .append(this.getKeywords(), rhs.getKeywords()) |
---|
471 | .append(this.getResources(), rhs.getResources()) |
---|
472 | .append(this.getGeneratedBy(), rhs.getGeneratedBy()) |
---|
473 | .isEquals(); |
---|
474 | } |
---|
475 | return false; |
---|
476 | } |
---|
477 | |
---|
478 | @Override |
---|
479 | public int hashCode() { |
---|
480 | return new HashCodeBuilder(1391, 295) |
---|
481 | .append(this.getOwner()) |
---|
482 | .append(this.getPersistentIdentifier()) |
---|
483 | .append(this.getState()) |
---|
484 | .append(this.getType()) |
---|
485 | .append(this.getName()) |
---|
486 | .append(this.getDescription()) |
---|
487 | .append(this.getCreationDate()) |
---|
488 | .append(this.getCreators()) |
---|
489 | .append(this.getPurpose()) |
---|
490 | .append(this.getReproducibility()) |
---|
491 | .append(this.getReproducibilityNotice()) |
---|
492 | .append(this.getKeywords()) |
---|
493 | .append(this.getResources()) |
---|
494 | .append(this.getGeneratedBy()) |
---|
495 | .toHashCode(); |
---|
496 | } |
---|
497 | |
---|
498 | } // class VirtualCollection |
---|