001/* 002 * Copyright (c) 2006-2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Thierry Delprat 011 * Thierry Martins 012 */ 013package org.nuxeo.ecm.webapp.dnd; 014 015import java.io.IOException; 016import java.io.Serializable; 017import java.util.ArrayList; 018import java.util.Calendar; 019import java.util.Date; 020import java.util.List; 021 022import javax.faces.context.ExternalContext; 023import javax.faces.context.FacesContext; 024import javax.servlet.http.HttpServletResponse; 025 026import org.apache.commons.lang.StringUtils; 027import org.jboss.seam.ScopeType; 028import org.jboss.seam.annotations.Factory; 029import org.jboss.seam.annotations.Name; 030import org.jboss.seam.annotations.Scope; 031import org.jboss.seam.annotations.web.RequestParameter; 032import org.json.JSONException; 033import org.json.JSONObject; 034import org.nuxeo.ecm.core.api.DataModel; 035import org.nuxeo.ecm.core.api.DocumentModel; 036import org.nuxeo.ecm.core.api.impl.SimpleDocumentModel; 037import org.nuxeo.ecm.core.schema.SchemaManager; 038import org.nuxeo.ecm.core.schema.types.Schema; 039import org.nuxeo.ecm.core.schema.utils.DateParser; 040import org.nuxeo.runtime.api.Framework; 041 042/** 043 * Seam action bean that is used to handle the meta-data form for the Drag&Drop feature 044 * 045 * @author Tiry (tdelprat@nuxeo.com) 046 */ 047@Name("dndFormActions") 048@Scope(ScopeType.PAGE) 049public class DndFormActionBean implements Serializable { 050 051 private static final long serialVersionUID = 1L; 052 053 protected DocumentModel metadataCollector; 054 055 /** 056 * @since 5.7 057 */ 058 protected List<String> currentSchemas; 059 060 /** 061 * @since 5.7 062 */ 063 protected String currentLayouts; 064 065 /** 066 * @since 5.7 067 */ 068 @RequestParameter 069 protected String schemas; 070 071 /** 072 * @since 5.7 073 */ 074 @RequestParameter 075 protected String layouts; 076 077 public List<String> getSchemas() { 078 currentSchemas = new ArrayList<>(); 079 if (StringUtils.isNotBlank(schemas)) { 080 SchemaManager sc = Framework.getLocalService(SchemaManager.class); 081 for (String schemaName : schemas.split(",")) { 082 Schema schema = sc.getSchemaFromPrefix(schemaName); 083 if (schema != null) { 084 currentSchemas.add(schema.getName()); 085 } else { 086 currentSchemas.add(schemaName); 087 } 088 } 089 } 090 return currentSchemas; 091 } 092 093 public String getLayouts() { 094 if (StringUtils.isNotBlank(layouts)) { 095 currentLayouts = layouts; 096 } 097 return currentLayouts; 098 } 099 100 @Factory(value = "dataCollector", scope = ScopeType.PAGE) 101 public DocumentModel getCollector() { 102 if (metadataCollector == null) { 103 metadataCollector = new SimpleDocumentModel(getSchemas()); 104 } 105 return metadataCollector; 106 } 107 108 public String save() throws JSONException { 109 sendHtmlJSONResponse(); 110 return null; 111 } 112 113 public void sendHtmlJSONResponse() throws JSONException { 114 FacesContext context = FacesContext.getCurrentInstance(); 115 ExternalContext econtext = context.getExternalContext(); 116 HttpServletResponse response = (HttpServletResponse) econtext.getResponse(); 117 118 response.setContentType("text/html"); 119 try { 120 response.getWriter().write(getCollectedData()); 121 response.flushBuffer(); 122 } catch (IOException e) { 123 124 } 125 context.responseComplete(); 126 } 127 128 public String getCollectedData() throws JSONException { 129 StringBuffer sb = new StringBuffer(); 130 sb.append("<html>\n"); 131 sb.append("<script>\n"); 132 sb.append("var collectedData= "); 133 JSONObject jsonObject = new JSONObject(); 134 135 DocumentModel collector = getCollector(); 136 // Collect meta-data 137 JSONObject formData = new JSONObject(); 138 for (String key : collector.getSchemas()) { 139 DataModel dataModel = collector.getDataModel(key); 140 for (String field : dataModel.getDirtyFields()) { 141 Object data = dataModel.getData(field); 142 if (data instanceof Date) { 143 data = DateParser.formatW3CDateTime((Date) data); 144 } else if (data instanceof Calendar) { 145 data = DateParser.formatW3CDateTime(((Calendar) data).getTime()); 146 } 147 formData.put(field, data); 148 } 149 } 150 jsonObject.put("docMetaData", formData); 151 152 // Collect Tags 153 // XXX 154 155 sb.append(jsonObject.toString()); 156 sb.append(";\n"); 157 sb.append("//console.log(collectedData);\n"); 158 sb.append("window.parent.dndFormFunctionCB(collectedData);\n"); 159 sb.append("</script>\n"); 160 sb.append("</html>"); 161 162 return sb.toString(); 163 } 164}