View Javadoc

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.infc.Attribute;
8   import org.kuali.student.r2.lum.lrc.dto.ResultScaleInfo;
9   import org.kuali.student.r2.lum.lrc.dto.ResultValueRangeInfo;
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.List;
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.entity.MetaEntity;
27  import org.kuali.student.r2.common.util.RichTextHelper;
28  
29  @Entity
30  @Table(name = "KSEN_LRC_RESULT_SCALE")
31  @NamedQueries({
32      @NamedQuery(name = "ResultScaleEntity.getIdsByType",
33      query = "select id from ResultScaleEntity where type = :type")
34  })
35  public class ResultScaleEntity extends MetaEntity implements AttributeOwner<ResultScaleAttributeEntity> {
36  
37      @Column(name = "RESULT_SCALE_TYPE")
38      private String type;
39      @Column(name = "RESULT_SCALE_STATE")
40      private String state;
41      @Column(name = "NAME")
42      private String name;
43      @Column(name = "DESCR_PLAIN", length = KSEntityConstants.EXTRA_LONG_TEXT_LENGTH)
44      private String descrPlain;
45      @Column(name = "DESCR_FORMATTED", length = KSEntityConstants.EXTRA_LONG_TEXT_LENGTH)
46      private String descrFormatted;
47      @Column(name = "RANGE_MIN_VALUE")
48      private String minValue;
49      @Column(name = "RANGE_MAX_VALUE")
50      private String maxValue;
51      @Column(name = "RANGE_INCREMENT")
52      private String increment;
53      @Temporal(TemporalType.TIMESTAMP)
54      @Column(name = "EFF_DT")
55      private Date effectiveDate;
56      @Temporal(TemporalType.TIMESTAMP)
57      @Column(name = "EXPIR_DT")
58      private Date expirationDate;
59      // orphan removal does not work so need to manually remove them ourselves
60      @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner", fetch = FetchType.EAGER, orphanRemoval = true)
61      private Set<ResultScaleAttributeEntity> attributes;
62  
63      public ResultScaleEntity() {
64      }
65  
66      public ResultScaleEntity(ResultScaleInfo dto) {
67          super(dto);
68          this.setId(dto.getKey());
69          this.setType(dto.getTypeKey());
70          fromDTO(dto);
71      }
72  
73      public void fromDTO(ResultScaleInfo dto) {
74  
75          this.setName(dto.getName());
76          this.setState(dto.getStateKey());
77          if (dto.getDescr() != null) {
78              this.setDescrFormatted(dto.getDescr().getFormatted());
79              this.setDescrPlain(dto.getDescr().getPlain());
80          } else {
81              this.setDescrFormatted(null);
82              this.setDescrPlain(null);
83          }
84          if (dto.getResultValueRange() == null) {
85              this.setMinValue(null);
86              this.setMaxValue(null);
87              this.setIncrement(null);
88          } else {
89              this.setMinValue(dto.getResultValueRange().getMinValue());
90              this.setMaxValue(dto.getResultValueRange().getMaxValue());
91              this.setIncrement(dto.getResultValueRange().getIncrement());
92          }
93          this.setEffectiveDate(dto.getEffectiveDate());
94          this.setExpirationDate(dto.getExpirationDate());
95          // dynamic attributes
96          if (this.getAttributes() == null) {
97              this.setAttributes(new HashSet<ResultScaleAttributeEntity>());
98          }
99          else
100             this.attributes.clear();
101        
102         
103         for (Attribute att : dto.getAttributes()) {
104             ResultScaleAttributeEntity attEntity = new ResultScaleAttributeEntity(att, this);
105             this.getAttributes().add(attEntity);
106         }
107         return;
108     }
109 
110     public String getDescrFormatted() {
111         return descrFormatted;
112     }
113 
114     public void setDescrFormatted(String descrFormatted) {
115         this.descrFormatted = descrFormatted;
116     }
117 
118     public String getDescrPlain() {
119         return descrPlain;
120     }
121 
122     public void setDescrPlain(String descrPlain) {
123         this.descrPlain = descrPlain;
124     }
125 
126     public Date getEffectiveDate() {
127         return effectiveDate;
128     }
129 
130     public void setEffectiveDate(Date effectiveDate) {
131         this.effectiveDate = effectiveDate;
132     }
133 
134     public Date getExpirationDate() {
135         return expirationDate;
136     }
137 
138     public void setExpirationDate(Date expirationDate) {
139         this.expirationDate = expirationDate;
140     }
141 
142     public String getIncrement() {
143         return increment;
144     }
145 
146     public void setIncrement(String increment) {
147         this.increment = increment;
148     }
149 
150     public String getMaxValue() {
151         return maxValue;
152     }
153 
154     public void setMaxValue(String maxValue) {
155         this.maxValue = maxValue;
156     }
157 
158     public String getMinValue() {
159         return minValue;
160     }
161 
162     public void setMinValue(String minValue) {
163         this.minValue = minValue;
164     }
165 
166     public String getName() {
167         return name;
168     }
169 
170     public void setName(String name) {
171         this.name = name;
172     }
173 
174     public String getState() {
175         return state;
176     }
177 
178     public void setState(String state) {
179         this.state = state;
180     }
181 
182     public String getType() {
183         return type;
184     }
185 
186     public void setType(String type) {
187         this.type = type;
188     }
189 
190     @Override
191     public void setAttributes(Set<ResultScaleAttributeEntity> attributes) {
192         this.attributes = attributes;
193     }
194 
195     @Override
196     public Set<ResultScaleAttributeEntity> getAttributes() {
197         return attributes;
198     }
199 
200     public ResultScaleInfo toDto() {
201 
202         ResultScaleInfo info = new ResultScaleInfo();
203 
204         info.setKey(getId());
205         info.setTypeKey(getType());
206         info.setStateKey(getState());
207         info.setName(getName());
208         info.setDescr(new RichTextHelper().toRichTextInfo(getDescrPlain(), getDescrFormatted()));
209         info.setEffectiveDate(getEffectiveDate());
210         info.setExpirationDate(getExpirationDate());
211         if (this.getMinValue() != null || this.getMaxValue() != null || this.getIncrement() != null) {
212             ResultValueRangeInfo resultValueRange = new ResultValueRangeInfo();
213             resultValueRange.setMaxValue(getMaxValue());
214             resultValueRange.setMinValue(getMinValue());
215             resultValueRange.setIncrement(getIncrement());
216             info.setResultValueRange(resultValueRange);
217         }
218         if (this.getAttributes() != null) {
219             for (ResultScaleAttributeEntity att : getAttributes()) {
220                 AttributeInfo attInfo = att.toDto();
221                 info.getAttributes().add(attInfo);
222             }
223         }
224         info.setMeta(super.toDTO());
225         return info;
226     }
227 }