1 package org.kuali.ole.deliver.controller.drools;
2
3 import org.apache.commons.collections.CollectionUtils;
4 import org.apache.commons.lang.StringUtils;
5 import org.apache.log4j.Logger;
6 import org.kuali.ole.deliver.bo.drools.DroolsEditorBo;
7 import org.kuali.ole.deliver.bo.drools.DroolsRuleBo;
8 import org.kuali.ole.deliver.bo.drools.FinesAndLimitsBo;
9 import org.kuali.ole.deliver.drools.DroolFileGenerator;
10 import org.kuali.ole.deliver.drools.DroolsConstants;
11 import org.kuali.ole.deliver.drools.rules.CheckoutDroolsFileGenerator;
12 import org.kuali.ole.deliver.drools.rules.GeneralChecksDroolFileGenerator;
13 import org.kuali.ole.deliver.form.drools.DroolEditorMaintenanceForm;
14 import org.kuali.ole.deliver.maintenance.drools.DroolEditorMaintaintainableImpl;
15 import org.kuali.rice.core.api.config.property.ConfigContext;
16 import org.kuali.rice.krad.datadictionary.DocumentEntry;
17 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
18 import org.kuali.rice.krad.maintenance.MaintenanceUtils;
19 import org.kuali.rice.krad.service.KRADServiceLocator;
20 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
21 import org.kuali.rice.krad.uif.UifConstants;
22 import org.kuali.rice.krad.uif.UifParameters;
23 import org.kuali.rice.krad.uif.container.CollectionGroup;
24 import org.kuali.rice.krad.uif.service.ViewHelperService;
25 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
26 import org.kuali.rice.krad.uif.view.View;
27 import org.kuali.rice.krad.util.GlobalVariables;
28 import org.kuali.rice.krad.util.KRADConstants;
29 import org.kuali.rice.krad.web.controller.MaintenanceDocumentController;
30 import org.kuali.rice.krad.web.form.DocumentFormBase;
31 import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
32 import org.kuali.rice.krad.web.form.UifFormBase;
33 import org.springframework.stereotype.Controller;
34 import org.springframework.validation.BindingResult;
35 import org.springframework.web.bind.annotation.ModelAttribute;
36 import org.springframework.web.bind.annotation.RequestMapping;
37 import org.springframework.web.bind.annotation.RequestMethod;
38 import org.springframework.web.servlet.ModelAndView;
39
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
42 import java.text.SimpleDateFormat;
43 import java.util.*;
44
45
46
47
48 @Controller
49 @RequestMapping(value = "/droolsEditorController")
50 public class DroolsEditorController extends MaintenanceDocumentController {
51 Logger LOG = Logger.getLogger(DroolsEditorController.class);
52
53 @Override
54 protected MaintenanceDocumentForm createInitialForm(HttpServletRequest request) {
55 return new DroolEditorMaintenanceForm();
56 }
57
58 public List<DroolFileGenerator> getDroolFileGenerators() {
59 List<DroolFileGenerator> droolFileGenerators = new ArrayList<>();
60 droolFileGenerators.add(new GeneralChecksDroolFileGenerator());
61 droolFileGenerators.add(new CheckoutDroolsFileGenerator());
62 return droolFileGenerators;
63 }
64
65 @Override
66 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
67 HttpServletRequest request, HttpServletResponse response) {
68 DroolEditorMaintenanceForm droolEditorMaintenanceForm = (DroolEditorMaintenanceForm) form;
69 ModelAndView modelAndView = super.start(droolEditorMaintenanceForm, result, request, response);
70 MaintenanceDocument document = droolEditorMaintenanceForm.getDocument();
71 enableSection(droolEditorMaintenanceForm);
72 document.getDocumentHeader().setDocumentDescription(getDocumentDescriptionForNew());
73 return modelAndView;
74 }
75
76 @Override
77 public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
78 HttpServletRequest request, HttpServletResponse response) {
79
80
81 DroolEditorMaintenanceForm droolEditorMaintenanceForm = (DroolEditorMaintenanceForm) form;
82 MaintenanceDocument document = droolEditorMaintenanceForm.getDocument();
83 DroolsEditorBo droolsEditorBo = (DroolsEditorBo) document.getNewMaintainableObject().getDataObject();
84
85 ModelAndView modelAndView = null;
86 modelAndView = super.route(droolEditorMaintenanceForm, result, request, response);
87 if (!GlobalVariables.getMessageMap().hasErrors()) {
88 generateFile(droolsEditorBo);
89 }
90
91 return modelAndView;
92 }
93
94 private boolean validateDocument(DroolsEditorBo droolsEditorBo) {
95 boolean validContent = true;
96 if(StringUtils.isNotBlank(droolsEditorBo.getEditorType())){
97 if(CollectionUtils.isEmpty(getRuleBoFromCurrentDocumentByType(droolsEditorBo))){
98 validContent = false;
99 }
100 }
101 return validContent;
102 }
103
104 private List<DroolsRuleBo> getRuleBoFromCurrentDocumentByType(DroolsEditorBo droolsEditorBo){
105 if(droolsEditorBo.getEditorType().equalsIgnoreCase(DroolsConstants.EDITOR_TYPE.GENERAL_CHECK)){
106 return droolsEditorBo.getDroolsRuleBos();
107 }else if(droolsEditorBo.getEditorType().equalsIgnoreCase(DroolsConstants.EDITOR_TYPE.CHECKOUT)){
108 return droolsEditorBo.getCheckoutRuleBos();
109 }else if(droolsEditorBo.getEditorType().equalsIgnoreCase(DroolsConstants.EDITOR_TYPE.CHECKIN)){
110 return droolsEditorBo.getCheckinRuleBos();
111 }else if(droolsEditorBo.getEditorType().equalsIgnoreCase(DroolsConstants.EDITOR_TYPE.NOTICE)){
112 return droolsEditorBo.getNoticesRuleBos();
113 }else if(droolsEditorBo.getEditorType().equalsIgnoreCase(DroolsConstants.EDITOR_TYPE.RENEW)){
114 return droolsEditorBo.getRenewRuleBos();
115 }
116 return null;
117 }
118
119 private void generateFile(DroolsEditorBo droolsEditorBo) {
120 List<DroolsRuleBo> droolsRuleBos = getRuleBoFromCurrentDocumentByType(droolsEditorBo);
121
122 for (Iterator<DroolFileGenerator> iterator = getDroolFileGenerators().iterator(); iterator.hasNext(); ) {
123 DroolFileGenerator droolFileGenerator = iterator.next();
124 if (droolFileGenerator.isInterested(droolsEditorBo.getEditorType())) {
125 try {
126 droolFileGenerator.generateFile(droolsRuleBos, droolsEditorBo.getFileName());
127 } catch (Exception e) {
128 LOG.error(e.getMessage());
129 }
130 }
131 }
132 }
133
134
135 @Override
136 public ModelAndView maintenanceEdit(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result, HttpServletRequest request, HttpServletResponse response) throws Exception {
137 DroolEditorMaintenanceForm droolEditorMaintenanceForm = (DroolEditorMaintenanceForm)form;
138 ModelAndView modelAndView = super.maintenanceEdit(droolEditorMaintenanceForm, result, request, response);
139 MaintenanceDocument document = droolEditorMaintenanceForm.getDocument();
140 enableSection(droolEditorMaintenanceForm);
141 droolEditorMaintenanceForm.setEditSection(true);
142 droolEditorMaintenanceForm.setShowFooterSection(true);
143 document.getDocumentHeader().setDocumentDescription(getDocumentDescriptionForEdit());
144 return modelAndView;
145 }
146
147 @RequestMapping(params = "methodToCall=" + "addRule")
148 public ModelAndView addRule(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
149 LOG.debug("Initialized addRule method");
150 DroolEditorMaintenanceForm droolEditorMaintenanceForm = (DroolEditorMaintenanceForm) uifForm;
151 MaintenanceDocument document = (MaintenanceDocument) droolEditorMaintenanceForm.getDocument();
152 DroolsEditorBo droolsEditorBo = (DroolsEditorBo) document.getNewMaintainableObject().getDataObject();
153
154 String selectedCollectionPath = droolEditorMaintenanceForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
155 CollectionGroup collectionGroup = droolEditorMaintenanceForm.getPostedView().getViewIndex().getCollectionGroupByPath(selectedCollectionPath);
156 String addLinePath = collectionGroup.getAddLineBindingInfo().getBindingPath();
157 Object eventObject = ObjectPropertyUtils.getPropertyValue(droolEditorMaintenanceForm, addLinePath);
158 DroolsRuleBo addedDroolRuleBo = (DroolsRuleBo) eventObject;
159 boolean isUniqueRule = true;
160 for (Iterator<DroolsRuleBo> iterator = droolsEditorBo.getDroolsRuleBos().iterator(); iterator.hasNext(); ) {
161 DroolsRuleBo droolsRuleBo = iterator.next();
162 if (addedDroolRuleBo.getRuleName().equalsIgnoreCase(droolsRuleBo.getRuleName())) {
163 isUniqueRule = false;
164 }
165 }
166 if (isUniqueRule) {
167 super.addLine(droolEditorMaintenanceForm, result, request, response);
168 if(validateDocument(droolsEditorBo)){
169 droolEditorMaintenanceForm.setShowFooterSection(true);
170 }else{
171 droolEditorMaintenanceForm.setShowFooterSection(false);
172 }
173 }
174 else{
175 GlobalVariables.getMessageMap().putErrorForSectionId("DroolsEditorBo-MaintenanceView-ruleGridSection", "drools.rule.already.exists", "\"" + addedDroolRuleBo.getRuleName() + "\"");
176 }
177
178 return getUIFModelAndView(uifForm);
179 }
180
181
182 @RequestMapping(params = "methodToCall=" + "addEditor")
183 public ModelAndView addEditor(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result, HttpServletRequest request, HttpServletResponse response) throws Exception {
184 DroolEditorMaintenanceForm droolEditorMaintenanceForm= (DroolEditorMaintenanceForm) uifForm;
185 MaintenanceDocument document = (MaintenanceDocument) droolEditorMaintenanceForm.getDocument();
186 DroolsEditorBo droolsEditorBo = (DroolsEditorBo) document.getNewMaintainableObject().getDataObject();
187 if(droolEditorMaintenanceForm.isDisableAddEditor()){
188 droolsEditorBo = new DroolsEditorBo();
189 document.getNewMaintainableObject().setDataObject(droolsEditorBo);
190 droolEditorMaintenanceForm.setDisableAddEditor(false);
191 droolEditorMaintenanceForm.resetFlagValues();
192 }else{
193 if(isAllowedToCreateRule(droolsEditorBo)){
194 droolEditorMaintenanceForm.setDisableAddEditor(true);
195 enableSection(droolEditorMaintenanceForm);
196 }else{
197 showDialog("droolEditorConformationDialog",uifForm,request,response);
198 }
199 }
200 return getUIFModelAndView(droolEditorMaintenanceForm);
201 }
202
203 private boolean isAllowedToCreateRule(DroolsEditorBo droolsEditorBo) {
204 if (droolsEditorBo.getEditorType().equalsIgnoreCase(DroolsConstants.EDITOR_TYPE.GENERAL_CHECK)) {
205 Map<String,String> editorParamMap = new HashMap<>();
206 editorParamMap.put("editorType", droolsEditorBo.getEditorType());
207 List<DroolsEditorBo> editorBoList = (List<DroolsEditorBo>) KRADServiceLocator.getBusinessObjectService().findMatching(DroolsEditorBo.class,editorParamMap);
208 if(CollectionUtils.isNotEmpty(editorBoList)){
209 return false;
210 }
211 }
212 return true;
213 }
214
215 private String getDateString() {
216 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");
217 return simpleDateFormat.format(new Date());
218 }
219
220 private String getDocumentDescriptionForNew() {
221 return "New-GeneralCheckRule-" +
222 GlobalVariables.getUserSession().getPrincipalName() + "-" +
223 getDateString();
224
225 }
226
227 private String getDocumentDescriptionForEdit() {
228 return "Edit-GeneralCheckRule-" +
229 GlobalVariables.getUserSession().getPrincipalName() + "-" +
230 getDateString();
231 }
232
233 private void enableSection(DroolEditorMaintenanceForm droolEditorMaintenanceForm){
234 droolEditorMaintenanceForm.resetFlagValues();
235 MaintenanceDocument document = (MaintenanceDocument) droolEditorMaintenanceForm.getDocument();
236 DroolsEditorBo droolsEditorBo = (DroolsEditorBo) document.getNewMaintainableObject().getDataObject();
237
238
239
240
241
242
243
244 if(StringUtils.isNotBlank(droolsEditorBo.getEditorType())){
245 if(droolsEditorBo.getEditorType().equalsIgnoreCase(DroolsConstants.EDITOR_TYPE.GENERAL_CHECK)){
246 droolEditorMaintenanceForm.setShowGeneralCheckSection(true);
247 }else if(droolsEditorBo.getEditorType().equalsIgnoreCase(DroolsConstants.EDITOR_TYPE.CHECKOUT)){
248 droolEditorMaintenanceForm.setShowCheckoutSection(true);
249 }else if(droolsEditorBo.getEditorType().equalsIgnoreCase(DroolsConstants.EDITOR_TYPE.CHECKIN)){
250 droolEditorMaintenanceForm.setShowCheckinSection(true);
251 }else if(droolsEditorBo.getEditorType().equalsIgnoreCase(DroolsConstants.EDITOR_TYPE.NOTICE)){
252 droolEditorMaintenanceForm.setShowNoticeSection(true);
253 }else if(droolsEditorBo.getEditorType().equalsIgnoreCase(DroolsConstants.EDITOR_TYPE.RENEW)){
254 droolEditorMaintenanceForm.setShowRenewCheckSection(true);
255 }
256 }
257 }
258
259
260
261 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addFee")
262 public ModelAndView addFee(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
263 HttpServletRequest request, HttpServletResponse response) {
264 LOG.debug("Initialized addLine method");
265 String selectedCollectionPath = uifForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
266 if (StringUtils.isBlank(selectedCollectionPath)) {
267 throw new RuntimeException("Selected collection was not set for add line action, cannot add new line");
268 }
269
270 View view = uifForm.getPostedView();
271 DroolEditorMaintaintainableImpl viewHelperService = (DroolEditorMaintaintainableImpl) view.getViewHelperService();
272 viewHelperService.processCollectionAddLineForFine(view, uifForm, selectedCollectionPath);
273 return getUIFModelAndView(uifForm);
274 }
275
276 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=openDocumentToEdit")
277 public ModelAndView openDocumentToEdit(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
278 HttpServletRequest request, HttpServletResponse response) throws Exception {
279 DroolEditorMaintenanceForm droolEditorMaintenanceForm= (DroolEditorMaintenanceForm) uifForm;
280 MaintenanceDocument document = (MaintenanceDocument) droolEditorMaintenanceForm.getDocument();
281 DroolsEditorBo droolsEditorBo = (DroolsEditorBo) document.getNewMaintainableObject().getDataObject();
282 String editorType = droolsEditorBo.getEditorType();
283 if(StringUtils.isNotBlank(editorType)){
284 Map<String,String> editorParamMap = new HashMap<>();
285 editorParamMap.put("editorType", editorType);
286 List<DroolsEditorBo> editorBoList = (List<DroolsEditorBo>) KRADServiceLocator.getBusinessObjectService().findMatching(DroolsEditorBo.class,editorParamMap);
287 if(CollectionUtils.isNotEmpty(editorBoList)){
288 droolsEditorBo = editorBoList.get(0);
289 }
290 }
291
292 request.setAttribute("editorType", editorType);
293 request.setAttribute("editorId", droolsEditorBo.getEditorId());
294 droolsEditorBo = new DroolsEditorBo();
295 document.getNewMaintainableObject().setDataObject(droolsEditorBo);
296 return maintenanceEdit(droolEditorMaintenanceForm,result,request,response);
297 }
298
299 @Override
300 public ModelAndView deleteLine(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
301 DroolEditorMaintenanceForm droolEditorMaintenanceForm= (DroolEditorMaintenanceForm) uifForm;
302 MaintenanceDocument document = (MaintenanceDocument) droolEditorMaintenanceForm.getDocument();
303 DroolsEditorBo droolsEditorBo = (DroolsEditorBo) document.getNewMaintainableObject().getDataObject();
304 ModelAndView modelAndView = super.deleteLine(droolEditorMaintenanceForm, result, request, response);
305 if(validateDocument(droolsEditorBo)){
306 droolEditorMaintenanceForm.setShowFooterSection(true);
307 }else{
308 droolEditorMaintenanceForm.setShowFooterSection(false);
309 }
310 return modelAndView;
311 }
312
313 @Override
314 protected void setupMaintenance(MaintenanceDocumentForm form, HttpServletRequest request, String maintenanceAction) {
315 MaintenanceDocument document = form.getDocument();
316
317 if (document == null) {
318 document = getMaintenanceDocumentService()
319 .setupNewMaintenanceDocument(form.getDataObjectClassName(), form.getDocTypeName(),
320 maintenanceAction);
321
322 form.setDocument(document);
323 form.setDocTypeName(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
324 }
325
326 form.setMaintenanceAction(maintenanceAction);
327
328 String editorType = (String) request.getAttribute("editorType");
329 Map parameterMap = new HashMap(request.getParameterMap());
330 if(StringUtils.isNotBlank(editorType)){
331 parameterMap.put("editorType", new String[]{editorType});
332 parameterMap.put("viewTypeName", new String[]{"MAINTENANCE"});
333 parameterMap.put("editorId", new String[]{(String)request.getAttribute("editorId")});
334 parameterMap.put("returnLocation", new String[]{ConfigContext.getCurrentContextConfig().getProperty("ole.fs.url.base")});
335 parameterMap.put("methodToCall", new String[]{"maintenanceEdit"});
336 parameterMap.put("dataObjectClassName", new String[]{"org.kuali.ole.deliver.bo.drools.DroolsEditorBo"});
337 }
338
339 getMaintenanceDocumentService().setupMaintenanceObject(document, maintenanceAction, parameterMap);
340
341 if (KRADConstants.MAINTENANCE_NEW_ACTION.equals(maintenanceAction)) {
342 MaintenanceUtils.checkForLockingDocument(document, false);
343 }
344
345 DocumentEntry entry = KRADServiceLocatorWeb.getDocumentDictionaryService()
346 .getMaintenanceDocumentEntry(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
347 document.setDisplayTopicFieldInNotes(entry.getDisplayTopicFieldInNotes());
348 }
349 }