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  
                                     //Save the start and end terms from the old version and put into filter properties
 78  0
                                     String startTerm = courseInfo.getStartTerm();
 79  0
                                     String endTerm = courseInfo.getEndTerm();
 80  0
                                     Map<String,String> proposalAttributes = new HashMap<String,String>();
 81  0
                                     if(startTerm!=null)
 82  0
                                             proposalAttributes.put("prevStartTerm",startTerm);
 83  0
                                     if(endTerm!=null)
 84  0
                                             proposalAttributes.put("prevEndTerm",endTerm);
 85  
                                     
 86  0
                                     properties.put(ProposalWorkflowFilter.PROPOSAL_ATTRIBUTES, proposalAttributes);
 87  
                                     
 88  0
                                 courseInfo = courseService.createNewCourseVersion(courseInfo.getVersionInfo().getVersionIndId(), courseInfo.getVersionInfo().getVersionComment());
 89  0
                             } else {
 90  0
                                 throw new OperationFailedException("Error creating new version for course, this course is currently under modification.");
 91  
                             }
 92  
                         }else{
 93  0
                                 courseInfo = courseService.updateCourse(courseInfo);
 94  
                         }
 95  
                 }else{
 96  0
                         if (courseInfo.getId() == null){
 97  0
                                 courseInfo = courseService.createCourse(courseInfo);
 98  
                         } else {
 99  0
                                 courseInfo = courseService.updateCourse(courseInfo);
 100  
                         }
 101  
                 }
 102  0
                 return courseInfo;
 103  
         }
 104  
         
 105  
         
 106  
         @Override
 107  
         protected List<ValidationResultInfo> validate(Object dto) throws Exception {
 108  0
                 return courseService.validateCourse("OBJECT", (CourseInfo)dto);
 109  
         }
 110  
 
 111  
         @Override
 112  
         protected String getDefaultMetaDataState() {
 113  0
                 return DEFAULT_METADATA_STATE;
 114  
         }
 115  
 
 116  
         @Override
 117  
         protected String getDefaultWorkflowDocumentType() {
 118  0
                 return LUConstants.PROPOSAL_TYPE_COURSE_CREATE;
 119  
         }
 120  
 
 121  
         @Override
 122  
         protected boolean checkDocumentLevelPermissions() {
 123  0
                 return true;
 124  
         }
 125  
 
 126  
         @Override
 127  
         protected Class<?> getDtoClass() {
 128  0
                 return CourseInfo.class;
 129  
         }
 130  
 
 131  
         public void setCourseService(CourseService courseService) {
 132  0
                 this.courseService = courseService;
 133  0
         }
 134  
 
 135  
         public void setLuService(LuService luService) {
 136  0
         this.luService = luService;
 137  0
     }
 138  
 
 139  
     /**
 140  
          * This calculates and sets fields on course object that are derived from other course object fields.
 141  
          */
 142  
         protected CourseInfo calculateCourseDerivedFields(CourseInfo courseInfo){
 143  
                 //Course code is not populated in UI, need to derive them from the subject area and suffix fields
 144  0
                 if(StringUtils.hasText(courseInfo.getCourseNumberSuffix()) && StringUtils.hasText(courseInfo.getSubjectArea())){
 145  0
                         courseInfo.setCode(calculateCourseCode(courseInfo.getSubjectArea(),courseInfo.getCourseNumberSuffix()));
 146  
                 }
 147  
                 
 148  
                 //Derive course code for crosslistings
 149  0
                 for(CourseCrossListingInfo crossListing:courseInfo.getCrossListings()){
 150  0
                          if(StringUtils.hasText(crossListing.getCourseNumberSuffix()) && StringUtils.hasText(crossListing.getSubjectArea())){
 151  0
                                  crossListing.setCode(calculateCourseCode(crossListing.getSubjectArea(), crossListing.getCourseNumberSuffix()));         
 152  
                          }
 153  
                 }
 154  
                 
 155  0
                 return courseInfo;
 156  
         }
 157  
         
 158  
         /**
 159  
          * 
 160  
          * This method calculates code for course and cross listed course.
 161  
          * 
 162  
          * @param subjectArea
 163  
          * @param suffixNumber
 164  
          * @return
 165  
          */
 166  
         private String calculateCourseCode(String subjectArea, String suffixNumber) {
 167  0
             return subjectArea + suffixNumber;
 168  
         }
 169  
         
 170  
         public Boolean isLatestVersion(String versionIndId) throws Exception {
 171  0
             VersionDisplayInfo currentVersion = luService.getCurrentVersion(LuServiceConstants.CLU_NAMESPACE_URI, versionIndId);
 172  
         //Perform a search to see if there are any new versions of the course that are approved, draft, etc.
 173  
         //We don't want to version if there are
 174  0
         SearchRequest request = new SearchRequest("lu.search.isVersionable");
 175  0
         request.addParam("lu.queryParam.versionIndId", versionIndId);
 176  0
         request.addParam("lu.queryParam.sequenceNumber", currentVersion.getSequenceNumber().toString());
 177  0
         List<String> states = new ArrayList<String>();
 178  0
         states.add("Approved");
 179  0
         states.add("Active");
 180  0
         states.add("Draft");
 181  0
         states.add("Superseded");
 182  0
         request.addParam("lu.queryParam.luOptionalState", states);
 183  0
         SearchResult result = luService.search(request);
 184  
         
 185  0
         String resultString = result.getRows().get(0).getCells().get(0).getValue();
 186  0
         return "0".equals(resultString);
 187  
     }
 188  
 }