001    /**
002     * Copyright (c) 2000-2012 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.json.JSONArray;
018    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
019    import com.liferay.portal.kernel.util.ContentTypes;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.util.PortalUtil;
022    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
023    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureServiceUtil;
024    import com.liferay.portlet.dynamicdatamapping.util.DDMXSDUtil;
025    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpServletResponse;
028    
029    import org.apache.struts.action.Action;
030    import org.apache.struts.action.ActionForm;
031    import org.apache.struts.action.ActionForward;
032    import org.apache.struts.action.ActionMapping;
033    
034    /**
035     * @author Eduardo Lundgren
036     */
037    public class GetStructureJSONAction extends Action {
038    
039            @Override
040            public ActionForward execute(
041                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
042                            HttpServletResponse response)
043                    throws Exception {
044    
045                    try {
046                            long structureId = ParamUtil.getLong(request, "structureId");
047    
048                            String xsd = ParamUtil.getString(request, "xsd");
049    
050                            DDMStructure structure = DDMStructureServiceUtil.getStructure(
051                                    structureId);
052    
053                            JSONArray jsonArray = DDMXSDUtil.getJSONArray(structure, xsd);
054    
055                            response.setContentType(ContentTypes.TEXT_JAVASCRIPT);
056    
057                            ServletResponseUtil.write(response, jsonArray.toString());
058                    }
059                    catch (Exception e) {
060                            PortalUtil.sendError(e, request, response);
061                    }
062    
063                    return null;
064            }
065    
066    }