Coverage Report - org.kuali.student.enrollment.class2.courseoffering.service.CourseOfferingInfoMaintainableImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
CourseOfferingInfoMaintainableImpl
0%
0/170
0%
0/28
5.556
 
 1  
 package org.kuali.student.enrollment.class2.courseoffering.service;
 2  
 
 3  
 import org.apache.commons.lang.StringUtils;
 4  
 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
 5  
 import org.kuali.rice.krad.document.MaintenanceDocument;
 6  
 import org.kuali.rice.krad.maintenance.MaintainableImpl;
 7  
 import org.kuali.rice.krad.util.KRADConstants;
 8  
 import org.kuali.student.enrollment.courseoffering.dto.ActivityOfferingInfo;
 9  
 import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
 10  
 import org.kuali.student.enrollment.courseoffering.dto.OfferingInstructorInfo;
 11  
 import org.kuali.student.enrollment.courseoffering.dto.RegistrationGroupInfo;
 12  
 import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
 13  
 import org.kuali.student.lum.course.dto.ActivityInfo;
 14  
 import org.kuali.student.lum.course.dto.CourseInfo;
 15  
 import org.kuali.student.lum.course.dto.FormatInfo;
 16  
 import org.kuali.student.lum.course.service.CourseService;
 17  
 import org.kuali.student.lum.course.service.CourseServiceConstants;
 18  
 import org.kuali.student.r2.common.dto.ContextInfo;
 19  
 import org.kuali.student.r2.common.dto.MeetingScheduleInfo;
 20  
 import org.kuali.student.r2.core.type.dto.TypeInfo;
 21  
 import org.kuali.student.r2.common.exceptions.*;
 22  
 import org.kuali.student.r2.common.util.constants.CourseOfferingServiceConstants;
 23  
 import org.kuali.student.r2.common.util.constants.LrcServiceConstants;
 24  
 import org.kuali.student.r2.common.util.constants.LuiPersonRelationServiceConstants;
 25  
 import org.kuali.student.r2.common.util.constants.LuiServiceConstants;
 26  
 
 27  
 import javax.xml.namespace.QName;
 28  
 import java.util.ArrayList;
 29  
 import java.util.List;
 30  
 import java.util.Map;
 31  
 import java.util.Random;
 32  
 
 33  
 
 34  0
 public class CourseOfferingInfoMaintainableImpl extends MaintainableImpl {
 35  
     private static final long serialVersionUID = 1L;
 36  
     private static final String DEFAULT_DOCUMENT_DESC_FOR_CREATING_COURSE_OFFERING =
 37  
                                                             "Create a new course offering";
 38  
     private static final String DEFAULT_DOCUMENT_DESC_FOR_EDITING_COURSE_OFFERING =
 39  
                                                             "Edit an existing course offering";
 40  
     private static final String DEFAULT_DOCUMENT_DESC_FOR_COPYING_COURSE_OFFERING =
 41  
                                                             "Copy from an existing course offering to create a new one";
 42  
     private transient CourseService courseService;
 43  
     private transient CourseOfferingService courseOfferingService;
 44  
 
 45  
     // TODO - all exception handling in this method needs to 'manually' roll back what has been
 46  
     // changed in the database before the exception was caught
 47  
     @Override
 48  
     public void saveDataObject() {
 49  0
         CourseOfferingInfo courseOfferingInfo = (CourseOfferingInfo) getDataObject();
 50  
 //        System.out.println(">>>>> in CourseOfferingInfoMaintainableImpl.saveDataObject method");
 51  
 
 52  
         //get termId from the user input through UI
 53  0
         String termId = courseOfferingInfo.getTermId();
 54  
         //get courseId from courseOfferingInfo, which is retrieved based on course Code that the user input through UI
 55  0
         String courseId = courseOfferingInfo.getCourseId();
 56  
 
 57  0
         CourseInfo course = null;
 58  
         try {
 59  0
             course = getCourseService().getCourse(courseId);
 60  0
         } catch (org.kuali.student.common.exceptions.OperationFailedException ofe) {
 61  0
             System.out.println("call getCourseService().getCourse(courseId), and get OperationFailedException:  " + ofe.toString());
 62  0
         } catch (org.kuali.student.common.exceptions.DoesNotExistException dnee) {
 63  0
             System.out.println("call getCourseService().getCourse(courseId), and get DoesNotExistException:  " + dnee.toString());
 64  0
         } catch (org.kuali.student.common.exceptions.InvalidParameterException ipe) {
 65  0
             System.out.println("call getCourseService().getCourse(courseId), and get InvalidParameterException:  " + ipe.toString());
 66  0
         } catch (org.kuali.student.common.exceptions.PermissionDeniedException pde) {
 67  0
             System.out.println("call getCourseService().getCourse(courseId), and get PermissionDeniedException:  " + pde.toString());
 68  0
         } catch (org.kuali.student.common.exceptions.MissingParameterException mpe) {
 69  0
             System.out.println("call getCourseService().getCourse(courseId), and get MissingParameterException:  " + mpe.toString());
 70  0
         }
 71  
         // TODO - this entire method needs more complete exception handling; then remove this
 72  0
         if (null == course) return;
 73  
 
 74  
         //form the formatIds
 75  0
         List<String> formatIds = new ArrayList<String>();
 76  
         /*
 77  
                             List<FormatInfo> formatList = course.getFormats();
 78  
                             for (FormatInfo format : formatList){
 79  
                                 formatIds.add(format.getId());
 80  
                             }
 81  
             */
 82  0
         FormatInfo firstFormat = null;
 83  
         // only pick the first format based on Larry's suggestion to simplify core slice development
 84  0
         if (course != null) {
 85  0
             firstFormat = course.getFormats().get(0);
 86  0
             formatIds.add(firstFormat.getId());
 87  
         }
 88  
 
 89  0
         CourseOfferingInfo coi = new CourseOfferingInfo ();
 90  0
         coi.setTypeKey(LuiServiceConstants.COURSE_OFFERING_TYPE_KEY);
 91  0
         coi.setStateKey(LuiServiceConstants.LUI_DRAFT_STATE_KEY);
 92  
         try {
 93  
             //create a CourseOfferingInfo coi
 94  0
             coi = getCourseOfferingService().createCourseOffering(courseId, 
 95  
                     termId, 
 96  
                     coi.getTypeKey(), 
 97  
                     coi, 
 98  
                     new ContextInfo());
 99  0
         } catch (Exception ex) {
 100  0
           throw new RuntimeException (ex);
 101  0
         }
 102  
 
 103  
         //If grading options not present in course, set a default one in CO
 104  0
         if (coi.getGradingOptionKeys() == null || coi.getGradingOptionKeys().isEmpty()){
 105  0
             List<String> gradingOptions = new ArrayList();
 106  0
             gradingOptions.add(LrcServiceConstants.RESULT_SCALE_TYPE_KEY_GRADE);
 107  0
             coi.setGradingOptionKeys(gradingOptions);
 108  
         }
 109  
 
 110  
         //create a list of instructors
 111  0
         List<OfferingInstructorInfo> instructors = courseOfferingInfo.getInstructors();
 112  
 
 113  
         //set the list of instructors to the CourseOfferingInfo coi
 114  0
         if (coi != null) {
 115  0
             coi.setInstructors(instructors);
 116  0
             coi.setStateKey(LuiServiceConstants.LUI_OFFERED_STATE_KEY);
 117  0
             coi.setMaximumEnrollment(courseOfferingInfo.getMaximumEnrollment());
 118  0
             coi.setExpenditure(null);
 119  
 
 120  
             //update the CourseOfferingInfo coi in DB with instructors info
 121  
             try {
 122  0
                 getCourseOfferingService().updateCourseOffering(coi.getId(), coi, new ContextInfo());
 123  0
             } catch (OperationFailedException ofe) {
 124  0
                 System.out.println("call courseOfferingService.updateCourseOffering() method, and get OperationFailedException:  " + ofe.toString());
 125  0
             } catch (InvalidParameterException ipe) {
 126  0
                 System.out.println("call courseOfferingService.updateCourseOffering() method, and get InvalidParameterException:  " + ipe.toString());
 127  0
             } catch (DoesNotExistException dnee) {
 128  0
                 System.out.println("call courseOfferingService.updateCourseOffering() method, and get DoesNotExistException:  " + dnee.toString());
 129  0
             } catch (PermissionDeniedException pde) {
 130  0
                 System.out.println("call courseOfferingService.updateCourseOffering() method, and get PermissionDeniedException:  " + pde.toString());
 131  0
             } catch (MissingParameterException mpe) {
 132  0
                 System.out.println("call courseOfferingService.updateCourseOffering() method, and get MissingParameterException:  " + mpe.toString());
 133  0
             } catch (VersionMismatchException vme) {
 134  0
                 System.out.println("call courseOfferingService.updateCourseOffering() method, and get VersionMismatchException:  " + vme.toString());
 135  0
             } catch (DataValidationErrorException dvee) {
 136  0
                 System.out.println("call courseOfferingService.updateCourseOffering() method, and get DataValidationErrorException:  " + dvee.toString());
 137  0
             }
 138  
         }
 139  
 
 140  
         //create a list of ActivityOfferingInfo based on activities defined in the format of the course
 141  0
         List<String> courseOfferingIds = new ArrayList<String>();
 142  0
         courseOfferingIds.add(coi.getId());
 143  
 
 144  0
         List<ActivityOfferingInfo> activityOfferingInfoList = new ArrayList<ActivityOfferingInfo>();
 145  0
         List<String> activityOfferingIds = new ArrayList<String>();
 146  0
         if (firstFormat != null) {
 147  0
             List<ActivityInfo> activities = firstFormat.getActivities();
 148  0
             for (ActivityInfo activity : activities) {
 149  0
                 ActivityOfferingInfo activityOfferingInfo = new ActivityOfferingInfo();
 150  0
                 activityOfferingInfo.setInstructors(instructors);
 151  
                 //It looks like termId and activityId are required fields to create an ActivityOfferingInfo data entry
 152  0
                 activityOfferingInfo.setTermId(termId);
 153  0
                 activityOfferingInfo.setActivityId(activity.getId());
 154  
                 try {
 155  0
                     List<TypeInfo> activityOfferingTypes = getCourseOfferingService().getActivityOfferingTypesForActivityType(activity.getActivityType(), new ContextInfo());
 156  0
                     if (activityOfferingTypes.size() > 1) {
 157  0
                         System.out.println(">>for core slice, it should be 1-to-1 mapping. so only take the first one -- " + activityOfferingTypes.get(0).getKey());
 158  
                     }
 159  
 
 160  
                     //for Core Slice -- if the mapping between Canonical Activity to Activity Offering is not 1-to-1,
 161  
                     //(see https://wiki.kuali.org/display/STUDENT/Learning+Unit+Instance+Types+and+States#LearningUnitInstanceTypesandStates-Types)
 162  
                     //only take the first one.
 163  0
                     activityOfferingInfo.setTypeKey(activityOfferingTypes.get(0).getKey());
 164  0
                     activityOfferingInfo.setStateKey(LuiServiceConstants.LUI_OFFERED_STATE_KEY);
 165  
                      //TODO remove this fake generation when we are getting real times from the form
 166  
                     // TODO: fix this to set the schedule id from the schedule service
 167  0
                     String scheduleId = null;
 168  0
                     activityOfferingInfo.setScheduleId(scheduleId);
 169  
 //                    activityOfferingInfo.setMeetingSchedules(generateFakeMeetingTimes());
 170  0
                     activityOfferingInfo = getCourseOfferingService().createActivityOffering
 171  
                             (coi.getId (),
 172  
                             activityOfferingInfo.getTypeKey(),
 173  
                             activityOfferingInfo, 
 174  
                             new ContextInfo());
 175  
 
 176  0
                     activityOfferingInfoList.add(activityOfferingInfo);
 177  0
                     activityOfferingIds.add(activityOfferingInfo.getId());
 178  
 
 179  
                     //create a RegiistrationGroup after successfully create all activityOfferingInfos
 180  0
                     RegistrationGroupInfo registrationGroupInfo = new RegistrationGroupInfo();
 181  0
                     registrationGroupInfo.setCourseOfferingId(coi.getId());
 182  0
                     registrationGroupInfo.setMaximumEnrollment(courseOfferingInfo.getMaximumEnrollment());
 183  0
                     registrationGroupInfo.setActivityOfferingIds(activityOfferingIds);
 184  0
                     registrationGroupInfo.setStateKey(LuiServiceConstants.LUI_OFFERED_STATE_KEY);
 185  0
                     registrationGroupInfo.setTypeKey(LuiServiceConstants.REGISTRATION_GROUP_TYPE_KEY);
 186  
                     try {
 187  0
                         getCourseOfferingService().createRegistrationGroup(coi.getId(), registrationGroupInfo, new ContextInfo());
 188  0
                     } catch (OperationFailedException ofe) {
 189  0
                         System.out.println("call courseOfferingService.createRegistrationGroup() method, and get OperationFailedException:  " + ofe.toString());
 190  0
                     } catch (InvalidParameterException ipe) {
 191  0
                         System.out.println("call courseOfferingService.createRegistrationGroup() method, and get InvalidParameterException:  " + ipe.toString());
 192  0
                     } catch (DoesNotExistException dnee) {
 193  0
                         System.out.println("call courseOfferingService.createRegistrationGroup() method, and get DoesNotExistException:  " + dnee.toString());
 194  0
                     } catch (PermissionDeniedException pde) {
 195  0
                         System.out.println("call courseOfferingService.createRegistrationGroup() method, and get PermissionDeniedException:  " + pde.toString());
 196  0
                     } catch (MissingParameterException mpe) {
 197  0
                         System.out.println("call courseOfferingService.createRegistrationGroup() method, and get MissingParameterException:  " + mpe.toString());
 198  0
                     } catch (DataValidationErrorException dvee) {
 199  0
                         System.out.println("call courseOfferingService.createRegistrationGroup() method, and get DataValidationErrorException:  " + dvee.toString());
 200  0
                     }
 201  
 
 202  0
                 } catch (OperationFailedException ofe) {
 203  0
                     System.out.println("call courseOfferingService.getActivityOfferingTypesForActivityType() or createActivityOffering() method, and get OperationFailedException:  " + ofe.toString());
 204  0
                 } catch (InvalidParameterException ipe) {
 205  0
                     System.out.println("call courseOfferingService.getActivityOfferingTypesForActivityType() or createActivityOffering() method, and get InvalidParameterException:  " + ipe.toString());
 206  0
                 } catch (PermissionDeniedException pde) {
 207  0
                     System.out.println("call courseOfferingService.createActivityOffering() method, and get PermissionDeniedException:  " + pde.toString());
 208  0
                 } catch (MissingParameterException mpe) {
 209  0
                     System.out.println("call courseOfferingService.getActivityOfferingTypesForActivityType() or createActivityOffering() method, and get MissingParameterException:  " + mpe.toString());
 210  0
                 } catch (DataValidationErrorException dvee) {
 211  0
                     System.out.println("call courseOfferingService.createActivityOffering() method, and get DataValidationErrorException:  " + dvee.toString());
 212  0
                 } catch (DoesNotExistException dnee) {
 213  0
                     System.out.println("call courseOfferingService.getActivityOfferingTypesForActivityType() method, and get DoesNotExistException:  " + dnee.toString());
 214  0
                 }
 215  
 
 216  0
             }
 217  
         }
 218  0
     }
 219  
 
 220  
     /**
 221  
      * @see org.kuali.rice.krad.maintenance.MaintainableImpl#prepareForSave()
 222  
      */
 223  
     @Override
 224  
     public void prepareForSave() {
 225  0
         if (getMaintenanceAction().equalsIgnoreCase(KRADConstants.MAINTENANCE_NEW_ACTION)) {
 226  
 //          System.out.println(">>>>> in CourseOfferingInfoMaintainableImpl.prepareForSave method");
 227  
 
 228  
             //set state and type value for the courseOfferingInfo
 229  0
             CourseOfferingInfo newCourseOffering = (CourseOfferingInfo) getDataObject();
 230  0
             newCourseOffering.setTypeKey(LuiServiceConstants.COURSE_OFFERING_TYPE_KEY);
 231  0
             newCourseOffering.setStateKey(LuiServiceConstants.LUI_OFFERED_STATE_KEY);
 232  
 
 233  
             //for each instructor, set personId to Id field, state, and type
 234  0
             List<OfferingInstructorInfo> instructors =  newCourseOffering.getInstructors();
 235  0
             for(OfferingInstructorInfo instructor: instructors){
 236  0
                 instructor.setId(instructor.getPersonId());
 237  0
                 instructor.setStateKey(LuiPersonRelationServiceConstants.ASSIGNED_STATE_KEY);
 238  0
                 instructor.setTypeKey(LuiPersonRelationServiceConstants.INSTRUCTOR_MAIN_TYPE_KEY);
 239  
             }
 240  
         }
 241  0
         super.prepareForSave();
 242  0
     }
 243  
 
 244  
     /**
 245  
      * @see org.kuali.rice.krad.maintenance.Maintainable#processAfterCopy
 246  
      */
 247  
     @Override
 248  
     public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> requestParameters) {
 249  
         //set documentDescription to document.documentHeader.documentDescription
 250  0
         document.getDocumentHeader().setDocumentDescription(DEFAULT_DOCUMENT_DESC_FOR_COPYING_COURSE_OFFERING);
 251  0
     }
 252  
 
 253  
     /**
 254  
      * @see org.kuali.rice.krad.maintenance.Maintainable#processAfterEdit
 255  
      */
 256  
     @Override
 257  
     public void processAfterEdit(MaintenanceDocument document, Map<String, String[]> requestParameters) {
 258  
         //set documentDescription to document.documentHeader.documentDescription
 259  0
         document.getDocumentHeader().setDocumentDescription(DEFAULT_DOCUMENT_DESC_FOR_EDITING_COURSE_OFFERING);
 260  
 
 261  0
     }
 262  
 
 263  
     /**
 264  
      * @see org.kuali.rice.krad.maintenance.Maintainable#processAfterNew
 265  
      */
 266  
     @Override
 267  
     public void processAfterNew(MaintenanceDocument document, Map<String, String[]> requestParameters) {
 268  
         //set documentDescription to document.documentHeader.documentDescription
 269  0
         document.getDocumentHeader().setDocumentDescription(DEFAULT_DOCUMENT_DESC_FOR_CREATING_COURSE_OFFERING);
 270  
 
 271  0
     }
 272  
 
 273  
     protected CourseService getCourseService() {
 274  0
         if (courseService == null) {
 275  0
             courseService = (CourseService) GlobalResourceLoader.getService(new QName(CourseServiceConstants.COURSE_NAMESPACE, "CourseService"));
 276  
         }
 277  0
         return courseService;
 278  
     }
 279  
 
 280  
     protected CourseOfferingService getCourseOfferingService() {
 281  0
         if (courseOfferingService == null) {
 282  0
             courseOfferingService = (CourseOfferingService) GlobalResourceLoader.getService(new QName(CourseOfferingServiceConstants.NAMESPACE, "CourseOfferingService"));
 283  
         }
 284  0
         return courseOfferingService;
 285  
     }
 286  
 
 287  
     //TODO remove this fake generation below when we are getting real times from the form
 288  0
     private static final String[] days = {"MO", "TU", "WE", "TH", "FR"};
 289  0
     private static final String[] hours = {"07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17"};
 290  0
     private static final String[] mins = {"00", "15", "30", "45"};
 291  
 
 292  
     private List<MeetingScheduleInfo> generateFakeMeetingTimes() {
 293  0
         List<MeetingScheduleInfo> infos = new ArrayList<MeetingScheduleInfo>();
 294  0
         Random generator = new Random();
 295  
 
 296  0
         int randomNum = generator.nextInt(2);
 297  0
         String daysString = "";
 298  0
         String daysString2 = "";
 299  0
         if (randomNum == 0) {
 300  0
             int day1Index = generator.nextInt(4);
 301  0
             daysString = days[day1Index];
 302  0
             daysString2 = days[day1Index + 1];
 303  0
         } else {
 304  0
             int day1Index = generator.nextInt(2);
 305  0
             int day2Index = generator.nextInt(3) + 2;
 306  0
             daysString = days[day1Index] + "," + days[day2Index];
 307  
         }
 308  
 
 309  0
         String time = daysString + ";" + generateHours();
 310  0
         MeetingScheduleInfo m1 = new MeetingScheduleInfo();
 311  0
         m1.setScheduleId(time);
 312  0
         infos.add(m1);
 313  0
         if (StringUtils.isNotBlank(daysString2)) {
 314  0
             String time2 = daysString2 + ";" + generateHours();
 315  0
             MeetingScheduleInfo m2 = new MeetingScheduleInfo();
 316  0
             m2.setScheduleId(time2);
 317  0
             infos.add(m2);
 318  
         }
 319  0
         return infos;
 320  
 
 321  
 
 322  
     }
 323  
 
 324  
     private String generateHours() {
 325  0
         Random generator = new Random();
 326  0
         int randomNum = generator.nextInt(2);
 327  
 
 328  0
         int hourIndex1 = generator.nextInt(10);
 329  0
         String hour1 = hours[hourIndex1];
 330  0
         String hour2 = hours[hourIndex1 + 1];
 331  
 
 332  0
         int minIndex1 = generator.nextInt(3);
 333  0
         String min1 = mins[minIndex1];
 334  0
         String min2 = mins[minIndex1 + randomNum];
 335  
 
 336  0
         return hour1 + min1 + "," + hour2 + min2;
 337  
     }
 338  
 }