1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.bean;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.bean.BeanLocator;
27  import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
28  import com.liferay.portal.kernel.bean.PortletBeanLocatorUtil;
29  import com.liferay.portal.kernel.bean.Renderer;
30  import com.liferay.portal.kernel.bean.RendererException;
31  import com.liferay.portal.kernel.log.Log;
32  import com.liferay.portal.kernel.log.LogFactoryUtil;
33  import com.liferay.portal.kernel.util.ParamUtil;
34  import com.liferay.portal.kernel.util.PropsUtil;
35  import com.liferay.portal.kernel.util.StringPool;
36  import com.liferay.portal.kernel.util.StringUtil;
37  import com.liferay.portal.kernel.util.Validator;
38  import com.liferay.portal.kernel.velocity.VelocityContext;
39  import com.liferay.portal.kernel.velocity.VelocityEngineUtil;
40  import com.liferay.portal.util.ContentUtil;
41  import com.liferay.portal.util.PortalUtil;
42  import com.liferay.portal.util.PrefsPropsUtil;
43  import com.liferay.portal.velocity.VelocityVariables;
44  import com.liferay.portlet.PortletPreferencesFactoryUtil;
45  
46  import java.io.StringWriter;
47  
48  import java.lang.reflect.Method;
49  
50  import javax.portlet.PortletPreferences;
51  import javax.portlet.PortletRequest;
52  import javax.portlet.PortletResponse;
53  
54  import javax.servlet.http.HttpServletRequest;
55  import javax.servlet.http.HttpServletResponse;
56  
57  import org.springframework.beans.factory.NoSuchBeanDefinitionException;
58  
59  /**
60   * <a href="RendererImpl.java.html"><b><i>View Source</i></b></a>
61   *
62   * @author Raymond Augé
63   *
64   */
65  public class RendererImpl implements Renderer {
66  
67      public String renderBean(
68              HttpServletRequest request, HttpServletResponse response,
69              Object bean)
70          throws RendererException {
71  
72          return renderBean(request, response, null, bean, null);
73      }
74  
75      public String renderBean(
76              HttpServletRequest request, HttpServletResponse response,
77              Object bean, String varientSuffix)
78          throws RendererException {
79  
80          return renderBean(request, response, null, bean, varientSuffix);
81      }
82  
83      public String renderBean(
84              HttpServletRequest request, HttpServletResponse response,
85              String servletContextName, Object bean)
86          throws RendererException {
87  
88          return renderBean(request, response, servletContextName, bean, null);
89      }
90  
91      public String renderBean(
92              HttpServletRequest request, HttpServletResponse response,
93              String servletContextName, Object bean, String varientSuffix)
94          throws RendererException {
95  
96          if (bean == null) {
97              return null;
98          }
99  
100         long companyId = PortalUtil.getCompanyId(request);
101 
102         String className = _normalizeClassName(bean.getClass().getName());
103 
104         if (Validator.isNotNull(varientSuffix)) {
105             className = varientSuffix;
106         }
107 
108         String velocityTemplateContent = null;
109 
110         PortletPreferences preferences = _getPortletPreferences(request);
111 
112         if (preferences != null) {
113             velocityTemplateContent = preferences.getValue(
114                 RENDERER_TEMPLATE_PREFIX + className, StringPool.BLANK);
115         }
116 
117         if (Validator.isNull(velocityTemplateContent) &&
118             Validator.isNotNull(servletContextName)) {
119 
120             if (servletContextName.startsWith(StringPool.SLASH)) {
121                 servletContextName = servletContextName.substring(1);
122             }
123 
124             try {
125                 BeanLocator locator = PortletBeanLocatorUtil.getBeanLocator(
126                     servletContextName);
127 
128                 velocityTemplateContent = ContentUtil.get(
129                     locator.getClassLoader(),
130                     PropsUtil.get(RENDERER_TEMPLATE_PREFIX + className));
131             }
132             catch (Exception e) {
133             }
134         }
135 
136         if (Validator.isNull(velocityTemplateContent)) {
137             try {
138                 velocityTemplateContent = PrefsPropsUtil.getContent(
139                     companyId, RENDERER_TEMPLATE_PREFIX + className);
140             }
141             catch (Exception e) {
142             }
143         }
144 
145         if (Validator.isNull(velocityTemplateContent)) {
146             _log.warn(
147                 "No entity renderer template found for " + className);
148 
149             return null;
150         }
151 
152         VelocityContext velocityContext =
153             VelocityEngineUtil.getWrappedStandardToolsContext();
154 
155         // Velocity variables
156 
157         VelocityVariables.insertVariables(velocityContext, request);
158 
159         velocityContext.put(_BEAN, bean);
160 
161         try {
162             StringWriter stringWriter = new StringWriter();
163 
164             VelocityEngineUtil.mergeTemplate(
165                 className, velocityTemplateContent, velocityContext,
166                 stringWriter);
167 
168             return stringWriter.toString();
169         }
170         catch (Exception e) {
171             _log.error(e, e);
172 
173             throw new RendererException(e);
174         }
175     }
176 
177     public String renderBean(
178             PortletRequest portletRequest, PortletResponse portletResponse,
179             Object bean)
180         throws RendererException {
181 
182         return renderBean(portletRequest, portletResponse, null, bean, null);
183     }
184 
185     public String renderBean(
186             PortletRequest portletRequest, PortletResponse portletResponse,
187             Object bean, String varientSuffix)
188         throws RendererException {
189 
190         return renderBean(
191             portletRequest, portletResponse, null, bean, varientSuffix);
192     }
193 
194     public String renderBean(
195             PortletRequest portletRequest, PortletResponse portletResponse,
196             String servletContextName, Object bean)
197         throws RendererException {
198 
199         return renderBean(
200             portletRequest, portletResponse, servletContextName, bean, null);
201     }
202 
203     public String renderBean(
204             PortletRequest portletRequest, PortletResponse portletResponse,
205             String servletContextName, Object bean, String varientSuffix)
206         throws RendererException {
207 
208         HttpServletRequest request = PortalUtil.getHttpServletRequest(
209             portletRequest);
210         HttpServletResponse response = PortalUtil.getHttpServletResponse(
211             portletResponse);
212 
213         return renderBean(
214             request, response, servletContextName, bean, varientSuffix);
215     }
216 
217     public String renderEntity(
218             HttpServletRequest request, HttpServletResponse response,
219             String className, Object classPK)
220         throws RendererException {
221 
222         return renderEntity(request, response, null, className, classPK, null);
223     }
224 
225     public String renderEntity(
226             HttpServletRequest request, HttpServletResponse response,
227             String className, Object classPK, String varientSuffix)
228         throws RendererException {
229 
230         return renderEntity(
231             request, response, null, className, classPK, varientSuffix);
232     }
233 
234     public String renderEntity(
235             HttpServletRequest request, HttpServletResponse response,
236             String servletContextName, String className, Object classPK)
237         throws RendererException {
238 
239         return renderEntity(
240             request, response, servletContextName, className, classPK, null);
241     }
242 
243     public String renderEntity(
244             HttpServletRequest request, HttpServletResponse response,
245             String servletContextName, String className, Object classPK,
246             String varientSuffix)
247         throws RendererException {
248 
249         if (Validator.isNull(className)) {
250             return null;
251         }
252 
253         className = _normalizeClassName(className);
254 
255         String[] beanNameParts = StringUtil.split(className, _MODEL);
256 
257         Object serviceBean = null;
258 
259         if (Validator.isNotNull(servletContextName)) {
260             if (servletContextName.startsWith(StringPool.SLASH)) {
261                 servletContextName = servletContextName.substring(1);
262             }
263 
264             try {
265                 serviceBean = PortletBeanLocatorUtil.locate(
266                     servletContextName,
267                     beanNameParts[0] + _SERVICE + beanNameParts[1] +
268                         _LOCAL_SERVICE_UTIL);
269             }
270             catch (NoSuchBeanDefinitionException nsbde) {
271             }
272         }
273         else {
274             try {
275                 serviceBean = PortalBeanLocatorUtil.locate(
276                     beanNameParts[0] + _SERVICE + beanNameParts[1] +
277                         _LOCAL_SERVICE_UTIL);
278             }
279             catch (NoSuchBeanDefinitionException nsbde) {
280             }
281         }
282 
283         Object bean = null;
284 
285         if (serviceBean != null) {
286             Method getMethod = null;
287 
288             try {
289                 getMethod = serviceBean.getClass().getDeclaredMethod(
290                     "get" + beanNameParts[1], classPK.getClass());
291             }
292             catch (Exception e) {
293             }
294 
295             if (getMethod == null) {
296                 try {
297                     getMethod = serviceBean.getClass().getDeclaredMethod(
298                         "get" + beanNameParts[1],
299                         _mapToPrimitive(classPK.getClass()));
300                 }
301                 catch (Exception e) {
302                 }
303             }
304 
305             if (getMethod != null) {
306                 try {
307                     bean = getMethod.invoke(null, classPK);
308                 }
309                 catch (Exception e) {
310                     _log.warn(e.getMessage());
311                 }
312             }
313         }
314 
315         return renderBean(
316             request, response, servletContextName, bean, varientSuffix);
317     }
318 
319     public String renderEntity(
320             PortletRequest portletRequest, PortletResponse portletResponse,
321             String className, Object classPK)
322         throws RendererException {
323 
324         return renderEntity(
325             portletRequest, portletResponse, null, className, classPK, null);
326     }
327 
328     public String renderEntity(
329             PortletRequest portletRequest, PortletResponse portletResponse,
330             String className, Object classPK, String varientSuffix)
331         throws RendererException {
332 
333         return renderEntity(
334             portletRequest, portletResponse, null, className, classPK,
335             varientSuffix);
336     }
337 
338     public String renderEntity(
339             PortletRequest portletRequest, PortletResponse portletResponse,
340             String servletContextName, String className, Object classPK)
341         throws RendererException {
342 
343         return renderEntity(
344             portletRequest, portletResponse, servletContextName, className,
345             classPK, null);
346     }
347 
348     public String renderEntity(
349             PortletRequest portletRequest, PortletResponse portletResponse,
350             String servletContextName, String className, Object classPK,
351             String varientSuffix)
352         throws RendererException {
353 
354         HttpServletRequest request = PortalUtil.getHttpServletRequest(
355             portletRequest);
356         HttpServletResponse response = PortalUtil.getHttpServletResponse(
357             portletResponse);
358 
359         return renderEntity(
360             request, response, servletContextName, className, classPK,
361             varientSuffix);
362     }
363 
364     protected PortletPreferences _getPortletPreferences(
365         HttpServletRequest request) {
366 
367         PortletPreferences preferences = PortalUtil.getPreferences(request);
368 
369         String portletResource = ParamUtil.getString(
370             request, "portletResource");
371 
372         if (Validator.isNotNull(portletResource)) {
373             try {
374                 preferences = PortletPreferencesFactoryUtil.getPortletSetup(
375                     request, portletResource);
376             }
377             catch (SystemException se) {
378             }
379         }
380 
381         return preferences;
382     }
383 
384     protected Class<?> _mapToPrimitive(Class<?> clazz) {
385         Class<?> mapping = clazz;
386 
387         if (clazz == Integer.class) {
388             mapping = int.class;
389         }
390         else if (clazz == Long.class) {
391             mapping = long.class;
392         }
393 
394         return mapping;
395     }
396 
397     protected String _normalizeClassName(String className) {
398         className = StringUtil.replace(
399             className,
400             new String[] {
401                 ".impl.",
402                 "Impl"
403             },
404             new String[] {
405                 StringPool.PERIOD,
406                 StringPool.BLANK
407             }
408         );
409 
410         return className;
411     }
412 
413     private static final String _BEAN = "bean";
414 
415     private static final String _LOCAL_SERVICE_UTIL = "LocalServiceUtil";
416 
417     private static final String _MODEL = ".model.";
418 
419     private static final String _SERVICE = ".service.";
420 
421     private static Log _log = LogFactoryUtil.getLog(RendererImpl.class);
422 
423 }