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.json.JSONArray;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
021    import com.liferay.portal.kernel.util.ContentTypes;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
024    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureServiceUtil;
025    import com.liferay.portlet.dynamicdatamapping.util.DDMXSDUtil;
026    
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
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 Eduardo Lundgren
037     */
038    public class GetStructureJSONAction 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                            long structureId = ParamUtil.getLong(request, "structureId");
048    
049                            String xsd = ParamUtil.getString(request, "xsd");
050    
051                            DDMStructure structure = null;
052    
053                            if (structureId > 0) {
054                                    structure = DDMStructureServiceUtil.getStructure(structureId);
055                            }
056    
057                            JSONArray jsonArray = DDMXSDUtil.getJSONArray(structure, xsd);
058    
059                            response.setContentType(ContentTypes.APPLICATION_JSON);
060    
061                            ServletResponseUtil.write(response, jsonArray.toString());
062                    }
063                    catch (Exception e) {
064                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
065    
066                            jsonObject.putException(e);
067    
068                            ServletResponseUtil.write(response, jsonObject.toString());
069                    }
070    
071                    return null;
072            }
073    
074    }