View Javadoc

1   package org.kuali.student.enrollment.class2.courseoffering.service.impl;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.log4j.Logger;
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.KRADConstants;
9   import org.kuali.rice.krad.web.form.MaintenanceForm;
10  import org.kuali.student.enrollment.class2.courseoffering.dto.CourseOfferingCreateWrapper;
11  import org.kuali.student.enrollment.class2.courseoffering.service.CourseOfferingMaintainable;
12  import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
13  import org.kuali.student.enrollment.courseoffering.dto.FormatOfferingInfo;
14  import org.kuali.student.r2.common.class1.type.dto.TypeInfo;
15  import org.kuali.student.r2.common.dto.ContextInfo;
16  import org.kuali.student.r2.common.util.ContextUtils;
17  import org.kuali.student.r2.common.util.constants.CourseOfferingServiceConstants;
18  import org.kuali.student.r2.common.util.constants.LuiServiceConstants;
19  import org.kuali.student.r2.lum.course.dto.ActivityInfo;
20  import org.kuali.student.r2.lum.course.dto.CourseInfo;
21  import org.kuali.student.r2.lum.course.dto.FormatInfo;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Map;
26  
27  public class CourseOfferingCreateMaintainableImpl extends CourseOfferingMaintainableImpl implements CourseOfferingMaintainable {
28  
29      private static final Logger LOG = org.apache.log4j.Logger.getLogger(CourseOfferingCreateMaintainableImpl.class);
30  
31      @Override
32      public void saveDataObject() {
33          if(getMaintenanceAction().equals(KRADConstants.MAINTENANCE_NEW_ACTION) ||
34                  getMaintenanceAction().equals(KRADConstants.MAINTENANCE_COPY_ACTION)) {
35              try {
36                  CourseOfferingCreateWrapper wrapper = (CourseOfferingCreateWrapper)getDataObject();
37                  CourseOfferingInfo courseOffering = new CourseOfferingInfo();
38                  List<String> optionKeys = new ArrayList<String>();
39  
40                  CourseInfo courseInfo = wrapper.getCourse();
41                  courseOffering.setTermId(wrapper.getTerm().getId());
42                  courseOffering.setCourseOfferingTitle(courseInfo.getCourseTitle());
43  //                  courseOffering.setCreditOptionId();
44  
45                  // if the course offering wrapper suffix is set, set the value in the CourseOfferingInfo
46                  if (!StringUtils.isEmpty(wrapper.getCourseOfferingSuffix())) {
47                      courseOffering.setCourseOfferingCode(wrapper.getCourseOfferingSuffix());
48                      courseOffering.setCourseNumberSuffix(wrapper.getCourseOfferingSuffix());
49                      optionKeys.add(CourseOfferingServiceConstants.APPEND_COURSE_OFFERING_CODE_SUFFIX_OPTION_KEY);
50                  }
51                  courseOffering.setCourseId(courseInfo.getId());
52                  courseOffering.setCourseCode(courseInfo.getCode());
53                  courseOffering.setTypeKey(LuiServiceConstants.COURSE_OFFERING_TYPE_KEY);
54                  courseOffering.setStateKey(LuiServiceConstants.LUI_CO_STATE_DRAFT_KEY);
55  
56                  CourseOfferingInfo info = getCourseOfferingService().createCourseOffering(courseInfo.getId(), wrapper.getTerm().getId(), LuiServiceConstants.COURSE_OFFERING_TYPE_KEY, courseOffering, optionKeys, ContextUtils.createDefaultContextInfo());
57                  wrapper.setCoInfo(info);
58                  createFormatOffering(wrapper);
59                  //FIXEM:create formatoffering relation
60              } catch (Exception e) {
61                  throw new RuntimeException(e);
62              }
63          }
64      }
65  
66      private void createFormatOffering(CourseOfferingCreateWrapper wrapper){
67          List<FormatOfferingInfo> updatedFOs = new ArrayList<FormatOfferingInfo>();
68          ContextInfo contextInfo = ContextUtils.createDefaultContextInfo();
69          for (FormatOfferingInfo formatOfferingInfo : wrapper.getFormatOfferingList()) {
70              formatOfferingInfo.setStateKey(LuiServiceConstants.LUI_FO_STATE_PLANNED_KEY);
71              formatOfferingInfo.setTypeKey(LuiServiceConstants.FORMAT_OFFERING_TYPE_KEY);
72              formatOfferingInfo.setTermId(wrapper.getCoInfo().getTermId());
73              formatOfferingInfo.setCourseOfferingId(wrapper.getCoInfo().getId());
74              try {
75                  FormatOfferingInfo createdFormatOffering = getCourseOfferingService().createFormatOffering(wrapper.getCoInfo().getId(), formatOfferingInfo.getFormatId(), formatOfferingInfo.getTypeKey(), formatOfferingInfo, contextInfo);
76                  updatedFOs.add(createdFormatOffering);
77              } catch (Exception e) {
78                  throw new RuntimeException(e);
79              }
80          }
81          wrapper.setFormatOfferingList(updatedFOs);
82      }
83  
84      @Override
85      protected void processBeforeAddLine(View view, CollectionGroup collectionGroup, Object model, Object addLine) {
86          if (addLine instanceof FormatOfferingInfo) {
87              FormatOfferingInfo formatOfferingInfo = (FormatOfferingInfo) addLine;
88              CourseOfferingCreateWrapper coCreateWrapper = (CourseOfferingCreateWrapper) ((MaintenanceForm) model).getDocument().getNewMaintainableObject().getDataObject();
89              ContextInfo contextInfo = ContextUtils.createDefaultContextInfo();
90              for (FormatInfo formatInfo : coCreateWrapper.getCourse().getFormats()) {
91                  if (StringUtils.equals(formatInfo.getId(), formatOfferingInfo.getFormatId())) {
92                      // TODO: fix R2 Format to include name and short name
93  //                    formatOfferingInfo.setName("FIX ME!");
94  //                    formatOfferingInfo.setShortName("FIX ME!");
95                      //Bonnie: this is only a temporary walk-around solution.
96                      //Still need to address the issue that FormatInfo does not include name and short name
97                      try {
98                          List<ActivityInfo> activityInfos = formatInfo.getActivities();
99                          StringBuffer st = new StringBuffer();
100                         for (ActivityInfo activityInfo : activityInfos) {
101                             TypeInfo activityType = getTypeService().getType(activityInfo.getTypeKey(), contextInfo);
102                             st.append(activityType.getName() + "/");
103                         }
104                         String name = st.toString();
105                         name = name.substring(0, name.length() - 1);
106                         formatOfferingInfo.setName(name);
107                         formatOfferingInfo.setShortName(name);
108                     } catch (Exception e) {
109                         //This was just swallowing the exception so a log entry was added
110                         LOG.error("An exception was thrown!", e);
111                     }
112 
113                 }
114             }
115         }
116     }
117 
118     @Override
119     public void processAfterNew(MaintenanceDocument document, Map<String, String[]> requestParameters) {
120         document.getDocumentHeader().setDocumentDescription("Course Offering");
121         if (requestParameters.get("targetTermCode") != null && requestParameters.get("targetTermCode").length != 0){
122             ((CourseOfferingCreateWrapper)document.getNewMaintainableObject().getDataObject()).setTargetTermCode(requestParameters.get("targetTermCode")[0]);
123         }
124     }
125 
126 
127 
128 }