Coverage Report - org.kuali.student.lum.lu.ui.course.server.gwt.CourseDataService
 
Classes in this File Line Coverage Branch Coverage Complexity
CourseDataService
0%
0/61
0%
0/26
2.25
 
 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.ArrayList;
 19  
 import java.util.HashMap;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 
 23  
 import org.apache.log4j.Logger;
 24  
 import org.kuali.student.common.dto.DtoConstants;
 25  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 26  
 import org.kuali.student.common.exceptions.OperationFailedException;
 27  
 import org.kuali.student.common.search.dto.SearchRequest;
 28  
 import org.kuali.student.common.search.dto.SearchResult;
 29  
 import org.kuali.student.common.ui.server.gwt.AbstractDataService;
 30  
 import org.kuali.student.common.validation.dto.ValidationResultInfo;
 31  
 import org.kuali.student.common.versionmanagement.dto.VersionDisplayInfo;
 32  
 import org.kuali.student.core.assembly.transform.ProposalWorkflowFilter;
 33  
 import org.kuali.student.lum.course.dto.CourseCrossListingInfo;
 34  
 import org.kuali.student.lum.course.dto.CourseInfo;
 35  
 import org.kuali.student.lum.course.service.CourseService;
 36  
 import org.kuali.student.lum.course.service.CourseServiceConstants;
 37  
 import org.kuali.student.lum.lu.LUConstants;
 38  
 import org.kuali.student.lum.lu.service.LuService;
 39  
 import org.kuali.student.lum.lu.service.LuServiceConstants;
 40  
 import org.kuali.student.lum.program.service.ProgramServiceConstants;
 41  
 import org.springframework.util.StringUtils;
 42  
 
 43  0
 public class CourseDataService extends AbstractDataService {
 44  
 
 45  
         private static final long serialVersionUID = 1L;
 46  0
         final static Logger LOG = Logger.getLogger(CourseDataService.class);
 47  
 
 48  
         private static final String DEFAULT_METADATA_STATE = DtoConstants.STATE_DRAFT;
 49  
         
 50  
         private CourseService courseService;
 51  
         private LuService luService;
 52  
 
 53  
         @Override
 54  
         protected Object get(String id) throws Exception {
 55  0
                 CourseInfo courseInfo = null;
 56  
 
 57  
                 try {
 58  0
                         courseInfo = courseService.getCourse(id);
 59  0
                 } catch (DoesNotExistException dne) {
 60  0
                         LOG.info("Course not found for key " + id + ". Course loaded from proposal instead.");
 61  0
                 }                
 62  
                 
 63  0
                 return courseInfo; 
 64  
         }
 65  
 
 66  
         @Override
 67  
         protected Object save(Object dto, Map<String, Object> properties) throws Exception {
 68  0
                 CourseInfo courseInfo = (CourseInfo)dto;
 69  
                 
 70  
                 //Set derived course fields before saving/updating
 71  0
                 courseInfo = calculateCourseDerivedFields(courseInfo);
 72  
                 
 73  0
                 if(properties!=null&&(LUConstants.PROPOSAL_TYPE_COURSE_MODIFY.equals((String)properties.get(ProposalWorkflowFilter.WORKFLOW_DOC_TYPE))||
 74  
                                 LUConstants.PROPOSAL_TYPE_COURSE_MODIFY_ADMIN.equals((String)properties.get(ProposalWorkflowFilter.WORKFLOW_DOC_TYPE)))){
 75  
                         //For Modify Course, see if we need to create a new version instead of create
 76  0
                         if(courseInfo.getId() == null){
 77  
                             
 78  0
                             if (isLatestVersion(courseInfo.getVersionInfo().getVersionIndId())){
 79  0
                             String courseIndId = courseInfo.getVersionInfo().getVersionIndId();
 80  
                             
 81  
                             //Get the currentCourse from the service
 82  0
                             VersionDisplayInfo versionInfo = courseService.getCurrentVersion(CourseServiceConstants.COURSE_NAMESPACE_URI, courseIndId);
 83  0
                             CourseInfo originalCourseInfo = courseService.getCourse(versionInfo.getId());
 84  
                             
 85  
                                     //Save the start and end terms from the old version and put into filter properties
 86  0
                                     String startTerm = originalCourseInfo.getStartTerm();
 87  0
                                     String endTerm = originalCourseInfo.getEndTerm();
 88  0
                                     Map<String,String> proposalAttributes = new HashMap<String,String>();
 89  0
                                     if(startTerm!=null)
 90  0
                                             proposalAttributes.put("prevStartTerm",startTerm);
 91  0
                                     if(endTerm!=null)
 92  0
                                             proposalAttributes.put("prevEndTerm",endTerm);
 93  
                                     
 94  0
                                     properties.put(ProposalWorkflowFilter.PROPOSAL_ATTRIBUTES, proposalAttributes);
 95  
                                     
 96  0
                                 courseInfo = courseService.createNewCourseVersion(courseInfo.getVersionInfo().getVersionIndId(), courseInfo.getVersionInfo().getVersionComment());
 97  0
                             } else {
 98  0
                                 throw new OperationFailedException("Error creating new version for course, this course is currently under modification.");
 99  
                             }
 100  
                         }else{
 101  0
                                 courseInfo = courseService.updateCourse(courseInfo);
 102  
                         }
 103  
                 }else{
 104  0
                         if (courseInfo.getId() == null){
 105  0
                                 courseInfo = courseService.createCourse(courseInfo);
 106  
                         } else {
 107  0
                                 courseInfo = courseService.updateCourse(courseInfo);
 108  
                         }
 109  
                 }
 110  0
                 return courseInfo;
 111  
         }
 112  
         
 113  
         
 114  
         @Override
 115  
         protected List<ValidationResultInfo> validate(Object dto) throws Exception {
 116  0
                 return courseService.validateCourse("OBJECT", (CourseInfo)dto);
 117  
         }
 118  
 
 119  
         @Override
 120  
         protected String getDefaultMetaDataState() {
 121  0
                 return DEFAULT_METADATA_STATE;
 122  
         }
 123  
 
 124  
         @Override
 125  
         protected String getDefaultWorkflowDocumentType() {
 126  0
                 return LUConstants.PROPOSAL_TYPE_COURSE_CREATE;
 127  
         }
 128  
 
 129  
         @Override
 130  
         protected boolean checkDocumentLevelPermissions() {
 131  0
                 return true;
 132  
         }
 133  
 
 134  
         @Override
 135  
         protected Class<?> getDtoClass() {
 136  0
                 return CourseInfo.class;
 137  
         }
 138  
 
 139  
         public void setCourseService(CourseService courseService) {
 140  0
                 this.courseService = courseService;
 141  0
         }
 142  
 
 143  
         public void setLuService(LuService luService) {
 144  0
         this.luService = luService;
 145  0
     }
 146  
 
 147  
     /**
 148  
          * This calculates and sets fields on course object that are derived from other course object fields.
 149  
          */
 150  
         protected CourseInfo calculateCourseDerivedFields(CourseInfo courseInfo){
 151  
                 //Course code is not populated in UI, need to derive them from the subject area and suffix fields
 152  0
                 if(StringUtils.hasText(courseInfo.getCourseNumberSuffix()) && StringUtils.hasText(courseInfo.getSubjectArea())){
 153  0
                         courseInfo.setCode(calculateCourseCode(courseInfo.getSubjectArea(),courseInfo.getCourseNumberSuffix()));
 154  
                 }
 155  
                 
 156  
                 //Derive course code for crosslistings
 157  0
                 for(CourseCrossListingInfo crossListing:courseInfo.getCrossListings()){
 158  0
                          if(StringUtils.hasText(crossListing.getCourseNumberSuffix()) && StringUtils.hasText(crossListing.getSubjectArea())){
 159  0
                                  crossListing.setCode(calculateCourseCode(crossListing.getSubjectArea(), crossListing.getCourseNumberSuffix()));         
 160  
                          }
 161  
                 }
 162  
                 
 163  0
                 return courseInfo;
 164  
         }
 165  
         
 166  
         /**
 167  
          * 
 168  
          * This method calculates code for course and cross listed course.
 169  
          * 
 170  
          * @param subjectArea
 171  
          * @param suffixNumber
 172  
          * @return
 173  
          */
 174  
         private String calculateCourseCode(String subjectArea, String suffixNumber) {
 175  0
             return subjectArea + suffixNumber;
 176  
         }
 177  
         
 178  
         public Boolean isLatestVersion(String versionIndId) throws Exception {
 179  0
             VersionDisplayInfo currentVersion = luService.getCurrentVersion(LuServiceConstants.CLU_NAMESPACE_URI, versionIndId);
 180  
         //Perform a search to see if there are any new versions of the course that are approved, draft, etc.
 181  
         //We don't want to version if there are
 182  0
         SearchRequest request = new SearchRequest("lu.search.isVersionable");
 183  0
         request.addParam("lu.queryParam.versionIndId", versionIndId);
 184  0
         request.addParam("lu.queryParam.sequenceNumber", currentVersion.getSequenceNumber().toString());
 185  0
         List<String> states = new ArrayList<String>();
 186  0
         states.add("Approved");
 187  0
         states.add("Active");
 188  0
         states.add("Draft");
 189  0
         states.add("Superseded");
 190  0
         request.addParam("lu.queryParam.luOptionalState", states);
 191  0
         SearchResult result = luService.search(request);
 192  
         
 193  0
         String resultString = result.getRows().get(0).getCells().get(0).getValue();
 194  0
         return "0".equals(resultString);
 195  
     }
 196  
 }