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