Coverage Report - org.kuali.student.lum.lu.ui.course.server.gwt.CourseDataService
 
Classes in this File Line Coverage Branch Coverage Complexity
CourseDataService
0%
0/35
0%
0/18
2.3
 
 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.Map;
 19  
 
 20  
 import org.apache.log4j.Logger;
 21  
 import org.kuali.student.common.assembly.data.Data;
 22  
 import org.kuali.student.common.dto.DtoConstants;
 23  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 24  
 import org.kuali.student.common.exceptions.OperationFailedException;
 25  
 import org.kuali.student.common.ui.server.gwt.AbstractDataService;
 26  
 import org.kuali.student.core.assembly.transform.ProposalWorkflowFilter;
 27  
 import org.kuali.student.lum.course.dto.CourseCrossListingInfo;
 28  
 import org.kuali.student.lum.course.dto.CourseInfo;
 29  
 import org.kuali.student.lum.course.service.CourseService;
 30  
 import org.springframework.util.StringUtils;
 31  
 
 32  0
 public class CourseDataService extends AbstractDataService {
 33  
 
 34  
         private static final long serialVersionUID = 1L;
 35  0
         final static Logger LOG = Logger.getLogger(CourseDataService.class);
 36  
 
 37  
         private static final String DEFAULT_METADATA_STATE = DtoConstants.STATE_DRAFT;
 38  
         
 39  
         private CourseService courseService;
 40  
         
 41  
 
 42  
         @Override
 43  
         protected Object get(String id) throws Exception {
 44  0
                 CourseInfo courseInfo = null;
 45  
 
 46  
                 try {
 47  0
                         courseInfo = courseService.getCourse(id);
 48  0
                 } catch (DoesNotExistException dne) {
 49  0
                         LOG.info("Course not found for key " + id + ". Course loaded from proposal instead.");
 50  0
                 }                
 51  
                 
 52  0
                 return courseInfo; 
 53  
         }
 54  
 
 55  
         @Override
 56  
         protected Object save(Object dto, Map<String, Object> properties) throws Exception {
 57  0
                 CourseInfo courseInfo = (CourseInfo)dto;
 58  
                 
 59  
                 //Set derived course fields before saving/updating
 60  0
                 courseInfo = calculateCourseDerivedFields(courseInfo);
 61  
                 
 62  0
                 if(properties!=null&&"kuali.proposal.type.course.modify".equals((String)properties.get(ProposalWorkflowFilter.WORKFLOW_DOC_TYPE))){
 63  
                         //For Modify Course, see if we need to create a new version instead of create
 64  0
                         if(courseInfo.getId() == null){
 65  0
                                 courseInfo = courseService.createNewCourseVersion(courseInfo.getVersionInfo().getVersionIndId(), courseInfo.getVersionInfo().getVersionComment());
 66  
                         }else{
 67  0
                                 courseInfo = courseService.updateCourse(courseInfo);
 68  
                         }
 69  
                 }else{
 70  0
                         if (courseInfo.getId() == null){
 71  0
                                 courseInfo = courseService.createCourse(courseInfo);
 72  
                         } else {
 73  0
                                 courseInfo = courseService.updateCourse(courseInfo);
 74  
                         }
 75  
                 }
 76  0
                 return courseInfo;
 77  
         }
 78  
         
 79  
         @Override
 80  
         protected String getDefaultMetaDataState() {
 81  0
                 return DEFAULT_METADATA_STATE;
 82  
         }
 83  
 
 84  
         @Override
 85  
         protected String getDefaultWorkflowDocumentType() {
 86  0
                 return "kuali.proposal.type.course.create";
 87  
         }
 88  
 
 89  
         @Override
 90  
         protected boolean checkDocumentLevelPermissions() {
 91  0
                 return true;
 92  
         }
 93  
 
 94  
         @Override
 95  
         protected Class<?> getDtoClass() {
 96  0
                 return CourseInfo.class;
 97  
         }
 98  
 
 99  
         public void setCourseService(CourseService courseService) {
 100  0
                 this.courseService = courseService;
 101  0
         }
 102  
 
 103  
         public Data createNewCourseVersion(String courseId, String versionComment) throws OperationFailedException {
 104  
                 try {
 105  
                         //FIXME calling getData after createNewCourseVersion is inefficient, but we need to have the transformations/filters be applied
 106  0
                         CourseInfo course = this.courseService.createNewCourseVersion(courseId, versionComment);
 107  0
                         return getData(course.getId());
 108  0
                 } catch (Exception e) {
 109  0
                         throw new OperationFailedException("Error getting data",e);
 110  
                 } 
 111  
                 
 112  
         }        
 113  
 
 114  
 
 115  
         /**
 116  
          * This calculates and sets fields on course object that are derived from other course object fields.
 117  
          */
 118  
         protected CourseInfo calculateCourseDerivedFields(CourseInfo courseInfo){
 119  
                 //Course code is not populated in UI, need to derive them from the subject area and suffix fields
 120  0
                 if(StringUtils.hasText(courseInfo.getCourseNumberSuffix()) && StringUtils.hasText(courseInfo.getSubjectArea())){
 121  0
                         courseInfo.setCode(calculateCourseCode(courseInfo.getSubjectArea(),courseInfo.getCourseNumberSuffix()));
 122  
                 }
 123  
                 
 124  
                 //Derive course code for crosslistings
 125  0
                 for(CourseCrossListingInfo crossListing:courseInfo.getCrossListings()){
 126  0
                          if(StringUtils.hasText(crossListing.getCourseNumberSuffix()) && StringUtils.hasText(crossListing.getSubjectArea())){
 127  0
                                  crossListing.setCode(calculateCourseCode(crossListing.getSubjectArea(), crossListing.getCourseNumberSuffix()));         
 128  
                          }
 129  
                 }
 130  
                 
 131  0
                 return courseInfo;
 132  
         }
 133  
         
 134  
         /**
 135  
          * 
 136  
          * This method calculates code for course and cross listed course.
 137  
          * 
 138  
          * @param subjectArea
 139  
          * @param suffixNumber
 140  
          * @return
 141  
          */
 142  
         private String calculateCourseCode(String subjectArea, String suffixNumber) {
 143  0
             return subjectArea + suffixNumber;
 144  
         }
 145  
         
 146  
 }