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