KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portal > faces > renderer > html > portlet > PortletRenderer


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.portal.faces.renderer.html.portlet;
6
7 import java.io.IOException JavaDoc;
8 import java.util.*;
9 import javax.faces.component.UIComponent;
10 import javax.faces.context.FacesContext;
11 import javax.faces.context.ResponseWriter;
12 import javax.portlet.WindowState;
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14 import javax.servlet.http.HttpServletResponse JavaDoc;
15 import org.exoplatform.Constants;
16 import org.exoplatform.commons.utils.ExceptionUtil ;
17 import org.exoplatform.container.PortalContainer;
18 import org.exoplatform.container.SessionContainer;
19 import org.exoplatform.faces.core.event.ExoActionEvent;
20 import org.exoplatform.portal.PortalConstants;
21 import org.exoplatform.portal.faces.component.UIBasicComponent;
22 import org.exoplatform.portal.faces.component.UIPortlet;
23 import org.exoplatform.portal.faces.renderer.BaseRenderer;
24 import org.exoplatform.portal.session.RequestInfo;
25 import org.exoplatform.services.log.LogUtil;
26 import org.exoplatform.services.organization.UserProfile;
27 import org.exoplatform.services.portal.model.Portlet;
28 import org.exoplatform.services.portletcontainer.PortletContainerService;
29 import org.exoplatform.services.portletcontainer.pci.ExoWindowID;
30 import org.exoplatform.services.portletcontainer.pci.RenderInput;
31 import org.exoplatform.services.portletcontainer.pci.RenderOutput;
32 /**
33  * Fri, May 30, 2003 @
34  * @author : Mestrallet Benjamin
35  * @email: benjmestrallet@users.sourceforge.net
36  * @author: Tuan Nguyen
37  * @email: tuan08@users.sourceforge.net
38  * @version: $Id: PortletRenderer.java,v 1.19 2004/11/01 21:06:50 tuan08 Exp $
39  */

