Coverage Report - org.kuali.student.enrollment.class2.courseoffering.validation.impl.CourseOfferingRule
 
Classes in this File Line Coverage Branch Coverage Complexity
CourseOfferingRule
0%
0/60
0%
0/24
4.2
 
 1  
 package org.kuali.student.enrollment.class2.courseoffering.validation.impl;
 2  
 
 3  
 import javax.xml.namespace.QName;
 4  
 import java.util.ArrayList;
 5  
 import java.util.List;
 6  
 import java.util.Map;
 7  
 import java.util.HashMap;
 8  
 
 9  
 import org.hibernate.mapping.Index;
 10  
 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
 11  
 import org.kuali.rice.kim.api.identity.IdentityService;
 12  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 13  
 import org.kuali.rice.krad.bo.PersistableBusinessObject;
 14  
 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
 15  
 import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
 16  
 import org.kuali.rice.krad.util.GlobalVariables;
 17  
 import org.kuali.rice.krad.util.KRADConstants;
 18  
 
 19  
 import org.kuali.rice.krad.web.form.LookupForm;
 20  
 import org.kuali.student.common.search.dto.*;
 21  
 import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
 22  
 import org.kuali.student.enrollment.courseoffering.dto.OfferingInstructorInfo;
 23  
 import org.kuali.student.lum.lu.service.LuService;
 24  
 import org.kuali.student.lum.lu.service.LuServiceConstants;
 25  
 
 26  
 /**
 27  
  * @deprecated This class is leftover from Core Slice. Delete when no longer needed or un deprecate if needed.
 28  
  */
 29  
 @Deprecated
 30  0
 public class CourseOfferingRule extends MaintenanceDocumentRuleBase {
 31  
      private static final String COURSE_CODE_PROPERTY_PATH = "document.newMaintainableObject.dataObject.courseOfferingCode";
 32  
 
 33  
      private transient LuService luService;
 34  
      private transient IdentityService identityService;
 35  
 
 36  
     /**
 37  
      * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
 38  
      */
 39  
     @Override
 40  
     protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
 41  0
         boolean isValid = true;
 42  
 
 43  
         // retrieve and verify a courseId based on course Code, and set it to the courseOfferingInfo.
 44  0
         CourseOfferingInfo courseOfferingInfo = (CourseOfferingInfo)document.getDocumentDataObject();
 45  0
         String courseCode =  courseOfferingInfo.getCourseOfferingCode();
 46  0
         String courseId = retrieveCourseIdFromCourseCode(courseCode);
 47  0
         if (courseId == null) {
 48  0
              GlobalVariables.getMessageMap().putError(
 49  
                 COURSE_CODE_PROPERTY_PATH,"error.enroll.courseoffering.courseOfferingCode.notExists");
 50  0
              isValid = false;
 51  
         }
 52  
         else{
 53  0
              courseOfferingInfo.setCourseId(courseId);
 54  
         }
 55  
 
 56  
         // need to verify principalIds again since the user might modify the input after add it to the collection
 57  0
         List<OfferingInstructorInfo> instructors = courseOfferingInfo.getInstructors();
 58  0
         int index = 0;
 59  0
         for(OfferingInstructorInfo instructor : instructors){
 60  0
             String principalId = instructor.getPersonId();
 61  0
             if (getIdentityService().getPrincipal(principalId) == null) {
 62  0
                     GlobalVariables.getMessageMap().putError(
 63  
                      "document.newMaintainableObject.dataObject.instructors["+index+"]", "error.enroll.courseoffering.instructors.notExists", "The instructor: "+principalId+" does not exist.");
 64  0
                 isValid = false;
 65  
             }
 66  0
             index++;
 67  0
         }
 68  0
         isValid &= super.processCustomRouteDocumentBusinessRules(document);
 69  0
         isValid &= GlobalVariables.getMessageMap().hasNoErrors();
 70  
 
 71  0
         return isValid;
 72  
     }
 73  
 
 74  
 /*
 75  
     @Override
 76  
     public boolean processCustomAddCollectionLineBusinessRules(MaintenanceDocument document, String collectionName,
 77  
             PersistableBusinessObject line) {
 78  
         // need to verify principalId when add it to the collection
 79  
         List<OfferingInstructorInfo> instructors = courseOfferingInfo.getInstructors();
 80  
         int index = 0;
 81  
         for(OfferingInstructorInfo instructor : instructors){
 82  
             String principalId = instructor.getPersonId();
 83  
             if (getIdentityService().getPrincipal(principalId) == null) {
 84  
                 System.out.println(">>>Error: Fail to retrieve an instructor with instructorId equal to "+principalId);
 85  
                     GlobalVariables.getMessageMap().putError(
 86  
                      "instructors("+principalId+")", "error.enroll.courseoffering.instructors.notExists", "The instructor: "+principalId+" does not exist.");
 87  
                 isValid = false;
 88  
             }
 89  
             index++;
 90  
         }
 91  
     }
 92  
 */
 93  
 
 94  
     //Note: here I am using r1 LuService implementation!!!
 95  
     protected LuService getLuService() {
 96  0
         if(luService == null) {
 97  0
             luService = (LuService)GlobalResourceLoader.getService(new QName(LuServiceConstants.LU_NAMESPACE,"LuService"));
 98  
         }
 99  0
         return this.luService;
 100  
     }
 101  
 
 102  
     protected IdentityService getIdentityService() {
 103  0
                 if ( identityService == null ) {
 104  0
                         identityService = KimApiServiceLocator.getIdentityService();
 105  
                 }
 106  0
                 return identityService;
 107  
         }
 108  
 
 109  
     /*
 110  
      * return courseId for a specified courseCode
 111  
      */
 112  
     private String retrieveCourseIdFromCourseCode (String courseCode){
 113  0
         Map<String,String> fieldValues = new HashMap<String, String>();
 114  0
         fieldValues.put("code",courseCode);
 115  0
         List courseIds = getSearchResults(null, fieldValues, false);
 116  0
         if(courseIds.size() != 1){
 117  0
             return null;
 118  
         }
 119  
         else{
 120  0
             return (String) courseIds.get(0);
 121  
         }
 122  
     }
 123  
     /*
 124  
      * Use LuService for a general search to retrieve the courseId based on a specified course code in the search criteria
 125  
      */
 126  
     private List<?> getSearchResults(LookupForm lookupForm, Map<String, String> fieldValues, boolean unbounded) {
 127  0
         List <String> courseIds = new ArrayList<String>();
 128  0
         List<SearchParam> searchParams = new ArrayList<SearchParam>();
 129  0
         SearchParam qpv1 = new SearchParam();
 130  0
         qpv1.setKey("lu.queryParam.luOptionalType");
 131  0
         qpv1.setValue("kuali.lu.type.CreditCourse");
 132  0
         searchParams.add(qpv1);
 133  0
         SearchParam qpv2 = new SearchParam();
 134  0
         qpv1.setKey("lu.queryParam.luOptionalCode");
 135  0
         qpv1.setValue(fieldValues.get("code"));
 136  0
         searchParams.add(qpv2);
 137  
 
 138  0
         SearchRequest searchRequest = new SearchRequest();
 139  0
         searchRequest.setParams(searchParams);
 140  0
         searchRequest.setSearchKey("lu.search.mostCurrent.union");
 141  
 
 142  
         try {
 143  0
             SearchResult searchResult = getLuService().search(searchRequest);
 144  
 
 145  0
             if (searchResult.getRows().size() > 0) {
 146  0
                 for(SearchResultRow srrow : searchResult.getRows()){
 147  0
                     List<SearchResultCell> srCells = srrow.getCells();
 148  0
                     if(srCells != null && srCells.size() > 0){
 149  0
                         for(SearchResultCell srcell : srCells){
 150  0
                             if (srcell.getKey().equals("lu.resultColumn.cluId")) {
 151  0
                                 String courseId = srcell.getValue();
 152  0
                                 courseIds.add(courseId);
 153  0
                             }
 154  
                         }
 155  
                     }
 156  0
                 }
 157  
             }
 158  
 
 159  0
             return courseIds;
 160  0
         } catch (Exception e) {
 161  0
             throw new RuntimeException(e);
 162  
         }
 163  
     }
 164  
 
 165  
 }