source: VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/main/java/eu/clarin/cmdi/virtualcollectionregistry/model/Resource.java @ 5448

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

Fixed adding and editing of resources to collections

  • Property svn:eol-style set to native
File size: 2.9 KB
Line 
1package eu.clarin.cmdi.virtualcollectionregistry.model;
2
3import java.io.Serializable;
4
5import javax.persistence.Column;
6import javax.persistence.Entity;
7import javax.persistence.EnumType;
8import javax.persistence.Enumerated;
9import javax.persistence.GeneratedValue;
10import javax.persistence.GenerationType;
11import javax.persistence.Id;
12import javax.persistence.Table;
13
14import org.apache.commons.lang.builder.EqualsBuilder;
15import org.apache.commons.lang.builder.HashCodeBuilder;
16
17@Entity
18@Table(name = "resource")
19public class Resource implements Serializable, IdentifiedEntity {
20   
21    private static final long serialVersionUID = 1L;
22   
23    public static enum Type {
24       
25        METADATA,
26        RESOURCE;
27    } // enum Resource.Type
28
29    @Id
30    @GeneratedValue(strategy = GenerationType.AUTO)
31    @Column(name = "id", nullable = false, updatable = false)
32    private Long id;
33   
34    @Column(name = "type", nullable = false)
35    @Enumerated(EnumType.ORDINAL)
36    private Type type;
37   
38    @Column(name = "ref", nullable = false, length = 255)
39    private String ref;
40   
41    public Resource() {
42        super();
43    }
44   
45    public Resource(Resource.Type type, String ref) {
46        super();
47        this.setType(type);
48        this.setRef(ref);
49    }
50   
51    public Long getId() {
52        return id;
53    }
54   
55    public Resource.Type getType() {
56        return type;
57    }
58   
59    public void setType(Resource.Type type) {
60        if (type == null) {
61            throw new NullPointerException("type == null");
62        }
63        this.type = type;
64    }
65   
66    public String getRef() {
67        return ref;
68    }
69   
70    public void setRef(String ref) {
71        if (ref == null) {
72            throw new NullPointerException("ref == null");
73        }
74        ref = ref.trim();
75        if (ref.isEmpty()) {
76            throw new IllegalArgumentException("ref is empty");
77        }
78        this.ref = ref;
79    }
80   
81    @Override
82    public boolean equals(Object obj) {
83        if (obj == null) {
84            return false;
85        }
86        if (obj == this) {
87            return true;
88        }
89        if (obj instanceof Resource) {
90            final Resource rhs = (Resource) obj;
91            return new EqualsBuilder()
92                    .append(this.getType(), rhs.getType())
93                    .append(this.getRef(), rhs.getRef())
94                    .isEquals();
95        }
96        return false;
97    }
98   
99    @Override
100    public int hashCode() {
101        return new HashCodeBuilder(25973, 1815)
102                .append(this.getType())
103                .append(this.getRef())
104                .toHashCode();
105    }
106   
107    public void valuesFrom(Resource resource) {
108        ref = resource.getRef();
109        type = resource.getType();
110    }
111   
112    public Resource getCopy() {
113        final Resource copy = new Resource();
114        copy.id = id;
115        copy.setRef(ref);
116        copy.setType(type);
117        return copy;
118    }
119   
120} // class Resource
Note: See TracBrowser for help on using the repository browser.