1 package org.kuali.student.r2.lum.lrc.model;
2
3 import java.math.BigDecimal;
4 import java.util.ArrayList;
5 import org.kuali.student.r1.common.entity.KSEntityConstants;
6 import org.kuali.student.r2.common.dto.AttributeInfo;
7 import org.kuali.student.r2.common.entity.AttributeOwner;
8 import org.kuali.student.r2.common.entity.MetaEntity;
9 import org.kuali.student.r2.common.infc.Attribute;
10 import org.kuali.student.r2.lum.lrc.dto.ResultValueInfo;
11
12 import javax.persistence.CascadeType;
13 import javax.persistence.Column;
14 import javax.persistence.Entity;
15 import javax.persistence.FetchType;
16 import javax.persistence.OneToMany;
17 import javax.persistence.Table;
18 import javax.persistence.Temporal;
19 import javax.persistence.TemporalType;
20 import java.util.Date;
21 import java.util.HashSet;
22 import java.util.Set;
23 import javax.persistence.EntityManager;
24 import javax.persistence.NamedQueries;
25 import javax.persistence.NamedQuery;
26 import org.kuali.student.r2.common.util.RichTextHelper;
27
28 @Entity
29 @Table(name = "KSEN_LRC_RESULT_VALUE")
30 @NamedQueries({
31 @NamedQuery(name = "ResultValueEntity.getIdsByType",
32 query = "select id from ResultValueEntity where type = :type"),
33 @NamedQuery(name = "ResultValueEntity.getByScale",
34 query = "select RV from ResultValueEntity RV where RV.resultScaleId = :resultScaleKey"),
35 @NamedQuery(name = "ResultValueEntity.getByScaleAndValue",
36 query = "select RV from ResultValueEntity RV where RV.resultScaleId = :resultScaleKey and RV.value = :value")
37 })
38 public class ResultValueEntity extends MetaEntity implements AttributeOwner<ResultValueAttributeEntity> {
39
40 @Column(name = "RESULT_VALUE_TYPE")
41 private String type;
42 @Column(name = "RESULT_VALUE_STATE")
43 private String state;
44 @Column(name = "NAME")
45 private String name;
46 @Column(name = "DESCR_PLAIN", length = KSEntityConstants.EXTRA_LONG_TEXT_LENGTH)
47 private String descrPlain;
48 @Column(name = "DESCR_FORMATTED", length = KSEntityConstants.EXTRA_LONG_TEXT_LENGTH)
49 private String descrFormatted;
50 @Column(name = "RESULT_SCALE_ID")
51 private String resultScaleId;
52 @Column(name = "NUMERIC_VALUE")
53 private BigDecimal numericValue;
54 @Column(name = "RESULT_VALUE")
55 private String value;
56 @Temporal(TemporalType.TIMESTAMP)
57 @Column(name = "EFF_DT")
58 private Date effectiveDate;
59 @Temporal(TemporalType.TIMESTAMP)
60 @Column(name = "EXPIR_DT")
61 private Date expirationDate;
62 @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner", fetch = FetchType.EAGER)
63 private Set<ResultValueAttributeEntity> attributes;
64
65 public ResultValueEntity() {
66 }
67
68 public ResultValueEntity(ResultValueInfo dto, EntityManager em) {
69 super(dto);
70 this.setId(dto.getKey());
71 this.setType(dto.getTypeKey());
72 setResultScaleId(dto.getResultScaleKey());
73 this.fromDTO(dto, em);
74 }
75
76 public void fromDTO(ResultValueInfo dto, EntityManager em) {
77 this.setName(dto.getName());
78 this.setState(dto.getStateKey());
79 if (dto.getDescr() != null) {
80 this.setDescrFormatted(dto.getDescr().getFormatted());
81 this.setDescrPlain(dto.getDescr().getPlain());
82 } else {
83 this.setDescrFormatted(null);
84 this.setDescrPlain(null);
85 }
86 setEffectiveDate(dto.getEffectiveDate());
87 setExpirationDate(dto.getExpirationDate());
88 if(dto.getNumericValue() != null){
89 setNumericValue(new BigDecimal(dto.getNumericValue()));
90 }
91 else
92 {
93 setNumericValue (null);
94 }
95 setValue(dto.getValue());
96
97 if (this.getAttributes() == null) {
98 this.setAttributes(new HashSet<ResultValueAttributeEntity>());
99 }
100 Set<String> idSet = new HashSet<String>(dto.getAttributes().size());
101 for (AttributeInfo attr : dto.getAttributes()) {
102 if (attr.getId() != null) {
103 idSet.add(attr.getId());
104 }
105 }
106 for (ResultValueAttributeEntity attEntity : new ArrayList<ResultValueAttributeEntity> (this.getAttributes())) {
107 if (!idSet.contains(attEntity.getId())) {
108 em.remove(attEntity);
109 this.getAttributes().remove(attEntity);
110 }
111 }
112 for (Attribute att : dto.getAttributes()) {
113 ResultValueAttributeEntity attEntity = new ResultValueAttributeEntity(att, this);
114 this.getAttributes().add(attEntity);
115 }
116 }
117
118 public String getResultScaleId() {
119 return resultScaleId;
120 }
121
122 public void setResultScaleId(String resultScaleId) {
123 this.resultScaleId = resultScaleId;
124 }
125
126 public BigDecimal getNumericValue() {
127 return numericValue;
128 }
129
130 public void setNumericValue(BigDecimal numericValue) {
131 this.numericValue = numericValue;
132 }
133
134 public String getValue() {
135 return value;
136 }
137
138 public void setValue(String value) {
139 this.value = value;
140 }
141
142 public Date getEffectiveDate() {
143 return effectiveDate;
144 }
145
146 public void setEffectiveDate(Date effectiveDate) {
147 this.effectiveDate = effectiveDate;
148 }
149
150 public Date getExpirationDate() {
151 return expirationDate;
152 }
153
154 public void setExpirationDate(Date expirationDate) {
155 this.expirationDate = expirationDate;
156 }
157
158 public String getName() {
159 return name;
160 }
161
162 public void setName(String name) {
163 this.name = name;
164 }
165
166 public String getDescrFormatted() {
167 return descrFormatted;
168 }
169
170 public void setDescrFormatted(String descrFormatted) {
171 this.descrFormatted = descrFormatted;
172 }
173
174 public String getDescrPlain() {
175 return descrPlain;
176 }
177
178 public void setDescrPlain(String descrPlain) {
179 this.descrPlain = descrPlain;
180 }
181
182 public String getType() {
183 return type;
184 }
185
186 public void setType(String type) {
187 this.type = type;
188 }
189
190 public String getState() {
191 return state;
192 }
193
194 public void setState(String state) {
195 this.state = state;
196 }
197
198 @Override
199 public void setAttributes(Set<ResultValueAttributeEntity> attributes) {
200 this.attributes = attributes;
201 }
202
203 @Override
204 public Set<ResultValueAttributeEntity> getAttributes() {
205 return attributes;
206 }
207
208 public ResultValueInfo toDto() {
209 ResultValueInfo info = new ResultValueInfo();
210 info.setKey(getId());
211 info.setTypeKey(getType());
212 info.setStateKey(getState());
213 info.setName(getName());
214 info.setDescr(new RichTextHelper().toRichTextInfo(getDescrPlain(), getDescrFormatted()));
215 info.setResultScaleKey(getResultScaleId());
216 info.setNumericValue(String.valueOf(getNumericValue()));
217 info.setValue(getValue());
218 info.setEffectiveDate(getEffectiveDate());
219 info.setExpirationDate(getExpirationDate());
220 if (this.getAttributes() != null) {
221 for (ResultValueAttributeEntity att : getAttributes()) {
222 AttributeInfo attInfo = att.toDto();
223 info.getAttributes().add(attInfo);
224 }
225 }
226 info.setMeta(super.toDTO());
227 return info;
228 }
229 }