0001: /*
0002: * Copyright 2005-2006 The Kuali Foundation.
0003: *
0004: *
0005: * Licensed under the Educational Community License, Version 1.0 (the "License");
0006: * you may not use this file except in compliance with the License.
0007: * You may obtain a copy of the License at
0008: *
0009: * http://www.opensource.org/licenses/ecl1.php
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: package edu.iu.uis.eden.routetemplate.web;
0018:
0019: import java.util.ArrayList;
0020: import java.util.HashMap;
0021: import java.util.Iterator;
0022: import java.util.List;
0023: import java.util.Map;
0024:
0025: import javax.servlet.http.HttpServletRequest;
0026: import javax.servlet.http.HttpServletResponse;
0027:
0028: import org.apache.struts.action.ActionErrors;
0029: import org.apache.struts.action.ActionForm;
0030: import org.apache.struts.action.ActionForward;
0031: import org.apache.struts.action.ActionMapping;
0032: import org.apache.struts.action.ActionMessage;
0033: import org.apache.struts.action.ActionMessages;
0034: import org.kuali.rice.resourceloader.GlobalResourceLoader;
0035:
0036: import edu.iu.uis.eden.EdenConstants;
0037: import edu.iu.uis.eden.KEWServiceLocator;
0038: import edu.iu.uis.eden.WorkflowServiceErrorException;
0039: import edu.iu.uis.eden.WorkflowServiceErrorImpl;
0040: import edu.iu.uis.eden.clientapp.IDocHandler;
0041: import edu.iu.uis.eden.clientapp.WorkflowDocument;
0042: import edu.iu.uis.eden.clientapp.vo.DocumentTypeVO;
0043: import edu.iu.uis.eden.clientapp.vo.WorkflowIdVO;
0044: import edu.iu.uis.eden.exception.WorkflowException;
0045: import edu.iu.uis.eden.export.ExportDataSet;
0046: import edu.iu.uis.eden.export.ExportFormat;
0047: import edu.iu.uis.eden.plugin.attributes.WorkflowLookupable;
0048: import edu.iu.uis.eden.routeheader.DocumentRouteHeaderValue;
0049: import edu.iu.uis.eden.routetemplate.MyRules2;
0050: import edu.iu.uis.eden.routetemplate.RuleBaseValues;
0051: import edu.iu.uis.eden.routetemplate.RuleDelegation;
0052: import edu.iu.uis.eden.routetemplate.RuleDelegationService;
0053: import edu.iu.uis.eden.routetemplate.RuleResponsibility;
0054: import edu.iu.uis.eden.routetemplate.RuleService;
0055: import edu.iu.uis.eden.routetemplate.RuleTemplate;
0056: import edu.iu.uis.eden.routetemplate.RuleTemplateService;
0057: import edu.iu.uis.eden.util.CodeTranslator;
0058: import edu.iu.uis.eden.util.Utilities;
0059: import edu.iu.uis.eden.web.ShowHideTree;
0060: import edu.iu.uis.eden.web.WorkflowAction;
0061:
0062: /**
0063: * A Struts Action for interacting with the Rules engine. Provides creation,
0064: * editing, and report for rules.
0065: *
0066: * @see RuleService
0067: * @see RuleBaseValues
0068: * @see WebRuleBaseValues
0069: * @see WebRuleResponsibility
0070: *
0071: * @author jhopf
0072: * @author ewestfal
0073: * @author rkirkend
0074: */
0075: public class Rule2Action extends WorkflowAction {
0076:
0077: public ActionForward start(ActionMapping mapping, ActionForm form,
0078: HttpServletRequest request, HttpServletResponse response)
0079: throws Exception {
0080: Rule2Form rule2Form = (Rule2Form) form;
0081: rule2Form.setChoosingTemplate(true);
0082: //createFlexDoc(request, rule2Form);
0083: return mapping.findForward("basic");
0084: }
0085:
0086: public ActionForward performLookup(ActionMapping mapping,
0087: ActionForm form, HttpServletRequest request,
0088: HttpServletResponse response) throws Exception {
0089: Rule2Form ruleForm = (Rule2Form) form;
0090:
0091: String basePath = request.getScheme() + "://"
0092: + request.getServerName() + ":"
0093: + request.getServerPort() + request.getContextPath()
0094: + mapping.getModuleConfig().getPrefix();
0095: ;
0096:
0097: String lookupType = ruleForm.getLookupType();
0098: ruleForm.setLookupType(null);
0099:
0100: StringBuffer lookupUrl = new StringBuffer(basePath);
0101: lookupUrl.append("/Lookup.do?methodToCall=start&docFormKey=")
0102: .append(getUserSession(request).addObject(form))
0103: .append("&lookupableImplServiceName=");
0104: lookupUrl.append(request
0105: .getParameter("lookupableImplServiceName"));
0106: lookupUrl.append("&conversionFields=").append(
0107: request.getParameter("conversionFields"));
0108:
0109: if (lookupType != null && !lookupType.equals("")) {
0110: WorkflowLookupable workflowLookupable = (WorkflowLookupable) GlobalResourceLoader
0111: .getService(request
0112: .getParameter("lookupableImplServiceName"));// SpringServiceLocator.getExtensionService().getLookupable(request.getParameter("lookupableImplServiceName"));
0113: if (!Utilities.isEmpty(request
0114: .getParameter("conversionFields"))) {
0115: lookupUrl.append(",");
0116: }
0117: for (Iterator iterator = workflowLookupable
0118: .getDefaultReturnType().iterator(); iterator
0119: .hasNext();) {
0120: String returnType = (String) iterator.next();
0121: lookupUrl.append(returnType).append(":").append(
0122: lookupType);
0123: }
0124: }
0125:
0126: lookupUrl.append("&returnLocation=").append(basePath).append(
0127: mapping.getPath()).append(".do");
0128: return new ActionForward(lookupUrl.toString(), true);
0129: }
0130:
0131: public ActionForward report(ActionMapping mapping, ActionForm form,
0132: HttpServletRequest request, HttpServletResponse response)
0133: throws Exception {
0134: Rule2Form ruleForm = (Rule2Form) form;
0135: List rules = new ArrayList();
0136: if (ruleForm.getCurrentRuleId() != null) {
0137: RuleBaseValues rule = getRuleService()
0138: .findRuleBaseValuesById(ruleForm.getCurrentRuleId());
0139: if (rule != null) {
0140: WebRuleBaseValues webRule = new WebRuleBaseValues(rule);
0141: rules.add(webRule);
0142: initializeShowHide(ruleForm.getShowHide(), webRule);
0143: /**
0144: * added on 2006-04-04 to support function of showing link to document type report page
0145: */
0146: if (!Utilities.isEmpty(webRule.getDocTypeName())) {
0147: DocumentTypeVO docType = KEWServiceLocator
0148: .getDocumentTypeService()
0149: .getDocumentTypeVO(webRule.getDocTypeName());
0150: if (docType != null) {
0151: ruleForm.setDocTypeId(docType.getDocTypeId());
0152: }
0153: }
0154: }
0155: } else if (ruleForm.getDocId() != null) {
0156: List docRules = getRuleService().findByRouteHeaderId(
0157: ruleForm.getDocId());
0158: for (Iterator iterator = docRules.iterator(); iterator
0159: .hasNext();) {
0160: RuleBaseValues docRule = (RuleBaseValues) iterator
0161: .next();
0162: WebRuleBaseValues webRule = new WebRuleBaseValues(
0163: docRule);
0164: rules.add(webRule);
0165: initializeShowHide(ruleForm.getShowHide(), webRule);
0166: }
0167: /**
0168: * added on 2006-04-04 to support function of showing link to document type report page
0169: */
0170: DocumentRouteHeaderValue doc = KEWServiceLocator
0171: .getRouteHeaderService().getRouteHeader(
0172: ruleForm.getDocId());
0173: if (doc != null) {
0174: ruleForm.setDocTypeId(doc.getDocumentTypeId());
0175: }
0176:
0177: }
0178: ruleForm.setRules(rules);
0179:
0180: // set up show/hide of delegation rules
0181: int ruleIndex = 0;
0182: for (Iterator ruleIt = rules.iterator(); ruleIt.hasNext();) {
0183: int respIndex = 0;
0184: for (Iterator respIt = ((RuleBaseValues) ruleIt.next())
0185: .getResponsibilities().iterator(); respIt.hasNext();) {
0186: RuleResponsibility responsibility = (RuleResponsibility) respIt
0187: .next();
0188: String key = "currrule" + ruleIndex + "resp"
0189: + respIndex;
0190: if (ruleForm.getShowDelegationsMap().get(key) == null) {
0191: int numDelegations = responsibility
0192: .getDelegationRules().size();
0193: ruleForm
0194: .getShowDelegationsMap()
0195: .put(
0196: key,
0197: new Boolean(
0198: numDelegations <= Integer
0199: .parseInt(Utilities
0200: .getApplicationConstant("Config.Application.DelegateLimit")))
0201: .toString());
0202: }
0203: respIndex++;
0204: }
0205: ruleIndex++;
0206: }
0207:
0208: establishRequiredState(request, form);
0209: return mapping.findForward("report");
0210: }
0211:
0212: public ActionForward export(ActionMapping mapping, ActionForm form,
0213: HttpServletRequest request, HttpServletResponse response)
0214: throws Exception {
0215: Rule2Form ruleForm = (Rule2Form) form;
0216: List rules = new ArrayList();
0217: if (ruleForm.getCurrentRuleId() != null) {
0218: RuleBaseValues rule = getRuleService()
0219: .findRuleBaseValuesById(ruleForm.getCurrentRuleId());
0220: if (rule != null) {
0221: rules.add(rule);
0222: }
0223: } else if (ruleForm.getDocId() != null) {
0224: List docRules = getRuleService().findByRouteHeaderId(
0225: ruleForm.getDocId());
0226: for (Iterator iterator = docRules.iterator(); iterator
0227: .hasNext();) {
0228: RuleBaseValues docRule = (RuleBaseValues) iterator
0229: .next();
0230: rules.add(docRule);
0231: }
0232: }
0233: ExportDataSet dataSet = new ExportDataSet(ExportFormat.XML);
0234: dataSet.getRules().addAll(rules);
0235: return exportDataSet(request, dataSet);
0236: }
0237:
0238: public ActionForward createNew(ActionMapping mapping,
0239: ActionForm form, HttpServletRequest request,
0240: HttpServletResponse response) throws Exception {
0241: Rule2Form ruleForm = (Rule2Form) form;
0242: List errors = new ArrayList();
0243: if (ruleForm.getRuleCreationValues().getRuleTemplateId() == null) {
0244: errors.add(new WorkflowServiceErrorImpl(
0245: "Rule template id required.",
0246: "rule.template.required"));
0247: }
0248: if (ruleForm.getRuleCreationValues().getDocTypeName() == null
0249: || ruleForm.getRuleCreationValues().getDocTypeName()
0250: .equals("")) {
0251: errors.add(new WorkflowServiceErrorImpl(
0252: "Document type name required.",
0253: "rule.docType.name.required"));
0254: }
0255: if (!errors.isEmpty()) {
0256: throw new WorkflowServiceErrorException(
0257: "Errors populating rule attributes.", errors);
0258: }
0259:
0260: RuleBaseValues defaultRule = getRuleService()
0261: .findDefaultRuleByRuleTemplateId(
0262: ruleForm.getRuleCreationValues()
0263: .getRuleTemplateId());
0264: WebRuleBaseValues rule = new WebRuleBaseValues();
0265:
0266: if (defaultRule != null) {
0267: defaultRule.setActivationDate(null);
0268: defaultRule.setCurrentInd(null);
0269: defaultRule.setDeactivationDate(null);
0270: defaultRule.setDocTypeName(null);
0271: defaultRule.setLockVerNbr(null);
0272: defaultRule.setRuleBaseValuesId(null);
0273: defaultRule.setTemplateRuleInd(Boolean.FALSE);
0274: defaultRule.setVersionNbr(null);
0275: rule.load(defaultRule);
0276: }
0277:
0278: rule.setRuleTemplateId(ruleForm.getRuleCreationValues()
0279: .getRuleTemplateId());
0280: rule.setDocTypeName(ruleForm.getRuleCreationValues()
0281: .getDocTypeName());
0282: rule.setDelegateRule(Boolean.FALSE);
0283: rule.loadFieldsWithDefaultValues();
0284:
0285: WebRuleResponsibility responsibility = rule
0286: .createNewRuleResponsibility();
0287: RuleTemplate ruleTemplate = getRuleTemplateService()
0288: .findByRuleTemplateId(rule.getRuleTemplateId());
0289: if (ruleTemplate.getDefaultActionRequestValue() != null
0290: && ruleTemplate.getDefaultActionRequestValue()
0291: .getValue() != null) {
0292: responsibility.setActionRequestedCd(ruleTemplate
0293: .getDefaultActionRequestValue().getValue());
0294: }
0295: if (ruleTemplate.getDelegationTemplate() != null) {
0296: responsibility.setHasDelegateRuleTemplate(true);
0297: }
0298:
0299: responsibility.setDelegationRulesMaterialized(true);
0300: ruleForm.getMyRules().addRule(rule);
0301: createFlexDoc(request, ruleForm, ruleForm.getMyRules()
0302: .getRules());
0303: ruleForm.getShowHide().append().append();
0304: establishRequiredState(request, ruleForm);
0305: return mapping.findForward("basic");
0306: }
0307:
0308: public ActionForward copy(ActionMapping mapping, ActionForm form,
0309: HttpServletRequest request, HttpServletResponse response)
0310: throws Exception {
0311: Rule2Form ruleForm = (Rule2Form) form;
0312: List errors = new ArrayList();
0313: WebRuleBaseValues webRule = null;
0314: if (ruleForm.getCurrentRuleId() != null) {
0315: RuleBaseValues rule = getRuleService()
0316: .findRuleBaseValuesById(ruleForm.getCurrentRuleId());
0317: if (rule != null) {
0318: if (rule.getDelegateRule().booleanValue()) {
0319: return new ActionForward(
0320: "DelegateRule.do?methodToCall=copy¤tRuleId="
0321: + ruleForm.getCurrentRuleId(), true);
0322: }
0323: webRule = WebRuleUtils
0324: .copyToNewRule(new WebRuleBaseValues(rule));
0325: initializeShowHide(ruleForm.getShowHide(), webRule);
0326: } else {
0327: errors.add(new WorkflowServiceErrorImpl(
0328: "Could not locate rule for given id "
0329: + ruleForm.getCurrentRuleId(),
0330: "general.workflow.error",
0331: "Could not locate rule for given id "
0332: + ruleForm.getCurrentRuleId()));
0333: }
0334: } else {
0335: errors.add(new WorkflowServiceErrorImpl(
0336: "Error copying rule, no id specified.",
0337: "general.workflow.error",
0338: "Error copying rule, no id specified."));
0339: }
0340: if (!errors.isEmpty()) {
0341: throw new WorkflowServiceErrorException(
0342: "Errors copying rule.", errors);
0343: }
0344:
0345: // set up show/hide of delegation rules
0346: /*int ruleIndex = 0;
0347: for (Iterator ruleIt = rules.iterator(); ruleIt.hasNext();) {
0348: int respIndex = 0;
0349: for (Iterator respIt = ((RuleBaseValues) ruleIt.next()).getResponsibilities().iterator(); respIt.hasNext();) {
0350: RuleResponsibility responsibility = (RuleResponsibility) respIt.next();
0351: String key = "currrule" + ruleIndex + "resp" + respIndex;
0352: if (ruleForm.getShowDelegationsMap().get(key) == null) {
0353: int numDelegations = responsibility.getDelegationRules().size();
0354: ruleForm.getShowDelegationsMap().put(key, new Boolean(numDelegations <= Integer.parseInt(Utilities.getApplicationConstant("Config.Application.DelegateLimit"))).toString());
0355: }
0356: respIndex++;
0357: }
0358: ruleIndex++;
0359: }*/
0360:
0361: //responsibility.setDelegationRulesMaterialized(true);
0362: ruleForm.getMyRules().addRule(webRule);
0363: createFlexDoc(request, ruleForm, ruleForm.getMyRules()
0364: .getRules());
0365: ruleForm.getShowHide().append().append();
0366: establishRequiredState(request, ruleForm);
0367: return mapping.findForward("basic");
0368: }
0369:
0370: public ActionForward copyRule(ActionMapping mapping,
0371: ActionForm form, HttpServletRequest request,
0372: HttpServletResponse response) throws Exception {
0373: Rule2Form ruleForm = (Rule2Form) form;
0374: WebRuleBaseValues rule = ruleForm.getMyRules().getRule(
0375: ruleForm.getRuleIndex().intValue());
0376: WebRuleBaseValues ruleCopy = WebRuleUtils
0377: .copyRuleOntoExistingDocument(rule);
0378:
0379: ruleForm.getMyRules().addRule(ruleCopy);
0380: // ruleForm.getShowHide().append().append();
0381: //
0382: // for (int i = 0; i < responsibilities.size(); i++) {
0383: // ruleForm.getShowHide().getChild(ruleForm.getRuleIndex().intValue()+1).getChild(i).append().append();
0384: // }
0385: ruleForm.setShowHide(initializeShowHide(ruleForm.getMyRules()));
0386:
0387: establishRequiredState(request, ruleForm);
0388: return mapping.findForward("basic");
0389: }
0390:
0391: public ActionForward refresh(ActionMapping mapping,
0392: ActionForm form, HttpServletRequest request,
0393: HttpServletResponse response) throws Exception {
0394: Rule2Form ruleForm = (Rule2Form) form;
0395: String ruleTemplateIdValue = request
0396: .getParameter("ruleTemplate.ruleTemplateId");
0397: if ("".equals(ruleTemplateIdValue)) {
0398: ruleForm.getRuleCreationValues().setRuleTemplateId(null);
0399: ruleForm.getRuleCreationValues().setRuleTemplateName("");
0400: } else if (ruleTemplateIdValue != null) {
0401: Long ruleTemplateId = new Long(ruleTemplateIdValue);
0402: ruleForm.getRuleCreationValues().setRuleTemplateId(
0403: ruleTemplateId);
0404: RuleTemplate template = getRuleTemplateService()
0405: .findByRuleTemplateId(
0406: ruleForm.getRuleCreationValues()
0407: .getRuleTemplateId());
0408: ruleForm.getRuleCreationValues().setRuleTemplateName(
0409: template.getName());
0410: }
0411: return mapping.findForward("basic");
0412: }
0413:
0414: public ActionForward removeResponsibility(ActionMapping mapping,
0415: ActionForm form, HttpServletRequest request,
0416: HttpServletResponse response) throws Exception {
0417: Rule2Form ruleForm = (Rule2Form) form;
0418: int removeIndex = ruleForm.getResponsibilityIndex().intValue();
0419: RuleBaseValues rule = ruleForm.getMyRules().getRule(
0420: ruleForm.getRuleIndex().intValue());
0421: if (ruleForm.getDelegationIndex() != null) {
0422: removeIndex = ruleForm.getDelegationResponsibilityIndex()
0423: .intValue();
0424: rule = rule.getResponsibility(
0425: ruleForm.getResponsibilityIndex().intValue())
0426: .getDelegationRule(
0427: ruleForm.getDelegationIndex().intValue())
0428: .getDelegationRuleBaseValues();
0429: ruleForm.getShowHide().getChild(ruleForm.getRuleIndex())
0430: .getChild(ruleForm.getResponsibilityIndex())
0431: .getChild(ruleForm.getDelegationIndex()).remove(
0432: removeIndex);
0433: } else {
0434: ruleForm.getShowHide().getChild(ruleForm.getRuleIndex())
0435: .remove(removeIndex);
0436: }
0437: rule.removeResponsibility(removeIndex);
0438: return mapping.findForward("basic");
0439: }
0440:
0441: public ActionForward removeRule(ActionMapping mapping,
0442: ActionForm form, HttpServletRequest request,
0443: HttpServletResponse response) throws Exception {
0444: Rule2Form ruleForm = (Rule2Form) form;
0445: int ruleIndex = ruleForm.getRuleIndex().intValue();
0446: if (ruleForm.getDelegationIndex() != null) {
0447: int respIndex = ruleForm.getResponsibilityIndex()
0448: .intValue();
0449: int delIndex = ruleForm.getDelegationIndex().intValue();
0450: ruleForm.getMyRules().getRule(ruleIndex).getResponsibility(
0451: respIndex).getDelegationRules().remove(delIndex);
0452: ruleForm.getShowHide().getChild(ruleIndex).getChild(
0453: respIndex).remove(delIndex);
0454: } else {
0455: ruleForm.getMyRules().getRules().remove(ruleIndex);
0456: ruleForm.getShowHide().remove(ruleIndex);
0457: }
0458: return mapping.findForward("basic");
0459: }
0460:
0461: public ActionForward edit(ActionMapping mapping, ActionForm form,
0462: HttpServletRequest request, HttpServletResponse response)
0463: throws Exception {
0464: Rule2Form ruleForm = (Rule2Form) form;
0465: RuleBaseValues rule = getRuleService().findRuleBaseValuesById(
0466: ruleForm.getCurrentRuleId());
0467: if (rule != null) {
0468: ActionErrors errors = new ActionErrors();
0469: if (checkLockedForRouting(errors, rule, false)) {
0470: saveErrors(request, errors);
0471: return mapping.findForward("basic");
0472: }
0473: if (rule.getDelegateRule().booleanValue()) {
0474: //ruleForm.setMethodToCall("edit");
0475: //return mapping.findForward("delegateEdit");
0476: return new ActionForward(
0477: "DelegateRule.do?methodToCall=edit¤tRuleId="
0478: + ruleForm.getCurrentRuleId(), true);
0479: }
0480:
0481: WebRuleBaseValues webRule = new WebRuleBaseValues(rule);
0482: webRule.populatePreviousVersionIds();
0483: webRule.setPreviousVersionId(rule.getRuleBaseValuesId());
0484: ruleForm.getRuleCreationValues().setDocTypeName(
0485: webRule.getDocTypeName());
0486: ruleForm.getRuleCreationValues().setRuleTemplateName(
0487: webRule.getRuleTemplateName());
0488: ruleForm.getRuleCreationValues().setRuleTemplateId(
0489: webRule.getRuleTemplateId());
0490:
0491: for (Iterator iter = webRule.getResponsibilities()
0492: .iterator(); iter.hasNext();) {
0493: WebRuleResponsibility responsibility = (WebRuleResponsibility) iter
0494: .next();
0495: if (rule.getRuleTemplate() != null
0496: && rule.getRuleTemplate()
0497: .getDelegationTemplate() != null) {
0498: responsibility.setHasDelegateRuleTemplate(true);
0499: }
0500: }
0501:
0502: ruleForm.getMyRules().addRule(webRule);
0503: if (rule.getDelegateRule().booleanValue()) {
0504: ruleForm.setEditingDelegate(true);
0505: }
0506: ruleForm.setShowHide(initializeShowHide(ruleForm
0507: .getMyRules()));
0508: String ruleDocumentTypeName = getRuleService()
0509: .getRuleDocmentTypeName(
0510: ruleForm.getMyRules().getRules());
0511: ruleForm.setFlexDoc(new WorkflowDocument(new WorkflowIdVO(
0512: getUserSession(request).getWorkflowUser()
0513: .getWorkflowId()), ruleDocumentTypeName));
0514: ruleForm.setDocId(ruleForm.getFlexDoc().getRouteHeaderId());
0515: }
0516: establishRequiredState(request, ruleForm);
0517: return mapping.findForward("basic");
0518: }
0519:
0520: public ActionForward delegateResponsibility(ActionMapping mapping,
0521: ActionForm form, HttpServletRequest request,
0522: HttpServletResponse response) throws Exception {
0523: Rule2Form ruleForm = (Rule2Form) form;
0524: WebRuleBaseValues rule = ruleForm.getMyRules().getRule(
0525: ruleForm.getRuleIndex().intValue());
0526: WebRuleResponsibility webResponsibility = (WebRuleResponsibility) rule
0527: .getResponsibility(ruleForm.getResponsibilityIndex()
0528: .intValue());
0529:
0530: RuleDelegation delegation = webResponsibility
0531: .addNewDelegation();
0532: WebRuleBaseValues delegationRule = ((WebRuleBaseValues) delegation
0533: .getDelegationRuleBaseValues());
0534: delegationRule.createNewRuleResponsibility();
0535: delegationRule.setDocTypeName(rule.getDocTypeName());
0536:
0537: RuleTemplate ruleTemplate = getRuleTemplateService()
0538: .findByRuleTemplateId(rule.getRuleTemplateId());
0539: if (ruleTemplate.getDelegationTemplate() != null) {
0540: delegation.getDelegationRuleBaseValues().setRuleTemplateId(
0541: ruleTemplate.getDelegationTemplate()
0542: .getRuleTemplateId());
0543:
0544: RuleBaseValues defaultRule = getRuleService()
0545: .findDefaultRuleByRuleTemplateId(
0546: ruleTemplate.getDelegationTemplate()
0547: .getRuleTemplateId());
0548: if (defaultRule != null) {
0549: List ruleDelegations = getRuleDelegationService()
0550: .findByDelegateRuleId(
0551: defaultRule.getRuleBaseValuesId());
0552: defaultRule.setActivationDate(null);
0553: defaultRule.setCurrentInd(null);
0554: defaultRule.setDeactivationDate(null);
0555: defaultRule.setDocTypeName(null);
0556: defaultRule.setLockVerNbr(null);
0557: defaultRule.setRuleBaseValuesId(null);
0558: defaultRule.setTemplateRuleInd(Boolean.FALSE);
0559: defaultRule.setVersionNbr(null);
0560: delegationRule.load(defaultRule);
0561:
0562: if (ruleDelegations != null
0563: && !ruleDelegations.isEmpty()) {
0564: RuleDelegation defaultDelegation = (RuleDelegation) ruleDelegations
0565: .get(0);
0566: delegation.setDelegationType(defaultDelegation
0567: .getDelegationType());
0568: }
0569: }
0570: delegationRule.loadFieldsWithDefaultValues();
0571: }
0572: webResponsibility.setDelegationRulesMaterialized(true); // TODO this is kinda nasty
0573: ruleForm.getShowHide().getChild(ruleForm.getRuleIndex())
0574: .getChild(ruleForm.getResponsibilityIndex()).append()
0575: .append();
0576: establishRequiredState(request, form);
0577: return mapping.findForward("basic");
0578: }
0579:
0580: public ActionForward addNewResponsibility(ActionMapping mapping,
0581: ActionForm form, HttpServletRequest request,
0582: HttpServletResponse response) throws Exception {
0583: Rule2Form ruleForm = (Rule2Form) form;
0584: int index = ruleForm.getRuleIndex().intValue();
0585: WebRuleBaseValues webRule = (WebRuleBaseValues) ruleForm
0586: .getMyRules().getRule(index);
0587: if (ruleForm.getDelegationIndex() != null) {
0588: webRule = (WebRuleBaseValues) webRule.getResponsibility(
0589: ruleForm.getResponsibilityIndex().intValue())
0590: .getDelegationRule(
0591: ruleForm.getDelegationIndex().intValue())
0592: .getDelegationRuleBaseValues();
0593: ruleForm.getShowHide().getChild(index).getChild(
0594: ruleForm.getResponsibilityIndex()).getChild(
0595: ruleForm.getDelegationIndex()).append();
0596: } else {
0597: ruleForm.getShowHide().getChild(index).append();
0598: }
0599: WebRuleResponsibility responsibility = webRule
0600: .createNewRuleResponsibility();
0601:
0602: RuleTemplate ruleTemplate = getRuleTemplateService()
0603: .findByRuleTemplateId(webRule.getRuleTemplateId());
0604: if (ruleTemplate.getDefaultActionRequestValue() != null
0605: && ruleTemplate.getDefaultActionRequestValue()
0606: .getValue() != null) {
0607: responsibility.setActionRequestedCd(ruleTemplate
0608: .getDefaultActionRequestValue().getValue());
0609: }
0610: if (ruleTemplate.getDelegationTemplate() != null) {
0611: responsibility.setHasDelegateRuleTemplate(true);
0612: }
0613:
0614: responsibility.setDelegationRulesMaterialized(true);
0615: establishRequiredState(request, form);
0616: return mapping.findForward("basic");
0617: }
0618:
0619: public ActionForward showDelegations(ActionMapping mapping,
0620: ActionForm form, HttpServletRequest request,
0621: HttpServletResponse response) throws Exception {
0622: Rule2Form ruleForm = (Rule2Form) form;
0623: int ruleIndex = ruleForm.getRuleIndex().intValue();
0624: int respIndex = ruleForm.getResponsibilityIndex().intValue();
0625: WebRuleResponsibility responsibility = (WebRuleResponsibility) ruleForm
0626: .getMyRules().getRule(ruleIndex).getResponsibility(
0627: respIndex);
0628: responsibility.setShowDelegations(true);
0629: responsibility.loadDelegations();
0630: ShowHideTree respTree = ruleForm.getShowHide().getChild(
0631: ruleIndex).getChild(respIndex);
0632: for (Iterator iterator = responsibility.getDelegationRules()
0633: .iterator(); iterator.hasNext();) {
0634: RuleDelegation delegation = (RuleDelegation) iterator
0635: .next();
0636: ShowHideTree delTree = respTree.append();
0637: for (Iterator delRespIt = delegation
0638: .getDelegationRuleBaseValues()
0639: .getResponsibilities().iterator(); delRespIt
0640: .hasNext();) {
0641: delRespIt.next();
0642: delTree.append();
0643: }
0644: }
0645: return mapping.findForward(ruleForm.getForward());
0646: }
0647:
0648: public ActionForward showDelegationsReport(ActionMapping mapping,
0649: ActionForm form, HttpServletRequest request,
0650: HttpServletResponse response) throws Exception {
0651: ActionForward reportForward = report(mapping, form, request,
0652: response);
0653: Rule2Form ruleForm = (Rule2Form) form;
0654: int ruleIndex = ruleForm.getRuleIndex().intValue();
0655: int respIndex = ruleForm.getResponsibilityIndex().intValue();
0656: String extraId = (ruleForm.getExtraId() == null ? "" : ruleForm
0657: .getExtraId());
0658: WebRuleResponsibility responsibility = (WebRuleResponsibility) ((WebRuleBaseValues) ruleForm
0659: .getRules().get(ruleIndex))
0660: .getResponsibility(respIndex);
0661: ShowHideTree respTree = ruleForm.getShowHide().getChild(
0662: ruleIndex).getChild(respIndex);
0663: for (Iterator iterator = responsibility.getDelegationRules()
0664: .iterator(); iterator.hasNext();) {
0665: RuleDelegation delegation = (RuleDelegation) iterator
0666: .next();
0667: ShowHideTree delTree = respTree.append();
0668: for (Iterator delRespIt = delegation
0669: .getDelegationRuleBaseValues()
0670: .getResponsibilities().iterator(); delRespIt
0671: .hasNext();) {
0672: delRespIt.next();
0673: delTree.append();
0674: }
0675: }
0676: ruleForm.getShowDelegationsMap().put(
0677: extraId + "rule" + ruleIndex + "resp" + respIndex,
0678: "true");
0679: return reportForward;
0680: }
0681:
0682: public ActionForward showDelegationsDocHandler(
0683: ActionMapping mapping, ActionForm form,
0684: HttpServletRequest request, HttpServletResponse response)
0685: throws Exception {
0686: Rule2Form ruleForm = (Rule2Form) form;
0687: ActionForward forward = docHandler(mapping, form, request,
0688: response);
0689: int ruleIndex = ruleForm.getRuleIndex().intValue();
0690: int respIndex = ruleForm.getResponsibilityIndex().intValue();
0691: String extraId = (ruleForm.getExtraId() == null ? "" : ruleForm
0692: .getExtraId());
0693: WebRuleResponsibility responsibility = null;
0694: ShowHideTree respTree = null;
0695: if ("prev".equals(extraId)) {
0696: WebRuleBaseValues previousRuleBaseValVersion = new WebRuleBaseValues(
0697: ((RuleBaseValues) ruleForm.getRules()
0698: .get(ruleIndex)).getPreviousVersion());
0699: responsibility = (WebRuleResponsibility) previousRuleBaseValVersion
0700: .getResponsibility(respIndex);
0701: respTree = ruleForm.getParentShowHide().getChild(ruleIndex)
0702: .getChild(respIndex);
0703: } else {
0704: responsibility = (WebRuleResponsibility) ((WebRuleBaseValues) ruleForm
0705: .getRules().get(ruleIndex))
0706: .getResponsibility(respIndex);
0707: respTree = ruleForm.getShowHide().getChild(ruleIndex)
0708: .getChild(respIndex);
0709: }
0710: for (Iterator iterator = responsibility.getDelegationRules()
0711: .iterator(); iterator.hasNext();) {
0712: RuleDelegation delegation = (RuleDelegation) iterator
0713: .next();
0714: ShowHideTree delTree = respTree.append();
0715: for (Iterator delRespIt = delegation
0716: .getDelegationRuleBaseValues()
0717: .getResponsibilities().iterator(); delRespIt
0718: .hasNext();) {
0719: delRespIt.next();
0720: delTree.append();
0721: }
0722: }
0723: ruleForm.getShowDelegationsMap().put(
0724: extraId + "rule" + ruleIndex + "resp" + respIndex,
0725: "true");
0726: return forward;
0727: }
0728:
0729: public ActionForward blanketApprove(ActionMapping mapping,
0730: ActionForm form, HttpServletRequest request,
0731: HttpServletResponse response) throws Exception {
0732: Rule2Form ruleForm = (Rule2Form) form;
0733: ActionErrors errors = new ActionErrors();
0734: ActionForward forward = routeRule(errors, mapping, form,
0735: request, response, true);
0736: if (!errors.isEmpty()) {
0737: return forward;
0738: }
0739: ActionErrors messages = new ActionErrors();
0740: messages.add("hasErrors", new ActionMessage(
0741: "general.routing.blanketApproved",
0742: "Rule with document id = " + ruleForm.getDocId()));
0743: saveErrors(request, messages);
0744: return forward;
0745: }
0746:
0747: public ActionForward route(ActionMapping mapping, ActionForm form,
0748: HttpServletRequest request, HttpServletResponse response)
0749: throws Exception {
0750: Rule2Form ruleForm = (Rule2Form) form;
0751: ActionErrors errors = new ActionErrors();
0752: ActionForward forward = routeRule(errors, mapping, form,
0753: request, response, false);
0754: if (!errors.isEmpty()) {
0755: return forward;
0756: }
0757: ActionErrors messages = new ActionErrors();
0758: messages.add("hasErrors", new ActionMessage(
0759: "rule.route.confirmation", "" + ruleForm.getDocId()));
0760: saveErrors(request, messages);
0761: return forward;
0762: }
0763:
0764: public ActionForward save(ActionMapping mapping, ActionForm form,
0765: HttpServletRequest request, HttpServletResponse response)
0766: throws Exception {
0767: return route(mapping, form, request, response);
0768: }
0769:
0770: public ActionForward routeRule(ActionErrors errors,
0771: ActionMapping mapping, ActionForm form,
0772: HttpServletRequest request, HttpServletResponse response,
0773: boolean blanketApprove) throws Exception {
0774: Rule2Form ruleForm = (Rule2Form) form;
0775:
0776: MyRules2 rules = ruleForm.getMyRules();
0777: int ruleIndex = 0;
0778: for (Iterator iter = rules.getRules().iterator(); iter
0779: .hasNext();) {
0780: WebRuleBaseValues rule = (WebRuleBaseValues) iter.next();
0781: rule.validateRule("myRules.rule[" + ruleIndex + "].",
0782: errors);
0783: ruleIndex++;
0784: }
0785: if (!errors.isEmpty()) {
0786: errors.add("hasErrors", new ActionMessage(
0787: "general.hasErrors"));
0788: saveErrors(request, errors);
0789: return mapping.findForward("basic");
0790: }
0791: for (Iterator iter = rules.getRules().iterator(); iter
0792: .hasNext();) {
0793: WebRuleBaseValues rule = (WebRuleBaseValues) iter.next();
0794:
0795: if (checkLockedForRouting(errors, rule, true)) {
0796: saveErrors(request, errors);
0797: return mapping.findForward("basic");
0798: }
0799: rule.materialize();
0800: for (Iterator iterator = rule.getResponsibilities()
0801: .iterator(); iterator.hasNext();) {
0802: WebRuleResponsibility responsibility = (WebRuleResponsibility) iterator
0803: .next();
0804: if (responsibility.isShowDelegations()) {
0805: for (Iterator iterator1 = responsibility
0806: .getDelegationRules().iterator(); iterator1
0807: .hasNext();) {
0808: RuleDelegation delegation = (RuleDelegation) iterator1
0809: .next();
0810: delegation
0811: .setRuleResponsibility(responsibility);
0812: ((WebRuleBaseValues) delegation
0813: .getDelegationRuleBaseValues())
0814: .materialize();
0815: for (Iterator iterator2 = delegation
0816: .getDelegationRuleBaseValues()
0817: .getResponsibilities().iterator(); iterator2
0818: .hasNext();) {
0819: RuleResponsibility responsibilityDelegate = (RuleResponsibility) iterator2
0820: .next();
0821: responsibilityDelegate
0822: .setRuleResponsibilityKey(null);
0823: }
0824: }
0825: } else {
0826: responsibility.prepareHiddenDelegationsForRoute();
0827: }
0828: responsibility.setRuleResponsibilityKey(null);
0829: }
0830: }
0831:
0832: Long routeId = getRuleService().route2(ruleForm.getDocId(),
0833: rules, getUserSession(request).getWorkflowUser(),
0834: ruleForm.getAnnotation(), blanketApprove);
0835:
0836: ruleForm.reset();
0837: ruleForm.setChoosingTemplate(true);
0838: ruleForm.setDocId(routeId);
0839: ruleForm.setCurrentRuleId(null);
0840: return report(mapping, ruleForm, request, response);
0841: }
0842:
0843: public ActionMessages establishRequiredState(
0844: HttpServletRequest request, ActionForm form)
0845: throws Exception {
0846: Rule2Form ruleForm = (Rule2Form) form;
0847: ruleForm.setActionRequestCodes(CodeTranslator.arLabels);
0848: ruleForm.setRuleTemplates(getRuleTemplateService().findAll());
0849: MyRules2 myRules = ruleForm.getMyRules();
0850: for (Iterator ruleIt = myRules.getRules().iterator(); ruleIt
0851: .hasNext();) {
0852: WebRuleBaseValues rule = (WebRuleBaseValues) ruleIt.next();
0853: rule.establishRequiredState();
0854: }
0855: if (ruleForm.getDocId() != null
0856: && ruleForm.getFlexDoc() == null) {
0857: ruleForm.setFlexDoc(new WorkflowDocument(new WorkflowIdVO(
0858: getUserSession(request).getWorkflowUser()
0859: .getWorkflowId()), ruleForm.getDocId()));
0860: }
0861: ruleForm.establishVisibleActionRequestCds();
0862: return null;
0863: }
0864:
0865: public ActionForward docHandler(ActionMapping mapping,
0866: ActionForm form, HttpServletRequest request,
0867: HttpServletResponse response) throws Exception {
0868: Rule2Form ruleForm = (Rule2Form) form;
0869:
0870: if (IDocHandler.INITIATE_COMMAND.equalsIgnoreCase(ruleForm
0871: .getCommand())) {
0872: return start(mapping, form, request, response);
0873: } else {
0874: WorkflowDocument flexDoc = new WorkflowDocument(
0875: new WorkflowIdVO(getUserSession(request)
0876: .getWorkflowUser().getWorkflowId()),
0877: ruleForm.getDocId());
0878: ruleForm.setFlexDoc(flexDoc);
0879: loadDocHandlerRules(flexDoc.getRouteHeaderId(), ruleForm);
0880: return mapping.findForward("docHandler");
0881: }
0882: }
0883:
0884: private void createFlexDoc(HttpServletRequest request,
0885: Rule2Form rule2Form, List rules) throws WorkflowException {
0886: if (rule2Form.getFlexDoc() == null) {
0887: try {
0888: String ruleDocTypeName = getRuleService()
0889: .getRuleDocmentTypeName(rules);
0890: rule2Form.setFlexDoc(new WorkflowDocument(
0891: new WorkflowIdVO(getUserSession(request)
0892: .getWorkflowUser().getWorkflowId()),
0893: ruleDocTypeName));
0894: } catch (Exception e) {
0895: throw new WorkflowException(e);
0896: }
0897:
0898: rule2Form.setDocId(rule2Form.getFlexDoc()
0899: .getRouteHeaderId());
0900: rule2Form.establishVisibleActionRequestCds();
0901: }
0902: }
0903:
0904: private void loadDocHandlerRules(Long routeHeaderId,
0905: Rule2Form ruleForm) throws Exception {
0906: List rules = getRuleService()
0907: .findByRouteHeaderId(routeHeaderId);
0908: rules = filterDocHandlerRules(rules);
0909: List webRules = new ArrayList();
0910: for (Iterator iterator = rules.iterator(); iterator.hasNext();) {
0911: RuleBaseValues rule = (RuleBaseValues) iterator.next();
0912: WebRuleBaseValues webRule = new WebRuleBaseValues(rule);
0913: if (webRule.getPreviousVersionId() != null) {
0914: webRule.setPreviousVersion(new WebRuleBaseValues(
0915: webRule.getPreviousVersion()));
0916: ((WebRuleBaseValues) webRule.getPreviousVersion())
0917: .establishRequiredState();
0918: }
0919: if (webRule.getCurrentInd().booleanValue()) {
0920: webRule.setPreviousVersion(null);
0921: webRule.setPreviousVersionId(null);
0922: }
0923: webRules.add(webRule);
0924: initializeShowHide(ruleForm.getShowHide(), webRule);
0925: if (webRule.getPreviousVersion() != null) {
0926: initializeShowHide(ruleForm.getParentShowHide(),
0927: webRule.getPreviousVersion());
0928: }
0929: }
0930: ruleForm.setRules(webRules);
0931:
0932: // set up show/hide of delegation rules
0933: int ruleIndex = 0;
0934: for (Iterator ruleIt = webRules.iterator(); ruleIt.hasNext();) {
0935: RuleBaseValues rule = (RuleBaseValues) ruleIt.next();
0936: int respIndex = 0;
0937: for (Iterator respIt = rule.getResponsibilities()
0938: .iterator(); respIt.hasNext();) {
0939: RuleResponsibility responsibility = (RuleResponsibility) respIt
0940: .next();
0941: String key = "currrule" + ruleIndex + "resp"
0942: + respIndex;
0943: if (ruleForm.getShowDelegationsMap().get(key) == null) {
0944: int numDelegations = responsibility
0945: .getDelegationRules().size();
0946: ruleForm
0947: .getShowDelegationsMap()
0948: .put(
0949: key,
0950: new Boolean(
0951: numDelegations <= Integer
0952: .parseInt(Utilities
0953: .getApplicationConstant("Config.Application.DelegateLimit")))
0954: .toString());
0955: }
0956: respIndex++;
0957: }
0958: ruleIndex++;
0959: if (rule.getPreviousVersion() != null) {
0960: int prevRespIndex = 0;
0961: for (Iterator prevRespIt = rule.getPreviousVersion()
0962: .getResponsibilities().iterator(); prevRespIt
0963: .hasNext();) {
0964: RuleResponsibility responsibility = (RuleResponsibility) prevRespIt
0965: .next();
0966: String key = "prevrule" + ruleIndex + "resp"
0967: + prevRespIndex;
0968: if (ruleForm.getShowDelegationsMap().get(key) == null) {
0969: int numDelegations = responsibility
0970: .getDelegationRules().size();
0971: ruleForm
0972: .getShowDelegationsMap()
0973: .put(
0974: key,
0975: new Boolean(
0976: numDelegations <= Integer
0977: .parseInt(Utilities
0978: .getApplicationConstant("Config.Application.DelegateLimit")))
0979: .toString());
0980: }
0981: prevRespIndex++;
0982: }
0983: }
0984: }
0985: }
0986:
0987: /**
0988: * This method looks for cases where we routed a delegate rule and filters the parent rule out of the list. This is done so that it doesn't show up in the doc handler list. From the user's perspective, they are really only interested in the delegate rule.
0989: */
0990: private List filterDocHandlerRules(List rules) {
0991: Map ruleMap = new HashMap();
0992: for (Iterator iterator = rules.iterator(); iterator.hasNext();) {
0993: RuleBaseValues rule = (RuleBaseValues) iterator.next();
0994: ruleMap.put(rule.getRuleBaseValuesId(), rule);
0995: }
0996: for (Iterator iterator = rules.iterator(); iterator.hasNext();) {
0997: RuleBaseValues rule = (RuleBaseValues) iterator.next();
0998: boolean foundDelegate = false;
0999: resp: for (Iterator respIt = rule.getResponsibilities()
1000: .iterator(); respIt.hasNext();) {
1001: RuleResponsibility responsibility = (RuleResponsibility) respIt
1002: .next();
1003: for (Iterator delIt = responsibility
1004: .getDelegationRules().iterator(); delIt
1005: .hasNext();) {
1006: RuleDelegation delegation = (RuleDelegation) delIt
1007: .next();
1008: if (ruleMap.containsKey(delegation
1009: .getDelegateRuleId())) {
1010: foundDelegate = true;
1011: break resp;
1012: }
1013: }
1014: }
1015: if (foundDelegate) {
1016: ruleMap.remove(rule.getRuleBaseValuesId());
1017: }
1018: }
1019: return new ArrayList(ruleMap.values());
1020: }
1021:
1022: private ShowHideTree initializeShowHide(MyRules2 myRules) {
1023: ShowHideTree showHide = new ShowHideTree();
1024: for (Iterator ruleIt = myRules.getRules().iterator(); ruleIt
1025: .hasNext();) {
1026: RuleBaseValues rule = (RuleBaseValues) ruleIt.next();
1027: initializeShowHide(showHide, rule);
1028: }
1029: return showHide;
1030: }
1031:
1032: private ShowHideTree initializeShowHide(ShowHideTree showHide,
1033: RuleBaseValues rule) {
1034: ShowHideTree ruleTree = showHide.append();
1035: for (Iterator respIt = rule.getResponsibilities().iterator(); respIt
1036: .hasNext();) {
1037: WebRuleResponsibility responsibility = (WebRuleResponsibility) respIt
1038: .next();
1039: ShowHideTree respTree = ruleTree.append();
1040: if (responsibility.isShowDelegations()) {
1041: for (Iterator delIt = responsibility
1042: .getDelegationRules().iterator(); delIt
1043: .hasNext();) {
1044: RuleDelegation delegation = (RuleDelegation) delIt
1045: .next();
1046: ShowHideTree delTree = respTree.append();
1047: for (Iterator delRespIt = delegation
1048: .getDelegationRuleBaseValues()
1049: .getResponsibilities().iterator(); delRespIt
1050: .hasNext();) {
1051: RuleResponsibility delResponsibility = (RuleResponsibility) delRespIt
1052: .next();
1053: delTree.append();
1054: }
1055: }
1056: }
1057: }
1058: return ruleTree;
1059: }
1060:
1061: private boolean checkLockedForRouting(ActionErrors errors,
1062: RuleBaseValues rule, boolean usePreviousId) {
1063: if ("true".equalsIgnoreCase(Utilities
1064: .getApplicationConstant(EdenConstants.RULE_LOCKING_ON))) {
1065: Long id = (usePreviousId ? rule.getPreviousVersionId()
1066: : rule.getRuleBaseValuesId());
1067: if (id != null) {
1068: Long routeHeaderId = getRuleService()
1069: .isLockedForRouting(id);
1070: if (routeHeaderId != null) {
1071: errors.add("hasErrors", new ActionMessage(
1072: "routetemplate.RuleService.ruleInRoute", ""
1073: + routeHeaderId.longValue()));
1074: return true;
1075: }
1076: }
1077: }
1078: return false;
1079: }
1080:
1081: private RuleService getRuleService() {
1082: return (RuleService) KEWServiceLocator
1083: .getService(KEWServiceLocator.RULE_SERVICE);
1084: }
1085:
1086: private RuleTemplateService getRuleTemplateService() {
1087: return (RuleTemplateService) KEWServiceLocator
1088: .getService(KEWServiceLocator.RULE_TEMPLATE_SERVICE);
1089: }
1090:
1091: private RuleDelegationService getRuleDelegationService() {
1092: return (RuleDelegationService) KEWServiceLocator
1093: .getService(KEWServiceLocator.RULE_DELEGATION_SERVICE);
1094: }
1095:
1096: }
|