001/* 002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Anahide Tchertchian 016 */ 017package org.nuxeo.ecm.platform.forms.layout.demo.jsf; 018 019import java.io.UnsupportedEncodingException; 020import java.util.HashMap; 021import java.util.Map; 022 023import net.sf.json.JSONObject; 024 025import org.apache.commons.lang.StringUtils; 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.jboss.seam.annotations.Name; 029import org.jboss.seam.annotations.Scope; 030import org.nuxeo.common.utils.URIUtils; 031import org.nuxeo.ecm.platform.forms.layout.api.LayoutDefinition; 032import org.nuxeo.ecm.platform.forms.layout.api.WidgetDefinition; 033import org.nuxeo.ecm.platform.forms.layout.api.impl.LayoutDefinitionImpl; 034import org.nuxeo.ecm.platform.forms.layout.api.impl.WidgetDefinitionImpl; 035import org.nuxeo.ecm.platform.forms.layout.demo.service.LayoutDemoManager; 036import org.nuxeo.ecm.platform.forms.layout.io.JSONLayoutExporter; 037 038import static org.jboss.seam.ScopeType.EVENT; 039 040/** 041 * Seam component handling preview. 042 * 043 * @since 5.4.2 044 */ 045@Name("layoutPreviewActions") 046@Scope(EVENT) 047public class LayoutPreviewActions { 048 049 private static final Log log = LogFactory.getLog(LayoutPreviewActions.class); 050 051 // XXX: use hard coded JSF category for now 052 public static final String LAYOUT_CATEGORY = "jsf"; 053 054 public String getPreviewLayoutURL(PreviewLayoutDefinition previewLayoutDef, String layoutMode, String layoutTemplate) 055 throws UnsupportedEncodingException { 056 Map<String, String> parameters = new HashMap<String, String>(); 057 parameters.put("layoutDefinition", getEncodedLayoutDefinition(previewLayoutDef)); 058 parameters.put("layoutMode", layoutMode); 059 parameters.put("layoutTemplate", layoutTemplate); 060 return URIUtils.addParametersToURIQuery(LayoutDemoManager.PREVIEW_PATH 061 + LayoutDemoURLCodec.LAYOUT_PREVIEW_FRAME_VIEW_ID, parameters); 062 } 063 064 public LayoutDefinition getLayoutDefinition(PreviewLayoutDefinition previewLayoutDef) { 065 if (previewLayoutDef == null) { 066 return null; 067 } 068 WidgetDefinition widgetDef = new WidgetDefinitionImpl("preview_widget", previewLayoutDef.getWidgetType(), 069 previewLayoutDef.getLabel(), previewLayoutDef.getHelpLabel(), 070 Boolean.TRUE.equals(previewLayoutDef.getTranslated()), null, previewLayoutDef.getFieldDefinitions(), 071 previewLayoutDef.getWidgetProperties(), previewLayoutDef.getSubWidgets()); 072 widgetDef.setHandlingLabels(Boolean.TRUE.equals(previewLayoutDef.getHandlingLabels())); 073 return new LayoutDefinitionImpl("preview_layout", null, widgetDef); 074 } 075 076 public String getEncodedLayoutDefinition(PreviewLayoutDefinition previewLayoutDef) 077 throws UnsupportedEncodingException { 078 LayoutDefinition def = getLayoutDefinition(previewLayoutDef); 079 return getEncodedLayoutDefinition(def); 080 } 081 082 public String getEncodedLayoutDefinition(LayoutDefinition def) throws UnsupportedEncodingException { 083 JSONObject json = JSONLayoutExporter.exportToJson(LAYOUT_CATEGORY, def); 084 if (log.isDebugEnabled()) { 085 log.debug("Encoded layout definition: " + json.toString()); 086 } 087 return JSONLayoutExporter.encode(json); 088 } 089 090 public LayoutDefinition getDecodedLayoutDefinition(String jsonEncodedLayoutDef) throws UnsupportedEncodingException { 091 if (StringUtils.isBlank(jsonEncodedLayoutDef)) { 092 return null; 093 } 094 JSONObject json = JSONLayoutExporter.decode(jsonEncodedLayoutDef); 095 if (log.isDebugEnabled()) { 096 log.debug("Decoded layout definition: " + json.toString()); 097 } 098 return JSONLayoutExporter.importLayoutDefinition(json); 099 } 100 101}