001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.routetemplate.web;
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import org.apache.struts.action.ActionForm;
028: import org.apache.struts.action.ActionForward;
029: import org.apache.struts.action.ActionMapping;
030: import org.apache.struts.action.ActionMessages;
031:
032: import edu.iu.uis.eden.KEWServiceLocator;
033: import edu.iu.uis.eden.doctype.DocumentType;
034: import edu.iu.uis.eden.doctype.DocumentTypeService;
035: import edu.iu.uis.eden.engine.node.RouteNode;
036: import edu.iu.uis.eden.routetemplate.RuleBaseValues;
037: import edu.iu.uis.eden.routetemplate.RuleService;
038: import edu.iu.uis.eden.routetemplate.RuleTemplate;
039: import edu.iu.uis.eden.routetemplate.RuleTemplateService;
040: import edu.iu.uis.eden.web.WorkflowAction;
041:
042: /**
043: * A Struts Action for building and interacting with the Rule Quick Links.
044: *
045: * @author rkirkend
046: */
047: public class RuleQuickLinksAction extends WorkflowAction {
048:
049: public ActionForward start(ActionMapping mapping, ActionForm form,
050: HttpServletRequest request, HttpServletResponse response)
051: throws Exception {
052: makeLookupPathParam(mapping, request);
053: return mapping.findForward("basic");
054: }
055:
056: public ActionMessages establishRequiredState(
057: HttpServletRequest request, ActionForm form)
058: throws Exception {
059: RuleQuickLinksForm qlForm = (RuleQuickLinksForm) form;
060: List documentTypes;
061: if (qlForm.getRootDocTypeName() != null) {
062: documentTypes = new ArrayList();
063: DocumentType docType = getDocumentTypeService().findByName(
064: qlForm.getRootDocTypeName());
065: documentTypes.add(docType);
066: request.setAttribute("renderOpened", Boolean.TRUE);
067: } else {
068: documentTypes = getDocumentTypeService()
069: .findAllCurrentRootDocuments();
070: }
071: qlForm
072: .setDocumentTypeQuickLinksStructures(getDocumentTypeDataStructure(documentTypes));
073:
074: return null;
075: }
076:
077: public ActionForward addDelegationRule(ActionMapping mapping,
078: ActionForm form, HttpServletRequest request,
079: HttpServletResponse response) throws Exception {
080: Long ruleTemplateId = new Long(request
081: .getParameter("ruleTemplate.ruleTemplateId"));
082: String docTypeName = request.getParameter("docTypeFullName");
083: List rules = getRuleService().search(docTypeName, null,
084: ruleTemplateId, "", null, null, "", Boolean.FALSE,
085: Boolean.TRUE, new HashMap());
086: if (rules.size() == 1) {
087: RuleBaseValues rule = (RuleBaseValues) rules.get(0);
088: RuleTemplate ruleTemplate = getRuleTemplateService()
089: .findByRuleTemplateId(ruleTemplateId);
090: String url = "DelegateRule.do?methodToCall=start"
091: + "&parentRule.getDocTypeName=" + docTypeName
092: + "&ruleCreationValues.ruleTemplateId="
093: + ruleTemplate.getDelegationTemplateId()
094: + "&ruleCreationValues.ruleTemplateName="
095: + ruleTemplate.getDelegationTemplate().getName()
096: + "&ruleCreationValues.ruleId="
097: + rule.getRuleBaseValuesId();
098: return new ActionForward(url, true);
099: }
100: makeLookupPathParam(mapping, request);
101: return new ActionForward("Lookup.do?"
102: + stripMethodToCall(request.getQueryString()), true);
103: }
104:
105: private List getDocumentTypeDataStructure(List rootDocuments) {
106: List documentTypeQuickLinksStructures = new ArrayList();
107: for (Iterator iter = rootDocuments.iterator(); iter.hasNext();) {
108: DocumentTypeQuickLinksStructure quickLinkStruct = new DocumentTypeQuickLinksStructure(
109: (DocumentType) iter.next());
110: if (!quickLinkStruct.getFlattenedNodes().isEmpty()
111: || !quickLinkStruct.getChildrenDocumentTypes()
112: .isEmpty()) {
113: documentTypeQuickLinksStructures.add(quickLinkStruct);
114: }
115:
116: }
117:
118: return documentTypeQuickLinksStructures;
119: }
120:
121: /**
122: * A bean to hold a DocumentType with its flattened nodes for rendering purposes
123: * on the quick links.
124: *
125: * @author rkirkend
126: */
127: public static class DocumentTypeQuickLinksStructure {
128: private DocumentType documentType;
129: private List flattenedNodes = new ArrayList();
130: private List childrenDocumentTypes = new ArrayList();
131:
132: private DocumentTypeQuickLinksStructure(
133: DocumentType documentType) {
134: this .documentType = documentType;
135: List tempFlattendNodes = KEWServiceLocator
136: .getRouteNodeService().getFlattenedNodes(
137: documentType, true);
138: for (Iterator iter = tempFlattendNodes.iterator(); iter
139: .hasNext();) {
140: RouteNode routeNode = (RouteNode) iter.next();
141: if (routeNode.isFlexRM()) {
142: flattenedNodes.add(routeNode);
143: }
144: }
145: for (Iterator iter = documentType.getChildrenDocTypes()
146: .iterator(); iter.hasNext();) {
147: childrenDocumentTypes
148: .add(new DocumentTypeQuickLinksStructure(
149: (DocumentType) iter.next()));
150: }
151: }
152:
153: public List getChildrenDocumentTypes() {
154: return childrenDocumentTypes;
155: }
156:
157: public void setChildrenDocumentTypes(List childrenDocumentTypes) {
158: this .childrenDocumentTypes = childrenDocumentTypes;
159: }
160:
161: public DocumentType getDocumentType() {
162: return documentType;
163: }
164:
165: public void setDocumentType(DocumentType documentType) {
166: this .documentType = documentType;
167: }
168:
169: public List getFlattenedNodes() {
170: return flattenedNodes;
171: }
172:
173: public void setFlattenedNodes(List flattenedNodes) {
174: this .flattenedNodes = flattenedNodes;
175: }
176: }
177:
178: private void makeLookupPathParam(ActionMapping mapping,
179: HttpServletRequest request) {
180: String basePath = request.getScheme() + "://"
181: + request.getServerName() + ":"
182: + request.getServerPort() + request.getContextPath()
183: + mapping.getModuleConfig().getPrefix();
184: request.setAttribute("basePath", basePath);
185: }
186:
187: private String stripMethodToCall(String queryString) {
188: return queryString.replaceAll(
189: "methodToCall=addDelegationRule&", "");
190: }
191:
192: private DocumentTypeService getDocumentTypeService() {
193: return KEWServiceLocator.getDocumentTypeService();
194: }
195:
196: private RuleService getRuleService() {
197: return KEWServiceLocator.getRuleService();
198: }
199:
200: private RuleTemplateService getRuleTemplateService() {
201: return KEWServiceLocator.getRuleTemplateService();
202: }
203:
204: }
|