View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.edl.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.rice.core.api.util.xml.XmlJotter;
21  import org.kuali.rice.kim.api.identity.Person;
22  import org.kuali.rice.krad.util.GlobalVariables;
23  import org.w3c.dom.Document;
24  
25  import javax.servlet.http.HttpServletResponse;
26  import javax.xml.transform.OutputKeys;
27  import javax.xml.transform.Transformer;
28  import javax.xml.transform.dom.DOMSource;
29  import javax.xml.transform.stream.StreamResult;
30  import java.util.Stack;
31  
32  
33  /**
34   * Contains a stack of EDLControllers.  Allows EDL components to add new controllers to the chain runtime.  The idea
35   * being that this is how you would page between edls.  Uses the template associated with the last edl controller
36   * to render the dom of the last edl controller.
37   *
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   *
40   */
41  public class EDLControllerChain {
42  
43  	private static final Logger LOG = Logger.getLogger(EDLControllerChain.class);
44  
45  	private Stack<EDLController> edlControllers = new Stack<EDLController>();
46  
47  	public void renderEDL(RequestParser requestParser, HttpServletResponse response) throws Exception {
48  		EDLController edlController = edlControllers.pop();
49  		edlController.setEdlContext(getInitialEDLContext(edlController, requestParser, edlController.getStyle().newTransformer()));
50  
51  		Document dom = edlController.notifyComponents();
52  		if (edlControllers.isEmpty()) {
53  			transform(edlController.getEdlContext(), dom, response);
54  		} else {
55  			renderEDL(requestParser, response);
56  		}
57  	}
58  
59  	public void addEdlController(EDLController edlController) {
60  		edlControllers.add(edlController);
61  	}
62  
63  	private void transform(EDLContext edlContext, Document dom, HttpServletResponse response) throws Exception {
64  		if (StringUtils.isNotBlank(edlContext.getRedirectUrl())) {
65  			response.sendRedirect(edlContext.getRedirectUrl());
66  			return;
67  		}
68  	    response.setContentType("text/html; charset=UTF-8");
69  		Transformer transformer = edlContext.getTransformer();
70  
71          transformer.setOutputProperty("indent", "yes");
72          transformer.setOutputProperty(OutputKeys.INDENT, "yes");
73          String user = null;
74          String loggedInUser = null;
75          if (edlContext.getUserSession() != null) {
76              Person wu = edlContext.getUserSession().getPerson();
77              if (wu != null) user = wu.getPrincipalId();
78              wu = edlContext.getUserSession().getPerson();
79              if (wu != null) loggedInUser = wu.getPrincipalId();
80          }
81          transformer.setParameter("user", user);
82          transformer.setParameter("loggedInUser", loggedInUser);
83          if (LOG.isDebugEnabled()) {
84          	LOG.debug("Transforming dom " + XmlJotter.jotNode(dom, true));
85          }
86          transformer.transform(new DOMSource(dom), new StreamResult(response.getOutputStream()));
87  	}
88  
89  	private EDLContext getInitialEDLContext(EDLController edlController, RequestParser requestParser, Transformer transformer) {
90  		EDLContext edlContext = new EDLContext();
91  		edlContext.setEdlControllerChain(this);
92  		edlContext.setEdocLiteAssociation(edlController.getEdocLiteAssociation());
93  		edlContext.setUserSession(GlobalVariables.getUserSession());
94  		edlContext.setTransformer(transformer);
95  		edlContext.setRequestParser(requestParser);
96  		return edlContext;
97  	}
98  
99  }