Coverage Report - org.kuali.student.lum.lu.ui.course.server.gwt.CourseRpcGwtServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
CourseRpcGwtServlet
0%
0/99
0%
0/61
4
CourseRpcGwtServlet$1
0%
0/1
N/A
4
 
 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.Date;
 19  
 import java.util.HashMap;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 
 23  
 import org.kuali.student.common.ui.client.widgets.rules.ReqComponentInfoUi;
 24  
 import org.kuali.student.common.ui.client.widgets.rules.RulesUtil;
 25  
 import org.kuali.student.common.ui.server.gwt.DataGwtServlet;
 26  
 import org.kuali.student.core.dto.StatusInfo;
 27  
 import org.kuali.student.core.exceptions.InvalidParameterException;
 28  
 import org.kuali.student.core.statement.dto.ReqComponentInfo;
 29  
 import org.kuali.student.core.statement.dto.StatementTreeViewInfo;
 30  
 import org.kuali.student.core.statement.service.StatementService;
 31  
 import org.kuali.student.core.versionmanagement.dto.VersionDisplayInfo;
 32  
 import org.kuali.student.lum.common.client.lu.LUUIConstants;
 33  
 import org.kuali.student.lum.course.dto.CourseInfo;
 34  
 import org.kuali.student.lum.course.service.CourseService;
 35  
 import org.kuali.student.lum.course.service.CourseServiceConstants;
 36  
 import org.kuali.student.lum.lu.ui.course.client.requirements.CourseRequirementsDataModel;
 37  
 import org.kuali.student.lum.lu.ui.course.client.service.CourseRpcService;
 38  
 import org.springframework.transaction.annotation.Transactional;
 39  
 
 40  
 @Transactional(readOnly = true, rollbackFor={Throwable.class})
 41  0
 public class CourseRpcGwtServlet extends DataGwtServlet implements CourseRpcService {
 42  
 
 43  
         private static final long serialVersionUID = 1L;
 44  
 
 45  
     private CourseService courseService;
 46  
         private StatementService statementService;
 47  
 
 48  
     @Override
 49  
     public List<StatementTreeViewInfo> getCourseStatements(String courseId, String nlUsageTypeKey, String language) throws Exception {
 50  0
         List<StatementTreeViewInfo> rules = courseService.getCourseStatements(courseId, nlUsageTypeKey, language);
 51  0
         if (rules != null) {
 52  0
                 for (StatementTreeViewInfo rule : rules) {
 53  0
                         setReqCompNL(rule);
 54  
                 }
 55  
         }
 56  0
         return rules;
 57  
     }
 58  
 
 59  
     public Map<Integer, StatementTreeViewInfo> storeCourseStatements(String courseId, Map<Integer, CourseRequirementsDataModel.requirementState> states,
 60  
                                                                         Map<Integer, StatementTreeViewInfo> rules) throws Exception {
 61  
 
 62  0
         Map<Integer, StatementTreeViewInfo> storedRules = new HashMap<Integer, StatementTreeViewInfo>();
 63  
 
 64  0
         for (Integer key : rules.keySet()) {
 65  0
             StatementTreeViewInfo rule = rules.get(key);
 66  0
             switch (states.get(key)) {
 67  
                 case STORED:
 68  
                     //rule was not changed so continue
 69  0
                     storedRules.put(key, null);
 70  0
                     break;
 71  
                 case ADDED:
 72  0
                     storedRules.put(key, createCourseStatement(courseId, rule));
 73  0
                     break;
 74  
                 case EDITED:
 75  0
                     storedRules.put(key, updateCourseStatement(courseId, rule));
 76  0
                     break;
 77  
                 case DELETED:
 78  0
                     storedRules.put(key, null);
 79  0
                     deleteCourseStatement(courseId, rule);
 80  0
                     break;
 81  
                 default:
 82  0
                     break;
 83  
             }
 84  0
         }
 85  0
         return storedRules;
 86  
     }
 87  
 
 88  
     @Override
 89  
     public StatementTreeViewInfo createCourseStatement(String courseId, StatementTreeViewInfo statementTreeViewInfo) throws Exception {
 90  0
         statementTreeViewInfo.setState("Active");
 91  0
         CourseRequirementsDataModel.stripStatementIds(statementTreeViewInfo);
 92  0
         StatementTreeViewInfo rule = courseService.createCourseStatement(courseId, statementTreeViewInfo);
 93  0
         setReqCompNL(rule);
 94  0
         return rule;
 95  
     }
 96  
 
 97  
     @Override
 98  
     public StatusInfo deleteCourseStatement(String courseId, StatementTreeViewInfo statementTreeViewInfo) throws Exception {
 99  0
         return courseService.deleteCourseStatement(courseId, statementTreeViewInfo);
 100  
     }
 101  
 
 102  
     @Override
 103  
     public StatementTreeViewInfo updateCourseStatement(String courseId, StatementTreeViewInfo statementTreeViewInfo) throws Exception {
 104  0
         statementTreeViewInfo.setState("Active");
 105  0
         CourseRequirementsDataModel.stripStatementIds(statementTreeViewInfo);
 106  0
         StatementTreeViewInfo rule = courseService.updateCourseStatement(courseId, statementTreeViewInfo);
 107  0
         setReqCompNL(rule);
 108  0
         return rule;
 109  
     }
 110  
 
 111  
     public StatusInfo changeState(String courseId, String newState) throws Exception {
 112  0
             return changeState(courseId, newState, null);
 113  
     }
 114  
 
 115  
     public StatusInfo changeState(String courseId, String newState, Date currentVersionStart) throws Exception {
 116  0
             CourseInfo thisVerCourse = courseService.getCourse(courseId);
 117  0
             String prevState = thisVerCourse.getState();
 118  0
                 String verIndId = thisVerCourse.getVersionInfo().getVersionIndId();
 119  0
                 String test = "test";
 120  0
                 VersionDisplayInfo curVerDisplayInfo = courseService.getCurrentVersion(CourseServiceConstants.COURSE_NAMESPACE_URI, verIndId);
 121  0
                 String curVerId = curVerDisplayInfo.getId();
 122  0
                 CourseInfo currVerCourse = courseService.getCourse(curVerId);
 123  0
                 String currVerState = currVerCourse.getState();
 124  0
                 boolean isCurrVer = (courseId.equals(currVerCourse.getId()));
 125  
 
 126  0
                 StatusInfo ret = new StatusInfo();
 127  
                 try {
 128  0
                         if (newState.equals(LUUIConstants.LU_STATE_ACTIVE)) {
 129  0
                                 if (prevState.equals(LUUIConstants.LU_STATE_APPROVED)) {
 130  
                                         // since this is approved if isCurrVer we can assume there are no previously active versions to deal with
 131  0
                                         if (isCurrVer) {
 132  
                                                 // setstate for thisVerCourse and setCurrentVersion(courseId)
 133  0
                                                 updateCourseVersionStates(thisVerCourse, newState, currVerCourse, null, true, currentVersionStart);
 134  0
                                         } else if (currVerState.equals(LUUIConstants.LU_STATE_ACTIVE) ||
 135  
                                                         currVerState.equals(LUUIConstants.LU_STATE_INACTIVE)) {
 136  0
                                                 updateCourseVersionStates(thisVerCourse, newState, currVerCourse, LUUIConstants.LU_STATE_SUPERSEDED, true, currentVersionStart);
 137  
                                         }                                        
 138  
 
 139  0
                                 } else if (prevState.equals(LUUIConstants.LU_STATE_INACTIVE)) {
 140  
 
 141  
                                 }
 142  
                         }
 143  0
                         ret.setSuccess(new Boolean(true));
 144  0
                 } catch (Exception e) {
 145  0
                         ret.setSuccess(new Boolean(false));
 146  0
                         ret.setMessage(e.getMessage());
 147  0
                 }
 148  
 
 149  0
                 return ret;
 150  
     }
 151  
 
 152  
     /**
 153  
      * Based on null values, updates states of thisVerCourse and currVerCourse and sets thisVerCourse as the current version.  Attempts to rollback transaction on exception.
 154  
      *
 155  
      * @param thisVerCourse this is the version that the user selected to change the state
 156  
      * @param thisVerNewState this is state that the user selected to change thisVerCourse to
 157  
      * @param currVerCourse this is the current version of the course (currentVersionStartDt <= now && currentVersionEndDt > now)
 158  
      * @param currVerNewState this is the state that we need to set the current version to.  Set to null to not update the currVerCourse state.
 159  
      * @param makeCurrent if true we'll set thisVerCourse as the current version.
 160  
      * @param currentVersionStart the start date for the new current version to start on and the old current version to end on.  Set to null to use now as the start date.
 161  
      * @throws Exception
 162  
      */
 163  
     @Transactional(readOnly = false)
 164  
     private void updateCourseVersionStates (CourseInfo thisVerCourse, String thisVerNewState, CourseInfo currVerCourse, String currVerNewState, boolean makeCurrent, Date currentVersionStart) throws Exception {
 165  0
             String thisVerPrevState = thisVerCourse.getState();
 166  0
             String currVerPrevState = currVerCourse.getState();
 167  
             
 168  
             // if already current, will throw error if you try to make the current version the current version.
 169  0
             boolean isCurrent = thisVerCourse.getId().equals(currVerCourse.getId());
 170  0
             makeCurrent &= !isCurrent;                
 171  
             
 172  0
             if (thisVerNewState == null) {
 173  0
                     throw new InvalidParameterException("new state cannot be null");
 174  
             } else {
 175  0
                     thisVerCourse.setState(thisVerNewState);
 176  0
                     courseService.updateCourse(thisVerCourse);
 177  
             }
 178  
 
 179  
             // won't get called if previous exception was thrown
 180  0
             if (currVerNewState != null) {
 181  0
                     currVerCourse.setState(currVerNewState);
 182  0
                     courseService.updateCourse(currVerCourse);
 183  
 
 184  
             }
 185  
 
 186  0
             if (makeCurrent == true) {
 187  0
                     courseService.setCurrentCourseVersion(thisVerCourse.getId(), currentVersionStart);
 188  
             }
 189  
             
 190  
             // for all draft and approved courses set the state to superseded.
 191  
             // we should only need to evaluated versions with sequence number
 192  
             // higher than previous active course.  If the course you're 
 193  
             // activating is the current course check all versions. 
 194  0
             if (thisVerPrevState.equals(LUUIConstants.LU_STATE_APPROVED) &&
 195  
                             thisVerNewState.equals(LUUIConstants.LU_STATE_ACTIVE)) {
 196  
 
 197  0
                     List<VersionDisplayInfo> versions = courseService.getVersions(CourseServiceConstants.COURSE_NAMESPACE_URI, thisVerCourse.getVersionInfo().getVersionIndId());                
 198  0
                     Long startSeq = new Long(1);
 199  
                     
 200  0
                         if (!isCurrent && (currVerCourse.getId() != thisVerCourse.getId())) {
 201  0
                                 startSeq = currVerCourse.getVersionInfo().getSequenceNumber() + 1;
 202  
                         }
 203  
                         
 204  0
                         for (VersionDisplayInfo versionInfo: versions) {
 205  0
                             if (versionInfo.getSequenceNumber() >= startSeq) {
 206  0
                                     CourseInfo otherCourse = courseService.getCourse(versionInfo.getId());
 207  0
                                     if (otherCourse.getState().equals(LUUIConstants.LU_STATE_APPROVED) ||
 208  
                                                     otherCourse.getState().equals(LUUIConstants.LU_STATE_SUBMITTED) ||
 209  
                                                     otherCourse.getState().equals(LUUIConstants.LU_STATE_DRAFT)) {
 210  0
                                             otherCourse.setState(LUUIConstants.LU_STATE_SUPERSEDED);
 211  0
                                             courseService.updateCourse(otherCourse);
 212  
                                     }
 213  0
                             }
 214  
                     }                  
 215  
             }
 216  
 
 217  0
     }
 218  
 
 219  
     private void setReqCompNL(StatementTreeViewInfo tree) throws Exception {
 220  0
         List<StatementTreeViewInfo> statements = tree.getStatements();
 221  0
         List<ReqComponentInfo> reqComponentInfos = tree.getReqComponents();
 222  
 
 223  0
          if ((statements != null) && (statements.size() > 0)) {
 224  
             // retrieve all statements
 225  0
             for (StatementTreeViewInfo statement : statements) {
 226  0
                 setReqCompNL(statement); // inside set the children of this statementTreeViewInfo
 227  
             }
 228  0
         } else if ((reqComponentInfos != null) && (reqComponentInfos.size() > 0)) {
 229  
             // retrieve all req. component LEAFS
 230  0
                 for (int i = 0; i < reqComponentInfos.size(); i++) {
 231  0
                         ReqComponentInfoUi reqUi = RulesUtil.clone(reqComponentInfos.get(i));
 232  0
                         reqUi.setNaturalLanguageTranslation(statementService.translateReqComponentToNL(reqUi, "KUALI.RULE", "en"));
 233  0
                         reqUi.setPreviewNaturalLanguageTranslation(statementService.translateReqComponentToNL(reqUi, "KUALI.RULE.PREVIEW", "en"));
 234  0
                         reqComponentInfos.set(i, reqUi);
 235  
                 }
 236  
         }
 237  0
     }
 238  
 
 239  
     public void setCourseService(CourseService courseService) {
 240  0
         this.courseService = courseService;
 241  0
     }
 242  
 
 243  
     public void setStatementService(StatementService statementService) {
 244  0
         this.statementService = statementService;
 245  0
     }
 246  
 }