001    /**
002     * Copyright 2005-2014 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 javax.servlet.http.HttpServletRequest;
019    import javax.servlet.http.HttpServletResponse;
020    
021    import org.apache.struts.action.ActionForm;
022    import org.apache.struts.action.ActionForward;
023    import org.apache.struts.action.ActionMapping;
024    import org.kuali.rice.kew.rule.RuleDelegationBo;
025    import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
026    import org.kuali.rice.kew.web.KewKualiAction;
027    import org.kuali.rice.krad.util.GlobalVariables;
028    import org.kuali.rice.krad.util.KRADConstants;
029    
030    /**
031     * Struts action for handling the initial Delegate Rule screen for selecting
032     * the parent rule and responsibility. 
033     * 
034     * @author Kuali Rice Team (rice.collab@kuali.org)
035     *
036     */
037    public class DelegateRuleAction extends KewKualiAction {
038    
039            private static final String PARENT_RULE_PROPERTY = "parentRuleId";
040            private static final String PARENT_RESPONSIBILITY_PROPERTY = "parentResponsibilityId";
041            
042            private static final String PARENT_RULE_ERROR = "delegateRule.parentRule.required";
043            private static final String PARENT_RESPONSIBILITY_ERROR = "delegateRule.parentResponsibility.required";
044            private static final String DELEGATE_RULE_INVALID_ERROR = "delegateRule.delegateRuleTemplate.invalid";
045            
046            public ActionForward createDelegateRule(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
047                    DelegateRuleForm form = (DelegateRuleForm) actionForm;
048                    if (!validateCreateDelegateRule(form)) {
049                            return mapping.findForward(getDefaultMapping());
050                    }
051                    return new ActionForward(generateMaintenanceUrl(request, form), true);
052            }
053            
054            protected boolean validateCreateDelegateRule(DelegateRuleForm form) {
055                    if (form.getParentRule() == null) {
056                            GlobalVariables.getMessageMap().putError(PARENT_RULE_PROPERTY, PARENT_RULE_ERROR);
057                    } else {
058                            RuleTemplateBo ruleTemplate = form.getParentRule().getRuleTemplate();
059                            if (ruleTemplate == null
060                                    || ruleTemplate.getDelegationTemplate() == null) {
061                                    GlobalVariables.getMessageMap().putError(PARENT_RULE_PROPERTY, DELEGATE_RULE_INVALID_ERROR);
062                            }
063                    }
064                    if (form.getParentResponsibility() == null) {
065                            GlobalVariables.getMessageMap().putError(PARENT_RESPONSIBILITY_PROPERTY, PARENT_RESPONSIBILITY_ERROR);
066                    }
067                    
068                    return GlobalVariables.getMessageMap().hasNoErrors();
069            }
070            
071            protected String generateMaintenanceUrl(HttpServletRequest request, DelegateRuleForm form) {
072                    return getApplicationBaseUrl() + "/kr/" + KRADConstants.MAINTENANCE_ACTION + "?" +
073                            KRADConstants.DISPATCH_REQUEST_PARAMETER + "=" + KRADConstants.START_METHOD + "&" +
074                            KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE + "=" + RuleDelegationBo.class.getName() +  "&" +
075                            WebRuleUtils.RESPONSIBILITY_ID_PARAM + "=" + form.getParentResponsibilityId() + "&" +
076                            WebRuleUtils.RULE_TEMPLATE_ID_PARAM + "=" + form.getParentRule().getRuleTemplate().getDelegationTemplateId() + "&" +
077                            WebRuleUtils.DOCUMENT_TYPE_NAME_PARAM + "=" + form.getParentRule().getDocTypeName();
078            }
079            
080    }