40 public class PortletRenderer extends BaseRenderer {
41   private PortletContainerService portletContainer_ ;
42   
43   public PortletRenderer() {
44   }
45
46   protected void decodeComponentAction(FacesContext context, UIComponent uiComponent) {
47     UIPortlet basicComponent = (UIPortlet) uiComponent ;
48     Map paramMap = context.getExternalContext().getRequestParameterMap() ;
49     basicComponent.broadcast(new ExoActionEvent(uiComponent, "portletAction", paramMap)) ;
50   }
51   
52   public void encodeChildren(FacesContext context, UIComponent uiComponent) throws IOException JavaDoc {
53     UIPortlet uiPortlet = (UIPortlet) uiComponent ;
54     ResponseWriter w = context.getResponseWriter();
55     if(uiPortlet.hasError()) {
56       w.write(uiPortlet.getErrorMessage()) ;
57       return ;
58     }
59     PortletContainerService portletContainer = getPortletContainerService() ;
60     HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) context.getExternalContext().getRequest() ;
61     HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) context.getExternalContext().getResponse();
62     SessionContainer scontainer = SessionContainer.getInstance() ;
63     RequestInfo rinfo = (RequestInfo) scontainer.getComponentInstanceOfType(RequestInfo.class);
64     UserProfile uprofile =
65       (UserProfile) scontainer.getComponentInstanceOfType(UserProfile.class) ;
66     ResourceBundle res = getApplicationResourceBundle(context.getExternalContext()) ;
67     
68     Map renderParams = uiPortlet.getRenderParameters() ;
69     if (renderParams == null) {
70       renderParams = copyRequestParameterMap(request.getParameterMap()) ;
71       uiPortlet.setRenderParameters(renderParams);
72     }
73     RenderInput input = new RenderInput();
74     String JavaDoc baseUrl = null ;
75     String JavaDoc configurationSource = uiPortlet.getWindowId().getConfigurationSource() ;
76     if(configurationSource == ExoWindowID.DEFAULT_PORTAL_CONFIG ||
77        configurationSource == ExoWindowID.MOBILE_PORTAL_CONFIG ) {
78       StringBuffer JavaDoc sB = new StringBuffer JavaDoc(rinfo.getOwnerURI()).
79       append("?" + Constants.COMPONENT_PARAMETER).append("=").
80       append(uiPortlet.getWindowId().getUniqueID());
81       baseUrl = sB.toString() ;
82     } else {
83       StringBuffer JavaDoc sB = new StringBuffer JavaDoc(rinfo.getPageURI()).
84       append("?" + Constants.COMPONENT_PARAMETER).append("=").
85       append(uiPortlet.getWindowId().getUniqueID());
86       baseUrl = sB.toString() ;
87     }
88     input.setBaseURL(baseUrl);
89     input.setWindowID(uiPortlet.getWindowId());
90     input.setUserAttributes(uprofile.getUserInfoMap());
91     input.setPortletMode(uiPortlet.getPortletMode());
92     input.setWindowState(uiPortlet.getWindowState());
93     input.setMarkup("text/html");
94     input.setRenderParameters(renderParams);
95     input.setTitle(uiPortlet.getDisplayTitle());
96     input.setUpdateCache(uiPortlet.getUpdateCache());
97     RenderOutput output = null;
98     String JavaDoc portletContent = "EXO-ERROR: Portlet container throw an exception\n" ;
99     try {
100       output = portletContainer.render(request, response, input);
101       uiPortlet.setUpdateCache(false) ;
102       if(output.getContent() == null) {
103         //System.out.println("portlet has error " + uiPortlet.getId()) ;
104
}
105       portletContent = new String JavaDoc(output.getContent()) ;
106     } catch (Throwable JavaDoc ex) {
107       ex = ExceptionUtil.getRootCause(ex) ;
108       portletContent += ExceptionUtil.getStackTrace(ex, 100) ;
109       LogUtil.getLog(getClass()).error("Render Portlet Error: ", ex) ;
110     }
111     if (portletContent == null) portletContent = "" ;
112
113     String JavaDoc portletTitle = output.getTitle() ;
114     if(portletTitle == null ) portletTitle = "Portlet" ;
115     
116     if(uiPortlet.getComponentMode() == UIBasicComponent.COMPONENT_EDIT_MODE) {
117         renderEditMode(w , uiPortlet, res, portletTitle, portletContent, baseUrl) ;
118     } else {
119         renderViewMode(w, uiPortlet, res, portletTitle, portletContent, baseUrl) ;
120     }
121   }
122
123   protected void renderViewMode(ResponseWriter w, UIPortlet uiPortlet, ResourceBundle res,
124                                 String JavaDoc title, String JavaDoc content, String JavaDoc baseUrl) throws IOException JavaDoc {
125      Portlet model = uiPortlet.getPortletModel() ;
126      w.write("<table class='"); w.write(model.getDecorator()) ; w.write("-decorator'") ;
127      w.write(" id='") ; w.write(uiPortlet.getId());w.write("'>") ;
128      renderPortletHeaderBar(w, uiPortlet, res, title, baseUrl) ;
129      if (uiPortlet.getWindowState() != WindowState.MINIMIZED) {
130        renderPortletBody(w, uiPortlet, content) ;
131      }
132      renderPortletFooterBar(w, uiPortlet, title, baseUrl) ;
133      w.write("</table>\n");
134   }
135   
136   protected void renderEditMode(ResponseWriter w , UIPortlet uiPortlet, ResourceBundle res,
137                                 String JavaDoc title, String JavaDoc content, String JavaDoc baseURL) throws IOException JavaDoc {
138     w.write("<table class='customizer'") ;
139     w.write(" id='") ; w.write(uiPortlet.getId());w.write("-customizer'>") ;
140     w. write("<tr>") ;
141     w. write("<th align='left'>") ;
142     w. write("Portlet") ;
143     w. write("</th>") ;
144     w. write("<th align='right'>") ;
145     linkRenderer_.render(w, uiPortlet, res.getString("UIPortlet.button.edit-portlet"), PortalConstants.editParams);
146     linkRenderer_.render(w, uiPortlet, res.getString("UIPortlet.button.remove-portlet"), baseURL, PortalConstants.deleteParams, null);
147     if(uiPortlet.getFloat() == UIBasicComponent.FLOAT_BOTTOM) {
148       linkRenderer_.render(w, uiPortlet, res.getString("UIPortlet.button.move-up"), PortalConstants.moveUpParams);
149       linkRenderer_.render(w, uiPortlet, res.getString("UIPortlet.button.move-down"), PortalConstants.moveDownParams);
150     } else {
151       linkRenderer_.render(w, uiPortlet, res.getString("UIPortlet.button.move-left"), PortalConstants.moveUpParams);
152       linkRenderer_.render(w, uiPortlet, res.getString("UIPortlet.button.move-right"), PortalConstants.moveDownParams);
153     }
154     w. write("</th>") ;
155     w. write("</tr>") ;
156     w. write("<tr>") ;
157     w. write("<td colspan='2' style='height: 100%'>") ;
158     renderViewMode(w, uiPortlet, res, title, content, baseURL) ;
159     w. write("</td>") ;
160     w. write("</tr>") ;
161     w.write("</table>") ;
162   }
163   
164   protected void renderModeLink(ResponseWriter w , UIPortlet uiPortlet, ResourceBundle res,
165                                 String JavaDoc mode, String JavaDoc baseUrl) throws IOException JavaDoc {
166     if(uiPortlet.isNew()) return ;
167     String JavaDoc currentMode = uiPortlet.getPortletMode().toString() ;
168     if(currentMode.equals(mode)) mode = UIPortlet.PORTLET_VIEW_MODE ;
169     w.write("<a HREF='"); w.write(baseUrl) ;
170     w.write(Constants.AMPERSAND + Constants.PORTLET_MODE_PARAMETER + "="); w.write(mode);
171     w.write("'>") ;
172     w.write(res.getString(mode)) ;
173     w.write("</a>");
174   }
175
176   protected void renderWindowStateLink(ResponseWriter w, UIPortlet portlet, ResourceBundle res,
177                                        WindowState state, String JavaDoc baseUrl) throws IOException JavaDoc {
178     WindowState currentState = portlet.getWindowState() ;
179     if(currentState == state) state = WindowState.NORMAL ;
180     w.write("<a HREF='") ;
181     w.write(baseUrl) ;
182     w.write(Constants.AMPERSAND + Constants.WINDOW_STATE_PARAMETER + "=");
183     w.write(state.toString());
184     w.write("'>") ;
185     w.write(res.getString(state.toString())) ;
186     w.write("</a>");
187   }
188
189   protected void renderPortletBody(ResponseWriter w, UIPortlet uiPortlet, String JavaDoc content) throws IOException JavaDoc {
190     w.write("<tr>\n<td colspan='2' class='");
191     w.write(uiPortlet.getPortletModel().getPortletStyle()); w.write("-portlet'>");
192     w.write(content) ;
193     w.write("\n</td>\n</tr>\n");
194   }
195
196   protected void renderPortletHeaderBar(ResponseWriter w, UIPortlet uiPortlet, ResourceBundle res,
197                                         String JavaDoc portletTitle, String JavaDoc baseUrl) throws IOException JavaDoc {
198     Portlet model = uiPortlet.getPortletModel() ;
199     if (!model.getShowInfoBar()) return ;
200     w.write("<tr>\n");
201     w. write("<td class='portlet-info-bar'>");
202     w. write(portletTitle);
203     w. write("</td>\n");
204     //icon actions
205
w. write("<td class='portlet-info-bar' align='right'>\n");
206     if(model.getShowPortletMode()) {
207       List modes = uiPortlet.getHtmlSupportModes() ;
208       if (uiPortlet.hasComponentAdminRole()) {
209         for (int i =0 ; i < modes.size() ; i++) {
210           String JavaDoc mode = (String JavaDoc) modes.get(i) ;
211           renderModeLink(w, uiPortlet,res, mode, baseUrl) ;
212         }
213       } else {
214         for (int i =0 ; i < modes.size() ; i++) {
215           String JavaDoc mode = (String JavaDoc) modes.get(i) ;
216           if("help".equals(mode)) {
217             renderModeLink(w, uiPortlet, res, mode, baseUrl) ;
218           }
219         }
220       }
221     }
222     if(model.getShowWindowState()) {
223       renderWindowStateLink(w, uiPortlet, res, WindowState.MINIMIZED, baseUrl) ;
224       renderWindowStateLink(w, uiPortlet, res, WindowState.MAXIMIZED, baseUrl) ;
225     }
226     w. write("</td>\n");
227     w.write("</tr>\n");
228   }
229
230   protected void renderPortletFooterBar(ResponseWriter w, UIPortlet uiPortlet,
231                                         String JavaDoc portletTitle, String JavaDoc baseUrl) throws IOException JavaDoc {
232   }
233
234   private PortletContainerService getPortletContainerService() {
235     if ( portletContainer_ == null) {
236       portletContainer_ =
237         (PortletContainerService) PortalContainer.getInstance().
238                                   getComponentInstanceOfType(PortletContainerService.class);
239     }
240     return portletContainer_ ;
241   }
242   
243   private Map copyRequestParameterMap(Map map) {
244     Map temp = new HashMap(10) ;
245     Iterator keys = map.keySet().iterator() ;
246     while (keys.hasNext()) {
247       String JavaDoc key = (String JavaDoc) keys.next() ;
248       temp.put(key, map.get(key)) ;
249     }
250     return temp ;
251   }
252 }
Popular Tags