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