View Javadoc

1   /**
2    * Copyright 2005-2015 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.rice.kew.rule.web;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.struts.action.ActionForm;
20  import org.apache.struts.action.ActionForward;
21  import org.apache.struts.action.ActionMapping;
22  import org.kuali.rice.core.api.CoreApiServiceLocator;
23  import org.kuali.rice.core.api.config.property.ConfigurationService;
24  import org.kuali.rice.core.api.util.RiceConstants;
25  import org.kuali.rice.kew.doctype.bo.DocumentType;
26  import org.kuali.rice.kew.rule.RuleBaseValues;
27  import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
28  import org.kuali.rice.kew.service.KEWServiceLocator;
29  import org.kuali.rice.kew.web.KewKualiAction;
30  import org.kuali.rice.kns.question.ConfirmationQuestion;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  import org.kuali.rice.krad.util.KRADConstants;
33  
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  
37  /**
38   * This class handles Actions for the DisbursementVoucher.
39   */
40  public class RuleAction extends KewKualiAction {
41      private static final String RULE_TEMPLATE_NAME_PROPERTY = "ruleTemplateName";
42      private static final String DOC_TYPE_NAME_PROPERTY = "documentTypeName";
43  
44      private static final String RULE_TEMPLATE_ERROR = "rule.template.name.required";
45      private static final String DOCUMENT_TYPE_ERROR = "rule.docType.name.required";
46      private ConfigurationService kualiConfigurationService;
47  
48      public ActionForward createRule(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
49          RuleForm form = (RuleForm) actionForm;
50          if (!validateCreateRule(form)) {
51              return mapping.findForward(getDefaultMapping());
52          }
53          return new ActionForward(generateMaintenanceUrl(request, form), true);
54      }
55  
56      public ActionForward clearInitFields(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
57          RuleForm form = (RuleForm) actionForm;
58          form.clearSearchableAttributeProperties();
59          return mapping.findForward(getDefaultMapping());
60      }
61  
62      public ActionForward cancel(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
63          RuleForm form = (RuleForm) actionForm;
64          Object question = request.getParameter(KRADConstants.QUESTION_INST_ATTRIBUTE_NAME);
65          // this should probably be moved into a private instance variable
66          // logic for cancel question
67          if (question == null) {
68              // ask question if not already asked
69              return this.performQuestionWithoutInput(mapping, form, request, response, KRADConstants.DOCUMENT_CANCEL_QUESTION, getKualiConfigurationService().getPropertyValueAsString(
70                      "document.question.cancel.text"), KRADConstants.CONFIRMATION_QUESTION, KRADConstants.MAPPING_CANCEL, "");
71          } else {
72              Object buttonClicked = request.getParameter(KRADConstants.QUESTION_CLICKED_BUTTON);
73              if ((KRADConstants.DOCUMENT_CANCEL_QUESTION.equals(question)) && ConfirmationQuestion.NO.equals(buttonClicked)) {
74                  // if no button clicked just reload the doc
75                  return mapping.findForward(RiceConstants.MAPPING_BASIC);
76              }
77              // else go to cancel logic below
78          }
79  
80          ActionForward dest = null;
81          if (StringUtils.isNotBlank(form.getBackLocation())) {
82              dest = new ActionForward(form.getBackLocation(), true);
83          } else {
84  
85              dest = mapping.findForward(KRADConstants.MAPPING_PORTAL);
86          }
87          return dest;
88      }
89  
90      protected String generateMaintenanceUrl(HttpServletRequest request, RuleForm form) {
91          return getApplicationBaseUrl() + "/kr/" + KRADConstants.MAINTENANCE_ACTION + "?" +
92              KRADConstants.DISPATCH_REQUEST_PARAMETER + "=" + KRADConstants.START_METHOD + "&" +
93              KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE + "=" + RuleBaseValues.class.getName() +  "&" +
94              WebRuleUtils.DOCUMENT_TYPE_NAME_PARAM + "=" + form.getDocumentTypeName() + "&" +
95              WebRuleUtils.RULE_TEMPLATE_NAME_PARAM + "=" + form.getRuleTemplateName();
96      }
97  
98      protected boolean validateCreateRule(RuleForm form) {
99          if (org.apache.commons.lang.StringUtils.isEmpty(form.getRuleTemplateName())) {
100             GlobalVariables.getMessageMap().putError(RULE_TEMPLATE_NAME_PROPERTY, RULE_TEMPLATE_ERROR);
101         } else {
102             RuleTemplateBo ruleTemplate = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(form.getRuleTemplateName().trim());
103             if (ruleTemplate == null) {
104                 GlobalVariables.getMessageMap().putError(RULE_TEMPLATE_NAME_PROPERTY, RULE_TEMPLATE_ERROR);
105             }
106         }
107 
108         if (org.apache.commons.lang.StringUtils.isEmpty(form.getDocumentTypeName())) {
109             GlobalVariables.getMessageMap().putError(DOC_TYPE_NAME_PROPERTY, DOCUMENT_TYPE_ERROR);
110         } else {
111             DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName(form.getDocumentTypeName());
112             if (docType == null) {
113                 GlobalVariables.getMessageMap().putError(DOC_TYPE_NAME_PROPERTY, DOCUMENT_TYPE_ERROR);
114             }
115         }
116 
117         return GlobalVariables.getMessageMap().hasNoErrors();
118     }
119     protected ConfigurationService getKualiConfigurationService() {
120         if (kualiConfigurationService == null) {
121             kualiConfigurationService = CoreApiServiceLocator.getKualiConfigurationService();
122         }
123         return this.kualiConfigurationService;
124     }
125 }