Coverage Report - org.kuali.student.enrollment.class2.courseofferingset.service.decorators.CourseOfferingSetServiceCalculationDecorator
 
Classes in this File Line Coverage Branch Coverage Complexity
CourseOfferingSetServiceCalculationDecorator
0%
0/151
0%
0/54
4.111
 
 1  
 /*
 2  
  * To change this template, choose Tools | Templates
 3  
  * and open the template in the editor.
 4  
  */
 5  
 package org.kuali.student.enrollment.class2.courseofferingset.service.decorators;
 6  
 
 7  
 import java.util.ArrayList;
 8  
 import java.util.List;
 9  
 import org.kuali.student.enrollment.acal.dto.TermInfo;
 10  
 import org.kuali.student.enrollment.acal.service.AcademicCalendarService;
 11  
 import org.kuali.student.enrollment.courseoffering.dto.ActivityOfferingInfo;
 12  
 import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
 13  
 import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
 14  
 import org.kuali.student.enrollment.courseofferingset.dto.SocInfo;
 15  
 import org.kuali.student.enrollment.courseofferingset.dto.SocRolloverResultInfo;
 16  
 import org.kuali.student.enrollment.courseofferingset.dto.SocRolloverResultItemInfo;
 17  
 import org.kuali.student.lum.course.service.CourseService;
 18  
 import org.kuali.student.r2.common.dto.ContextInfo;
 19  
 import org.kuali.student.r2.common.dto.StatusInfo;
 20  
 import org.kuali.student.r2.common.exceptions.*;
 21  
 import org.kuali.student.r2.common.util.constants.CourseOfferingSetServiceConstants;
 22  
 import org.springframework.transaction.annotation.Transactional;
 23  
 
 24  
 @Transactional(readOnly = true, noRollbackFor = {DoesNotExistException.class}, rollbackFor = {Throwable.class})
 25  0
 public class CourseOfferingSetServiceCalculationDecorator extends CourseOfferingSetServiceDecorator {
 26  
 
 27  
     private CourseOfferingService coService;
 28  
     private CourseService courseService;
 29  
     private AcademicCalendarService acalService;
 30  
 
 31  
     public CourseOfferingService getCoService() {
 32  0
         return coService;
 33  
     }
 34  
 
 35  
     public void setCoService(CourseOfferingService coService) {
 36  0
         this.coService = coService;
 37  0
     }
 38  
 
 39  
     public CourseService getCourseService() {
 40  0
         return courseService;
 41  
     }
 42  
 
 43  
     public void setCourseService(CourseService courseService) {
 44  0
         this.courseService = courseService;
 45  0
     }
 46  
 
 47  
     public AcademicCalendarService getAcalService() {
 48  0
         return acalService;
 49  
     }
 50  
 
 51  
     public void setAcalService(AcademicCalendarService acalService) {
 52  0
         this.acalService = acalService;
 53  0
     }
 54  
 
 55  
     @Override
 56  
     @Transactional(readOnly = false)
 57  
     public StatusInfo scheduleSoc(String socId, ContextInfo context)
 58  
             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
 59  
             PermissionDeniedException {
 60  0
         throw new OperationFailedException("scheduleSoc has not been implemented");
 61  
     }
 62  
 
 63  
     @Override
 64  
     @Transactional(readOnly = false)
 65  
     public SocInfo rolloverSoc(String sourceSocId, String targetTermId, List<String> optionKeys, ContextInfo context)
 66  
             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
 67  
             PermissionDeniedException {
 68  
         // validate the target term
 69  0
         TermInfo targetTerm = this.acalService.getTerm(targetTermId, context);
 70  
         // first create the new soc
 71  0
         SocInfo sourceSoc = this.getSoc(sourceSocId, context);
 72  0
         if (sourceSoc.getTermId().equals(targetTermId)) {
 73  0
             throw new InvalidParameterException("The term of the source soc and the target term must be different");
 74  
         }
 75  
         // TODO: try to find the soc in the target term and use it instead of just creating a new one
 76  0
         SocInfo targetSoc = new SocInfo(sourceSoc);
 77  0
         targetSoc.setId(null);
 78  0
         targetSoc.setTermId(targetTermId);
 79  
         try {
 80  0
             targetSoc = this.createSoc(targetSoc.getTermId(), targetSoc.getTypeKey(), targetSoc, context);
 81  0
         } catch (DataValidationErrorException ex) {
 82  0
             throw new OperationFailedException("Unexpected", ex);
 83  0
         } catch (ReadOnlyException ex) {
 84  0
             throw new OperationFailedException("Unexpected", ex);
 85  0
         }
 86  
         // then build the result so we can track stuff
 87  0
         SocRolloverResultInfo result = new SocRolloverResultInfo();
 88  0
         result.setTypeKey(CourseOfferingSetServiceConstants.ROLLOVER_RESULT_TYPE_KEY);
 89  0
         result.setStateKey(CourseOfferingSetServiceConstants.SUBMITTED_RESULT_STATE_KEY);
 90  0
         result.setSourceSocId(sourceSocId);
 91  0
         result.setTargetTermId(targetTermId);
 92  0
         result.setOptionKeys(optionKeys);
 93  0
         result.setTargetSocId(targetSoc.getId());
 94  
         try {
 95  0
             result = this.createSocRolloverResult(result.getTypeKey(), result, context);
 96  0
         } catch (DataValidationErrorException ex) {
 97  0
             throw new OperationFailedException("Unexpected", ex);
 98  0
         } catch (ReadOnlyException ex) {
 99  0
             throw new OperationFailedException("Unexpected", ex);
 100  0
         }
 101  
         // create the runner so we can kick it off in another thread
 102  0
         CourseOfferingRolloverRunner runner = new CourseOfferingRolloverRunner();
 103  0
         runner.setContext(context);
 104  0
         runner.setCoService(coService);
 105  0
         runner.setCourseService(courseService);
 106  0
         runner.setAcalService(acalService);
 107  0
         runner.setSocService(this);
 108  0
         runner.setResult(result);
 109  0
         if (optionKeys.contains(CourseOfferingSetServiceConstants.RUN_SYNCHRONOUSLY_OPTION_KEY)) {
 110  0
             runner.run();
 111  
         } else {
 112  0
             Thread thread = new Thread(runner);
 113  0
             thread.start();
 114  
         }
 115  0
         return targetSoc;
 116  
     }
 117  
 
 118  
     // this is for setting parameters if using the general batch job result service
 119  
 //    private AttributeInfo conv2Attr(String key, String value) {
 120  
 //        AttributeInfo attr = new AttributeInfo();
 121  
 //        attr.setKey(key);
 122  
 //        attr.setValue(value);
 123  
 //        return attr;
 124  
 //    }
 125  
     @Override
 126  
     @Transactional(readOnly = false)
 127  
     public SocRolloverResultInfo reverseRollover(String rolloverResultId, List<String> optionKeys, ContextInfo context)
 128  
             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
 129  
             PermissionDeniedException {// validate the target term
 130  0
         SocRolloverResultInfo rolloverResult = this.getSocRolloverResult(rolloverResultId, context);
 131  0
         if (optionKeys.contains(CourseOfferingSetServiceConstants.REVERSE_JUST_CREATES_OPTION_KEY)) {
 132  0
             if (!rolloverResult.getOptionKeys().contains(CourseOfferingSetServiceConstants.LOG_SUCCESSES_OPTION_KEY)) {
 133  0
                 throw new InvalidParameterException(
 134  
                         "You cannot reverse just the creates if the original rollover did not log the course offerings that it successfully created");
 135  
             }
 136  
         }
 137  0
         SocRolloverResultInfo reverseResult = new SocRolloverResultInfo(rolloverResult);
 138  0
         reverseResult.setTypeKey(CourseOfferingSetServiceConstants.ROLLOVER_RESULT_TYPE_KEY);
 139  0
         reverseResult.setStateKey(CourseOfferingSetServiceConstants.SUBMITTED_RESULT_STATE_KEY);
 140  0
         reverseResult.setOptionKeys(optionKeys);
 141  
 
 142  
         try {
 143  0
             reverseResult = this.createSocRolloverResult(reverseResult.getTypeKey(), reverseResult, context);
 144  0
         } catch (DataValidationErrorException ex) {
 145  0
             throw new OperationFailedException("Unexpected", ex);
 146  0
         } catch (ReadOnlyException ex) {
 147  0
             throw new OperationFailedException("Unexpected", ex);
 148  0
         }
 149  
         // create the runner so we can kick it off in another thread
 150  0
         CourseOfferingReverseRolloverRunner runner = new CourseOfferingReverseRolloverRunner();
 151  0
         runner.setContext(context);
 152  0
         runner.setCoService(coService);
 153  0
         runner.setSocService(this);
 154  0
         runner.setCourseService(courseService);
 155  0
         runner.setAcalService(acalService);
 156  0
         runner.setRolloverResult(rolloverResult);
 157  0
         runner.setReverseResult(reverseResult);
 158  0
         if (optionKeys.contains(CourseOfferingSetServiceConstants.RUN_SYNCHRONOUSLY_OPTION_KEY)) {
 159  0
             runner.run();
 160  
         } else {
 161  0
             Thread thread = new Thread(runner);
 162  0
             thread.start();
 163  
         }
 164  0
         return reverseResult;
 165  
     }
 166  
 
 167  
     @Override
 168  
     public List<String> getCourseOfferingIdsBySoc(String socId, ContextInfo context)
 169  
             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
 170  
             PermissionDeniedException {
 171  
         // THIS IS BASICALLY A SWITCH STATEMENT BASED ON THE TYPE OF THE SOC
 172  0
         SocInfo soc = this.getSoc(socId, context);
 173  
         // main
 174  0
         if (soc.getTypeKey().equals(CourseOfferingSetServiceConstants.MAIN_SOC_TYPE_KEY)) {
 175  0
             return coService.getCourseOfferingIdsByTerm(soc.getTermId(), Boolean.TRUE, context);
 176  
         }
 177  
         // subject area
 178  0
         if (soc.getTypeKey().equals(CourseOfferingSetServiceConstants.SUBJECT_AREA_SOC_TYPE_KEY)) {
 179  0
             return coService.getCourseOfferingIdsByTermAndSubjectArea(soc.getTermId(), soc.getSubjectArea(), context);
 180  
         }
 181  
         // units content owner
 182  0
         if (soc.getTypeKey().equals(CourseOfferingSetServiceConstants.UNITS_CONTENT_OWNER_SOC_TYPE_KEY)) {
 183  0
             return coService.getCourseOfferingIdsByTermAndUnitsContentOwner(soc.getTermId(), soc.getUnitsContentOwnerId(), context);
 184  
         }
 185  0
         throw new OperationFailedException(soc.getTypeKey() + " is an unsupported type for this implementation");
 186  
 //        List<String> list = new ArrayList<String>();
 187  
 //        for (CourseOfferingInfo info : courseOfferingMap.values()) {
 188  
 //            if (socId.equals(info.getSocId())) {
 189  
 //                list.add(info.getId());
 190  
 //            }
 191  
 //        }
 192  
 //        return list;
 193  
     }
 194  
 
 195  
     @Override
 196  
     @Transactional(readOnly = false)
 197  
     public Integer deleteCourseOfferingsBySoc(String socId, ContextInfo context)
 198  
             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
 199  
             PermissionDeniedException {
 200  
         // TODO: add bulk ops to CourseOfferingService so this can call them 
 201  
         // to delete all for a term or delete all for a subject area intead of doing it one by one
 202  0
         List<String> ids = this.getCourseOfferingIdsBySoc(socId, context);
 203  0
         for (String id : ids) {
 204  
             try {
 205  0
                 this.coService.deleteCourseOffering(socId, context);
 206  0
             } catch (DependentObjectsExistException e) {
 207  0
                 throw new OperationFailedException( e.getMessage());
 208  0
             }
 209  
         }
 210  0
         return ids.size();
 211  
     }
 212  
 
 213  
     @Override
 214  
     public Boolean isCourseOfferingInSoc(String socId, String courseOfferingId, ContextInfo context)
 215  
             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
 216  
             PermissionDeniedException {
 217  
 
 218  0
         SocInfo soc = this.getSoc(socId, context);
 219  0
         CourseOfferingInfo co = this.coService.getCourseOffering(courseOfferingId, context);
 220  
         // main
 221  0
         if (soc.getTypeKey().equals(CourseOfferingSetServiceConstants.MAIN_SOC_TYPE_KEY)) {
 222  0
             if (co.getTermId().equals(soc.getTermId())) {
 223  0
                 return true;
 224  
             }
 225  
             // TODO: handle sub-terms  before returning false
 226  0
             return false;
 227  
         }
 228  
         // subject area
 229  0
         if (soc.getTypeKey().equals(CourseOfferingSetServiceConstants.SUBJECT_AREA_SOC_TYPE_KEY)) {
 230  0
             if (co.getTermId().equals(soc.getTermId())) {
 231  0
                 if (co.getSubjectArea().equals(soc.getSubjectArea())) {
 232  0
                     return true;
 233  
                 }
 234  
             }
 235  
             // TODO: handle sub-terms  before returning false
 236  0
             return false;
 237  
         }
 238  
         // units content owner
 239  0
         if (soc.getTypeKey().equals(CourseOfferingSetServiceConstants.UNITS_CONTENT_OWNER_SOC_TYPE_KEY)) {
 240  0
             if (co.getTermId().equals(soc.getTermId())) {
 241  0
                 if (co.getUnitsContentOwnerOrgIds().equals(soc.getUnitsContentOwnerId())) {
 242  0
                     return true;
 243  
                 }
 244  
             }
 245  
             // TODO: handle sub-terms before returning false
 246  0
             return false;
 247  
         }
 248  
         // else get all of them and check if in the list
 249  0
         List<String> ids = this.getCourseOfferingIdsBySoc(socId, context);
 250  0
         return ids.contains(courseOfferingId);
 251  
     }
 252  
 
 253  
     @Override
 254  
     public List<String> getPublishedCourseOfferingIdsBySoc(String socId, ContextInfo context)
 255  
             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
 256  
             PermissionDeniedException {
 257  0
         List<String> list = new ArrayList<String>();
 258  0
         List<String> list2 = this.getCourseOfferingIdsBySoc(socId, context);
 259  0
         for (CourseOfferingInfo info : this.coService.getCourseOfferingsByIds(list2, context)) {
 260  
             // TODO: add the published course offering state to the constants 
 261  
 //            if (info.getStateKey().equals(CourseOfferingServiceConstants.PUBLISHED_STATE_KEY) {
 262  0
             list.add(info.getId());
 263  
 //            }
 264  
         }
 265  0
         return list;
 266  
     }
 267  
 
 268  
     @Override
 269  
     public List<String> getUnpublishedCourseOfferingIdsBySoc(String socId, ContextInfo context)
 270  
             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
 271  
             PermissionDeniedException {
 272  0
         List<String> list = new ArrayList<String>();
 273  0
         List<String> list2 = this.getCourseOfferingIdsBySoc(socId, context);
 274  0
         for (CourseOfferingInfo info : this.coService.getCourseOfferingsByIds(list2, context)) {
 275  
             // TODO: add the published course offering state to the constants 
 276  
 //            if (info.getStateKey().equals(CourseOfferingServiceConstants.PUBLISHED_STATE_KEY) {
 277  0
             list.add(info.getId());
 278  
 //            }
 279  
         }
 280  0
         return list;
 281  
     }
 282  
 
 283  
     @Override
 284  
     public List<String> getUnpublishedActivityOfferingIdsBySoc(String socId, ContextInfo context)
 285  
             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
 286  
             PermissionDeniedException {
 287  0
         List<String> list = new ArrayList<String>();
 288  0
         List<String> list2 = this.getCourseOfferingIdsBySoc(socId, context);
 289  0
         for (String coId : list2) {
 290  0
             for (ActivityOfferingInfo ao : this.coService.getActivityOfferingsByCourseOffering(coId, context)) {
 291  
                 // TODO: add the published course offering state to the constants 
 292  
 //            if (!ao.getStateKey().equals(CourseOfferingServiceConstants.PUBLISHED_STATE_KEY) {
 293  0
                 list.add(ao.getId());
 294  
 //            }
 295  
             }
 296  
         }
 297  0
         return list;
 298  
     }
 299  
 
 300  
     @Override
 301  
     public List<String> getUnscheduledActivityOfferingIdsBySoc(String socId, ContextInfo context)
 302  
             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
 303  
             PermissionDeniedException {
 304  0
         List<String> list = new ArrayList<String>();
 305  0
         List<String> list2 = this.getCourseOfferingIdsBySoc(socId, context);
 306  0
         for (String coId : list2) {
 307  0
             for (ActivityOfferingInfo ao : this.coService.getActivityOfferingsByCourseOffering(coId, context)) {
 308  
                 // TODO: add the published course offering state to the constants 
 309  
 //            if (!ao.getStateKey().equals(CourseOfferingServiceConstants.SCHEDULED_STATE_KEY) {
 310  0
                 list.add(ao.getId());
 311  
 //            }
 312  
             }
 313  
         }
 314  0
         return list;
 315  
     }
 316  
 
 317  
     @Override
 318  
     public SocRolloverResultInfo getSocRolloverResult(String rolloverResultId, ContextInfo context) throws DoesNotExistException,
 319  
             InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 320  0
         SocRolloverResultInfo result = this.getNextDecorator().getSocRolloverResult(rolloverResultId, context);
 321  0
         this.updateCalculatedFields(result, context);
 322  0
         return result;
 323  
     }
 324  
 
 325  
     // TODO: push this logic down into the JPA layer for efficiency once the logic for the counts gets settled on 
 326  
     // My GUT says that they may want more counts than just the 2 we are getting now... I.e. count of warnings?
 327  
     private void updateCalculatedFields(SocRolloverResultInfo info, ContextInfo context) throws OperationFailedException {
 328  
         try {
 329  0
             if (info.getSourceSocId() != null) {
 330  0
                 SocInfo sourceSoc = this.getSoc(info.getSourceSocId(), context);
 331  0
                 info.setSourceTermId(sourceSoc.getTermId());
 332  
             }
 333  
             // only do the calc once finished or the querying while running will be too long
 334  0
             if (info.getStateKey().equals(CourseOfferingSetServiceConstants.FINISHED_RESULT_STATE_KEY)) {
 335  0
                 List<SocRolloverResultItemInfo> items = this.getSocRolloverResultItemsByResultId(info.getId(), context);
 336  0
                 int success = 0;
 337  0
                 int failure = 0;
 338  0
                 for (SocRolloverResultItemInfo item : items) {
 339  0
                     if (item.getStateKey().equals(CourseOfferingSetServiceConstants.SUCCESS_RESULT_ITEM_STATE_KEY)) {
 340  0
                         success++;
 341  
                     } else {
 342  0
                         failure++;
 343  
                     }
 344  
                 }
 345  0
                 info.setCourseOfferingsCreated(success);
 346  0
                 info.setCourseOfferingsSkipped(failure);
 347  
             }
 348  0
         } catch (Exception ex) {
 349  0
             throw new OperationFailedException("unexpected", ex);
 350  0
         }
 351  0
     }
 352  
 }