View Javadoc

1   package org.kuali.student.enrollment.class2.courseoffering.service.impl;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.rice.krad.maintenance.MaintainableImpl;
5   import org.kuali.rice.krad.maintenance.MaintenanceDocument;
6   import org.kuali.rice.krad.uif.container.CollectionGroup;
7   import org.kuali.rice.krad.uif.view.View;
8   import org.kuali.rice.krad.util.GlobalVariables;
9   import org.kuali.rice.krad.util.KRADConstants;
10  import org.kuali.rice.krad.web.form.MaintenanceForm;
11  import org.kuali.student.enrollment.class2.courseoffering.dto.CourseOfferingCreateWrapper;
12  import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingResourceLoader;
13  import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
14  import org.kuali.student.enrollment.courseoffering.dto.FormatOfferingInfo;
15  import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
16  import org.kuali.student.r2.core.class1.type.dto.TypeInfo;
17  import org.kuali.student.r2.lum.course.dto.ActivityInfo;
18  import org.kuali.student.r2.lum.course.dto.CourseInfo;
19  import org.kuali.student.r2.lum.course.dto.FormatInfo;
20  import org.kuali.student.r2.lum.course.service.CourseService;
21  import org.kuali.student.r2.common.dto.ContextInfo;
22  import org.kuali.student.r2.common.dto.LocaleInfo;
23  import org.kuali.student.r2.common.util.constants.CourseOfferingServiceConstants;
24  import org.kuali.student.r2.common.util.constants.LuiServiceConstants;
25  import org.kuali.student.r2.core.class1.state.service.StateService;
26  import org.kuali.student.r2.core.class1.type.service.TypeService;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  import java.util.Locale;
31  import java.util.Map;
32  
33  public class CourseOfferingCreateMaintainableImpl extends MaintainableImpl {
34  
35      private transient CourseOfferingService courseOfferingService;
36      private ContextInfo contextInfo;
37      private transient TypeService typeService;
38      private transient StateService stateService;
39      private transient CourseService courseService;
40  
41      @Override
42      public void saveDataObject() {
43          if(getMaintenanceAction().equals(KRADConstants.MAINTENANCE_NEW_ACTION) ||
44                  getMaintenanceAction().equals(KRADConstants.MAINTENANCE_COPY_ACTION)) {
45              try {
46                  CourseOfferingCreateWrapper wrapper = (CourseOfferingCreateWrapper)getDataObject();
47                  CourseOfferingInfo courseOffering = new CourseOfferingInfo();
48                  List<String> optionKeys = new ArrayList<String>();
49  
50                  CourseInfo courseInfo = wrapper.getCourse();
51                  courseOffering.setTermId(wrapper.getTerm().getId());
52                  courseOffering.setCourseOfferingTitle(courseInfo.getCourseTitle());
53  //                  courseOffering.setCreditOptionId();
54  
55                  // if the course offering wrapper suffix is set, set the value in the CourseOfferingInfo
56                  if (!StringUtils.isEmpty(wrapper.getCourseOfferingSuffix())) {
57                      courseOffering.setCourseOfferingCode(wrapper.getCourseOfferingSuffix());
58                      courseOffering.setCourseNumberSuffix(wrapper.getCourseOfferingSuffix());
59                      optionKeys.add(CourseOfferingServiceConstants.APPEND_COURSE_OFFERING_CODE_SUFFIX_OPTION_KEY);
60                  }
61                  courseOffering.setCourseId(courseInfo.getId());
62                  courseOffering.setCourseCode(courseInfo.getCode());
63                  courseOffering.setTypeKey(LuiServiceConstants.COURSE_OFFERING_TYPE_KEY);
64                  courseOffering.setStateKey(LuiServiceConstants.LUI_CO_STATE_DRAFT_KEY);
65  
66                  CourseOfferingInfo info = getCourseOfferingService().createCourseOffering(courseInfo.getId(), wrapper.getTerm().getId(), LuiServiceConstants.COURSE_OFFERING_TYPE_KEY, courseOffering, optionKeys, getContextInfo());
67                  wrapper.setCoInfo(info);
68                  createFormatOffering(wrapper);
69                  //FIXEM:create formatoffering relation
70              } catch (Exception e) {
71                  throw new RuntimeException(e);
72              }
73          }
74      }
75  
76      private void createFormatOffering(CourseOfferingCreateWrapper wrapper){
77          List<FormatOfferingInfo> updatedFOs = new ArrayList();
78          for (FormatOfferingInfo formatOfferingInfo : wrapper.getFormatOfferingList()) {
79              formatOfferingInfo.setStateKey(LuiServiceConstants.LUI_FO_STATE_PLANNED_KEY);
80              formatOfferingInfo.setTypeKey(LuiServiceConstants.FORMAT_OFFERING_TYPE_KEY);
81              formatOfferingInfo.setTermId(wrapper.getCoInfo().getTermId());
82              formatOfferingInfo.setCourseOfferingId(wrapper.getCoInfo().getId());
83              try {
84                  FormatOfferingInfo createdFormatOffering = getCourseOfferingService().createFormatOffering(wrapper.getCoInfo().getId(), formatOfferingInfo.getFormatId(), formatOfferingInfo.getTypeKey(), formatOfferingInfo, getContextInfo());
85                  updatedFOs.add(createdFormatOffering);
86              } catch (Exception e) {
87                  throw new RuntimeException(e);
88              }
89          }
90          wrapper.setFormatOfferingList(updatedFOs);
91      }
92  
93      @Override
94      protected void processBeforeAddLine(View view, CollectionGroup collectionGroup, Object model, Object addLine) {
95          if (addLine instanceof FormatOfferingInfo){
96            FormatOfferingInfo formatOfferingInfo = (FormatOfferingInfo)addLine;
97              CourseOfferingCreateWrapper coCreateWrapper = (CourseOfferingCreateWrapper)((MaintenanceForm)model).getDocument().getNewMaintainableObject().getDataObject();
98              for( FormatInfo formatInfo : coCreateWrapper.getCourse().getFormats()){
99                  if (StringUtils.equals(formatInfo.getId(), formatOfferingInfo.getFormatId())){
100                     // TODO: fix R2 Format to include name and short name
101 //                    formatOfferingInfo.setName("FIX ME!");
102 //                    formatOfferingInfo.setShortName("FIX ME!");
103                     //Bonnie: this is only a temporary walk-around solution.
104                     //Still need to address the issue that FormatInfo does not include name and short name
105                     try{
106                         List<ActivityInfo> activityInfos = formatInfo.getActivities();
107                         StringBuffer st = new StringBuffer();
108                         for (ActivityInfo activityInfo : activityInfos) {
109                             TypeInfo activityType = getTypeService().getType(activityInfo.getTypeKey(), getContextInfo());
110                             st.append(activityType.getName()+"/");
111                         }
112                         String name =st.toString();
113                         name=name.substring(0,name.length()-1);
114                         formatOfferingInfo.setName(name);
115                         formatOfferingInfo.setShortName(name);
116                     } catch(Exception e) {
117                         //need to log error
118                     }
119 
120                 }
121             }
122         }
123     }
124 
125     @Override
126     public void processAfterNew(MaintenanceDocument document, Map<String, String[]> requestParameters) {
127         document.getDocumentHeader().setDocumentDescription("Course Offering");
128         if (requestParameters.get("targetTermCode") != null && requestParameters.get("targetTermCode").length != 0){
129             ((CourseOfferingCreateWrapper)document.getNewMaintainableObject().getDataObject()).setTargetTermCode(requestParameters.get("targetTermCode")[0]);
130         }
131     }
132 
133     public ContextInfo getContextInfo() {
134         if (null == contextInfo) {
135             contextInfo = new ContextInfo();
136             contextInfo.setAuthenticatedPrincipalId(GlobalVariables.getUserSession().getPrincipalId());
137             contextInfo.setPrincipalId(GlobalVariables.getUserSession().getPrincipalId());
138             LocaleInfo localeInfo = new LocaleInfo();
139             localeInfo.setLocaleLanguage(Locale.getDefault().getLanguage());
140             localeInfo.setLocaleRegion(Locale.getDefault().getCountry());
141             contextInfo.setLocale(localeInfo);
142         }
143         return contextInfo;
144     }
145 
146     public TypeService getTypeService() {
147         if(typeService == null) {
148             typeService = CourseOfferingResourceLoader.loadTypeService();
149         }
150         return this.typeService;
151     }
152 
153     public StateService getStateService() {
154         if(stateService == null) {
155             stateService = CourseOfferingResourceLoader.loadStateService();
156         }
157         return stateService;
158     }
159 
160     protected CourseOfferingService getCourseOfferingService() {
161         if (courseOfferingService == null) {
162             courseOfferingService = CourseOfferingResourceLoader.loadCourseOfferingService();
163         }
164         return courseOfferingService;
165     }
166 
167     protected CourseService getCourseService() {
168         if(courseService == null) {
169             courseService = CourseOfferingResourceLoader.loadCourseService();
170         }
171         return this.courseService;
172     }
173 
174 }