Coverage Report - org.kuali.student.lum.program.server.MajorDisciplineProposalRpcServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
MajorDisciplineProposalRpcServlet
0%
0/87
0%
0/29
2.438
MajorDisciplineProposalRpcServlet$1
0%
0/1
N/A
2.438
 
 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.search.dto.SearchRequest;
 13  
 import org.kuali.student.common.search.dto.SearchResult;
 14  
 import org.kuali.student.common.ui.client.service.DataSaveResult;
 15  
 import org.kuali.student.common.ui.server.gwt.DataGwtServlet;
 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.lu.service.LuService;
 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.MajorDisciplineProposalRpcService;
 28  
 import org.kuali.student.lum.program.dto.ProgramRequirementInfo;
 29  
 import org.kuali.student.lum.program.service.ProgramService;
 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  
     private LuService luService;
 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  0
                 throw new UnsupportedOperationException("Proposals can not update state explicitly");                 
 53  
         }
 54  
         
 55  
     public List<ProgramRequirementInfo> getProgramRequirements(List<String> programRequirementIds) throws Exception {
 56  
 
 57  0
         List<ProgramRequirementInfo> programReqInfos = new ArrayList<ProgramRequirementInfo>();
 58  
 
 59  0
         for (String programReqId : programRequirementIds) {
 60  0
             ProgramRequirementInfo rule = programService.getProgramRequirement(programReqId, null, null);
 61  0
             setProgReqNL(rule);
 62  0
             programReqInfos.add(rule);
 63  0
         }
 64  
 
 65  0
         return programReqInfos;
 66  
     }
 67  
 
 68  
     public Map<Integer, ProgramRequirementInfo> storeProgramRequirements(Map<Integer, ProgramRequirementsDataModel.requirementState> states,
 69  
                                                                         Map<Integer, ProgramRequirementInfo> progReqs) throws Exception {
 70  0
         Map<Integer, ProgramRequirementInfo> storedRules = new HashMap<Integer, ProgramRequirementInfo>();
 71  
 
 72  0
         for (Integer key : progReqs.keySet()) {
 73  0
             ProgramRequirementInfo rule = progReqs.get(key);
 74  0
             switch (states.get(key)) {
 75  
                 case STORED:
 76  
                     //rule was not changed so continue
 77  0
                     storedRules.put(key, null);
 78  0
                     break;
 79  
                 case ADDED:
 80  0
                     storedRules.put(key, createProgramRequirement(rule));
 81  0
                     break;
 82  
                 case EDITED:
 83  0
                     storedRules.put(key, updateProgramRequirement(rule));
 84  0
                     break;
 85  
                 case DELETED:
 86  0
                     storedRules.put(key, null);
 87  0
                     deleteProgramRequirement(rule.getId());
 88  0
                     break;
 89  
                 default:
 90  
                     break;
 91  
             }
 92  0
         }
 93  0
         return storedRules;
 94  
     }
 95  
 
 96  
  
 97  
     public ProgramRequirementInfo createProgramRequirement(ProgramRequirementInfo programRequirementInfo) throws Exception {
 98  
 
 99  
         // If this requirement is using a temporary statement ID set the state to null
 100  0
         if (programRequirementInfo.getId().indexOf(ProgramRequirementsSummaryView.NEW_PROG_REQ_ID) >= 0) {
 101  0
             programRequirementInfo.setId(null);
 102  
         }
 103  
         
 104  
         // Strip the temporary statement IDs and allow permanent IDs to be created when written to the web service
 105  0
         StatementUtil.stripStatementIds(programRequirementInfo.getStatement());
 106  
         
 107  
         // Update the state of the statement tree to match the state of the requirement
 108  
         // Note: the requirement state already matches the program state (e.g. Draft, Approved, etc)
 109  0
         StatementUtil.updateStatementTreeViewInfoState(programRequirementInfo.getState(), programRequirementInfo.getStatement());
 110  
        
 111  
         // Call the web service to create the requirement and statement tree in the database
 112  0
         ProgramRequirementInfo rule = programService.createProgramRequirement(programRequirementInfo);
 113  
         
 114  
         // Translate the requirement into its natural language equivalent
 115  0
         setProgReqNL(rule);
 116  
 
 117  0
         return rule;
 118  
     }
 119  
 
 120  
     public StatusInfo deleteProgramRequirement(String programRequirementId) throws Exception {
 121  0
         return programService.deleteProgramRequirement(programRequirementId);
 122  
     }
 123  
 
 124  
     public ProgramRequirementInfo updateProgramRequirement(ProgramRequirementInfo programRequirementInfo) throws Exception {
 125  
         
 126  
         // Strip the temporary statement IDs and allow permanent IDs to be created when written to the web service
 127  0
         StatementUtil.stripStatementIds(programRequirementInfo.getStatement());
 128  
 
 129  
         // Update the state of the statement tree to match the state of the requirement
 130  
         // Note: the requirement state already matches the program state (e.g. Draft, Approved, etc)
 131  0
         StatementUtil.updateStatementTreeViewInfoState(programRequirementInfo.getState(), programRequirementInfo.getStatement());
 132  
         
 133  
         //TODO temporary fix - see KSLUM 1421
 134  0
         if (programRequirementInfo.getDescr() == null) {
 135  0
             programRequirementInfo.setDescr(new RichTextInfo());    
 136  
         }
 137  
 
 138  0
         ProgramRequirementInfo rule = programService.updateProgramRequirement(programRequirementInfo);
 139  0
         setProgReqNL(rule);
 140  0
         return rule;
 141  
     }
 142  
 
 143  
     private void setProgReqNL(ProgramRequirementInfo programRequirementInfo) throws Exception {
 144  0
         setReqCompNL(programRequirementInfo.getStatement());
 145  0
     }
 146  
 
 147  
     private void setReqCompNL(StatementTreeViewInfo tree) throws Exception {
 148  0
         List<StatementTreeViewInfo> statements = tree.getStatements();
 149  0
         List<ReqComponentInfo> reqComponentInfos = tree.getReqComponents();
 150  
 
 151  0
          if ((statements != null) && (statements.size() > 0)) {
 152  
             // retrieve all statements
 153  0
             for (StatementTreeViewInfo statement : statements) {
 154  0
                 setReqCompNL(statement); // inside set the children of this statementTreeViewInfo
 155  
             }
 156  0
         } else if ((reqComponentInfos != null) && (reqComponentInfos.size() > 0)) {
 157  
             // retrieve all req. component LEAFS
 158  0
                 for (int i = 0; i < reqComponentInfos.size(); i++) {
 159  0
                         ReqComponentInfoUi reqUi = RulesUtil.clone(reqComponentInfos.get(i));
 160  0
                         reqUi.setNaturalLanguageTranslation(statementService.translateReqComponentToNL(reqUi, "KUALI.RULE", "en"));
 161  0
                         reqUi.setPreviewNaturalLanguageTranslation(statementService.translateReqComponentToNL(reqUi, "KUALI.RULE.PREVIEW", "en"));
 162  0
                         reqComponentInfos.set(i, reqUi);
 163  
                 }
 164  
         }
 165  0
     }
 166  
     
 167  
     @Override
 168  
         public Boolean isLatestVersion(String versionIndId, Long versionSequenceNumber) throws Exception {
 169  
             //Perform a search to see if there are any new versions of the course that are approved, draft, etc.
 170  
             //We don't want to version if there are
 171  
             try{
 172  0
                     SearchRequest request = new SearchRequest("lu.search.isVersionable");
 173  0
                     request.addParam("lu.queryParam.versionIndId", versionIndId);
 174  0
                     request.addParam("lu.queryParam.sequenceNumber", versionSequenceNumber.toString());
 175  0
                     List<String> states = new ArrayList<String>();
 176  0
                     states.add("Approved");
 177  0
                     states.add("Active");
 178  0
                     states.add("Draft");
 179  0
                     states.add("Superseded");
 180  0
                     request.addParam("lu.queryParam.luOptionalState", states);
 181  0
                     SearchResult result = luService.search(request);
 182  
                     
 183  0
                     String resultString = result.getRows().get(0).getCells().get(0).getValue();
 184  0
                     return "0".equals(resultString);                    
 185  0
             } catch (Exception e) {
 186  0
                     LOG.error("IS LATEST VERSION ERROR");
 187  0
                     return false;
 188  
                 }
 189  
             
 190  
         }
 191  
 
 192  
         public void setProgramService(ProgramService programService) {
 193  0
         this.programService = programService;
 194  0
     }
 195  
 
 196  
     public void setStatementService(StatementService statementService) {
 197  0
         this.statementService = statementService;
 198  0
     }
 199  
 
 200  
     public void setStateChangeService(StateChangeService stateChangeService) {
 201  0
         this.stateChangeService = stateChangeService;
 202  0
     }
 203  
     
 204  
     /**
 205  
      * 
 206  
      * This method will check to see if an object with the given reference ID is a proposal.
 207  
      * <p>
 208  
      * At the moment, it is used by the UI to decide if we should hide the action box when
 209  
      * opening a draft proposal.
 210  
      * 
 211  
      * @see org.kuali.student.lum.program.client.rpc.MajorDisciplineRpcService#isProposal(java.lang.String, java.lang.String)
 212  
      */
 213  
     @Override
 214  
     public Boolean isProposal(String referenceTypeKey, String referenceId){
 215  
         try {
 216  
             // Wire in proposal service from spring
 217  
             // Call method getProposalByReference().  
 218  
             // ProposalWorkflowFilter.applyOutboundDataFilter().  Set on line 130-131.  Use these for reference ID.
 219  
            
 220  
             // Ask the proposal service to return a list of proposals with this reference id    
 221  0
             List<ProposalInfo> proposals = proposalService.getProposalsByReference(referenceTypeKey, referenceId);
 222  
             
 223  
             // If at least one proposal is returned, this is a proposal, so return true
 224  0
             if (proposals != null && proposals.size() >= 1){
 225  0
                 return new Boolean(true);
 226  
             }
 227  
             
 228  
             // This was not a proposal, so return false
 229  0
             return  new Boolean(false);
 230  
             }
 231  0
             catch(Exception ex){
 232  
                 // Log exception 
 233  0
                 ex.printStackTrace();
 234  0
                 throw new RuntimeException(ex);
 235  
             }
 236  
      }
 237  
     
 238  
     /**
 239  
      * 
 240  
      * Proposal service is injected by spring in the lum-gwt-context.xml file
 241  
      * 
 242  
      * @return
 243  
      */
 244  
     public ProposalService getProposalService() {
 245  0
         return proposalService;
 246  
     }
 247  
     
 248  
     public void setProposalService(ProposalService proposalService) {
 249  0
         this.proposalService = proposalService;
 250  0
     }
 251  
     
 252  
         public void setLuService(LuService luService) {
 253  0
                 this.luService = luService;
 254  0
         }
 255  
     
 256  
     
 257  
 }