001    package org.kuali.student.r2.lum.lrc.model;
002    
003    import java.math.BigDecimal;
004    import java.util.ArrayList;
005    import org.kuali.student.r1.common.entity.KSEntityConstants;
006    import org.kuali.student.r2.common.dto.AttributeInfo;
007    import org.kuali.student.r2.common.entity.AttributeOwner;
008    import org.kuali.student.r2.common.entity.MetaEntity;
009    import org.kuali.student.r2.common.infc.Attribute;
010    import org.kuali.student.r2.lum.lrc.dto.ResultValueInfo;
011    
012    import javax.persistence.CascadeType;
013    import javax.persistence.Column;
014    import javax.persistence.Entity;
015    import javax.persistence.FetchType;
016    import javax.persistence.OneToMany;
017    import javax.persistence.Table;
018    import javax.persistence.Temporal;
019    import javax.persistence.TemporalType;
020    import java.util.Date;
021    import java.util.HashSet;
022    import java.util.Set;
023    import javax.persistence.EntityManager;
024    import javax.persistence.NamedQueries;
025    import javax.persistence.NamedQuery;
026    import org.kuali.student.r2.common.util.RichTextHelper;
027    
028    @Entity
029    @Table(name = "KSEN_LRC_RESULT_VALUE")
030    @NamedQueries({
031        @NamedQuery(name = "ResultValueEntity.getIdsByType",
032        query = "select id from ResultValueEntity where type = :type"),
033        @NamedQuery(name = "ResultValueEntity.getByScale",
034        query = "select RV from ResultValueEntity RV where RV.resultScaleId = :resultScaleKey"),
035        @NamedQuery(name = "ResultValueEntity.getByScaleAndValue",
036        query = "select RV from ResultValueEntity RV where RV.resultScaleId = :resultScaleKey and RV.value = :value")
037    })
038    public class ResultValueEntity extends MetaEntity implements AttributeOwner<ResultValueAttributeEntity> {
039    
040        @Column(name = "RESULT_VALUE_TYPE")
041        private String type;
042        @Column(name = "RESULT_VALUE_STATE")
043        private String state;
044        @Column(name = "NAME")
045        private String name;
046        @Column(name = "DESCR_PLAIN", length = KSEntityConstants.EXTRA_LONG_TEXT_LENGTH)
047        private String descrPlain;
048        @Column(name = "DESCR_FORMATTED", length = KSEntityConstants.EXTRA_LONG_TEXT_LENGTH)
049        private String descrFormatted;
050        @Column(name = "RESULT_SCALE_ID")
051        private String resultScaleId;
052        @Column(name = "NUMERIC_VALUE")
053        private BigDecimal numericValue;
054        @Column(name = "RESULT_VALUE")
055        private String value;
056        @Temporal(TemporalType.TIMESTAMP)
057        @Column(name = "EFF_DT")
058        private Date effectiveDate;
059        @Temporal(TemporalType.TIMESTAMP)
060        @Column(name = "EXPIR_DT")
061        private Date expirationDate;
062        @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner", fetch = FetchType.EAGER)
063        private Set<ResultValueAttributeEntity> attributes;
064    
065        public ResultValueEntity() {
066        }
067    
068        public ResultValueEntity(ResultValueInfo dto, EntityManager em) {
069            super(dto);
070            this.setId(dto.getKey());
071            this.setType(dto.getTypeKey());
072            setResultScaleId(dto.getResultScaleKey());
073            this.fromDTO(dto, em);
074        }
075    
076        public void fromDTO(ResultValueInfo dto, EntityManager em) {
077            this.setName(dto.getName());
078            this.setState(dto.getStateKey());
079            if (dto.getDescr() != null) {
080                this.setDescrFormatted(dto.getDescr().getFormatted());
081                this.setDescrPlain(dto.getDescr().getPlain());
082            } else {
083                this.setDescrFormatted(null);
084                this.setDescrPlain(null);
085            }
086            setEffectiveDate(dto.getEffectiveDate());
087            setExpirationDate(dto.getExpirationDate());
088            if(dto.getNumericValue() != null){
089                setNumericValue(new BigDecimal(dto.getNumericValue()));
090            }
091            else
092            {
093                setNumericValue (null);
094            }
095            setValue(dto.getValue()); 
096            // dynamic attributes
097            if (this.getAttributes() == null) {
098                this.setAttributes(new HashSet<ResultValueAttributeEntity>());
099            }
100            Set<String> idSet = new HashSet<String>(dto.getAttributes().size());
101            for (AttributeInfo attr : dto.getAttributes()) {
102                if (attr.getId() != null) {
103                    idSet.add(attr.getId());
104                }
105            }
106            for (ResultValueAttributeEntity attEntity : new ArrayList<ResultValueAttributeEntity> (this.getAttributes())) {
107                if (!idSet.contains(attEntity.getId())) {
108                    em.remove(attEntity);
109                    this.getAttributes().remove(attEntity);
110                }
111            }
112            for (Attribute att : dto.getAttributes()) {
113                ResultValueAttributeEntity attEntity = new ResultValueAttributeEntity(att, this);
114                this.getAttributes().add(attEntity);
115            }
116        }
117    
118        public String getResultScaleId() {
119            return resultScaleId;
120        }
121    
122        public void setResultScaleId(String resultScaleId) {
123            this.resultScaleId = resultScaleId;
124        }
125    
126        public BigDecimal getNumericValue() {
127            return numericValue;
128        }
129    
130        public void setNumericValue(BigDecimal numericValue) {
131            this.numericValue = numericValue;
132        }
133    
134        public String getValue() {
135            return value;
136        }
137    
138        public void setValue(String value) {
139            this.value = value;
140        }
141    
142        public Date getEffectiveDate() {
143            return effectiveDate;
144        }
145    
146        public void setEffectiveDate(Date effectiveDate) {
147            this.effectiveDate = effectiveDate;
148        }
149    
150        public Date getExpirationDate() {
151            return expirationDate;
152        }
153    
154        public void setExpirationDate(Date expirationDate) {
155            this.expirationDate = expirationDate;
156        }
157    
158        public String getName() {
159            return name;
160        }
161    
162        public void setName(String name) {
163            this.name = name;
164        }
165    
166        public String getDescrFormatted() {
167            return descrFormatted;
168        }
169    
170        public void setDescrFormatted(String descrFormatted) {
171            this.descrFormatted = descrFormatted;
172        }
173    
174        public String getDescrPlain() {
175            return descrPlain;
176        }
177    
178        public void setDescrPlain(String descrPlain) {
179            this.descrPlain = descrPlain;
180        }
181    
182        public String getType() {
183            return type;
184        }
185    
186        public void setType(String type) {
187            this.type = type;
188        }
189    
190        public String getState() {
191            return state;
192        }
193    
194        public void setState(String state) {
195            this.state = state;
196        }
197    
198        @Override
199        public void setAttributes(Set<ResultValueAttributeEntity> attributes) {
200            this.attributes = attributes;
201        }
202    
203        @Override
204        public Set<ResultValueAttributeEntity> getAttributes() {
205            return attributes;
206        }
207    
208        public ResultValueInfo toDto() {
209            ResultValueInfo info = new ResultValueInfo();
210            info.setKey(getId());
211            info.setTypeKey(getType());
212            info.setStateKey(getState());
213            info.setName(getName());
214            info.setDescr(new RichTextHelper().toRichTextInfo(getDescrPlain(), getDescrFormatted()));
215            info.setResultScaleKey(getResultScaleId());
216            info.setNumericValue(String.valueOf(getNumericValue()));
217            info.setValue(getValue());
218            info.setEffectiveDate(getEffectiveDate());
219            info.setExpirationDate(getExpirationDate());
220            if (this.getAttributes() != null) {
221                for (ResultValueAttributeEntity att : getAttributes()) {
222                    AttributeInfo attInfo = att.toDto();
223                    info.getAttributes().add(attInfo);
224                }
225            }
226            info.setMeta(super.toDTO());
227            return info;
228        }
229    }