1 | package eu.clarin.cmdi.virtualcollectionregistry.model; |
---|
2 | |
---|
3 | import java.io.Serializable; |
---|
4 | |
---|
5 | import javax.persistence.Column; |
---|
6 | import javax.persistence.Entity; |
---|
7 | import javax.persistence.EnumType; |
---|
8 | import javax.persistence.Enumerated; |
---|
9 | import javax.persistence.GeneratedValue; |
---|
10 | import javax.persistence.GenerationType; |
---|
11 | import javax.persistence.Id; |
---|
12 | import javax.persistence.Lob; |
---|
13 | import javax.persistence.Table; |
---|
14 | |
---|
15 | import org.apache.commons.lang.builder.EqualsBuilder; |
---|
16 | import org.apache.commons.lang.builder.HashCodeBuilder; |
---|
17 | |
---|
18 | @Entity |
---|
19 | @Table(name = "resource") |
---|
20 | public class Resource implements Serializable, IdentifiedEntity { |
---|
21 | |
---|
22 | private static final long serialVersionUID = 1L; |
---|
23 | |
---|
24 | public static enum Type { |
---|
25 | |
---|
26 | METADATA, |
---|
27 | RESOURCE; |
---|
28 | } // enum Resource.Type |
---|
29 | |
---|
30 | @Id |
---|
31 | @GeneratedValue(strategy = GenerationType.AUTO) |
---|
32 | @Column(name = "id", nullable = false, updatable = false) |
---|
33 | private Long id; |
---|
34 | |
---|
35 | @Column(name = "type", nullable = false) |
---|
36 | @Enumerated(EnumType.ORDINAL) |
---|
37 | private Type type; |
---|
38 | |
---|
39 | @Column(name = "ref", nullable = false, length = 255) |
---|
40 | private String ref; |
---|
41 | |
---|
42 | @Column(name = "label", nullable = true, length = 255) |
---|
43 | private String label; |
---|
44 | |
---|
45 | @Lob |
---|
46 | @Column(name = "description", length = 8192) |
---|
47 | private String description; |
---|
48 | |
---|
49 | public Resource() { |
---|
50 | super(); |
---|
51 | } |
---|
52 | |
---|
53 | public Resource(Resource.Type type, String ref) { |
---|
54 | super(); |
---|
55 | this.setType(type); |
---|
56 | this.setRef(ref); |
---|
57 | } |
---|
58 | |
---|
59 | @Override |
---|
60 | public Long getId() { |
---|
61 | return id; |
---|
62 | } |
---|
63 | |
---|
64 | public Resource.Type getType() { |
---|
65 | return type; |
---|
66 | } |
---|
67 | |
---|
68 | public void setType(Resource.Type type) { |
---|
69 | if (type == null) { |
---|
70 | throw new NullPointerException("type == null"); |
---|
71 | } |
---|
72 | this.type = type; |
---|
73 | } |
---|
74 | |
---|
75 | public String getRef() { |
---|
76 | return ref; |
---|
77 | } |
---|
78 | |
---|
79 | public void setRef(String ref) { |
---|
80 | if (ref == null) { |
---|
81 | throw new NullPointerException("ref == null"); |
---|
82 | } |
---|
83 | ref = ref.trim(); |
---|
84 | if (ref.isEmpty()) { |
---|
85 | throw new IllegalArgumentException("ref is empty"); |
---|
86 | } |
---|
87 | this.ref = ref; |
---|
88 | } |
---|
89 | |
---|
90 | public String getLabel() { |
---|
91 | return label; |
---|
92 | } |
---|
93 | |
---|
94 | public void setLabel(String label) { |
---|
95 | this.label = label; |
---|
96 | } |
---|
97 | |
---|
98 | public String getDescription() { |
---|
99 | return description; |
---|
100 | } |
---|
101 | |
---|
102 | public void setDescription(String description) { |
---|
103 | this.description = description; |
---|
104 | } |
---|
105 | |
---|
106 | @Override |
---|
107 | public boolean equals(Object obj) { |
---|
108 | if (obj == null) { |
---|
109 | return false; |
---|
110 | } |
---|
111 | if (obj == this) { |
---|
112 | return true; |
---|
113 | } |
---|
114 | if (obj instanceof Resource) { |
---|
115 | final Resource rhs = (Resource) obj; |
---|
116 | return new EqualsBuilder() |
---|
117 | .append(this.getType(), rhs.getType()) |
---|
118 | .append(this.getRef(), rhs.getRef()) |
---|
119 | .isEquals(); |
---|
120 | } |
---|
121 | return false; |
---|
122 | } |
---|
123 | |
---|
124 | @Override |
---|
125 | public int hashCode() { |
---|
126 | return new HashCodeBuilder(25973, 1815) |
---|
127 | .append(this.getType()) |
---|
128 | .append(this.getRef()) |
---|
129 | .toHashCode(); |
---|
130 | } |
---|
131 | |
---|
132 | public void valuesFrom(Resource resource) { |
---|
133 | ref = resource.getRef(); |
---|
134 | type = resource.getType(); |
---|
135 | label = resource.getLabel(); |
---|
136 | description = resource.getDescription(); |
---|
137 | } |
---|
138 | |
---|
139 | public Resource getCopy() { |
---|
140 | final Resource copy = new Resource(); |
---|
141 | copy.id = id; |
---|
142 | copy.setRef(ref); |
---|
143 | copy.setType(type); |
---|
144 | copy.setLabel(label); |
---|
145 | copy.setDescription(description); |
---|
146 | return copy; |
---|
147 | } |
---|
148 | |
---|
149 | } // class Resource |
---|