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