View Javadoc

1   package org.kuali.student.enrollment.class1.krms.controller;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.rice.krad.uif.UifParameters;
5   import org.kuali.rice.krad.util.GlobalVariables;
6   import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
7   import org.kuali.rice.krad.web.form.UifFormBase;
8   import org.kuali.rice.krms.dto.AgendaEditor;
9   import org.kuali.rice.krms.dto.PropositionEditor;
10  import org.kuali.rice.krms.dto.RuleEditor;
11  import org.kuali.rice.krms.dto.RuleManagementWrapper;
12  import org.kuali.rice.krms.util.AgendaUtilities;
13  import org.kuali.rice.krms.util.KRMSConstants;
14  import org.kuali.student.enrollment.class1.krms.dto.AORuleEditor;
15  import org.kuali.student.enrollment.class1.krms.dto.AORuleManagementWrapper;
16  import org.kuali.student.enrollment.class1.krms.service.impl.AORuleViewHelperServiceImpl;
17  import org.kuali.student.enrollment.class1.krms.util.EnrolKRMSConstants;
18  import org.kuali.student.lum.lu.ui.krms.dto.LURuleEditor;
19  import org.kuali.student.enrollment.class1.krms.service.impl.AORuleEditorMaintainableImpl;
20  import org.springframework.stereotype.Controller;
21  import org.springframework.validation.BindingResult;
22  import org.springframework.web.bind.annotation.ModelAttribute;
23  import org.springframework.web.bind.annotation.RequestMapping;
24  import org.springframework.web.servlet.ModelAndView;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  import java.util.Map;
29  
30  /**
31   * Override of RuleEditorController for Student
32   *
33   * @author Kuali Student Team
34   */
35  @Controller
36  @RequestMapping(value = "/activityOfferingRules")
37  public class AORuleEditorController extends EnrolRuleEditorController {
38  
39      /**
40       * @param form
41       * @param result
42       * @param request
43       * @param response
44       * @return
45       */
46      @Override
47      @RequestMapping(params = "methodToCall=addRule")
48      public ModelAndView addRule(@ModelAttribute("KualiForm") UifFormBase form, @SuppressWarnings("unused") BindingResult result,
49                                  @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) {
50  
51          form.getActionParameters().put(UifParameters.NAVIGATE_TO_PAGE_ID, EnrolKRMSConstants.KSKRMS_RULE_AO_MAINTENANCE_PAGE_ID);
52          //Clear the client state on new edit rule.
53          form.getClientStateForSyncing().clear();
54  
55          AORuleEditor ruleEditor = (AORuleEditor) AgendaUtilities.retrieveSelectedRuleEditor((MaintenanceDocumentForm) form);
56  
57          this.getViewHelper(form).refreshInitTrees(ruleEditor);
58          return super.navigate(form, result, request, response);
59      }
60  
61      /**
62       * Method used to invoke the CO inquiry view from Manage Course Offering screen while search input is Course Offering
63       * Code (04a screen)
64       *
65       * @param form
66       * @param result
67       * @param request
68       * @param response
69       * @return
70       */
71      @RequestMapping(params = "methodToCall=goToRuleView")
72      public ModelAndView goToRuleView(@ModelAttribute("KualiForm") UifFormBase form, @SuppressWarnings("unused") BindingResult result,
73                                       @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) {
74  
75          form.getActionParameters().put(UifParameters.NAVIGATE_TO_PAGE_ID, EnrolKRMSConstants.KSKRMS_RULE_AO_MAINTENANCE_PAGE_ID);
76          return super.goToRuleView(form, result, request, response);
77      }
78  
79      /**
80       * Reverts rule to previous state and navigates to agenda maintenance page.
81       *
82       * @param form
83       * @param result
84       * @param request
85       * @param response
86       * @return
87       */
88      @RequestMapping(params = "methodToCall=cancelEditRule")
89      public ModelAndView cancelEditRule(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
90                                         HttpServletRequest request, HttpServletResponse response) {
91  
92          form.getActionParameters().put(UifParameters.NAVIGATE_TO_PAGE_ID, EnrolKRMSConstants.KSKRMS_AGENDA_AO_MAINTENANCE_PAGE_ID);
93          return super.cancelEditRule(form, result, request, response);
94      }
95  
96      /**
97       * Updates rule and redirects to agenda maintenance page.
98       *
99       * @param form
100      * @param result
101      * @param request
102      * @param response
103      * @return
104      */
105     @RequestMapping(params = "methodToCall=updateRule")
106     public ModelAndView updateRule(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
107                                    HttpServletRequest request, HttpServletResponse response) {
108 
109         form.getActionParameters().put(UifParameters.NAVIGATE_TO_PAGE_ID, EnrolKRMSConstants.KSKRMS_AGENDA_AO_MAINTENANCE_PAGE_ID);
110         return super.updateRule(form, result, request, response);
111     }
112 
113     /**
114      * Retrieves selected proposition key and initializes edit on propostion.
115      *
116      * @param form
117      * @param result
118      * @param request
119      * @param response
120      * @return
121      */
122     @RequestMapping(params = "methodToCall=getSelectedKey")
123     public ModelAndView getSelectedKey(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
124                                        HttpServletRequest request, HttpServletResponse response) {
125 
126         //Clear the current states of the tabs to open the first tab again with the edit tree.
127         Map<String, String> states = (Map<String, String>) form.getClientStateForSyncing().get(EnrolKRMSConstants.KSKRMS_RULE_AO_TABS_ID);
128         states.put(KRMSConstants.KRMS_PARM_ACTIVE_TAB, EnrolKRMSConstants.KSKRMS_RULE_AO_EDITWITHOBJECT_ID);
129 
130         //Set the selected rule statement key.
131         String selectedKey = request.getParameter(KRMSConstants.KRMS_PARM_SELECTED_KEY);
132         getRuleEditor(form).setSelectedKey(selectedKey);
133 
134         return this.goToEditProposition(form, result, request, response);
135     }
136 
137     /**
138      * Test method for a controller that invokes a dialog lightbox.
139      *
140      * @param form     - test form
141      * @param result   - Spring form binding result
142      * @param request  - http request
143      * @param response - http response
144      * @return
145      */
146     @RequestMapping(params = "methodToCall=compareRules")
147     public ModelAndView compareRules(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
148                                      HttpServletRequest request, HttpServletResponse response) {
149 
150         MaintenanceDocumentForm document = (MaintenanceDocumentForm) form;
151         AORuleManagementWrapper ruleWrapper = (AORuleManagementWrapper) document.getDocument().getNewMaintainableObject().getDataObject();
152 
153         RuleEditor aoRuleEditor = ruleWrapper.getRuleEditor();
154 
155         if(aoRuleEditor == null) {
156             String ruleId = document.getActionParamaterValue(KRMSConstants.KRMS_PARM_RULE_KEY);
157             if ((ruleId != null) && (StringUtils.isNotBlank(ruleId))) {
158                 //Get a specific ruleEditor based on the ruleId.
159                 aoRuleEditor = AgendaUtilities.getSelectedRuleEditor(ruleWrapper, ruleId);
160             }
161         }
162 
163         RuleEditor cluRuleEditor = ((AORuleEditor) aoRuleEditor).getCluEditor();
164         //Build the compare rule tree
165         ruleWrapper.setCompareTree(this.getViewHelper(form).buildCompareTree(aoRuleEditor, cluRuleEditor));
166         ruleWrapper.setCompareLightBoxHeader(aoRuleEditor.getRuleTypeInfo().getDescription());
167 
168         // redirect back to client to display lightbox
169         return showDialog(EnrolKRMSConstants.KSKRMS_DIALOG_COMPARE_CLU_CO_AO, form, request, response);
170     }
171 
172     /**
173      * Test method for a controller that invokes a dialog lightbox.
174      *
175      * @param form     - test form
176      * @param result   - Spring form binding result
177      * @param request  - http request
178      * @param response - http response
179      * @return
180      */
181     @RequestMapping(params = "methodToCall=viewCoAndCluRules")
182     public ModelAndView viewCoAndCluRules(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
183                                           HttpServletRequest request, HttpServletResponse response) {
184 
185         MaintenanceDocumentForm document = (MaintenanceDocumentForm) form;
186         Object dataObject = document.getDocument().getNewMaintainableObject().getDataObject();
187         if (dataObject instanceof AORuleManagementWrapper) {
188             AORuleManagementWrapper ruleWrapper = (AORuleManagementWrapper) dataObject;
189             String ruleId = document.getActionParamaterValue("ruleKey");
190             RuleEditor aoRuleEditor = null;
191             if ((ruleId != null) && (StringUtils.isNotBlank(ruleId))) {
192                 //Get a specific ruleEditor based on the ruleId.
193                 aoRuleEditor = AgendaUtilities.getSelectedRuleEditor(ruleWrapper, ruleId);
194             } else {
195                 //Get the current editing ruleEditor.
196                 aoRuleEditor = ruleWrapper.getRuleEditor();
197             }
198 
199             RuleEditor cluRuleEditor = ((AORuleEditor) aoRuleEditor).getCluEditor();
200             //Build the compare rule tree
201             ruleWrapper.setCompareTree(this.getViewHelper(form).buildMultiViewTree(aoRuleEditor.getParent(), cluRuleEditor));
202             ruleWrapper.setCompareLightBoxHeader(aoRuleEditor.getRuleTypeInfo().getDescription());
203         }
204 
205         // redirect back to client to display lightbox
206         return showDialog(EnrolKRMSConstants.KSKRMS_DIALOG_VIEW_CLU_CO, form, request, response);
207     }
208 
209     /**
210      * Deletes selected rule from agenda on Manage Course Requistes page
211      *
212      * @param form
213      * @param result
214      * @param request
215      * @param response
216      * @return
217      */
218     @RequestMapping(params = "methodToCall=deleteRuleStatements")
219     public ModelAndView deleteRuleStatements(@ModelAttribute("KualiForm") UifFormBase form, @SuppressWarnings("unused") BindingResult result,
220                                              @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) {
221 
222         MaintenanceDocumentForm document = (MaintenanceDocumentForm) form;
223         RuleManagementWrapper ruleWrapper = AgendaUtilities.getRuleWrapper(document);
224         String ruleKey = AgendaUtilities.getRuleKey(document);
225 
226         AgendaEditor agenda = AgendaUtilities.getSelectedAgendaEditor(ruleWrapper, ruleKey);
227         if (agenda != null) {
228             RuleEditor ruleEditor = agenda.getRuleEditors().get(ruleKey);
229             if(ruleEditor.getProposition() != null){
230             ruleEditor.setProposition(null);
231             }
232             else{
233                 form.getClientStateForSyncing().clear();
234                 ruleEditor.setDummy(false);
235             }
236             //Compare rule with parent rule.
237             compareRulePropositions((MaintenanceDocumentForm) form, ruleEditor);
238             this.getViewHelper(form).refreshViewTree(ruleEditor);
239 
240         }
241 
242         return getUIFModelAndView(document);
243     }
244 
245     /**
246      *
247      * @param form
248      * @param result
249      * @param request
250      * @param response
251      * @return
252      */
253     @RequestMapping(params = "methodToCall=copyEditCoRule")
254     public ModelAndView copyEditCoRule(@ModelAttribute("KualiForm") UifFormBase form, @SuppressWarnings("unused") BindingResult result,
255                                      @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) {
256 
257         //Clear the client state on new edit rule.
258         form.getClientStateForSyncing().clear();
259         MaintenanceDocumentForm document = (MaintenanceDocumentForm) form;
260 
261         AORuleEditor ruleEditor = (AORuleEditor) AgendaUtilities.getSelectedRuleEditor(document);
262         AORuleEditor enrolRuleEditor = new AORuleEditor(ruleEditor.getKey(), false, ruleEditor.getRuleTypeInfo());
263         enrolRuleEditor.setParent(ruleEditor.getParent());
264         enrolRuleEditor.setCluEditor(ruleEditor.getCluEditor());
265 
266         PropositionEditor parentProp = ruleEditor.getParent().getPropositionEditor();
267         AORuleEditorMaintainableImpl aoMaintainable = (AORuleEditorMaintainableImpl) document.getDocument().getNewMaintainableObject();
268         aoMaintainable.initPropositionEditor(parentProp);
269 
270         enrolRuleEditor.setProposition(((AORuleViewHelperServiceImpl) this.getViewHelper(form)).copyCOProposition(parentProp));
271         AgendaUtilities.getRuleWrapper(document).setRuleEditor(enrolRuleEditor);
272 
273         this.getViewHelper(form).refreshInitTrees(enrolRuleEditor);
274         form.getActionParameters().put(UifParameters.NAVIGATE_TO_PAGE_ID, EnrolKRMSConstants.KSKRMS_RULE_AO_MAINTENANCE_PAGE_ID);
275 
276         return super.navigate(form, result, request, response);
277 
278     }
279 
280     /**
281      * Deletes selected rule from agenda on Manage Course Requistes page
282      *
283      * @param form
284      * @param result
285      * @param request
286      * @param response
287      * @return
288      */
289     @Override
290     @RequestMapping(params = "methodToCall=deleteRule")
291     public ModelAndView deleteRule(@ModelAttribute("KualiForm") UifFormBase form, @SuppressWarnings("unused") BindingResult result,
292                                    @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) {
293 
294         MaintenanceDocumentForm document = (MaintenanceDocumentForm) form;
295         RuleManagementWrapper ruleWrapper = AgendaUtilities.getRuleWrapper(document);
296         String ruleKey = AgendaUtilities.getRuleKey(document);
297 
298         AgendaEditor agenda = AgendaUtilities.getSelectedAgendaEditor(ruleWrapper, ruleKey);
299         if (agenda != null) {
300             AORuleEditor ruleEditor = (AORuleEditor) agenda.getRuleEditors().get(ruleKey);
301 
302             //Only add rules to delete list that are already persisted.
303             if (ruleEditor.getId() != null) {
304                 agenda.getDeletedRules().add(ruleEditor);
305             }
306 
307             AORuleEditor dummyRule = new AORuleEditor(ruleEditor.getKey(), true, ruleEditor.getRuleTypeInfo());
308             dummyRule.setParent(ruleEditor.getParent());
309             dummyRule.setCluEditor(ruleEditor.getCluEditor());
310             agenda.getRuleEditors().put(ruleEditor.getKey(), dummyRule);
311         }
312 
313         return getUIFModelAndView(document);
314     }
315 
316     protected void compareRulePropositions(MaintenanceDocumentForm form, RuleEditor ruleEditor) {
317 
318         RuleManagementWrapper ruleWrapper = (RuleManagementWrapper) form.getDocument().getNewMaintainableObject().getDataObject();
319 
320         //Compare CO to CLU and display info message
321         if (ruleEditor.getProposition() != null) {
322             if (!this.getViewHelper(form).compareRules(ruleWrapper.getRuleEditor())) {
323                 GlobalVariables.getMessageMap().putInfoForSectionId(KRMSConstants.KRMS_RULE_TREE_GROUP_ID, EnrolKRMSConstants.KSKRMS_MSG_INFO_AO_RULE_CHANGED);
324             } else if (GlobalVariables.getMessageMap().containsMessageKey(KRMSConstants.KRMS_RULE_TREE_GROUP_ID)) {
325                 GlobalVariables.getMessageMap().removeAllInfoMessagesForProperty(KRMSConstants.KRMS_RULE_TREE_GROUP_ID);
326             }
327         }
328     }
329 
330 }
331 
332