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