001/* 002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Anahide Tchertchian 018 */ 019package org.nuxeo.ecm.platform.forms.layout.demo.jsf; 020 021import java.io.UnsupportedEncodingException; 022import java.util.HashMap; 023import java.util.Map; 024 025import net.sf.json.JSONObject; 026 027import org.apache.commons.lang.StringUtils; 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030import org.jboss.seam.annotations.Name; 031import org.jboss.seam.annotations.Scope; 032import org.nuxeo.common.utils.URIUtils; 033import org.nuxeo.ecm.platform.forms.layout.api.LayoutDefinition; 034import org.nuxeo.ecm.platform.forms.layout.api.WidgetDefinition; 035import org.nuxeo.ecm.platform.forms.layout.api.impl.LayoutDefinitionImpl; 036import org.nuxeo.ecm.platform.forms.layout.api.impl.WidgetDefinitionImpl; 037import org.nuxeo.ecm.platform.forms.layout.demo.service.LayoutDemoManager; 038import org.nuxeo.ecm.platform.forms.layout.io.JSONLayoutExporter; 039 040import static org.jboss.seam.ScopeType.EVENT; 041 042/** 043 * Seam component handling preview. 044 * 045 * @since 5.4.2 046 */ 047@Name("layoutPreviewActions") 048@Scope(EVENT) 049public class LayoutPreviewActions { 050 051 private static final Log log = LogFactory.getLog(LayoutPreviewActions.class); 052 053 // XXX: use hard coded JSF category for now 054 public static final String LAYOUT_CATEGORY = "jsf"; 055 056 public String getPreviewLayoutURL(PreviewLayoutDefinition previewLayoutDef, String layoutMode, String layoutTemplate) 057 throws UnsupportedEncodingException { 058 Map<String, String> parameters = new HashMap<String, String>(); 059 parameters.put("layoutDefinition", getEncodedLayoutDefinition(previewLayoutDef)); 060 parameters.put("layoutMode", layoutMode); 061 parameters.put("layoutTemplate", layoutTemplate); 062 return URIUtils.addParametersToURIQuery(LayoutDemoManager.PREVIEW_PATH 063 + LayoutDemoURLCodec.LAYOUT_PREVIEW_FRAME_VIEW_ID, parameters); 064 } 065 066 public LayoutDefinition getLayoutDefinition(PreviewLayoutDefinition previewLayoutDef) { 067 if (previewLayoutDef == null) { 068 return null; 069 } 070 WidgetDefinition widgetDef = new WidgetDefinitionImpl("preview_widget", previewLayoutDef.getWidgetType(), 071 previewLayoutDef.getLabel(), previewLayoutDef.getHelpLabel(), 072 Boolean.TRUE.equals(previewLayoutDef.getTranslated()), null, previewLayoutDef.getFieldDefinitions(), 073 previewLayoutDef.getWidgetProperties(), previewLayoutDef.getSubWidgets()); 074 widgetDef.setHandlingLabels(Boolean.TRUE.equals(previewLayoutDef.getHandlingLabels())); 075 return new LayoutDefinitionImpl("preview_layout", null, widgetDef); 076 } 077 078 public String getEncodedLayoutDefinition(PreviewLayoutDefinition previewLayoutDef) 079 throws UnsupportedEncodingException { 080 LayoutDefinition def = getLayoutDefinition(previewLayoutDef); 081 return getEncodedLayoutDefinition(def); 082 } 083 084 public String getEncodedLayoutDefinition(LayoutDefinition def) throws UnsupportedEncodingException { 085 JSONObject json = JSONLayoutExporter.exportToJson(LAYOUT_CATEGORY, def); 086 if (log.isDebugEnabled()) { 087 log.debug("Encoded layout definition: " + json.toString()); 088 } 089 return JSONLayoutExporter.encode(json); 090 } 091 092 public LayoutDefinition getDecodedLayoutDefinition(String jsonEncodedLayoutDef) throws UnsupportedEncodingException { 093 if (StringUtils.isBlank(jsonEncodedLayoutDef)) { 094 return null; 095 } 096 JSONObject json = JSONLayoutExporter.decode(jsonEncodedLayoutDef); 097 if (log.isDebugEnabled()) { 098 log.debug("Decoded layout definition: " + json.toString()); 099 } 100 return JSONLayoutExporter.importLayoutDefinition(json); 101 } 102 103}