1 package org.kuali.student.enrollment.class1.lui.model;
2
3 import org.kuali.student.enrollment.lui.dto.LuiLuiRelationInfo;
4 import org.kuali.student.enrollment.lui.infc.LuiLuiRelation;
5 import org.kuali.student.r1.common.entity.KSEntityConstants;
6 import org.kuali.student.r2.common.assembler.TransformUtility;
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.util.RichTextHelper;
10
11 import javax.persistence.CascadeType;
12 import javax.persistence.Column;
13 import javax.persistence.Entity;
14 import javax.persistence.JoinColumn;
15 import javax.persistence.ManyToOne;
16 import javax.persistence.NamedQueries;
17 import javax.persistence.NamedQuery;
18 import javax.persistence.OneToMany;
19 import javax.persistence.Table;
20 import javax.persistence.Temporal;
21 import javax.persistence.TemporalType;
22 import java.util.ArrayList;
23 import java.util.Date;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27
28 @Entity
29 @Table(name = "KSEN_LUILUI_RELTN")
30 @NamedQueries({
31 @NamedQuery(name="LuiLuiRelationENR.getLuiLuiRelationsByLui", query="SELECT rel from LuiLuiRelationEntity rel where rel.lui.id=:luiId OR rel.relatedLui.id=:luiId"),
32 @NamedQuery(name="LuiLuiRelationENR.getLuiIdsByRelation", query="select rel.lui.id from LuiLuiRelationEntity rel where rel.relatedLui.id=:relatedLuiId and rel.luiLuiRelationType=:luLuRelationTypeKey"),
33 @NamedQuery(name="LuiLuiRelationENR.getLuiIdsAndStateByRelatedLuisAndRelationType", query="Select distinct rel.lui.id, rel.lui.luiState from LuiLuiRelationEntity rel where rel.relatedLui.id in (:luiIds) AND rel.luiLuiRelationType=:luiLuiRelationTypeKey"),
34 @NamedQuery(name="LuiLuiRelationENR.getLuisByRelation", query = "select rel.lui from LuiLuiRelationEntity rel where rel.relatedLui.id=:relatedLuiId and rel.luiLuiRelationType=:luLuRelationTypeKey"),
35 @NamedQuery(name="LuiLuiRelationENR.getRelatedLuisByLuiId", query="select rel.relatedLui.id from LuiLuiRelationEntity rel where rel.lui.id=:luiId and rel.luiLuiRelationType=:luLuRelationTypeKey"),
36 @NamedQuery(name="LuiLuiRelationENR.getLuiLuiRelationsByRelatedLuiAndLuiId", query="Select rel from LuiLuiRelationEntity rel where rel.lui.id=:luiId AND rel.relatedLui.id=:relatedLuiId"),
37 @NamedQuery(name="LuiLuiRelationENR.getRelatedLuisByLuiIdAndRelationType", query="Select rel.relatedLui from LuiLuiRelationEntity rel where rel.lui.id=:luiId AND rel.luiLuiRelationType=:luiLuiRelationTypeKey"),
38 @NamedQuery(name="LuiLuiRelationENR.getRelatedLuiIdsByLuiIdAndRelationType", query="Select rel.relatedLui.id from LuiLuiRelationEntity rel where rel.lui.id=:luiId AND rel.luiLuiRelationType=:luiLuiRelationTypeKey")
39 })
40 public class LuiLuiRelationEntity extends MetaEntity implements AttributeOwner<LuiLuiRelationAttributeEntity> {
41
42 @Column(name = "NAME")
43 private String name;
44 @Column(name = "DESCR_FORMATTED", length = KSEntityConstants.EXTRA_LONG_TEXT_LENGTH)
45 private String formatted;
46 @Column(name = "DESCR_PLAIN", length = KSEntityConstants.EXTRA_LONG_TEXT_LENGTH)
47 private String plain;
48 @ManyToOne
49 @JoinColumn(name = "LUI_ID")
50 private LuiEntity lui;
51 @ManyToOne
52 @JoinColumn(name = "RELATED_LUI_ID")
53 private LuiEntity relatedLui;
54 @Column(name = "LUILUI_RELTN_TYPE")
55 private String luiLuiRelationType;
56 @Column(name = "LUILUI_RELTN_STATE")
57 private String luiLuiRelationState;
58 @Temporal(TemporalType.TIMESTAMP)
59 @Column(name = "EFF_DT")
60 private Date effectiveDate;
61 @Temporal(TemporalType.TIMESTAMP)
62 @Column(name = "EXPIR_DT")
63 private Date expirationDate;
64 @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
65 private Set<LuiLuiRelationAttributeEntity> attributes = new HashSet<LuiLuiRelationAttributeEntity>();
66
67 public LuiLuiRelationEntity() {
68 }
69
70 public LuiLuiRelationEntity(LuiLuiRelation luiLuiRelation) {
71 this.setId(luiLuiRelation.getId());
72 this.setLuiLuiRelationType(luiLuiRelation.getTypeKey());
73 fromDto(luiLuiRelation);
74 }
75
76 public List<Object> fromDto(LuiLuiRelation luiLuiRelation) {
77 List<Object> orphansToDelete = new ArrayList<Object>();
78
79 this.setEffectiveDate(luiLuiRelation.getEffectiveDate());
80 this.setExpirationDate(luiLuiRelation.getExpirationDate());
81 this.setLuiLuiRelationState(luiLuiRelation.getStateKey());
82 this.setName(luiLuiRelation.getName());
83 if (luiLuiRelation.getDescr() == null) {
84 this.setDescrFormatted(null);
85 this.setDescrPlain(null);
86 } else {
87 this.setDescrFormatted(luiLuiRelation.getDescr().getFormatted());
88 this.setDescrPlain(luiLuiRelation.getDescr().getPlain());
89 }
90
91
92 orphansToDelete.addAll(TransformUtility.mergeToEntityAttributes(LuiLuiRelationAttributeEntity.class, luiLuiRelation, this));
93
94 return orphansToDelete;
95 }
96
97 public LuiLuiRelationInfo toDto() {
98 LuiLuiRelationInfo info = new LuiLuiRelationInfo();
99 info.setId(getId());
100 if (lui != null) {
101 info.setLuiId(lui.getId());
102 }
103 if (relatedLui != null) {
104 info.setRelatedLuiId(relatedLui.getId());
105 }
106 info.setEffectiveDate(effectiveDate);
107 info.setExpirationDate(expirationDate);
108 info.setStateKey(luiLuiRelationState);
109 info.setTypeKey(luiLuiRelationType);
110 info.setMeta(super.toDTO());
111 info.setDescr(new RichTextHelper().toRichTextInfo(plain, formatted));
112
113
114 info.setAttributes(TransformUtility.toAttributeInfoList(this));
115
116 return info;
117 }
118
119 public String getName() {
120 return name;
121 }
122
123 public void setName(String name) {
124 this.name = name;
125 }
126
127 public String getDescrFormatted() {
128 return formatted;
129 }
130
131 public void setDescrFormatted(String formatted) {
132 this.formatted = formatted;
133 }
134
135 public String getDescrPlain() {
136 return plain;
137 }
138
139 public void setDescrPlain(String plain) {
140 this.plain = plain;
141 }
142
143 public LuiEntity getLui() {
144 return lui;
145 }
146
147 public void setLui(LuiEntity lui) {
148 this.lui = lui;
149 }
150
151 public LuiEntity getRelatedLui() {
152 return relatedLui;
153 }
154
155 public void setRelatedLui(LuiEntity relatedLui) {
156 this.relatedLui = relatedLui;
157 }
158
159 public String getLuiLuiRelationType() {
160 return luiLuiRelationType;
161 }
162
163 public void setLuiLuiRelationType(String luiLuiRelationType) {
164 this.luiLuiRelationType = luiLuiRelationType;
165 }
166
167 public String getLuiLuiRelationState() {
168 return luiLuiRelationState;
169 }
170
171 public void setLuiLuiRelationState(String luiLuiRelationState) {
172 this.luiLuiRelationState = luiLuiRelationState;
173 }
174
175 public Date getEffectiveDate() {
176 return effectiveDate;
177 }
178
179 public void setEffectiveDate(Date effectiveDate) {
180 this.effectiveDate = effectiveDate;
181 }
182
183 public Date getExpirationDate() {
184 return expirationDate;
185 }
186
187 public void setExpirationDate(Date expirationDate) {
188 this.expirationDate = expirationDate;
189 }
190
191 public void setAttributes(Set<LuiLuiRelationAttributeEntity> attributes) {
192 this.attributes = attributes;
193
194 }
195
196 public Set<LuiLuiRelationAttributeEntity> getAttributes() {
197 return attributes;
198 }
199 }