1 package org.kuali.student.enrollment.class2.courseofferingset.model;
2
3 import org.apache.log4j.Logger;
4 import org.kuali.student.enrollment.courseofferingset.dto.SocRolloverResultInfo;
5 import org.kuali.student.enrollment.courseofferingset.infc.SocRolloverResult;
6 import org.kuali.student.r1.common.entity.KSEntityConstants;
7 import org.kuali.student.r2.common.assembler.TransformUtility;
8 import org.kuali.student.r2.common.dto.AttributeInfo;
9 import org.kuali.student.r2.common.entity.AttributeOwner;
10 import org.kuali.student.r2.common.entity.MetaEntity;
11 import org.kuali.student.r2.common.util.RichTextHelper;
12 import org.kuali.student.r2.common.util.constants.CourseOfferingSetServiceConstants;
13
14 import javax.persistence.CascadeType;
15 import javax.persistence.Column;
16 import javax.persistence.Entity;
17 import javax.persistence.FetchType;
18 import javax.persistence.NamedQueries;
19 import javax.persistence.NamedQuery;
20 import javax.persistence.OneToMany;
21 import javax.persistence.Table;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27
28 @Entity
29 @Table(name = "KSEN_SOC_ROR")
30 @NamedQueries({
31 @NamedQuery(name="SocRor.getSocRorsByType", query="Select a from SocRolloverResultEntity a where a.socRorType =:socRorType"),
32 @NamedQuery(name="SocRor.getSocRorsBySourceSocId", query="Select a from SocRolloverResultEntity a where a.sourceSocId=:sourceSocId"),
33 @NamedQuery(name="SocRor.getSocRolloverResultIdsBySourceSocId", query="Select a.id from SocRolloverResultEntity a where a.sourceSocId=:sourceSocId"),
34 @NamedQuery(name="SocRor.getSocRorsByTargetSocId", query="Select a from SocRolloverResultEntity a where a.targetSocId=:targetSocId"),
35 @NamedQuery(name="SocRor.getSocRolloverResultIdsByTargetSocId", query="Select a.id from SocRolloverResultEntity a where a.targetSocId=:targetSocId")
36
37 })
38 public class SocRolloverResultEntity extends MetaEntity implements AttributeOwner<SocRolloverResultAttributeEntity> {
39
40 @Column(name = "SOC_ROR_TYPE", nullable = false)
41 private String socRorType;
42 @Column(name = "SOC_ROR_STATE", nullable = false)
43 private String socRorState;
44 @Column(name = "TARGET_TERM_ID")
45 private String targetTermId;
46 @Column(name = "ITEMS_PROCESSED")
47 private Integer itemsProcessed;
48 @Column(name = "ITEMS_EXPECTED")
49 private Integer itemsExpected;
50 @Column(name = "SOURCE_SOC_ID")
51 private String sourceSocId;
52 @Column(name = "TARGET_SOC_ID")
53 private String targetSocId;
54 @Column(name = "MESG_FORMATTED", length = KSEntityConstants.EXTRA_LONG_TEXT_LENGTH)
55 private String mesgFormatted;
56 @Column(name = "MESG_PLAIN", length = KSEntityConstants.EXTRA_LONG_TEXT_LENGTH)
57 private String mesgPlain;
58 @OneToMany(cascade = CascadeType.ALL, mappedBy = "socRolloverResult",fetch = FetchType.EAGER)
59 private Set<SocRolloverResultOptionEntity> options = new HashSet<SocRolloverResultOptionEntity>();
60 @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner", fetch = FetchType.EAGER, orphanRemoval = true)
61 private final Set<SocRolloverResultAttributeEntity> attributes = new HashSet<SocRolloverResultAttributeEntity>();
62
63 private static Logger LOGGER = Logger.getLogger(SocRolloverResultEntity.class);
64
65 public SocRolloverResultEntity() {
66 }
67
68 public SocRolloverResultEntity(SocRolloverResult socRolloverResult) {
69 super(socRolloverResult);
70 this.setId(socRolloverResult.getId());
71 this.setSocRorType(socRolloverResult.getTypeKey());
72 this.fromDTO(socRolloverResult);
73 }
74
75 private Map<String, SocRolloverResultAttributeEntity> _computeKeyAttributeMap(Set<SocRolloverResultAttributeEntity> attributes) {
76 Map<String, SocRolloverResultAttributeEntity> keyAttributeMap =
77 new HashMap<String, SocRolloverResultAttributeEntity>();
78 for (SocRolloverResultAttributeEntity attr: attributes) {
79 keyAttributeMap.put(attr.getKey(), attr);
80 }
81 return keyAttributeMap;
82 }
83
84 private Set<SocRolloverResultAttributeEntity> _filterForDynAttrs(SocRolloverResultEntity entity) {
85 Set<SocRolloverResultAttributeEntity> filtered = new HashSet<SocRolloverResultAttributeEntity>();
86 for (SocRolloverResultAttributeEntity attrEntity: this.getAttributes()) {
87 if (CourseOfferingSetServiceConstants.ALL_RESULT_DYNATTR_KEYS.contains(attrEntity.getKey())) {
88 filtered.add(attrEntity);
89 }
90 }
91
92 this.getAttributes().removeAll(filtered);
93 return filtered;
94 }
95
96 public void fromDTO(SocRolloverResult socRolloverResult) {
97 this.setSocRorState(socRolloverResult.getStateKey());
98 this.setSourceSocId(socRolloverResult.getSourceSocId());
99 this.setTargetSocId(socRolloverResult.getTargetSocId());
100 this.setTargetTermId(socRolloverResult.getTargetTermId());
101
102 this.setItemsProcessed(socRolloverResult.getItemsProcessed());
103 this.setItemsExpected(socRolloverResult.getItemsExpected());
104 if (socRolloverResult.getMessage() != null) {
105 this.setMesgFormatted(socRolloverResult.getMessage().getFormatted());
106 this.setMesgPlain(socRolloverResult.getMessage().getPlain());
107 } else {
108 this.setMesgFormatted(null);
109 this.setMesgPlain(null);
110 }
111
112
113 for (String optionKey : socRolloverResult.getOptionKeys()) {
114 if (!alreadyExists(optionKey)) {
115 this.getOptions().add(new SocRolloverResultOptionEntity(optionKey, this));
116 }
117 }
118
119
120 Set<SocRolloverResultAttributeEntity> dynamicAttrSet = _filterForDynAttrs(this);
121
122 List<Object> toDelete =
123 TransformUtility.mergeToEntityAttributes(SocRolloverResultAttributeEntity.class, socRolloverResult, this);
124 if (!toDelete.isEmpty()) {
125 LOGGER.warn("Unexpected attributes--should handle by deleting orphans");
126 }
127
128 this.getAttributes().addAll(dynamicAttrSet);
129
130 SocRolloverResultDynAttrConverter.copyDtoDynAttrsToEntity(socRolloverResult, this);
131 }
132
133 private boolean alreadyExists(String optionKey) {
134 for (SocRolloverResultOptionEntity optionEntity : this.getOptions()) {
135 if (optionEntity.getOptionId().equals(optionKey)) {
136 return true;
137 }
138 }
139 return false;
140 }
141
142 private Integer _parseInt(String intStr) {
143 if (intStr == null) {
144 return null;
145 }
146 try {
147 return Integer.parseInt(intStr);
148 } catch (NumberFormatException e) {
149
150 }
151 return null;
152 }
153
154 public SocRolloverResultInfo toDto() {
155 SocRolloverResultInfo socRolloverResult = new SocRolloverResultInfo();
156 socRolloverResult.setId(getId());
157 socRolloverResult.setTypeKey(socRorType);
158 socRolloverResult.setStateKey(socRorState);
159 socRolloverResult.setSourceSocId(sourceSocId);
160 socRolloverResult.setTargetTermId(targetTermId);
161
162 socRolloverResult.setTargetSocId(targetSocId);
163 socRolloverResult.setItemsExpected(itemsExpected);
164 socRolloverResult.setItemsProcessed(itemsProcessed);
165 socRolloverResult.setMessage(new RichTextHelper().toRichTextInfo(getMesgPlain(), getMesgFormatted()));
166 if (getOptions() != null) {
167 for (SocRolloverResultOptionEntity optionEntity : getOptions()) {
168 String optionKey = optionEntity.getOptionId();
169 socRolloverResult.getOptionKeys().add(optionKey);
170 }
171 }
172 socRolloverResult.setMeta(super.toDTO());
173 if (getAttributes() != null) {
174
175 for (SocRolloverResultAttributeEntity att : getAttributes()) {
176 AttributeInfo attInfo = att.toDto();
177 if (!CourseOfferingSetServiceConstants.ALL_RESULT_DYNATTR_KEYS.contains(attInfo.getKey())) {
178 socRolloverResult.getAttributes().add(attInfo);
179 }
180 }
181
182 SocRolloverResultDynAttrConverter.copyEntityAttrsToDtoDynAttrs(this, socRolloverResult);
183 }
184 return socRolloverResult;
185 }
186
187 public Set<SocRolloverResultAttributeEntity> getAttributes() {
188 return attributes;
189 }
190
191 public void setAttributes(Set<SocRolloverResultAttributeEntity> attributes) {
192 this.attributes.clear();
193 if (attributes != null) {
194 this.attributes.addAll(attributes);
195 }
196 }
197
198 public Integer getItemsExpected() {
199 return itemsExpected;
200 }
201
202 public void setItemsExpected(Integer itemsExpected) {
203 this.itemsExpected = itemsExpected;
204 }
205
206 public Integer getItemsProcessed() {
207 return itemsProcessed;
208 }
209
210 public void setItemsProcessed(Integer itemsProcessed) {
211 this.itemsProcessed = itemsProcessed;
212 }
213
214 public String getMesgFormatted() {
215 return mesgFormatted;
216 }
217
218 public void setMesgFormatted(String mesgFormatted) {
219 this.mesgFormatted = mesgFormatted;
220 }
221
222 public String getMesgPlain() {
223 return mesgPlain;
224 }
225
226 public void setMesgPlain(String mesgPlain) {
227 this.mesgPlain = mesgPlain;
228 }
229
230 public String getSocRorState() {
231 return socRorState;
232 }
233
234 public void setSocRorState(String socRorState) {
235 this.socRorState = socRorState;
236 }
237
238 public String getSocRorType() {
239 return socRorType;
240 }
241
242 public void setSocRorType(String socRorType) {
243 this.socRorType = socRorType;
244 }
245
246 public String getSourceSocId() {
247 return sourceSocId;
248 }
249
250 public void setSourceSocId(String sourceSocId) {
251 this.sourceSocId = sourceSocId;
252 }
253
254 public String getTargetSocId() {
255 return targetSocId;
256 }
257
258 public void setTargetSocId(String targetSocId) {
259 this.targetSocId = targetSocId;
260 }
261
262 public String getTargetTermId() {
263 return targetTermId;
264 }
265
266 public void setTargetTermId(String targetTermId) {
267 this.targetTermId = targetTermId;
268 }
269
270 public Set<SocRolloverResultOptionEntity> getOptions() {
271 if (this.options == null) {
272 this.options = new HashSet<SocRolloverResultOptionEntity>();
273 }
274 return options;
275 }
276
277 public void setOptions(Set<SocRolloverResultOptionEntity> options) {
278 this.options = options;
279 }
280 }