View Javadoc

1   package org.kuali.student.enrollment.class2.courseofferingset.model;
2   
3   import org.kuali.student.enrollment.courseofferingset.dto.SocInfo;
4   import org.kuali.student.enrollment.courseofferingset.infc.Soc;
5   import org.kuali.student.r1.common.entity.KSEntityConstants;
6   import org.kuali.student.r2.common.dto.AttributeInfo;
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.infc.Attribute;
10  import org.kuali.student.r2.common.util.RichTextHelper;
11  import org.kuali.student.r2.common.util.constants.CourseOfferingSetServiceConstants;
12  import org.kuali.student.r2.common.util.date.DateFormatters;
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.Date;
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  @Entity
27  @Table(name = "KSEN_SOC")
28  @NamedQueries({
29          @NamedQuery(name="Soc.getSocsBySocTypeId", query="Select a from SocEntity a where a.socType =:socType"),
30          @NamedQuery(name="Soc.getSocIdsByType", query="Select a.id from SocEntity a where a.socType =:socType"),
31          @NamedQuery(name="Soc.getSocsByTerm", query="Select a from SocEntity a where a.termId =:termId"),
32          @NamedQuery(name="Soc.getSocIdsByTerm", query="SELECT soc.id FROM SocEntity soc WHERE soc.termId = :termId"),
33          @NamedQuery(name="Soc.getSocsByTermAndSubjectArea", query="Select a from SocEntity a where a.termId=:termId and a.subjectArea = :subjectArea"),
34          @NamedQuery(name="Soc.getSocIdsByTermAndSubjectArea", query="Select soc.id from SocEntity soc where soc.termId=:termId and soc.subjectArea = :subjectArea"),
35          @NamedQuery(name="Soc.getSocsByTermAndUnitsContentOwner", query="Select a from SocEntity a where a.termId=:termId and a.unitsContentOwnerId = :unitsContentOwnerId"),
36          @NamedQuery(name="Soc.getSocIdsByTermAndUnitsContentOwner", query="Select a.id from SocEntity a where a.termId=:termId and a.unitsContentOwnerId = :unitsContentOwnerId")
37  
38  })
39  public class SocEntity extends MetaEntity implements AttributeOwner<SocAttributeEntity> {
40  
41      @Column(name = "SOC_TYPE", nullable = false)
42      private String socType;
43      @Column(name = "SOC_STATE", nullable = false)
44      private String socState;
45      @Column(name = "NAME")
46      private String name;
47      @Column(name = "DESCR_FORMATTED", length = KSEntityConstants.EXTRA_LONG_TEXT_LENGTH)
48      private String formatted;
49      @Column(name = "DESCR_PLAIN", length = KSEntityConstants.EXTRA_LONG_TEXT_LENGTH)
50      private String plain;
51      @Column(name = "TERM_ID")
52      private String termId;
53      @Column(name = "SUBJECT_AREA")
54      private String subjectArea;
55      @Column(name = "UNITS_CONTENT_OWNER_ID")
56      private String unitsContentOwnerId;
57      @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner", fetch = FetchType.EAGER, orphanRemoval = true)
58      private final Set<SocAttributeEntity> attributes = new HashSet<SocAttributeEntity>();
59  
60      public SocEntity() {
61      }
62  
63      public SocEntity(Soc soc) {
64          super(soc);
65          this.setId(soc.getId());
66          this.setSocType(soc.getTypeKey());
67          this.setSocState(soc.getStateKey());
68          this.setTermId(soc.getTermId());
69          this.fromDTO(soc);
70      }
71  
72      public void fromDTO(Soc soc) {
73          this.setName(soc.getName());
74          if (soc.getDescr() != null) {
75              this.setDescrFormatted(soc.getDescr().getFormatted());
76              this.setDescrPlain(soc.getDescr().getPlain());
77          } else {
78              this.setDescrFormatted(null);
79              this.setDescrPlain(null);
80          }
81          this.setSubjectArea(soc.getSubjectArea());
82          this.setUnitsContentOwnerId(soc.getUnitsContentOwnerId()); // dynamic attributes
83          this.attributes.clear();
84          for (Attribute att : soc.getAttributes()) {
85              SocAttributeEntity attEntity = new SocAttributeEntity(att, this);
86              this.getAttributes().add(attEntity);
87          }
88      }
89  
90      public String getName() {
91          return name;
92      }
93  
94      public void setName(String name) {
95          this.name = name;
96      }
97  
98     
99      public String getSocType() {
100         return socType;
101     }
102 
103     public void setSocType(String socType) {
104         this.socType = socType;
105     }
106 
107     public String getSocState() {
108         return socState;
109     }
110 
111     public void setSocState(String socState) {
112         this.socState = socState;
113     }
114 
115     public void setAttributes(Set<SocAttributeEntity> attributes) {
116         this.attributes.clear();
117         if (attributes != null) {
118             this.attributes.addAll(attributes);
119         }
120     }
121 
122     public Set<SocAttributeEntity> getAttributes() {
123         return attributes;
124     }
125 
126     public SocInfo toDto() {
127         SocInfo soc = new SocInfo();
128         soc.setId(getId());
129         soc.setTypeKey(socType);
130         soc.setStateKey(socState);
131         soc.setName(name);
132         soc.setDescr(new RichTextHelper().toRichTextInfo(getDescrPlain(), getDescrFormatted()));
133         soc.setTermId(termId);
134         soc.setSubjectArea(subjectArea);
135         soc.setUnitsContentOwnerId(unitsContentOwnerId);
136         soc.setMeta(super.toDTO());
137         if (getAttributes() != null) {
138             for (SocAttributeEntity att : getAttributes()) {
139                 AttributeInfo attInfo = att.toDto();
140                 soc.getAttributes().add(attInfo);
141             }
142         }
143 
144         Date schedulingStarted = parseStateChangeDateString(soc, CourseOfferingSetServiceConstants.SOC_SCHEDULING_STATE_IN_PROGRESS);
145         Date schedulingCompleted = parseStateChangeDateString(soc, CourseOfferingSetServiceConstants.SOC_SCHEDULING_STATE_COMPLETED);
146         Date publishingStarted = parseStateChangeDateString(soc, CourseOfferingSetServiceConstants.PUBLISHING_SOC_STATE_KEY);
147         Date publishingCompleted = parseStateChangeDateString(soc, CourseOfferingSetServiceConstants.PUBLISHED_SOC_STATE_KEY);
148         String schedulingState;
149 
150         //
151         if(schedulingStarted == null) {
152             schedulingState = CourseOfferingSetServiceConstants.SOC_SCHEDULING_STATE_NOT_STARTED;
153         }
154         else {
155             soc.setLastSchedulingRunStarted(schedulingStarted);
156             if(schedulingCompleted == null) {
157                 schedulingState = CourseOfferingSetServiceConstants.SOC_SCHEDULING_STATE_IN_PROGRESS;
158             }
159             else {
160                 schedulingState = CourseOfferingSetServiceConstants.SOC_SCHEDULING_STATE_COMPLETED;
161                 soc.setLastSchedulingRunCompleted(schedulingCompleted);
162             }
163         }
164         soc.setSchedulingStateKey(schedulingState);
165 
166         if (publishingStarted != null) {
167             soc.setPublishingStarted(publishingStarted);
168         }
169 
170         if (publishingCompleted != null) {
171             soc.setPublishingCompleted(publishingCompleted);
172         }
173 
174         return soc;
175     }
176 
177     /**
178      *  Reads a date string from the given state key from a dynamic attribute and attempts to create a java.util.Date object
179      *  from it.
180      *  @return A java.util.Date if the date string is parsable. If the state key doesn't exist returns null.
181      *  Throws a RuntimeException if the key exists but the value is not parsable.
182      */
183     private Date parseStateChangeDateString(SocInfo soc, String stateKey) {
184         Date dateOut = null;
185         String value = soc.getAttributeValue(stateKey);
186         if (value != null) {
187             try {
188                 dateOut = DateFormatters.STATE_CHANGE_DATE_FORMATTER.parse(value);
189             } catch (IllegalArgumentException e) {
190                 throw new RuntimeException(String.format("Could not parse date string [%s] stored in SOC %s attribute %s.",
191                         value, soc.getId(), stateKey));
192             }
193         }
194         return dateOut;
195     }
196 
197     public String getDescrFormatted() {
198         return formatted;
199     }
200 
201     public void setDescrFormatted(String formatted) {
202         this.formatted = formatted;
203     }
204 
205     public String getDescrPlain() {
206         return plain;
207     }
208 
209     public void setDescrPlain(String plain) {
210         this.plain = plain;
211     }
212 
213     public String getSubjectArea() {
214         return subjectArea;
215     }
216 
217     public void setSubjectArea(String subjectArea) {
218         this.subjectArea = subjectArea;
219     }
220 
221     public String getTermId() {
222         return termId;
223     }
224 
225     public void setTermId(String termId) {
226         this.termId = termId;
227     }
228 
229     public String getUnitsContentOwnerId() {
230         return unitsContentOwnerId;
231     }
232 
233     public void setUnitsContentOwnerId(String unitsContentOwnerId) {
234         this.unitsContentOwnerId = unitsContentOwnerId;
235     }
236 }