001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.kew.rule.web;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.struts.action.ActionForm;
020import org.apache.struts.action.ActionForward;
021import org.apache.struts.action.ActionMapping;
022import org.kuali.rice.core.api.CoreApiServiceLocator;
023import org.kuali.rice.core.api.config.property.ConfigurationService;
024import org.kuali.rice.core.api.util.RiceConstants;
025import org.kuali.rice.kew.doctype.bo.DocumentType;
026import org.kuali.rice.kew.rule.RuleBaseValues;
027import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
028import org.kuali.rice.kew.service.KEWServiceLocator;
029import org.kuali.rice.kew.web.KewKualiAction;
030import org.kuali.rice.kns.question.ConfirmationQuestion;
031import org.kuali.rice.krad.util.GlobalVariables;
032import org.kuali.rice.krad.util.KRADConstants;
033
034import javax.servlet.http.HttpServletRequest;
035import javax.servlet.http.HttpServletResponse;
036
037/**
038 * This class handles Actions for the DisbursementVoucher.
039 */
040public class RuleAction extends KewKualiAction {
041    private static final String RULE_TEMPLATE_NAME_PROPERTY = "ruleTemplateName";
042    private static final String DOC_TYPE_NAME_PROPERTY = "documentTypeName";
043
044    private static final String RULE_TEMPLATE_ERROR = "rule.template.name.required";
045    private static final String DOCUMENT_TYPE_ERROR = "rule.docType.name.required";
046    private ConfigurationService kualiConfigurationService;
047
048    public ActionForward createRule(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
049        RuleForm form = (RuleForm) actionForm;
050        if (!validateCreateRule(form)) {
051            return mapping.findForward(getDefaultMapping());
052        }
053        return new ActionForward(generateMaintenanceUrl(request, form), true);
054    }
055
056    public ActionForward clearInitFields(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
057        RuleForm form = (RuleForm) actionForm;
058        form.clearSearchableAttributeProperties();
059        return mapping.findForward(getDefaultMapping());
060    }
061
062    public ActionForward cancel(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
063        RuleForm form = (RuleForm) actionForm;
064        Object question = request.getParameter(KRADConstants.QUESTION_INST_ATTRIBUTE_NAME);
065        // this should probably be moved into a private instance variable
066        // logic for cancel question
067        if (question == null) {
068            // ask question if not already asked
069            return this.performQuestionWithoutInput(mapping, form, request, response, KRADConstants.DOCUMENT_CANCEL_QUESTION, getKualiConfigurationService().getPropertyValueAsString(
070                    "document.question.cancel.text"), KRADConstants.CONFIRMATION_QUESTION, KRADConstants.MAPPING_CANCEL, "");
071        } else {
072            Object buttonClicked = request.getParameter(KRADConstants.QUESTION_CLICKED_BUTTON);
073            if ((KRADConstants.DOCUMENT_CANCEL_QUESTION.equals(question)) && ConfirmationQuestion.NO.equals(buttonClicked)) {
074                // if no button clicked just reload the doc
075                return mapping.findForward(RiceConstants.MAPPING_BASIC);
076            }
077            // else go to cancel logic below
078        }
079
080        ActionForward dest = null;
081        if (StringUtils.isNotBlank(form.getBackLocation())) {
082            dest = new ActionForward(form.getBackLocation(), true);
083        } else {
084
085            dest = mapping.findForward(KRADConstants.MAPPING_PORTAL);
086        }
087        return dest;
088    }
089
090    protected String generateMaintenanceUrl(HttpServletRequest request, RuleForm form) {
091        return getApplicationBaseUrl() + "/kr/" + KRADConstants.MAINTENANCE_ACTION + "?" +
092            KRADConstants.DISPATCH_REQUEST_PARAMETER + "=" + KRADConstants.START_METHOD + "&" +
093            KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE + "=" + RuleBaseValues.class.getName() +  "&" +
094            WebRuleUtils.DOCUMENT_TYPE_NAME_PARAM + "=" + form.getDocumentTypeName() + "&" +
095            WebRuleUtils.RULE_TEMPLATE_NAME_PARAM + "=" + form.getRuleTemplateName();
096    }
097
098    protected boolean validateCreateRule(RuleForm form) {
099        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}