001/* 002 * (C) Copyright 2006-2015 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 * Thierry Delprat 018 * Thierry Martins 019 */ 020package org.nuxeo.ecm.webapp.dnd; 021 022import java.io.IOException; 023import java.io.Serializable; 024import java.util.ArrayList; 025import java.util.Calendar; 026import java.util.Date; 027import java.util.List; 028 029import javax.faces.context.ExternalContext; 030import javax.faces.context.FacesContext; 031import javax.servlet.http.HttpServletResponse; 032 033import org.apache.commons.lang3.StringUtils; 034import org.jboss.seam.ScopeType; 035import org.jboss.seam.annotations.Factory; 036import org.jboss.seam.annotations.Name; 037import org.jboss.seam.annotations.Scope; 038import org.jboss.seam.annotations.web.RequestParameter; 039import org.json.JSONException; 040import org.json.JSONObject; 041import org.nuxeo.ecm.core.api.DocumentModel; 042import org.nuxeo.ecm.core.api.impl.SimpleDocumentModel; 043import org.nuxeo.ecm.core.api.model.Property; 044import org.nuxeo.ecm.core.schema.SchemaManager; 045import org.nuxeo.ecm.core.schema.types.Schema; 046import org.nuxeo.ecm.core.schema.utils.DateParser; 047import org.nuxeo.runtime.api.Framework; 048 049/** 050 * Seam action bean that is used to handle the meta-data form for the Drag&Drop feature 051 * 052 * @author Tiry ([email protected]) 053 */ 054@Name("dndFormActions") 055@Scope(ScopeType.PAGE) 056public class DndFormActionBean implements Serializable { 057 058 private static final long serialVersionUID = 1L; 059 060 protected DocumentModel metadataCollector; 061 062 /** 063 * @since 5.7 064 */ 065 protected List<String> currentSchemas; 066 067 /** 068 * @since 5.7 069 */ 070 protected String currentLayouts; 071 072 /** 073 * @since 5.7 074 */ 075 @RequestParameter 076 protected String schemas; 077 078 /** 079 * @since 5.7 080 */ 081 @RequestParameter 082 protected String layouts; 083 084 public List<String> getSchemas() { 085 currentSchemas = new ArrayList<>(); 086 if (StringUtils.isNotBlank(schemas)) { 087 SchemaManager sc = Framework.getService(SchemaManager.class); 088 for (String schemaName : schemas.split(",")) { 089 Schema schema = sc.getSchemaFromPrefix(schemaName); 090 if (schema != null) { 091 currentSchemas.add(schema.getName()); 092 } else { 093 currentSchemas.add(schemaName); 094 } 095 } 096 } 097 return currentSchemas; 098 } 099 100 public String getLayouts() { 101 if (StringUtils.isNotBlank(layouts)) { 102 currentLayouts = layouts; 103 } 104 return currentLayouts; 105 } 106 107 @Factory(value = "dataCollector", scope = ScopeType.PAGE) 108 public DocumentModel getCollector() { 109 if (metadataCollector == null) { 110 metadataCollector = new SimpleDocumentModel(getSchemas()); 111 } 112 return metadataCollector; 113 } 114 115 public String save() throws JSONException { 116 sendHtmlJSONResponse(); 117 return null; 118 } 119 120 public void sendHtmlJSONResponse() throws JSONException { 121 FacesContext context = FacesContext.getCurrentInstance(); 122 ExternalContext econtext = context.getExternalContext(); 123 HttpServletResponse response = (HttpServletResponse) econtext.getResponse(); 124 125 response.setContentType("text/html"); 126 try { 127 response.getWriter().write(getCollectedData()); 128 response.flushBuffer(); 129 } catch (IOException e) { 130 131 } 132 context.responseComplete(); 133 } 134 135 public String getCollectedData() throws JSONException { 136 StringBuffer sb = new StringBuffer(); 137 sb.append("<html>\n"); 138 sb.append("<script>\n"); 139 sb.append("var collectedData= "); 140 JSONObject jsonObject = new JSONObject(); 141 142 DocumentModel collector = getCollector(); 143 // Collect meta-data 144 JSONObject formData = new JSONObject(); 145 for (String schema : collector.getSchemas()) { 146 for (Property property : collector.getPropertyObjects(schema)) { 147 if (!property.isDirty()) { 148 continue; 149 } 150 Serializable data = property.getValue(); 151 if (data instanceof Date) { 152 data = DateParser.formatW3CDateTime((Date) data); 153 } else if (data instanceof Calendar) { 154 data = DateParser.formatW3CDateTime(((Calendar) data).getTime()); 155 } 156 formData.put(property.getName(), data); 157 } 158 } 159 jsonObject.put("docMetaData", formData); 160 161 // Collect Tags 162 // XXX 163 164 sb.append(jsonObject.toString()); 165 sb.append(";\n"); 166 sb.append("//console.log(collectedData);\n"); 167 sb.append("window.parent.dndFormFunctionCB(collectedData);\n"); 168 sb.append("</script>\n"); 169 sb.append("</html>"); 170 171 return sb.toString(); 172 } 173}