001    /**
002     * Copyright 2005-2012 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.edl.impl;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.apache.log4j.Logger;
020    import org.kuali.rice.core.api.util.xml.XmlJotter;
021    import org.kuali.rice.kim.api.identity.Person;
022    import org.kuali.rice.krad.util.GlobalVariables;
023    import org.w3c.dom.Document;
024    
025    import javax.servlet.http.HttpServletResponse;
026    import javax.xml.transform.OutputKeys;
027    import javax.xml.transform.Transformer;
028    import javax.xml.transform.dom.DOMSource;
029    import javax.xml.transform.stream.StreamResult;
030    import javax.xml.xpath.XPathFactory;
031    import java.util.Stack;
032    
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 Kuali Rice Team (rice.collab@kuali.org)
040     *
041     */
042    public class EDLControllerChain {
043    
044            private static final Logger LOG = Logger.getLogger(EDLControllerChain.class);
045    
046            private Stack<EDLController> edlControllers = new Stack<EDLController>();
047    
048            public void renderEDL(RequestParser requestParser, HttpServletResponse response) throws Exception {
049                    EDLController edlController = edlControllers.pop();
050                    edlController.setEdlContext(getInitialEDLContext(edlController, requestParser, edlController.getStyle().newTransformer()));
051    
052                    Document dom = edlController.notifyComponents();
053                    if (edlControllers.isEmpty()) {
054                            transform(edlController.getEdlContext(), dom, response);
055                    } else {
056                            renderEDL(requestParser, response);
057                    }
058            }
059    
060            public void addEdlController(EDLController edlController) {
061                    edlControllers.add(edlController);
062            }
063    
064            private void transform(EDLContext edlContext, Document dom, HttpServletResponse response) throws Exception {
065                    if (StringUtils.isNotBlank(edlContext.getRedirectUrl())) {
066                            response.sendRedirect(edlContext.getRedirectUrl());
067                            return;
068                    }
069                response.setContentType("text/html; charset=UTF-8");
070                    Transformer transformer = edlContext.getTransformer();
071    
072            transformer.setOutputProperty("indent", "yes");
073            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
074            String user = null;
075            String loggedInUser = null;
076            if (edlContext.getUserSession() != null) {
077                Person wu = edlContext.getUserSession().getPerson();
078                if (wu != null) user = wu.getPrincipalId();
079                wu = edlContext.getUserSession().getPerson();
080                if (wu != null) loggedInUser = wu.getPrincipalId();
081            }
082            transformer.setParameter("user", user);
083            transformer.setParameter("loggedInUser", loggedInUser);
084            if (LOG.isDebugEnabled()) {
085                    LOG.debug("Transforming dom " + XmlJotter.jotNode(dom, true));
086            }
087            transformer.transform(new DOMSource(dom), new StreamResult(response.getOutputStream()));
088            }
089    
090            private EDLContext getInitialEDLContext(EDLController edlController, RequestParser requestParser, Transformer transformer) {
091                    EDLContext edlContext = new EDLContext();
092                    edlContext.setEdlControllerChain(this);
093                    edlContext.setEdocLiteAssociation(edlController.getEdocLiteAssociation());
094                    edlContext.setUserSession(GlobalVariables.getUserSession());
095                    edlContext.setTransformer(transformer);
096                    edlContext.setRequestParser(requestParser);
097            edlContext.setXpath(XPathFactory.newInstance().newXPath());
098            return edlContext;
099            }
100    
101    }