View Javadoc

1   package org.kuali.student.enrollment.class1.lui.model;
2   
3   import org.kuali.student.enrollment.lui.dto.LuiInfo;
4   import org.kuali.student.enrollment.lui.infc.Lui;
5   import org.kuali.student.enrollment.lui.infc.LuiIdentifier;
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.entity.AttributeOwner;
9   import org.kuali.student.r2.common.entity.MetaEntity;
10  import org.kuali.student.r2.common.util.RichTextHelper;
11  import org.kuali.student.r2.common.util.constants.LuiServiceConstants;
12  import org.kuali.student.r2.lum.clu.infc.LuCode;
13  
14  import javax.persistence.CascadeType;
15  import javax.persistence.CollectionTable;
16  import javax.persistence.Column;
17  import javax.persistence.ElementCollection;
18  import javax.persistence.Entity;
19  import javax.persistence.FetchType;
20  import javax.persistence.JoinColumn;
21  import javax.persistence.NamedQueries;
22  import javax.persistence.NamedQuery;
23  import javax.persistence.OneToMany;
24  import javax.persistence.Table;
25  import javax.persistence.Temporal;
26  import javax.persistence.TemporalType;
27  import java.util.ArrayList;
28  import java.util.Date;
29  import java.util.HashMap;
30  import java.util.HashSet;
31  import java.util.List;
32  import java.util.Map;
33  import java.util.Set;
34  
35  @Entity
36  @Table(name = "KSEN_LUI")
37  @NamedQueries({
38      @NamedQuery(name="Lui.getLuisByType", query="Select lui from LuiEntity lui where lui.luiType =:typeId"),
39      @NamedQuery(name="Lui.getLuisByClu", query="Select lui from LuiEntity lui where lui.cluId=:cluId"),
40      @NamedQuery(name="Lui.getLuisByAtpAndType", query="Select lui from LuiEntity lui where lui.atpId=:atpId and lui.luiType = :typeKey"),
41      @NamedQuery(name="Lui.getLuiIdsByAtpAndType", query="Select id from LuiEntity lui where lui.atpId=:atpId and lui.luiType = :typeKey"),
42      @NamedQuery(name="Lui.getLuisByAtpAndClu", query="Select lui from LuiEntity lui where lui.atpId=:atpId and lui.cluId = :cluId"),
43      @NamedQuery(name="Lui.getLuisByLuiId", query="SELECT lui FROM LuiEntity lui WHERE lui.id = :aoId")
44  })
45  public class LuiEntity extends MetaEntity implements AttributeOwner<LuiAttributeEntity> {
46  
47      @Column(name = "NAME")
48      private String name;
49      @Column(name = "DESCR_FORMATTED", length = KSEntityConstants.EXTRA_LONG_TEXT_LENGTH)
50      private String formatted;
51      @Column(name = "DESCR_PLAIN", length = KSEntityConstants.EXTRA_LONG_TEXT_LENGTH)
52      private String plain;
53      @Column(name = "LUI_TYPE")
54      private String luiType;
55      @Column(name = "LUI_STATE")
56      private String luiState;
57      @Column(name = "CLU_ID")
58      private String cluId;
59      @Column(name = "ATP_ID")
60      private String atpId;
61      @Column(name = "REF_URL")
62      private String referenceURL;
63      @Column(name = "MAX_SEATS")
64      private Integer maxSeats;
65      @Column(name = "MIN_SEATS")
66      private Integer minSeats;
67      @Temporal(TemporalType.TIMESTAMP)
68      @Column(name = "EFF_DT")
69      private Date effectiveDate;
70      @Temporal(TemporalType.TIMESTAMP)
71      @Column(name = "EXPIR_DT")
72      private Date expirationDate;
73  
74  
75      @ElementCollection
76      @CollectionTable(name ="KSEN_LUI_SCHEDULE",joinColumns = @JoinColumn(name = "LUI_ID"))
77      @Column(name="SCHED_ID")
78      private List<String> scheduleIds;
79  
80      @ElementCollection
81      @CollectionTable(name ="KSEN_LUI_RESULT_VAL_GRP",joinColumns = @JoinColumn(name = "LUI_ID"))
82      @Column(name="RESULT_VAL_GRP_ID")
83      private List<String> resultValuesGroupKeys;
84  
85      @OneToMany(cascade = CascadeType.ALL, mappedBy = "lui")
86      private List<LuiIdentifierEntity> identifiers;
87  
88      @ElementCollection
89      @CollectionTable(name ="KSEN_LUI_UNITS_CONT_OWNER",joinColumns = @JoinColumn(name = "LUI_ID"))
90      @Column(name="ORG_ID")
91      private List<String> luiContentOwner;
92  
93      @ElementCollection
94      @CollectionTable(name ="KSEN_LUI_RELATED_LUI_TYPES",joinColumns = @JoinColumn(name = "LUI_ID"))
95      @Column(name="RELATED_LUI_TYPE")
96      private List<String> relatedLuiTypes;
97  
98      @OneToMany(cascade = CascadeType.ALL, mappedBy = "lui")
99      private List<LuiUnitsDeploymentEntity> luiUnitsDeployment;
100 
101     @OneToMany(cascade = CascadeType.ALL, mappedBy = "lui")
102     private List<LuCodeEntity> luiCodes;
103 
104     @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner", fetch = FetchType.EAGER)
105     private Set<LuiAttributeEntity> attributes = new HashSet<LuiAttributeEntity>();
106 
107     public LuiEntity() {
108     }
109 
110     public LuiEntity(Lui lui) {
111         super(lui);
112         this.setId(lui.getId());
113         this.setLuiType(lui.getTypeKey());
114         this.setAtpId(lui.getAtpId());
115         this.setCluId(lui.getCluId());
116         fromDto(lui);
117     }
118 
119     public List<Object> fromDto(Lui lui) {
120 
121         List<Object> orphansToDelete = new ArrayList<Object>();
122 
123         this.setName(lui.getName());
124         this.setMaxSeats(lui.getMaximumEnrollment());
125         this.setMinSeats(lui.getMinimumEnrollment());
126         this.setReferenceURL(lui.getReferenceURL());
127         this.setLuiState(lui.getStateKey());
128         this.setEffectiveDate(lui.getEffectiveDate());
129         this.setExpirationDate(lui.getExpirationDate());
130         this.scheduleIds = new ArrayList<String>(lui.getScheduleIds());
131         this.setAtpId(lui.getAtpId());
132 
133         if (lui.getDescr() == null) {
134             this.setDescrFormatted(null);
135             this.setDescrPlain(null);
136         } else {
137             this.setDescrFormatted(lui.getDescr().getFormatted());
138             this.setDescrPlain(lui.getDescr().getPlain());
139         }
140 
141         // LuiCodes
142         Map<String, LuCodeEntity> existingLuiCodes = new HashMap<String, LuCodeEntity>();
143         if (luiCodes != null) {
144             for (LuCodeEntity luiCode: luiCodes) {
145                 existingLuiCodes.put(luiCode.getId(), luiCode);
146             }
147         }
148 
149         //Clear the list
150         luiCodes = new ArrayList<LuCodeEntity>();
151 
152         //Update existing or create new
153         for (LuCode luCode:lui.getLuiCodes()) {
154             LuCodeEntity luCodeEntity;
155             if (existingLuiCodes.containsKey(luCode.getId())) {
156                 luCodeEntity = existingLuiCodes.remove(luCode.getId());
157                 orphansToDelete.addAll(luCodeEntity.fromDto(luCode));
158             } else {
159                 luCodeEntity = new LuCodeEntity(luCode);
160                 luCodeEntity.setLui(this);
161             }
162             luiCodes.add(luCodeEntity);
163         }
164 
165         // Delete the orphans (add to list of objects to delete)
166         orphansToDelete.addAll(existingLuiCodes.values());
167 
168         // Map the existing result group keys by their id
169         if (lui.getResultValuesGroupKeys() != null){
170             resultValuesGroupKeys = new ArrayList<String>(lui.getResultValuesGroupKeys());
171         } else {
172             resultValuesGroupKeys = null;
173         }
174 
175 
176         // Lui Identifiers
177 
178         // Map the existing idents by their id
179         Map<String,LuiIdentifierEntity> existingIdents = new HashMap<String,LuiIdentifierEntity>();
180         if (identifiers != null) {
181             for (LuiIdentifierEntity ident : identifiers) {
182                 existingIdents.put(ident.getId(), ident);
183             }
184         }
185         // Clear the list of current idents
186         identifiers = new ArrayList<LuiIdentifierEntity>();
187 
188         // Add official identifiers
189         if (lui.getOfficialIdentifier() != null) {
190             LuiIdentifierEntity identEntity;
191             // See if this exists already
192             if (existingIdents.containsKey(lui.getOfficialIdentifier().getId())) {
193                 // Pull the existing one out of the map
194                 identEntity = existingIdents.remove(lui.getOfficialIdentifier().getId());
195                 orphansToDelete.addAll(identEntity.fromDto(lui.getOfficialIdentifier())); //Make sure this copies all fields
196             } else {
197                 // This is new so create a new identifier
198                 identEntity = new LuiIdentifierEntity(lui.getOfficialIdentifier());
199                 identEntity.setLui(this);
200             }
201 
202             identifiers.add(identEntity);
203         }
204 
205         // Add alternate identifiers
206         for (LuiIdentifier identifier : lui.getAlternateIdentifiers()) {
207             LuiIdentifierEntity identEntity;
208             // See if this exists already
209             if (existingIdents.containsKey(identifier.getId())) {
210                 // Pull the existing one out of the map
211                 identEntity = existingIdents.remove(identifier.getId());
212                 orphansToDelete.addAll(identEntity.fromDto(identifier)); //Make sure this copies all fields
213             } else {
214                 // This is new so create a new identifier
215                 identEntity = new LuiIdentifierEntity(identifier);
216                 identEntity.setLui(this);
217             }
218 
219             identifiers.add(identEntity);
220         }
221 
222         // Now we need to delete the leftovers (orphaned idents)
223         orphansToDelete.addAll(existingIdents.values());
224 
225         //lui-org relations
226 
227         // Map the existing org relations by their id
228         if (lui.getUnitsContentOwner() != null) {
229            luiContentOwner = new ArrayList<String>(lui.getUnitsContentOwner());
230         } else {
231            luiContentOwner = null;
232         }
233 
234         // Map the related types id
235         if (lui.getRelatedLuiTypes() != null) {
236             relatedLuiTypes = new ArrayList<String>(lui.getRelatedLuiTypes());
237         } else {
238             relatedLuiTypes = null;
239         }
240 
241         // Map the existing org relations by their id
242         Map<String,LuiUnitsDeploymentEntity> existinguLuiUnitsDeploymentEntities = new HashMap<String,LuiUnitsDeploymentEntity>();
243         if (luiUnitsDeployment != null) {
244             for (LuiUnitsDeploymentEntity unitEntity : luiUnitsDeployment) {
245                 existinguLuiUnitsDeploymentEntities.put(unitEntity.getOrgId(), unitEntity);
246             }
247         }
248 
249         // Clear out the current list
250         luiUnitsDeployment = new ArrayList<LuiUnitsDeploymentEntity>();
251 
252         if (lui.getUnitsDeployment() != null){
253             for (String unitDeploymentOrgId : lui.getUnitsDeployment()){
254                 LuiUnitsDeploymentEntity luiUnitDeployment;
255                 if (existinguLuiUnitsDeploymentEntities.containsKey(unitDeploymentOrgId)){
256                     luiUnitDeployment = existinguLuiUnitsDeploymentEntities.remove(unitDeploymentOrgId);
257                 } else {
258                     luiUnitDeployment = new LuiUnitsDeploymentEntity(this, unitDeploymentOrgId);
259                 }
260                 luiUnitsDeployment.add(luiUnitDeployment);
261             }
262         }
263 
264         // Now we need to delete the leftovers (orphaned entities)
265         orphansToDelete.addAll(existinguLuiUnitsDeploymentEntities.values());
266 
267         // Merge attributes into entity and add leftovers to be deleted
268         orphansToDelete.addAll(TransformUtility.mergeToEntityAttributes(LuiAttributeEntity.class, lui, this));
269 
270         return orphansToDelete;
271     }
272 
273     public LuiInfo toDto() {
274         LuiInfo info = new LuiInfo();
275         info.setId(getId());
276         info.setName(name);
277         info.setAtpId(atpId);
278         info.setCluId(cluId);
279         info.setMaximumEnrollment(maxSeats);
280         info.setMinimumEnrollment(minSeats);
281         info.setEffectiveDate(effectiveDate);
282         info.setExpirationDate(expirationDate);
283         info.setReferenceURL(referenceURL);
284         info.setTypeKey(luiType);
285         info.setStateKey(luiState);
286         if(scheduleIds != null) {
287             info.getScheduleIds().addAll(scheduleIds);
288         }
289 
290         info.setMeta(super.toDTO());
291         info.setDescr(new RichTextHelper().toRichTextInfo(plain, formatted));
292 
293         // lucCodes
294         if (luiCodes != null) {
295             for (LuCodeEntity luCode : luiCodes) {
296                 info.getLuiCodes().add(luCode.toDto());
297             }
298         }
299 
300         // Result Value Group Keys
301         info.getResultValuesGroupKeys().clear();
302         if (resultValuesGroupKeys != null){
303             info.getResultValuesGroupKeys().addAll(resultValuesGroupKeys);
304         }
305 
306         // Identifiers
307         if (identifiers != null) {
308             for (LuiIdentifierEntity identifier : identifiers) {
309                 if (LuiServiceConstants.LUI_IDENTIFIER_OFFICIAL_TYPE_KEY.equals(identifier.getType())) {
310                     info.setOfficialIdentifier(identifier.toDto());
311                 } else {
312                     info.getAlternateIdentifiers().add(identifier.toDto());
313                 }
314             }
315         }
316 
317         // Attributes
318         info.setAttributes(TransformUtility.toAttributeInfoList(this));
319 
320         List<String> unitsDeploymentOrgIds = new ArrayList<String>();
321         if (this.luiUnitsDeployment!= null) {
322             for (LuiUnitsDeploymentEntity unitsDep : this.luiUnitsDeployment) {
323                 unitsDeploymentOrgIds.add(unitsDep.getOrgId());
324             }
325         }
326         info.setUnitsDeployment(unitsDeploymentOrgIds);
327 
328         info.getUnitsContentOwner().clear();
329         if (luiContentOwner != null) {
330             info.getUnitsContentOwner().addAll(luiContentOwner);
331         }
332 
333         info.getRelatedLuiTypes().clear();
334         if (relatedLuiTypes != null){
335             info.getRelatedLuiTypes().addAll(relatedLuiTypes);
336         }
337 
338         return info;
339     }
340 
341     public Integer getMaxSeats() {
342         return maxSeats;
343     }
344 
345     public void setMaxSeats(Integer maxSeats) {
346         this.maxSeats = maxSeats;
347     }
348 
349     public Integer getMinSeats() {
350         return minSeats;
351     }
352 
353     public void setMinSeats(Integer minSeats) {
354         this.minSeats = minSeats;
355     }
356 
357     public Date getEffectiveDate() {
358         return effectiveDate;
359     }
360 
361     public void setEffectiveDate(Date effectiveDate) {
362         this.effectiveDate = effectiveDate;
363     }
364 
365     public Date getExpirationDate() {
366         return expirationDate;
367     }
368 
369     public void setExpirationDate(Date expirationDate) {
370         this.expirationDate = expirationDate;
371     }
372 
373     public String getCluId() {
374         return cluId;
375     }
376 
377     public void setCluId(String cluId) {
378         this.cluId = cluId;
379     }
380 
381     public String getName() {
382         return name;
383     }
384 
385     public void setName(String name) {
386         this.name = name;
387     }
388 
389     public String getAtpId() {
390         return atpId;
391     }
392 
393     public void setAtpId(String atpId) {
394         this.atpId = atpId;
395     }
396 
397     public String getDescrFormatted() {
398         return formatted;
399     }
400 
401     public void setDescrFormatted(String formatted) {
402         this.formatted = formatted;
403     }
404 
405     public String getDescrPlain() {
406         return plain;
407     }
408 
409     public void setDescrPlain(String plain) {
410         this.plain = plain;
411     }
412 
413     public String getLuiType() {
414         return luiType;
415     }
416 
417     public void setLuiType(String luiType) {
418         this.luiType = luiType;
419     }
420 
421     public String getLuiState() {
422         return luiState;
423     }
424 
425     public void setLuiState(String luiState) {
426         this.luiState = luiState;
427     }
428 
429     public String getReferenceURL() {
430         return referenceURL;
431     }
432 
433     public void setReferenceURL(String referenceURL) {
434         this.referenceURL = referenceURL;
435     }
436 
437     public void setAttributes(Set<LuiAttributeEntity> attributes) {
438         this.attributes = attributes;
439     }
440 
441     public List<LuiIdentifierEntity> getIdentifiers() {
442         return identifiers;
443     }
444 
445     public void setIdentifiers(List<LuiIdentifierEntity> identifiers) {
446         this.identifiers = identifiers;
447     }
448 
449     public Set<LuiAttributeEntity> getAttributes() {
450         return attributes;
451     }
452 
453     public String getFormatted() {
454         return formatted;
455     }
456 
457     public void setFormatted(String formatted) {
458         this.formatted = formatted;
459     }
460 
461     public String getPlain() {
462         return plain;
463     }
464 
465     public void setPlain(String plain) {
466         this.plain = plain;
467     }
468 
469     public List<LuCodeEntity> getLuiCodes() {
470         return luiCodes;
471     }
472 
473     public void setLuiCodes(List<LuCodeEntity> luiCodes) {
474         this.luiCodes = luiCodes;
475     }
476 
477     public List<String> getLuiContentOwner() {
478         return luiContentOwner;
479     }
480 
481     public void setLuiContentOwner(List<String> luiContentOwner) {
482         this.luiContentOwner = luiContentOwner;
483     }
484 
485     public List<String> getRelatedLuiTypes() {
486         return relatedLuiTypes;
487     }
488 
489     public void setRelatedLuiTypes(List<String> relatedLuiTypes) {
490         this.relatedLuiTypes = relatedLuiTypes;
491     }
492 
493     public List<LuiUnitsDeploymentEntity> getLuiUnitsDeployment() {
494         return luiUnitsDeployment;
495     }
496 
497     public void setLuiUnitsDeployment(List<LuiUnitsDeploymentEntity> luiUnitsDeployment) {
498         this.luiUnitsDeployment = luiUnitsDeployment;
499     }
500 
501     public List<String> getResultValuesGroupKeys() {
502         return resultValuesGroupKeys;
503     }
504 
505     public void setResultValuesGroupKeys(List<String> resultValuesGroupKeys) {
506         this.resultValuesGroupKeys = resultValuesGroupKeys;
507     }
508 
509     public List<String> getScheduleIds() {
510         return scheduleIds;
511     }
512 
513     public void setScheduleIds(List<String> scheduleIds) {
514         this.scheduleIds = scheduleIds;
515     }
516 }