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          super.fromDTO(soc);
74          
75          this.setName(soc.getName());
76          if (soc.getDescr() != null) {
77              this.setDescrFormatted(soc.getDescr().getFormatted());
78              this.setDescrPlain(soc.getDescr().getPlain());
79          } else {
80              this.setDescrFormatted(null);
81              this.setDescrPlain(null);
82          }
83          this.setSubjectArea(soc.getSubjectArea());
84          this.setUnitsContentOwnerId(soc.getUnitsContentOwnerId()); // dynamic attributes
85          this.attributes.clear();
86          for (Attribute att : soc.getAttributes()) {
87              SocAttributeEntity attEntity = new SocAttributeEntity(att, this);
88              this.getAttributes().add(attEntity);
89          }
90      }
91  
92      public String getName() {
93          return name;
94      }
95  
96      public void setName(String name) {
97          this.name = name;
98      }
99  
100    
101     public String getSocType() {
102         return socType;
103     }
104 
105     public void setSocType(String socType) {
106         this.socType = socType;
107     }
108 
109     public String getSocState() {
110         return socState;
111     }
112 
113     public void setSocState(String socState) {
114         this.socState = socState;
115     }
116 
117     public void setAttributes(Set<SocAttributeEntity> attributes) {
118         this.attributes.clear();
119         if (attributes != null) {
120             this.attributes.addAll(attributes);
121         }
122     }
123 
124     public Set<SocAttributeEntity> getAttributes() {
125         return attributes;
126     }
127 
128     public SocInfo toDto() {
129         SocInfo soc = new SocInfo();
130         soc.setId(getId());
131         soc.setTypeKey(socType);
132         soc.setStateKey(socState);
133         soc.setName(name);
134         soc.setDescr(new RichTextHelper().toRichTextInfo(getDescrPlain(), getDescrFormatted()));
135         soc.setTermId(termId);
136         soc.setSubjectArea(subjectArea);
137         soc.setUnitsContentOwnerId(unitsContentOwnerId);
138         soc.setMeta(super.toDTO());
139         if (getAttributes() != null) {
140             for (SocAttributeEntity att : getAttributes()) {
141                 AttributeInfo attInfo = att.toDto();
142                 soc.getAttributes().add(attInfo);
143             }
144         }
145 
146         Date schedulingStarted = parseStateChangeDateString(soc, CourseOfferingSetServiceConstants.SOC_SCHEDULING_STATE_IN_PROGRESS);
147         Date schedulingCompleted = parseStateChangeDateString(soc, CourseOfferingSetServiceConstants.SOC_SCHEDULING_STATE_COMPLETED);
148         Date publishingStarted = parseStateChangeDateString(soc, CourseOfferingSetServiceConstants.PUBLISHING_SOC_STATE_KEY);
149         Date publishingCompleted = parseStateChangeDateString(soc, CourseOfferingSetServiceConstants.PUBLISHED_SOC_STATE_KEY);
150         String schedulingState;
151 
152         //
153         if(schedulingStarted == null) {
154             schedulingState = CourseOfferingSetServiceConstants.SOC_SCHEDULING_STATE_NOT_STARTED;
155         }
156         else {
157             soc.setLastSchedulingRunStarted(schedulingStarted);
158             if(schedulingCompleted == null) {
159                 schedulingState = CourseOfferingSetServiceConstants.SOC_SCHEDULING_STATE_IN_PROGRESS;
160             }
161             else {
162                 schedulingState = CourseOfferingSetServiceConstants.SOC_SCHEDULING_STATE_COMPLETED;
163                 soc.setLastSchedulingRunCompleted(schedulingCompleted);
164             }
165         }
166         soc.setSchedulingStateKey(schedulingState);
167 
168         if (publishingStarted != null) {
169             soc.setPublishingStarted(publishingStarted);
170         }
171 
172         if (publishingCompleted != null) {
173             soc.setPublishingCompleted(publishingCompleted);
174         }
175 
176         return soc;
177     }
178 
179     /**
180      *  Reads a date string from the given state key from a dynamic attribute and attempts to create a java.util.Date object
181      *  from it.
182      *  @return A java.util.Date if the date string is parsable. If the state key doesn't exist returns null.
183      *  Throws a RuntimeException if the key exists but the value is not parsable.
184      */
185     private Date parseStateChangeDateString(SocInfo soc, String stateKey) {
186         Date dateOut = null;
187         String value = soc.getAttributeValue(stateKey);
188         if (value != null) {
189             try {
190                 dateOut = DateFormatters.SERVER_DATE_PARSER_FORMATTER.parse(value);
191             } catch (IllegalArgumentException e) {
192                 throw new RuntimeException(String.format("Could not parse date string [%s] stored in SOC %s attribute %s.",
193                         value, soc.getId(), stateKey));
194             }
195         }
196         return dateOut;
197     }
198 
199     public String getDescrFormatted() {
200         return formatted;
201     }
202 
203     public void setDescrFormatted(String formatted) {
204         this.formatted = formatted;
205     }
206 
207     public String getDescrPlain() {
208         return plain;
209     }
210 
211     public void setDescrPlain(String plain) {
212         this.plain = plain;
213     }
214 
215     public String getSubjectArea() {
216         return subjectArea;
217     }
218 
219     public void setSubjectArea(String subjectArea) {
220         this.subjectArea = subjectArea;
221     }
222 
223     public String getTermId() {
224         return termId;
225     }
226 
227     public void setTermId(String termId) {
228         this.termId = termId;
229     }
230 
231     public String getUnitsContentOwnerId() {
232         return unitsContentOwnerId;
233     }
234 
235     public void setUnitsContentOwnerId(String unitsContentOwnerId) {
236         this.unitsContentOwnerId = unitsContentOwnerId;
237     }
238 }