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.lang.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.DataModel;
042import org.nuxeo.ecm.core.api.DocumentModel;
043import org.nuxeo.ecm.core.api.impl.SimpleDocumentModel;
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 (tdelprat@nuxeo.com)
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.getLocalService(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 key : collector.getSchemas()) {
146            DataModel dataModel = collector.getDataModel(key);
147            for (String field : dataModel.getDirtyFields()) {
148                Object data = dataModel.getData(field);
149                if (data instanceof Date) {
150                    data = DateParser.formatW3CDateTime((Date) data);
151                } else if (data instanceof Calendar) {
152                    data = DateParser.formatW3CDateTime(((Calendar) data).getTime());
153                }
154                formData.put(field, data);
155            }
156        }
157        jsonObject.put("docMetaData", formData);
158
159        // Collect Tags
160        // XXX
161
162        sb.append(jsonObject.toString());
163        sb.append(";\n");
164        sb.append("//console.log(collectedData);\n");
165        sb.append("window.parent.dndFormFunctionCB(collectedData);\n");
166        sb.append("</script>\n");
167        sb.append("</html>");
168
169        return sb.toString();
170    }
171}