Coverage Report - org.kuali.student.lum.program.server.MajorDisciplineRpcServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
MajorDisciplineRpcServlet
0%
0/105
0%
0/31
2.625
MajorDisciplineRpcServlet$1
0%
0/1
N/A
2.625
 
 1  
 package org.kuali.student.lum.program.server;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.HashMap;
 5  
 import java.util.List;
 6  
 import java.util.Map;
 7  
 
 8  
 import org.apache.log4j.Logger;
 9  
 import org.kuali.student.common.assembly.data.Data;
 10  
 import org.kuali.student.common.dto.RichTextInfo;
 11  
 import org.kuali.student.common.dto.StatusInfo;
 12  
 import org.kuali.student.common.exceptions.DataValidationErrorException;
 13  
 import org.kuali.student.common.search.dto.SearchRequest;
 14  
 import org.kuali.student.common.search.dto.SearchResult;
 15  
 import org.kuali.student.common.ui.client.service.DataSaveResult;
 16  
 import org.kuali.student.common.ui.server.gwt.DataGwtServlet;
 17  
 import org.kuali.student.core.proposal.dto.ProposalInfo;
 18  
 import org.kuali.student.core.proposal.service.ProposalService;
 19  
 import org.kuali.student.core.statement.dto.ReqComponentInfo;
 20  
 import org.kuali.student.core.statement.dto.StatementTreeViewInfo;
 21  
 import org.kuali.student.core.statement.service.StatementService;
 22  
 import org.kuali.student.core.statement.ui.client.widgets.rules.ReqComponentInfoUi;
 23  
 import org.kuali.student.core.statement.ui.client.widgets.rules.RulesUtil;
 24  
 import org.kuali.student.lum.common.server.StatementUtil;
 25  
 import org.kuali.student.lum.lu.service.LuService;
 26  
 import org.kuali.student.lum.program.client.ProgramConstants;
 27  
 import org.kuali.student.lum.program.client.requirements.ProgramRequirementsDataModel;
 28  
 import org.kuali.student.lum.program.client.requirements.ProgramRequirementsSummaryView;
 29  
 import org.kuali.student.lum.program.client.rpc.MajorDisciplineRpcService;
 30  
 import org.kuali.student.lum.program.dto.ProgramRequirementInfo;
 31  
 import org.kuali.student.lum.program.service.ProgramService;
 32  
 
 33  0
 public class MajorDisciplineRpcServlet extends DataGwtServlet implements MajorDisciplineRpcService {
 34  
 
 35  
     public static final String PREVIOUS_VERSION_INFO = "proposal";
 36  
     
 37  0
     final Logger LOG = Logger.getLogger(MajorDisciplineRpcServlet.class);
 38  
     
 39  
     private static final long serialVersionUID = 1L;
 40  
 
 41  
     private ProposalService proposalService;
 42  
     private ProgramService programService;
 43  
     private StatementService statementService;
 44  
     protected StateChangeService stateChangeService;
 45  
     private LuService luService;
 46  
  
 47  
     /**
 48  
      * 
 49  
      * This method will update the state of a major discipline. 
 50  
      * 
 51  
      * @see org.kuali.student.lum.program.client.rpc.MajorDisciplineRpcService#updateStatus(org.kuali.student.common.assembly.data.Data, java.lang.String)
 52  
      */
 53  
         public DataSaveResult updateState(Data data, String state ) throws Exception {
 54  
              try {
 55  
                 // Pull program ID from model
 56  0
                 String programId = data.get(ProgramConstants.ID);
 57  
                 
 58  
                 // Pull endEntryTerm and endEnrollTerm from model
 59  
                 // These are set using drop downs when a program is activated
 60  0
                   Data previousVersionInfo = data.query(PREVIOUS_VERSION_INFO);
 61  0
                   String endEntryTerm = null;
 62  0
                   String endEnrollTerm = null;
 63  0
                   String endInstAdmitTerm = null;
 64  0
                  if (previousVersionInfo != null) {
 65  0
                      endEntryTerm = previousVersionInfo.get(ProgramConstants.PREV_END_PROGRAM_ENTRY_TERM); 
 66  0
               endEnrollTerm = previousVersionInfo.get(ProgramConstants.PREV_END_PROGRAM_ENROLL_TERM);
 67  0
               endInstAdmitTerm = previousVersionInfo.get(ProgramConstants.PREV_END_INST_ADMIN_TERM);
 68  0
               stateChangeService.changeState(endEntryTerm, endEnrollTerm, endInstAdmitTerm, programId, state);
 69  
                  }
 70  
                  else{
 71  
                     // previousVersionInfo is null if this is the first version 
 72  0
                     stateChangeService.changeState( programId, state);  
 73  
                  }
 74  
           
 75  
     
 76  
                 // Return updates to view
 77  0
                     DataSaveResult result = new DataSaveResult();
 78  0
                     result.setValue(data);
 79  0
                     return result;
 80  0
              } catch(DataValidationErrorException e){
 81  0
                      LOG.error("Error Updating Major Dicipline State", e);                 
 82  0
                      DataSaveResult result = new DataSaveResult();
 83  0
                     result.setValidationResults(e.getValidationResults());
 84  0
                     return result;
 85  0
              } catch(Exception e){
 86  0
                      LOG.error("Error Updating Major Dicipline State", e);                 
 87  0
                  throw e;
 88  
              }
 89  
                  
 90  
         }
 91  
     public List<ProgramRequirementInfo> getProgramRequirements(List<String> programRequirementIds) throws Exception {
 92  
 
 93  0
         List<ProgramRequirementInfo> programReqInfos = new ArrayList<ProgramRequirementInfo>();
 94  
 
 95  0
         for (String programReqId : programRequirementIds) {
 96  0
             ProgramRequirementInfo rule = programService.getProgramRequirement(programReqId, null, null);
 97  0
             setProgReqNL(rule);
 98  0
             programReqInfos.add(rule);
 99  0
         }
 100  
 
 101  0
         return programReqInfos;
 102  
     }
 103  
 
 104  
     public Map<Integer, ProgramRequirementInfo> storeProgramRequirements(Map<Integer, ProgramRequirementsDataModel.requirementState> states,
 105  
                                                                         Map<Integer, ProgramRequirementInfo> progReqs) throws Exception {
 106  0
         Map<Integer, ProgramRequirementInfo> storedRules = new HashMap<Integer, ProgramRequirementInfo>();
 107  
 
 108  0
         for (Integer key : progReqs.keySet()) {
 109  0
             ProgramRequirementInfo rule = progReqs.get(key);
 110  0
             switch (states.get(key)) {
 111  
                 case STORED:
 112  
                     //rule was not changed so continue
 113  0
                     storedRules.put(key, null);
 114  0
                     break;
 115  
                 case ADDED:
 116  0
                     storedRules.put(key, createProgramRequirement(rule));
 117  0
                     break;
 118  
                 case EDITED:
 119  0
                     storedRules.put(key, updateProgramRequirement(rule));
 120  0
                     break;
 121  
                 case DELETED:
 122  0
                     storedRules.put(key, null);
 123  0
                     deleteProgramRequirement(rule.getId());
 124  0
                     break;
 125  
                 default:
 126  
                     break;
 127  
             }
 128  0
         }
 129  0
         return storedRules;
 130  
     }
 131  
 
 132  
  
 133  
     public ProgramRequirementInfo createProgramRequirement(ProgramRequirementInfo programRequirementInfo) throws Exception {
 134  
 
 135  
         // If this requirement is using a temporary statement ID set the state to null
 136  0
         if (programRequirementInfo.getId().indexOf(ProgramRequirementsSummaryView.NEW_PROG_REQ_ID) >= 0) {
 137  0
             programRequirementInfo.setId(null);
 138  
         }
 139  
         
 140  
         // Strip the temporary statement IDs and allow permanent IDs to be created when written to the web service
 141  0
         StatementUtil.stripStatementIds(programRequirementInfo.getStatement());
 142  
         
 143  
         // Update the state of the statement tree to match the state of the requirement
 144  
         // Note: the requirement state already matches the program state (e.g. Draft, Approved, etc)
 145  0
         StatementUtil.updateStatementTreeViewInfoState(programRequirementInfo.getState(), programRequirementInfo.getStatement());
 146  
        
 147  
         // Call the web service to create the requirement and statement tree in the database
 148  0
         ProgramRequirementInfo rule = programService.createProgramRequirement(programRequirementInfo);
 149  
         
 150  
         // Translate the requirement into its natural language equivalent
 151  0
         setProgReqNL(rule);
 152  
 
 153  0
         return rule;
 154  
     }
 155  
 
 156  
     public StatusInfo deleteProgramRequirement(String programRequirementId) throws Exception {
 157  0
         return programService.deleteProgramRequirement(programRequirementId);
 158  
     }
 159  
 
 160  
     public ProgramRequirementInfo updateProgramRequirement(ProgramRequirementInfo programRequirementInfo) throws Exception {
 161  
         
 162  
         // Strip the temporary statement IDs and allow permanent IDs to be created when written to the web service
 163  0
         StatementUtil.stripStatementIds(programRequirementInfo.getStatement());
 164  
 
 165  
         // Update the state of the statement tree to match the state of the requirement
 166  
         // Note: the requirement state already matches the program state (e.g. Draft, Approved, etc)
 167  0
         StatementUtil.updateStatementTreeViewInfoState(programRequirementInfo.getState(), programRequirementInfo.getStatement());
 168  
         
 169  
         //TODO temporary fix - see KSLUM 1421
 170  0
         if (programRequirementInfo.getDescr() == null) {
 171  0
             programRequirementInfo.setDescr(new RichTextInfo());    
 172  
         }
 173  
 
 174  0
         ProgramRequirementInfo rule = programService.updateProgramRequirement(programRequirementInfo);
 175  0
         setProgReqNL(rule);
 176  0
         return rule;
 177  
     }
 178  
 
 179  
     private void setProgReqNL(ProgramRequirementInfo programRequirementInfo) throws Exception {
 180  0
         setReqCompNL(programRequirementInfo.getStatement());
 181  0
     }
 182  
 
 183  
     private void setReqCompNL(StatementTreeViewInfo tree) throws Exception {
 184  0
         List<StatementTreeViewInfo> statements = tree.getStatements();
 185  0
         List<ReqComponentInfo> reqComponentInfos = tree.getReqComponents();
 186  
 
 187  0
          if ((statements != null) && (statements.size() > 0)) {
 188  
             // retrieve all statements
 189  0
             for (StatementTreeViewInfo statement : statements) {
 190  0
                 setReqCompNL(statement); // inside set the children of this statementTreeViewInfo
 191  
             }
 192  0
         } else if ((reqComponentInfos != null) && (reqComponentInfos.size() > 0)) {
 193  
             // retrieve all req. component LEAFS
 194  0
                 for (int i = 0; i < reqComponentInfos.size(); i++) {
 195  0
                         ReqComponentInfoUi reqUi = RulesUtil.clone(reqComponentInfos.get(i));
 196  0
                         reqUi.setNaturalLanguageTranslation(statementService.translateReqComponentToNL(reqUi, "KUALI.RULE", "en"));
 197  0
                         reqUi.setPreviewNaturalLanguageTranslation(statementService.translateReqComponentToNL(reqUi, "KUALI.RULE.PREVIEW", "en"));
 198  0
                         reqComponentInfos.set(i, reqUi);
 199  
                 }
 200  
         }
 201  0
     }
 202  
     
 203  
     @Override
 204  
         public Boolean isLatestVersion(String versionIndId, Long versionSequenceNumber) throws Exception {
 205  
             //Perform a search to see if there are any new versions of the course that are approved, draft, etc.
 206  
             //We don't want to version if there are
 207  0
             SearchRequest request = new SearchRequest("lu.search.isVersionable");
 208  0
             request.addParam("lu.queryParam.versionIndId", versionIndId);
 209  0
             request.addParam("lu.queryParam.sequenceNumber", versionSequenceNumber.toString());
 210  0
             List<String> states = new ArrayList<String>();
 211  0
             states.add("Approved");
 212  0
             states.add("Active");
 213  0
             states.add("Draft");
 214  0
             states.add("Superseded");
 215  0
             request.addParam("lu.queryParam.luOptionalState", states);
 216  0
             SearchResult result = luService.search(request);
 217  
             
 218  0
             String resultString = result.getRows().get(0).getCells().get(0).getValue();
 219  0
             return "0".equals(resultString);                    
 220  
          }
 221  
 
 222  
         public void setProgramService(ProgramService programService) {
 223  0
         this.programService = programService;
 224  0
     }
 225  
 
 226  
     public void setStatementService(StatementService statementService) {
 227  0
         this.statementService = statementService;
 228  0
     }
 229  
 
 230  
     public void setStateChangeService(StateChangeService stateChangeService) {
 231  0
         this.stateChangeService = stateChangeService;
 232  0
     }
 233  
    
 234  
     /**
 235  
      * 
 236  
      * This method will check to see if an object with the given reference ID is a proposal.
 237  
      * <p>
 238  
      * At the moment, it is used by the UI to decide if we should hide the action box when
 239  
      * opening a draft proposal.
 240  
      * 
 241  
      * @see org.kuali.student.lum.program.client.rpc.MajorDisciplineRpcService#isProposal(java.lang.String, java.lang.String)
 242  
      */
 243  
     @Override
 244  
     public Boolean isProposal(String referenceTypeKey, String referenceId){
 245  
         try {
 246  
         // Wire in proposal service from spring
 247  
         // Call method getProposalByReference().  
 248  
         // ProposalWorkflowFilter.applyOutboundDataFilter().  Set on line 130-131.  Use these for reference ID.
 249  
        
 250  
         // Ask the proposal service to return a list of proposals with this reference id    
 251  0
         List<ProposalInfo> proposals = proposalService.getProposalsByReference(referenceTypeKey, referenceId);
 252  
         
 253  
         // If at least one proposal is returned, this is a proposal, so return true
 254  0
         if (proposals != null && proposals.size() >= 1){
 255  0
             return new Boolean(true);
 256  
         }
 257  
         
 258  
         // This was not a proposal, so return false
 259  0
         return  new Boolean(false);
 260  
         }
 261  0
         catch(Exception ex){
 262  
             // Log exception 
 263  0
             ex.printStackTrace();
 264  0
             throw new RuntimeException(ex);
 265  
         }
 266  
       
 267  
     }
 268  
     
 269  
     /**
 270  
      * 
 271  
      * Proposal service is injected by spring in the lum-gwt-context.xml file
 272  
      * 
 273  
      * @return
 274  
      */
 275  
     public ProposalService getProposalService() {
 276  0
         return proposalService;
 277  
     }
 278  
     
 279  
     public void setProposalService(ProposalService proposalService) {
 280  0
         this.proposalService = proposalService;
 281  
 
 282  0
     }
 283  
     
 284  
         public void setLuService(LuService luService) {
 285  0
                 this.luService = luService;
 286  0
         }
 287  
  
 288  
 
 289  
 }