001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.dynamicdatamapping.action;
016    
017    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
018    import com.liferay.portal.kernel.util.ContentTypes;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.theme.ThemeDisplay;
021    import com.liferay.portal.util.PortalUtil;
022    import com.liferay.portal.util.WebKeys;
023    import com.liferay.portlet.dynamicdatamapping.util.DDMXSDUtil;
024    
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpServletResponse;
027    import javax.servlet.jsp.JspFactory;
028    import javax.servlet.jsp.JspWriter;
029    import javax.servlet.jsp.PageContext;
030    
031    import org.apache.struts.action.Action;
032    import org.apache.struts.action.ActionForm;
033    import org.apache.struts.action.ActionForward;
034    import org.apache.struts.action.ActionMapping;
035    
036    /**
037     * @author Bruno Basto
038     */
039    public class RenderStructureFieldAction extends Action {
040    
041            @Override
042            public ActionForward execute(
043                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
044                            HttpServletResponse response)
045                    throws Exception {
046    
047                    try {
048                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
049                                    WebKeys.THEME_DISPLAY);
050    
051                            JspFactory jspFactory = JspFactory.getDefaultFactory();
052    
053                            PageContext pageContext = jspFactory.getPageContext(
054                                    getServlet(), request, response, null, true,
055                                    JspWriter.DEFAULT_BUFFER, true);
056    
057                            long classNameId = ParamUtil.getLong(request, "classNameId");
058                            long classPK = ParamUtil.getLong(request, "classPK");
059                            String fieldName = ParamUtil.getString(request, "fieldName");
060                            String namespace = ParamUtil.getString(request, "namespace");
061                            String portletNamespace = ParamUtil.getString(
062                                    request, "portletNamespace");
063                            boolean readOnly = ParamUtil.getBoolean(request, "readOnly");
064    
065                            request.setAttribute("aui:form:portletNamespace", portletNamespace);
066    
067                            String fieldHTML = DDMXSDUtil.getFieldHTMLByName(
068                                    pageContext, classNameId, classPK, fieldName, null,
069                                    portletNamespace, namespace, null, readOnly,
070                                    themeDisplay.getLocale());
071    
072                            response.setContentType(ContentTypes.TEXT_HTML);
073    
074                            ServletResponseUtil.write(response, fieldHTML);
075    
076                            return null;
077                    }
078                    catch (Exception e) {
079                            PortalUtil.sendError(e, request, response);
080    
081                            return null;
082                    }
083            }
084    
085    }