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.PageContext;
029    
030    import org.apache.struts.action.Action;
031    import org.apache.struts.action.ActionForm;
032    import org.apache.struts.action.ActionForward;
033    import org.apache.struts.action.ActionMapping;
034    
035    /**
036     * @author Bruno Basto
037     */
038    public class RenderStructureFieldAction extends Action {
039    
040            @Override
041            public ActionForward execute(
042                            ActionMapping actionMapping, ActionForm actionForm,
043                            HttpServletRequest request, HttpServletResponse response)
044                    throws Exception {
045    
046                    try {
047                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
048                                    WebKeys.THEME_DISPLAY);
049    
050                            JspFactory jspFactory = JspFactory.getDefaultFactory();
051    
052                            PageContext pageContext = jspFactory.getPageContext(
053                                    getServlet(), request, response, null, true, 0, true);
054    
055                            long classNameId = ParamUtil.getLong(request, "classNameId");
056                            long classPK = ParamUtil.getLong(request, "classPK");
057                            String fieldName = ParamUtil.getString(request, "fieldName");
058                            String namespace = ParamUtil.getString(request, "namespace");
059                            String portletNamespace = ParamUtil.getString(
060                                    request, "portletNamespace");
061                            boolean readOnly = ParamUtil.getBoolean(request, "readOnly");
062    
063                            request.setAttribute("aui:form:portletNamespace", portletNamespace);
064    
065                            String fieldHTML = DDMXSDUtil.getFieldHTMLByName(
066                                    pageContext, classNameId, classPK, fieldName, null,
067                                    portletNamespace, namespace, null, readOnly,
068                                    themeDisplay.getLocale());
069    
070                            response.setContentType(ContentTypes.TEXT_HTML);
071    
072                            ServletResponseUtil.write(response, fieldHTML);
073    
074                            return null;
075                    }
076                    catch (Exception e) {
077                            PortalUtil.sendError(e, request, response);
078    
079                            return null;
080                    }
081            }
082    
083    }