001/* 002 * (C) Copyright 2006-2007 Nuxeo SAS (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 * Nuxeo - initial API and implementation 016 * 017 */ 018package org.nuxeo.ecm.webapp.dnd; 019 020import java.io.IOException; 021import java.io.Serializable; 022import java.util.ArrayList; 023import java.util.Arrays; 024import java.util.Calendar; 025import java.util.Date; 026import java.util.HashMap; 027import java.util.List; 028import java.util.Map; 029 030import javax.faces.context.ExternalContext; 031import javax.faces.context.FacesContext; 032import javax.servlet.http.HttpServletResponse; 033 034import org.apache.commons.lang.StringUtils; 035import org.jboss.seam.ScopeType; 036import org.jboss.seam.annotations.Factory; 037import org.jboss.seam.annotations.Name; 038import org.jboss.seam.annotations.Scope; 039import org.jboss.seam.annotations.web.RequestParameter; 040import org.json.JSONException; 041import org.json.JSONObject; 042import org.nuxeo.ecm.core.schema.utils.DateParser; 043 044/** 045 * Seam action bean that is used to handle the meta-data form for the Drag&Drop feature 046 * 047 * @author Tiry (tdelprat@nuxeo.com) 048 */ 049@Name("dndFormActions") 050@Scope(ScopeType.PAGE) 051public class DndFormActionBean implements Serializable { 052 053 private static final long serialVersionUID = 1L; 054 055 protected Map<String, Map<String, Serializable>> metadataCollector; 056 057 /** 058 * @deprecated since 5.7 059 */ 060 @Deprecated 061 protected String currentSchema; 062 063 /** 064 * @deprecated since 5.7 065 */ 066 @Deprecated 067 protected String currentLayout; 068 069 /** 070 * @deprecated since 5.7 071 */ 072 @RequestParameter 073 @Deprecated 074 protected String schema; 075 076 /** 077 * @deprecated since 5.7 078 */ 079 @RequestParameter 080 @Deprecated 081 protected String layout; 082 083 /** 084 * @since 5.7 085 */ 086 protected List<String> currentSchemas; 087 088 /** 089 * @since 5.7 090 */ 091 protected String currentLayouts; 092 093 /** 094 * @since 5.7 095 */ 096 @RequestParameter 097 protected String schemas; 098 099 /** 100 * @since 5.7 101 */ 102 @RequestParameter 103 protected String layouts; 104 105 /** 106 * @deprecated since 5.7. Use {@link #getSchemas()}. 107 */ 108 @Deprecated 109 public String getSchema() { 110 if (schema != null && !schema.isEmpty()) { 111 currentSchema = schema; 112 } 113 return currentSchema; 114 } 115 116 /** 117 * @deprecated since 5.7. Use {@link #getLayouts()}. 118 */ 119 @Deprecated 120 public String getLayout() { 121 if (layout != null && !layout.isEmpty()) { 122 currentLayout = layout; 123 } 124 return currentLayout; 125 } 126 127 public List<String> getSchemas() { 128 currentSchemas = new ArrayList<>(); 129 if (StringUtils.isNotBlank(schemas)) { 130 currentSchemas.addAll(Arrays.asList(schemas.split(","))); 131 } else if (StringUtils.isNotBlank(schema)) { 132 currentSchemas.add(schema); 133 } 134 return currentSchemas; 135 } 136 137 public String getLayouts() { 138 if (StringUtils.isNotBlank(layouts)) { 139 currentLayouts = layouts; 140 } else if (StringUtils.isNotBlank(layout)) { 141 currentLayouts = layout; 142 } 143 return currentLayouts; 144 } 145 146 @Factory(value = "dataCollector", scope = ScopeType.PAGE) 147 public Map<String, Map<String, Serializable>> getCollector() { 148 if (metadataCollector == null) { 149 metadataCollector = new HashMap<String, Map<String, Serializable>>(); 150 for (String schema : getSchemas()) { 151 metadataCollector.put(schema, new HashMap<String, Serializable>()); 152 } 153 } 154 return metadataCollector; 155 } 156 157 public String save() throws JSONException { 158 sendHtmlJSONResponse(); 159 return null; 160 } 161 162 public void sendHtmlJSONResponse() throws JSONException { 163 FacesContext context = FacesContext.getCurrentInstance(); 164 ExternalContext econtext = context.getExternalContext(); 165 HttpServletResponse response = (HttpServletResponse) econtext.getResponse(); 166 167 response.setContentType("text/html"); 168 try { 169 response.getWriter().write(getCollectedData()); 170 response.flushBuffer(); 171 } catch (IOException e) { 172 173 } 174 context.responseComplete(); 175 } 176 177 public String getCollectedData() throws JSONException { 178 StringBuffer sb = new StringBuffer(); 179 sb.append("<html>\n"); 180 sb.append("<script>\n"); 181 sb.append("var collectedData= "); 182 JSONObject jsonObject = new JSONObject(); 183 184 // Collect meta-data 185 JSONObject formData = new JSONObject(); 186 for (String key : metadataCollector.keySet()) { 187 for (String field : metadataCollector.get(key).keySet()) { 188 Object data = metadataCollector.get(key).get(field); 189 if (data instanceof Date) { 190 data = DateParser.formatW3CDateTime((Date) data); 191 } else if (data instanceof Calendar) { 192 data = DateParser.formatW3CDateTime(((Calendar) data).getTime()); 193 } 194 formData.put(key + ":" + field, data); 195 } 196 } 197 jsonObject.put("docMetaData", formData); 198 199 // Collect Tags 200 // XXX 201 202 sb.append(jsonObject.toString()); 203 sb.append(";\n"); 204 sb.append("//console.log(collectedData);\n"); 205 sb.append("window.parent.dndFormFunctionCB(collectedData);\n"); 206 sb.append("</script>\n"); 207 sb.append("</html>"); 208 209 return sb.toString(); 210 } 211}