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.edl;
018:
019: import java.util.Stack;
020:
021: import javax.servlet.http.HttpServletResponse;
022: import javax.xml.transform.OutputKeys;
023: import javax.xml.transform.Transformer;
024: import javax.xml.transform.dom.DOMSource;
025: import javax.xml.transform.stream.StreamResult;
026:
027: import org.apache.log4j.Logger;
028: import org.w3c.dom.Document;
029:
030: import edu.iu.uis.eden.user.WorkflowUser;
031: import edu.iu.uis.eden.util.XmlHelper;
032: import edu.iu.uis.eden.web.UserLoginFilter;
033:
034: /**
035: * Contains a stack of EDLControllers. Allows EDL components to add new controllers to the chain runtime. The idea
036: * being that this is how you would page between edls. Uses the template associated with the last edl controller
037: * to render the dom of the last edl controller.
038: *
039: * @author rkirkend
040: *
041: */
042: public class EDLControllerChain {
043:
044: private static final Logger LOG = Logger
045: .getLogger(EDLControllerChain.class);
046:
047: private Stack edlControllers = new Stack();
048:
049: public void renderEDL(RequestParser requestParser,
050: HttpServletResponse response) throws Exception {
051: EDLController edlController = (EDLController) edlControllers
052: .pop();
053: edlController.setEdlContext(getEDLContext(edlController,
054: requestParser, edlController.getStyle()
055: .newTransformer()));
056:
057: Document dom = edlController.notifyComponents();
058: if (edlControllers.isEmpty()) {
059: transform(edlController.getEdlContext(), dom, response);
060: } else {
061: renderEDL(requestParser, response);
062: }
063: }
064:
065: public void addEdlController(EDLController edlController) {
066: edlControllers.add(edlController);
067: }
068:
069: private void transform(EDLContext edlContext, Document dom,
070: HttpServletResponse response) throws Exception {
071: Transformer transformer = edlContext.getTransformer();
072:
073: transformer.setOutputProperty("indent", "yes");
074: transformer.setOutputProperty(OutputKeys.INDENT, "yes");
075: String user = null;
076: String loggedInUser = null;
077: if (edlContext.getUserSession() != null) {
078: WorkflowUser wu = edlContext.getUserSession()
079: .getWorkflowUser();
080: if (wu != null)
081: user = wu.getWorkflowUserId().getId();
082: wu = edlContext.getUserSession().getLoggedInWorkflowUser();
083: if (wu != null)
084: loggedInUser = wu.getWorkflowUserId().getId();
085: }
086: transformer.setParameter("user", user);
087: transformer.setParameter("loggedInUser", loggedInUser);
088: if (LOG.isDebugEnabled()) {
089: LOG.debug("Transforming dom "
090: + XmlHelper.jotNode(dom, true));
091: }
092: transformer.transform(new DOMSource(dom), new StreamResult(
093: response.getOutputStream()));
094: }
095:
096: private EDLContext getEDLContext(EDLController edlController,
097: RequestParser requestParser, Transformer transformer) {
098: EDLContext edlContext = new EDLContext();
099: edlContext.setEdlControllerChain(this);
100: edlContext.setEdocLiteAssociation(edlController
101: .getEdocLiteAssociation());
102: edlContext.setUserSession(UserLoginFilter
103: .getUserSession(requestParser.getRequest()));
104: edlContext.setTransformer(transformer);
105: edlContext.setRequestParser(requestParser);
106: return edlContext;
107: }
108:
109: }
|