View Javadoc

1   /**
2    * Copyright 2005-2012 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.kew.doctype.bo.DocumentType;
23  import org.kuali.rice.kew.rule.RuleBaseValues;
24  import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
25  import org.kuali.rice.kew.service.KEWServiceLocator;
26  import org.kuali.rice.kew.web.KewKualiAction;
27  import org.kuali.rice.krad.util.GlobalVariables;
28  import org.kuali.rice.krad.util.KRADConstants;
29  
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  
33  /**
34   * This class handles Actions for the DisbursementVoucher.
35   */
36  public class RuleAction extends KewKualiAction {
37      private static final String RULE_TEMPLATE_NAME_PROPERTY = "ruleTemplateName";
38      private static final String DOC_TYPE_NAME_PROPERTY = "documentTypeName";
39  
40      private static final String RULE_TEMPLATE_ERROR = "rule.template.name.required";
41      private static final String DOCUMENT_TYPE_ERROR = "rule.docType.name.required";
42  
43      public ActionForward createRule(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
44          RuleForm form = (RuleForm) actionForm;
45          if (!validateCreateRule(form)) {
46              return mapping.findForward(getDefaultMapping());
47          }
48          return new ActionForward(generateMaintenanceUrl(request, form), true);
49      }
50  
51      public ActionForward clearInitFields(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
52          RuleForm form = (RuleForm) actionForm;
53          form.clearSearchableAttributeProperties();
54          return mapping.findForward(getDefaultMapping());
55      }
56  
57      public ActionForward cancel(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
58          RuleForm form = (RuleForm) actionForm;
59          ActionForward dest = null;
60          if (StringUtils.isNotBlank(form.getBackLocation())) {
61              dest = new ActionForward(form.getBackLocation(), true);
62          } else {
63              dest = mapping.findForward(KRADConstants.MAPPING_PORTAL);
64          }
65          return dest;
66      }
67  
68      protected String generateMaintenanceUrl(HttpServletRequest request, RuleForm form) {
69          return getApplicationBaseUrl() + "/kr/" + KRADConstants.MAINTENANCE_ACTION + "?" +
70              KRADConstants.DISPATCH_REQUEST_PARAMETER + "=" + KRADConstants.START_METHOD + "&" +
71              KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE + "=" + RuleBaseValues.class.getName() +  "&" +
72              WebRuleUtils.DOCUMENT_TYPE_NAME_PARAM + "=" + form.getDocumentTypeName() + "&" +
73              WebRuleUtils.RULE_TEMPLATE_NAME_PARAM + "=" + form.getRuleTemplateName();
74      }
75  
76      protected boolean validateCreateRule(RuleForm form) {
77          if (org.apache.commons.lang.StringUtils.isEmpty(form.getRuleTemplateName())) {
78              GlobalVariables.getMessageMap().putError(RULE_TEMPLATE_NAME_PROPERTY, RULE_TEMPLATE_ERROR);
79          } else {
80              RuleTemplateBo ruleTemplate = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(form.getRuleTemplateName().trim());
81              if (ruleTemplate == null) {
82                  GlobalVariables.getMessageMap().putError(RULE_TEMPLATE_NAME_PROPERTY, RULE_TEMPLATE_ERROR);
83              }
84          }
85  
86          if (org.apache.commons.lang.StringUtils.isEmpty(form.getDocumentTypeName())) {
87              GlobalVariables.getMessageMap().putError(DOC_TYPE_NAME_PROPERTY, DOCUMENT_TYPE_ERROR);
88          } else {
89              DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName(form.getDocumentTypeName());
90              if (docType == null) {
91                  GlobalVariables.getMessageMap().putError(DOC_TYPE_NAME_PROPERTY, DOCUMENT_TYPE_ERROR);
92              }
93          }
94  
95          return GlobalVariables.getMessageMap().hasNoErrors();
96      }
97  }