1 package de.campussource.cse.cdmm.domain;
2
3
4 import java.util.ArrayList;
5 import java.util.Date;
6 import java.util.List;
7
8 import javax.persistence.Column;
9 import javax.persistence.EnumType;
10 import javax.persistence.Enumerated;
11 import javax.persistence.GeneratedValue;
12 import javax.persistence.GenerationType;
13 import javax.persistence.Id;
14 import javax.persistence.Inheritance;
15 import javax.persistence.InheritanceType;
16 import javax.persistence.Table;
17 import javax.persistence.Temporal;
18 import javax.persistence.TemporalType;
19 import javax.persistence.Transient;
20 import javax.persistence.Version;
21 import javax.xml.bind.annotation.XmlElement;
22 import javax.xml.bind.annotation.XmlElementWrapper;
23 import javax.xml.bind.annotation.XmlID;
24 import javax.xml.bind.annotation.XmlRootElement;
25 import javax.xml.bind.annotation.XmlTransient;
26 import javax.xml.bind.annotation.XmlType;
27 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
28
29 import org.apache.commons.lang.builder.ToStringBuilder;
30 import org.apache.commons.lang.builder.ToStringStyle;
31
32 import de.campussource.cse.cdmm.Constants;
33 import de.campussource.cse.cdmm.IdAdapter;
34
35
36
37
38
39
40 @javax.persistence.Entity(name=Constants.ENTITY)
41 @Table(name = Constants.TABLENAME_ENTITY)
42 @Inheritance(strategy=InheritanceType.JOINED)
43 @XmlRootElement(namespace=Constants.NAMESPACE_DATATYPES)
44 @XmlType(name=Constants.ENTITY_TYPE,propOrder={"id", Constants.PROPERTY_STATE, Constants.PROPERTY_ATTRIBUTES}, namespace=Constants.NAMESPACE_DATATYPES)
45 public class Entity{
46
47 @Id
48 @GeneratedValue(strategy = GenerationType.AUTO)
49 private Long id;
50
51 @Transient
52 private List<Attribute> attributes;
53
54 @Column(name=Constants.COLUMNNAME_STATE, nullable=false, insertable=true, updatable=true)
55 @Enumerated(EnumType.STRING)
56 private State state = State.EXISTS;
57
58 @Version
59 @SuppressWarnings("unused")
60 private long version;
61
62 @Column(name="LAST_UPDATE")
63 @Temporal(TemporalType.TIMESTAMP)
64 private Date date;
65
66 public Entity(){
67 }
68
69 public Entity(Long id){
70 this.id = id;
71 }
72
73 private TransientAttribute attribute2TransientAttribute(Attribute attribute){
74 return new TransientAttribute(attribute.getName(), attribute.getValue());
75 }
76
77 private PersistentAttribute attribute2PersistentAttribute(Attribute attribute){
78 return new PersistentAttribute(attribute.getName(), attribute.getValue());
79 }
80
81 public State getState() {
82 return state;
83 }
84
85 @XmlElement(name=Constants.PROPERTY_STATE)
86 public void setState(State state) {
87 this.state = state;
88 }
89
90 public void setId(Long id) {
91 this.id = id;
92 }
93
94 @XmlID
95 @XmlJavaTypeAdapter(value=IdAdapter.class)
96 @XmlElement(name=Constants.ID)
97 public Long getId() {
98 return id;
99 }
100
101 @XmlElementWrapper(name=Constants.ATTRIBUTES)
102 @XmlElement(name=Constants.ATTRIBUTE)
103 public List<Attribute> getAttributes() {
104 return attributes;
105 }
106
107
108 public void setAttributes(List<Attribute> attributes) {
109 List<Attribute> newAttributeList = new ArrayList<Attribute>();
110 for (Attribute att : attributes){
111 if (!att.isTransient()){
112 newAttributeList.add(attribute2PersistentAttribute(att));
113 } else{
114 newAttributeList.add(attribute2TransientAttribute(att));
115 }
116 }
117 this.attributes = newAttributeList;
118 }
119
120
121
122 @Override
123 public String toString() {
124 return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE).append(id).toString();
125 }
126
127 public Date getDate() {
128 return date;
129 }
130
131 @XmlTransient
132 public void setDate(Date date) {
133 this.date = date;
134 }
135
136 @Override
137 public int hashCode() {
138 final int prime = 31;
139 int result = 1;
140 result = prime * result + ((id == null) ? 0 : id.hashCode());
141 return result;
142 }
143
144 @Override
145 public boolean equals(Object obj) {
146 if (this == obj)
147 return true;
148 if (obj == null)
149 return false;
150 if (getClass() != obj.getClass())
151 return false;
152 Entity other = (Entity) obj;
153 if (id == null) {
154 if (other.id != null)
155 return false;
156 } else if (!id.equals(other.id))
157 return false;
158 return true;
159 }
160
161
162 }