source: VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/main/java/eu/clarin/cmdi/virtualcollectionregistry/model/VirtualCollection.java @ 5494

Last change on this file since 5494 was 5494, checked in by Twan Goosen, 10 years ago

Implemented basic moving of references within collection. Refs #597

  • Property svn:eol-style set to native
File size: 15.3 KB
RevLine 
[146]1package eu.clarin.cmdi.virtualcollectionregistry.model;
2
[5373]3import eu.clarin.cmdi.virtualcollectionregistry.pid.PersistentIdentifier;
[778]4import java.io.Serializable;
5import java.util.ArrayList;
[146]6import java.util.Date;
[780]7import java.util.HashSet;
[778]8import java.util.List;
[780]9import java.util.Set;
[146]10
[900]11import javax.persistence.Basic;
[146]12import javax.persistence.CascadeType;
[778]13import javax.persistence.CollectionTable;
[146]14import javax.persistence.Column;
[778]15import javax.persistence.ElementCollection;
[146]16import javax.persistence.Embedded;
17import javax.persistence.Entity;
18import javax.persistence.EnumType;
19import javax.persistence.Enumerated;
20import javax.persistence.FetchType;
21import javax.persistence.GeneratedValue;
22import javax.persistence.GenerationType;
23import javax.persistence.Id;
24import javax.persistence.JoinColumn;
[778]25import javax.persistence.Lob;
[146]26import javax.persistence.ManyToOne;
27import javax.persistence.NamedQueries;
28import javax.persistence.NamedQuery;
29import javax.persistence.OneToMany;
[234]30import javax.persistence.OneToOne;
[780]31import javax.persistence.OrderBy;
[5494]32import javax.persistence.OrderColumn;
[146]33import javax.persistence.Table;
34import javax.persistence.Temporal;
35import javax.persistence.TemporalType;
36import javax.persistence.Version;
37
[778]38import org.apache.commons.lang.builder.EqualsBuilder;
39import org.apache.commons.lang.builder.HashCodeBuilder;
[146]40
41@Entity
[778]42@Table(name = "virtualcollection")
[146]43@NamedQueries({
[507]44        @NamedQuery(name = "VirtualCollection.findAllPublic",
45                    query = "SELECT c FROM VirtualCollection c " +
46                            "WHERE c.state = eu.clarin.cmdi." +
47                            "virtualcollectionregistry.model." +
[509]48                            "VirtualCollection$State.PUBLIC " +
49                            "ORDER BY c.id"),
[507]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"),
[503]55        @NamedQuery(name = "VirtualCollection.findByOwner",
[507]56                    query = "SELECT c FROM VirtualCollection c " +
[509]57                            "WHERE c.owner = :owner ORDER BY c.id"),
[503]58        @NamedQuery(name = "VirtualCollection.countByOwner",
[507]59                    query = "SELECT COUNT(c) FROM VirtualCollection c " +
60                            "WHERE c.owner = :owner"),
[509]61        @NamedQuery(name = "VirtualCollection.findAllByState",
[507]62                    query = "SELECT c FROM VirtualCollection c " +
[790]63                            "WHERE c.state = :state AND c.dateModified < :date")
[146]64})
[5446]65public class VirtualCollection implements Serializable, IdentifiedEntity {
[778]66    private static final long serialVersionUID = 1L;
67
[507]68    public static enum State {
[509]69        PRIVATE,
70        PUBLIC_PENDING,
71        PUBLIC,
[507]72        DELETED,
[509]73        DEAD
[778]74    } // enum VirtualCollection.State
75
[503]76    public static enum Type {
[507]77        EXTENSIONAL,
[503]78        INTENSIONAL
[778]79    } // enum VirtualCollecion.Type
80
[556]81    public static enum Purpose {
82        RESEARCH,
83        REFERENCE,
84        SAMPLE,
85        FUTURE_USE
[778]86    } // enum VirtualCollecion.Purpose
87
[556]88    public static enum Reproducibility {
89        INTENDED,
90        FLUCTUATING,
91        UNTENDED
[778]92    } // enum VirtualCollecion.Reproducibility
93
[503]94    @Id
95    @GeneratedValue(strategy = GenerationType.AUTO)
[778]96    @Column(name = "id", nullable = false, updatable = false)
[780]97    private Long id;
[778]98
[503]99    @ManyToOne(cascade = { CascadeType.PERSIST,
100                           CascadeType.REFRESH,
[5436]101                           CascadeType.MERGE,
102                           CascadeType.DETACH
103    },
[146]104               fetch = FetchType.EAGER)
[503]105    @JoinColumn(name = "owner_id",
106                nullable = false)
107    private User owner;
[778]108
[510]109    @OneToOne(cascade = CascadeType.ALL,
[507]110              fetch = FetchType.EAGER,
[510]111              mappedBy = "vc",
112              optional = true)
[778]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
[900]122    @Column(name = "name", nullable = false, length = 255)
[503]123    private String name;
[778]124
125    @Lob
[900]126    @Basic(fetch = FetchType.EAGER)
127    @Column(name = "description", length = 8192)
[503]128    private String description;
[778]129
130    @Temporal(TemporalType.DATE)
[503]131    @Column(name = "creation_date")
132    private Date creationDate;
[778]133
134    @OneToMany(cascade = CascadeType.ALL,
135               fetch = FetchType.LAZY,
136               orphanRemoval = true)
137    @JoinColumn(name = "vc_id", nullable = false)
[780]138    @OrderBy("id")
[785]139    private List<Creator> creators;
[778]140
[503]141    @Enumerated(EnumType.ORDINAL)
[556]142    @Column(name = "purpose")
[778]143    private VirtualCollection.Purpose purpose;
144
[556]145    @Enumerated(EnumType.ORDINAL)
146    @Column(name = "reproducibility")
[778]147    private VirtualCollection.Reproducibility reproducibility;
[3940]148
[900]149    @Lob
150    @Basic(fetch = FetchType.EAGER)
151    @Column(name = "reproducibility_notice", length = 8192)
[556]152    private String reproducibilityNotice;
[778]153
154    @ElementCollection
155    @CollectionTable(name = "keyword",
156                     joinColumns = @JoinColumn(name="vc_id"))
[785]157    private List<String> keywords;
[778]158
[503]159    @OneToMany(cascade = CascadeType.ALL,
[556]160               fetch = FetchType.LAZY,
161               orphanRemoval = true)
[5494]162    @JoinColumn(name = "vc_id", nullable = false)   
163    @OrderColumn(nullable = false)
[785]164    private List<Resource> resources;
[778]165
[556]166    @Embedded
[778]167    private GeneratedBy generatedBy;
168
[503]169    @Temporal(TemporalType.TIMESTAMP)
[778]170    @Column(name = "created", nullable = false, updatable = false)
[790]171    private Date dateCreated = new Date();
[778]172
[503]173    @Temporal(TemporalType.TIMESTAMP)
174    @Version
[778]175    @Column(name = "modified", nullable = false)
[790]176    private Date dateModified;
[146]177
[787]178    public VirtualCollection() {
[778]179        super();
180        this.setState(VirtualCollection.State.PRIVATE);
[507]181    }
[3940]182
[780]183    public Long getId() {
[778]184        return id;
[507]185    }
[5417]186
[5405]187    public void setId(Long id) {
188        this.id = id;
189    }
[507]190
[778]191    public User getOwner() {
192        return owner;
[511]193    }
[3940]194
[503]195    public void setOwner(User owner) {
196        if (owner == null) {
197            throw new NullPointerException("owner == null");
198        }
[784]199        if (this.owner != null) {
200            this.owner.getVirtualCollections().remove(this);
201        }
[503]202        this.owner = owner;
[784]203        this.owner.getVirtualCollections().add(this);
[503]204    }
[146]205
[778]206    public PersistentIdentifier getPersistentIdentifier() {
207        return persistentId;
[503]208    }
[146]209
[778]210    public void setPersistentIdentifier(PersistentIdentifier persistentId) {
211        if (persistentId == null) {
[503]212            throw new NullPointerException("pid == null");
213        }
[778]214        if ((this.persistentId != null) || (state != State.PUBLIC_PENDING)) {
[509]215            throw new IllegalStateException("illegal state");
216        }
[778]217        this.persistentId = persistentId;
[509]218        this.state = State.PUBLIC;
[503]219    }
[234]220
[778]221    public State getState() {
222        return state;
[503]223    }
[234]224
[778]225    public void setState(State state) {
226        if (state == null) {
227            throw new NullPointerException("state == null");
[507]228        }
[778]229        this.state = state;
[507]230    }
231
[778]232    public boolean isPrivate() {
233        return state == State.PRIVATE;
[503]234    }
[146]235
[778]236    public boolean isPublic() {
[3940]237        return (state == State.PUBLIC_PENDING) || (state == State.PUBLIC);
[778]238    }
239
240    public boolean isDeleted() {
[3940]241        return (state == State.DELETED) || (state == State.DEAD);
[778]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
[503]252    public String getName() {
253        return name;
254    }
[146]255
[778]256    public void setName(String name) {
[784]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        }
[778]264        this.name = name;
[503]265    }
[146]266
[503]267    public String getDescription() {
268        return description;
269    }
[146]270
[778]271    public void setDescription(String description) {
[784]272        if (description != null) {
273            description = description.trim();
274            if (description.isEmpty()) {
275                description = null;
276            }
277        }
[778]278        this.description = description;
[503]279    }
[146]280
[503]281    public Date getCreationDate() {
282        return creationDate;
283    }
[146]284
[778]285    public void setCreationDate(Date creationDate) {
286        this.creationDate = creationDate;
[503]287    }
[146]288
[778]289    public List<Creator> getCreators() {
[780]290        if (creators == null) {
291            this.creators = new ArrayList<Creator>();
292        }
[778]293        return creators;
[503]294    }
[146]295
[778]296    public VirtualCollection.Purpose getPurpose() {
297        return purpose;
[503]298    }
[146]299
[778]300    public void setPurpose(VirtualCollection.Purpose purpose) {
[556]301        this.purpose = purpose;
302    }
[778]303
304    public VirtualCollection.Reproducibility getReproducibility() {
305        return reproducibility;
[556]306    }
[778]307
308    public void setReproducibility(
309            VirtualCollection.Reproducibility reproducibility) {
[556]310        this.reproducibility = reproducibility;
311    }
312
[778]313    public String getReproducibilityNotice() {
314        return reproducibilityNotice;
[556]315    }
316
317    public void setReproducibilityNotice(String reproducibilityNotice) {
[784]318        if (reproducibilityNotice != null) {
319            reproducibilityNotice = reproducibilityNotice.trim();
320            if (reproducibilityNotice.isEmpty()) {
321                reproducibilityNotice = null;
322            }
323        }
[556]324        this.reproducibilityNotice = reproducibilityNotice;
325    }
326
[778]327    public List<String> getKeywords() {
[780]328        if (keywords == null) {
329            keywords = new ArrayList<String>();
330        }
[778]331        return keywords;
[556]332    }
333
[778]334    public List<Resource> getResources() {
[780]335        if (resources == null) {
336            resources = new ArrayList<Resource>();
337        }
[503]338        return resources;
339    }
[146]340
[556]341    public GeneratedBy getGeneratedBy() {
[778]342        return generatedBy;
[556]343    }
344
[778]345    public void setGeneratedBy(GeneratedBy generatedBy) {
346        this.generatedBy = generatedBy;
347    }
348
[790]349    public Date getDateCreated() {
350        return dateCreated;
[503]351    }
[146]352
[790]353    public Date getDateModified() {
354        return dateModified;
[778]355    }
356
[790]357    public void setDateModified(Date dateModified) {
358        if (dateModified == null) {
359            throw new NullPointerException("dateModified == null");
[503]360        }
[790]361        this.dateModified = dateModified;
[503]362    }
[146]363
[503]364    public void updateFrom(VirtualCollection vc) {
365        if (this == vc) {
366            return;
367        }
[509]368        if (vc.getPersistentIdentifier() != null) {
369            this.setPersistentIdentifier(vc.getPersistentIdentifier());
370        }
[778]371        this.setState(state);
372        this.setType(vc.getType());
373        this.setName(vc.getName());
[503]374        this.setDescription(vc.getDescription());
375        this.setCreationDate(vc.getCreationDate());
[3940]376
[780]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);
[503]385        }
[780]386        if (!obsolete_creators.isEmpty()) {
387            for (Creator creator : obsolete_creators) {
388                this.getCreators().remove(creator);
389            }
390            obsolete_creators = null;
391        }
392
[556]393        this.setPurpose(vc.getPurpose());
394        this.setReproducibility(vc.getReproducibility());
395        this.setReproducibilityNotice(vc.getReproducibilityNotice());
[780]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);
[778]405        }
[780]406        if (!obsolete_keywords.isEmpty()) {
407            for (String keyword : obsolete_keywords) {
408                this.getKeywords().remove(keyword);
409            }
410            obsolete_keywords = null;
[778]411        }
[780]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
[778]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) {
[3940]438                final GeneratedByQuery q = genBy.getQuery();
439                GeneratedByQuery query =
440                    new GeneratedByQuery(q.getProfile(), q.getValue());
[778]441                this.generatedBy.setQuery(query);
442            }
443        }
444    }
[220]445
[778]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()
[780]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())
[778]473                .isEquals();
474        }
475        return false;
[503]476    }
477
[778]478    @Override
479    public int hashCode() {
480        return new HashCodeBuilder(1391, 295)
[780]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())
[778]495            .toHashCode();
496    }
497
[146]498} // class VirtualCollection
Note: See TracBrowser for help on using the repository browser.