Coverage Report - org.kuali.student.lum.lu.ui.course.server.gwt.CourseDataService
 
Classes in this File Line Coverage Branch Coverage Complexity
CourseDataService
0%
0/36
0%
0/18
2.182
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.lum.lu.ui.course.server.gwt;
 17  
 
 18  
 import java.util.List;
 19  
 import java.util.Map;
 20  
 
 21  
 import org.apache.log4j.Logger;
 22  
 import org.kuali.student.common.assembly.data.Data;
 23  
 import org.kuali.student.common.dto.DtoConstants;
 24  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 25  
 import org.kuali.student.common.exceptions.OperationFailedException;
 26  
 import org.kuali.student.common.ui.server.gwt.AbstractDataService;
 27  
 import org.kuali.student.common.validation.dto.ValidationResultInfo;
 28  
 import org.kuali.student.core.assembly.transform.ProposalWorkflowFilter;
 29  
 import org.kuali.student.lum.course.dto.CourseCrossListingInfo;
 30  
 import org.kuali.student.lum.course.dto.CourseInfo;
 31  
 import org.kuali.student.lum.course.service.CourseService;
 32  
 import org.springframework.util.StringUtils;
 33  
 
 34  0
 public class CourseDataService extends AbstractDataService {
 35  
 
 36  
         private static final long serialVersionUID = 1L;
 37  0
         final static Logger LOG = Logger.getLogger(CourseDataService.class);
 38  
 
 39  
         private static final String DEFAULT_METADATA_STATE = DtoConstants.STATE_DRAFT;
 40  
         
 41  
         private CourseService courseService;
 42  
         
 43  
 
 44  
         @Override
 45  
         protected Object get(String id) throws Exception {
 46  0
                 CourseInfo courseInfo = null;
 47  
 
 48  
                 try {
 49  0
                         courseInfo = courseService.getCourse(id);
 50  0
                 } catch (DoesNotExistException dne) {
 51  0
                         LOG.info("Course not found for key " + id + ". Course loaded from proposal instead.");
 52  0
                 }                
 53  
                 
 54  0
                 return courseInfo; 
 55  
         }
 56  
 
 57  
         @Override
 58  
         protected Object save(Object dto, Map<String, Object> properties) throws Exception {
 59  0
                 CourseInfo courseInfo = (CourseInfo)dto;
 60  
                 
 61  
                 //Set derived course fields before saving/updating
 62  0
                 courseInfo = calculateCourseDerivedFields(courseInfo);
 63  
                 
 64  0
                 if(properties!=null&&"kuali.proposal.type.course.modify".equals((String)properties.get(ProposalWorkflowFilter.WORKFLOW_DOC_TYPE))){
 65  
                         //For Modify Course, see if we need to create a new version instead of create
 66  0
                         if(courseInfo.getId() == null){
 67  0
                                 courseInfo = courseService.createNewCourseVersion(courseInfo.getVersionInfo().getVersionIndId(), courseInfo.getVersionInfo().getVersionComment());
 68  
                         }else{
 69  0
                                 courseInfo = courseService.updateCourse(courseInfo);
 70  
                         }
 71  
                 }else{
 72  0
                         if (courseInfo.getId() == null){
 73  0
                                 courseInfo = courseService.createCourse(courseInfo);
 74  
                         } else {
 75  0
                                 courseInfo = courseService.updateCourse(courseInfo);
 76  
                         }
 77  
                 }
 78  0
                 return courseInfo;
 79  
         }
 80  
         
 81  
         
 82  
         @Override
 83  
         protected List<ValidationResultInfo> validate(Object dto) throws Exception {
 84  0
                 return courseService.validateCourse("OBJECT", (CourseInfo)dto);
 85  
         }
 86  
 
 87  
         @Override
 88  
         protected String getDefaultMetaDataState() {
 89  0
                 return DEFAULT_METADATA_STATE;
 90  
         }
 91  
 
 92  
         @Override
 93  
         protected String getDefaultWorkflowDocumentType() {
 94  0
                 return "kuali.proposal.type.course.create";
 95  
         }
 96  
 
 97  
         @Override
 98  
         protected boolean checkDocumentLevelPermissions() {
 99  0
                 return true;
 100  
         }
 101  
 
 102  
         @Override
 103  
         protected Class<?> getDtoClass() {
 104  0
                 return CourseInfo.class;
 105  
         }
 106  
 
 107  
         public void setCourseService(CourseService courseService) {
 108  0
                 this.courseService = courseService;
 109  0
         }
 110  
 
 111  
         public Data createNewCourseVersion(String courseId, String versionComment) throws OperationFailedException {
 112  
                 try {
 113  
                         //FIXME calling getData after createNewCourseVersion is inefficient, but we need to have the transformations/filters be applied
 114  0
                         CourseInfo course = this.courseService.createNewCourseVersion(courseId, versionComment);
 115  0
                         return getData(course.getId());
 116  0
                 } catch (Exception e) {
 117  0
                         throw new OperationFailedException("Error getting data",e);
 118  
                 } 
 119  
                 
 120  
         }        
 121  
 
 122  
 
 123  
         /**
 124  
          * This calculates and sets fields on course object that are derived from other course object fields.
 125  
          */
 126  
         protected CourseInfo calculateCourseDerivedFields(CourseInfo courseInfo){
 127  
                 //Course code is not populated in UI, need to derive them from the subject area and suffix fields
 128  0
                 if(StringUtils.hasText(courseInfo.getCourseNumberSuffix()) && StringUtils.hasText(courseInfo.getSubjectArea())){
 129  0
                         courseInfo.setCode(calculateCourseCode(courseInfo.getSubjectArea(),courseInfo.getCourseNumberSuffix()));
 130  
                 }
 131  
                 
 132  
                 //Derive course code for crosslistings
 133  0
                 for(CourseCrossListingInfo crossListing:courseInfo.getCrossListings()){
 134  0
                          if(StringUtils.hasText(crossListing.getCourseNumberSuffix()) && StringUtils.hasText(crossListing.getSubjectArea())){
 135  0
                                  crossListing.setCode(calculateCourseCode(crossListing.getSubjectArea(), crossListing.getCourseNumberSuffix()));         
 136  
                          }
 137  
                 }
 138  
                 
 139  0
                 return courseInfo;
 140  
         }
 141  
         
 142  
         /**
 143  
          * 
 144  
          * This method calculates code for course and cross listed course.
 145  
          * 
 146  
          * @param subjectArea
 147  
          * @param suffixNumber
 148  
          * @return
 149  
          */
 150  
         private String calculateCourseCode(String subjectArea, String suffixNumber) {
 151  0
             return subjectArea + suffixNumber;
 152  
         }
 153  
         
 154  
 }