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