1 package org.kuali.student.enrollment.class2.courseoffering.rule; 2 3 import org.apache.commons.lang.StringUtils; 4 import org.kuali.rice.core.api.criteria.PredicateFactory; 5 import org.kuali.rice.core.api.criteria.QueryByCriteria; 6 import org.kuali.rice.krad.maintenance.MaintenanceDocument; 7 import org.kuali.rice.krad.util.GlobalVariables; 8 import org.kuali.student.common.uif.rule.KsMaintenanceDocumentRuleBase; 9 import org.kuali.student.enrollment.class2.courseoffering.dto.CourseOfferingCreateWrapper; 10 import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingConstants; 11 import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingResourceLoader; 12 import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo; 13 import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService; 14 import org.kuali.student.r2.common.exceptions.InvalidParameterException; 15 import org.kuali.student.r2.common.exceptions.MissingParameterException; 16 import org.kuali.student.r2.common.exceptions.OperationFailedException; 17 import org.kuali.student.r2.common.exceptions.PermissionDeniedException; 18 import org.kuali.student.r2.common.util.ContextUtils; 19 20 import java.util.ArrayList; 21 import java.util.List; 22 23 public class CourseOfferingCreateRule extends KsMaintenanceDocumentRuleBase { 24 25 private CourseOfferingService courseOfferingService; 26 27 @Override 28 protected boolean isDocumentValidForSave(MaintenanceDocument document) { 29 if ( ! super.isDocumentValidForSave(document)) { 30 return false; 31 } 32 33 if (document.getNewMaintainableObject().getDataObject() instanceof CourseOfferingCreateWrapper){ 34 CourseOfferingCreateWrapper coWrapper = (CourseOfferingCreateWrapper)document.getNewMaintainableObject().getDataObject(); 35 36 if ( ! validateRequiredFields(coWrapper)) { 37 return false; 38 } 39 if ( ! validateDuplicateSuffix(coWrapper)) { 40 return false; 41 } 42 } 43 44 return true; 45 } 46 47 48 protected boolean validateRequiredFields(CourseOfferingCreateWrapper coWrapper){ 49 if (coWrapper.getFormatOfferingWrappers().isEmpty()){ 50 GlobalVariables.getMessageMap().putError(CourseOfferingConstants.DELIVERY_FORMAT_SECTION_ID,CourseOfferingConstants.DELIVERY_FORMAT_REQUIRED_ERROR); 51 return false; 52 } 53 return true; 54 } 55 56 protected boolean validateDuplicateSuffix(CourseOfferingCreateWrapper coWrapper){ 57 String courseCode = coWrapper.getCatalogCourseCode().toUpperCase(); 58 String newCoCode = courseCode + coWrapper.getCourseOfferingSuffix().toUpperCase(); 59 try { 60 List<CourseOfferingInfo> wrapperList = 61 _findCourseOfferingsByTermAndCourseCode(coWrapper.getTerm().getId(), newCoCode); 62 for (CourseOfferingInfo courseOfferingInfo : wrapperList) { 63 if (StringUtils.equals(newCoCode, courseOfferingInfo.getCourseOfferingCode())) { 64 GlobalVariables.getMessageMap().putError( 65 "document.newMaintainableObject.dataObject.courseOfferingSuffix", 66 CourseOfferingConstants.COURSEOFFERING_ERROR_CREATE_DUPLICATECODE, newCoCode, courseCode); 67 coWrapper.setEnableCreateButton(true); 68 return false; 69 } 70 } 71 } catch (Exception e) { 72 throw new RuntimeException(e); 73 } 74 75 return true; 76 } 77 78 protected CourseOfferingService getCourseOfferingService() { 79 if (courseOfferingService == null) { 80 courseOfferingService = CourseOfferingResourceLoader.loadCourseOfferingService(); 81 } 82 return courseOfferingService; 83 } 84 85 private List<CourseOfferingInfo> _findCourseOfferingsByTermAndCourseCode(String termId, String courseCode) 86 throws InvalidParameterException, MissingParameterException, PermissionDeniedException, OperationFailedException { 87 List<CourseOfferingInfo> courseOfferings = new ArrayList<CourseOfferingInfo>(); 88 if (StringUtils.isNotBlank(courseCode) && StringUtils.isNotBlank(termId)) { 89 QueryByCriteria.Builder qbcBuilder = QueryByCriteria.Builder.create(); 90 qbcBuilder.setPredicates(PredicateFactory.and( 91 PredicateFactory.equal("courseOfferingCode", courseCode), 92 PredicateFactory.equalIgnoreCase("atpId", termId))); 93 QueryByCriteria criteria = qbcBuilder.build(); 94 95 courseOfferings = getCourseOfferingService().searchForCourseOfferings(criteria, ContextUtils.createDefaultContextInfo()); 96 } 97 return courseOfferings; 98 } 99 }