View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.student.enrollment.class1.krms.controller;
17  
18  import org.kuali.rice.krad.uif.UifConstants;
19  import org.kuali.rice.krad.uif.UifParameters;
20  import org.kuali.rice.krad.util.KRADConstants;
21  import org.kuali.rice.krad.web.form.DocumentFormBase;
22  import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
23  import org.kuali.rice.krad.web.form.UifFormBase;
24  import org.kuali.rice.krms.controller.RuleEditorController;
25  import org.kuali.rice.krms.dto.RuleEditor;
26  import org.kuali.rice.krms.util.AgendaUtilities;
27  import org.kuali.rice.krms.util.KRMSConstants;
28  import org.kuali.rice.krms.util.PropositionTreeUtil;
29  import org.kuali.student.common.uif.util.KSControllerHelper;
30  import org.kuali.student.enrollment.class1.krms.dto.CORuleManagementWrapper;
31  import org.kuali.student.enrollment.class1.krms.util.EnrolKRMSConstants;
32  import org.kuali.student.lum.lu.ui.krms.dto.CluSetInformation;
33  import org.kuali.student.lum.lu.ui.krms.dto.CluSetRangeInformation;
34  import org.kuali.student.lum.lu.ui.krms.dto.LUPropositionEditor;
35  import org.kuali.student.lum.lu.ui.krms.dto.LURuleEditor;
36  import org.kuali.student.lum.lu.ui.krms.service.impl.LURuleViewHelperServiceImpl;
37  import org.kuali.student.lum.lu.ui.krms.util.CluSetRangeHelper;
38  import org.kuali.student.r2.lum.clu.dto.MembershipQueryInfo;
39  import org.springframework.validation.BindingResult;
40  import org.springframework.web.bind.annotation.ModelAttribute;
41  import org.springframework.web.bind.annotation.RequestMapping;
42  import org.springframework.web.servlet.ModelAndView;
43  
44  import javax.servlet.http.HttpServletRequest;
45  import javax.servlet.http.HttpServletResponse;
46  
47  /**
48   * Override of RuleEditorController for Student
49   *
50   * @author Kuali Student Team
51   */
52  public class EnrolRuleEditorController extends RuleEditorController {
53  
54      /**
55       * Setups a new <code>MaintenanceDocumentView</code> with the edit maintenance
56       * action
57       */
58      @RequestMapping(params = "methodToCall=" + KRADConstants.Maintenance.METHOD_TO_CALL_EDIT)
59      public ModelAndView maintenanceEdit(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
60                                          HttpServletRequest request, HttpServletResponse response) throws Exception {
61  
62          setupMaintenance(form, request, KRADConstants.MAINTENANCE_EDIT_ACTION);
63  
64          return getUIFModelAndView(form);
65      }
66  
67      /**
68       *
69       * @param form
70       * @param result
71       * @param request
72       * @param response
73       * @return
74       */
75      @Override
76      @RequestMapping(params = "methodToCall=route")
77      public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
78                                HttpServletRequest request, HttpServletResponse response) {
79          super.route(form, result, request, response);
80          return back(form, result, request, response);
81      }
82  
83      /**
84       *
85       * @param form
86       * @param result
87       * @param request
88       * @param response
89       * @return
90       */
91      @RequestMapping(params = "methodToCall=cancel")
92      @Override
93      public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
94                                 HttpServletRequest request, HttpServletResponse response) {
95  
96          DocumentFormBase documentForm = (DocumentFormBase) form;
97          performWorkflowAction(documentForm, UifConstants.WorkflowAction.CANCEL, false);
98  
99          return back(form,result,request,response);
100     }
101 
102     /**
103      *
104      * @param form
105      * @param result
106      * @param request
107      * @param response
108      * @return
109      */
110     @Override
111     @RequestMapping(params = "methodToCall=addRule")
112     public ModelAndView addRule(@ModelAttribute("KualiForm") UifFormBase form, @SuppressWarnings("unused") BindingResult result,
113                                 @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) {
114 
115         //Clear the client state on new edit rule.
116         form.getClientStateForSyncing().clear();
117         MaintenanceDocumentForm document = (MaintenanceDocumentForm) form;
118 
119         RuleEditor ruleEditor = AgendaUtilities.getSelectedRuleEditor(document);
120         LURuleEditor enrolRuleEditor = new LURuleEditor(ruleEditor.getKey(), true, ruleEditor.getRuleTypeInfo());
121         enrolRuleEditor.setParent(ruleEditor.getParent());
122         AgendaUtilities.getRuleWrapper(document).setRuleEditor(enrolRuleEditor);
123 
124         this.getViewHelper(form).refreshInitTrees(enrolRuleEditor);
125 
126         if(!form.getActionParameters().containsKey(UifParameters.NAVIGATE_TO_PAGE_ID)){
127             form.getActionParameters().put(UifParameters.NAVIGATE_TO_PAGE_ID, KRMSConstants.KRMS_RULE_MAINTENANCE_PAGE_ID);
128         }
129         return super.navigate(form, result, request, response);
130     }
131 
132     /**
133      * Controller method used to display the lightbox with the list of courses in the selected range.
134      *
135      * @param form
136      * @param result
137      * @param request
138      * @param response
139      * @return
140      */
141     @RequestMapping(params="methodToCall=viewCourseRange")
142     public ModelAndView viewCourseRange(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
143                                              HttpServletRequest request, HttpServletResponse response) {
144 
145         MaintenanceDocumentForm document = (MaintenanceDocumentForm) form;
146         CORuleManagementWrapper ruleWrapper = (CORuleManagementWrapper) document.getDocument().getNewMaintainableObject().getDataObject();
147         LUPropositionEditor proposition = (LUPropositionEditor) PropositionTreeUtil.getProposition(ruleWrapper.getRuleEditor());
148 
149         String index = form.getActionParameters().get("selectedIndex");
150         if ((proposition.getCluSet() != null) && (proposition.getCluSet().getCluSetRanges()!=null)){
151             ruleWrapper.setClusInRange(proposition.getCluSet().getCluSetRanges().get(Integer.valueOf(index)).getClusInRange());
152         }
153 
154         return showDialog(EnrolKRMSConstants.KSKRMS_DIALOG_COURSERANGE_LOOKUP, form, request, response);
155      }
156 
157     /**
158      * Controller method used to add a new course range to the list of course ranges.
159      *
160      * @param form
161      * @param result
162      * @param request
163      * @param response
164      * @return
165      */
166     @RequestMapping(params = "methodToCall=addRange")
167     public ModelAndView addRange(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
168                                       HttpServletRequest request, HttpServletResponse response) {
169 
170         LURuleEditor rule = (LURuleEditor) getRuleEditor(form);
171         LUPropositionEditor prop = (LUPropositionEditor) PropositionTreeUtil.getProposition(rule);
172 
173         if(prop.getCluSet()==null){
174             prop.setCluSet(new CluSetInformation());
175         }
176 
177         //Build the membershipquery
178         CluSetRangeHelper rangeHelper = prop.getCluSet().getRangeHelper();
179         if(!rangeHelper.validateCourseRange(prop)){
180             return getUIFModelAndView(form);
181         }
182 
183         MembershipQueryInfo membershipQueryInfo = rangeHelper.buildMembershipQuery();
184 
185         //Build the cluset range wrapper object
186         CluSetRangeInformation cluSetRange = new CluSetRangeInformation();
187         cluSetRange.setCluSetRangeLabel(rangeHelper.buildLabelFromQuery(membershipQueryInfo));
188         cluSetRange.setMembershipQueryInfo(membershipQueryInfo);
189         cluSetRange.setClusInRange(this.getViewHelper(form).getCoursesInRange(membershipQueryInfo));
190 
191         if(!rangeHelper.validateCoursesInRange(prop, cluSetRange)) {
192             return getUIFModelAndView(form);
193         }
194 
195         prop.getCluSet().getCluSetRanges().add(cluSetRange);
196 
197         //Reset range helper to clear values on screen.
198         rangeHelper.reset();
199 
200         return getUIFModelAndView(form);
201     }
202 
203     /**
204      *
205      * @param form
206      * @return
207      */
208     protected LURuleViewHelperServiceImpl getViewHelper(UifFormBase form) {
209         return (LURuleViewHelperServiceImpl) KSControllerHelper.getViewHelperService(form);
210     }
211 }