001    /*
002     * Copyright 2005-2008 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kew.rule.web;
017    
018    import org.apache.struts.action.ActionForm;
019    import org.apache.struts.action.ActionForward;
020    import org.apache.struts.action.ActionMapping;
021    import org.kuali.rice.kew.doctype.bo.DocumentType;
022    import org.kuali.rice.kew.rule.RuleBaseValues;
023    import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
024    import org.kuali.rice.kew.service.KEWServiceLocator;
025    import org.kuali.rice.kew.web.KewKualiAction;
026    import org.kuali.rice.krad.util.GlobalVariables;
027    import org.kuali.rice.krad.util.KRADConstants;
028    
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.http.HttpServletResponse;
031    
032    /**
033     * This class handles Actions for the DisbursementVoucher.
034     */
035    public class RuleAction extends KewKualiAction {
036        private static final String RULE_TEMPLATE_NAME_PROPERTY = "ruleTemplateName";
037        private static final String DOC_TYPE_NAME_PROPERTY = "documentTypeName";
038    
039        private static final String RULE_TEMPLATE_ERROR = "rule.template.name.required";
040        private static final String DOCUMENT_TYPE_ERROR = "rule.docType.name.required";
041    
042        public ActionForward createRule(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
043            RuleForm form = (RuleForm) actionForm;
044            if (!validateCreateRule(form)) {
045                return mapping.findForward(getDefaultMapping());
046            }
047            return new ActionForward(generateMaintenanceUrl(request, form), true);
048        }
049    
050        public ActionForward clearInitFields(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
051            RuleForm form = (RuleForm) actionForm;
052            form.clearSearchableAttributeProperties();
053            return mapping.findForward(getDefaultMapping());
054        }
055    
056        public ActionForward cancel(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
057            return mapping.findForward("actionTaken");
058        }
059    
060        protected String generateMaintenanceUrl(HttpServletRequest request, RuleForm form) {
061            return getApplicationBaseUrl() + "/kr/" + KRADConstants.MAINTENANCE_ACTION + "?" +
062                KRADConstants.DISPATCH_REQUEST_PARAMETER + "=" + KRADConstants.START_METHOD + "&" +
063                KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE + "=" + RuleBaseValues.class.getName() +  "&" +
064                WebRuleUtils.DOCUMENT_TYPE_NAME_PARAM + "=" + form.getDocumentTypeName() + "&" +
065                WebRuleUtils.RULE_TEMPLATE_NAME_PARAM + "=" + form.getRuleTemplateName();
066        }
067    
068        protected boolean validateCreateRule(RuleForm form) {
069            if (org.apache.commons.lang.StringUtils.isEmpty(form.getRuleTemplateName())) {
070                GlobalVariables.getMessageMap().putError(RULE_TEMPLATE_NAME_PROPERTY, RULE_TEMPLATE_ERROR);
071            } else {
072                RuleTemplateBo ruleTemplate = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(form.getRuleTemplateName().trim());
073                if (ruleTemplate == null) {
074                    GlobalVariables.getMessageMap().putError(RULE_TEMPLATE_NAME_PROPERTY, RULE_TEMPLATE_ERROR);
075                }
076            }
077    
078            if (org.apache.commons.lang.StringUtils.isEmpty(form.getDocumentTypeName())) {
079                GlobalVariables.getMessageMap().putError(DOC_TYPE_NAME_PROPERTY, DOCUMENT_TYPE_ERROR);
080            } else {
081                DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName(form.getDocumentTypeName());
082                if (docType == null) {
083                    GlobalVariables.getMessageMap().putError(DOC_TYPE_NAME_PROPERTY, DOCUMENT_TYPE_ERROR);
084                }
085            }
086    
087            return GlobalVariables.getMessageMap().hasNoErrors();
088        }
089    }