View Javadoc

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