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