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