001/*
002 * (C) Copyright 2006-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 *     bstefanescu
018 */
019package org.nuxeo.ecm.platform.picture.operation;
020
021import java.io.IOException;
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.HashMap;
025import java.util.Iterator;
026import java.util.Map;
027import java.util.Map.Entry;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.ecm.automation.core.Constants;
032import org.nuxeo.ecm.automation.core.annotations.Context;
033import org.nuxeo.ecm.automation.core.annotations.Operation;
034import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
035import org.nuxeo.ecm.automation.core.annotations.Param;
036import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector;
037import org.nuxeo.ecm.automation.core.util.ComplexTypeJSONDecoder;
038import org.nuxeo.ecm.automation.core.util.DocumentHelper;
039import org.nuxeo.ecm.automation.core.util.Properties;
040import org.nuxeo.ecm.core.api.Blob;
041import org.nuxeo.ecm.core.api.CoreSession;
042import org.nuxeo.ecm.core.api.DocumentModel;
043import org.nuxeo.ecm.core.api.DocumentRef;
044import org.nuxeo.ecm.platform.picture.api.adapters.PictureResourceAdapter;
045
046import com.fasterxml.jackson.databind.JsonNode;
047import com.fasterxml.jackson.databind.ObjectMapper;
048
049/**
050 * Create a Picture document into the input document
051 *
052 * @author <a href="mailto:tdelprat@nuxeo.com">Thierry Delprat</a>
053 */
054@Operation(id = CreatePicture.ID, category = Constants.CAT_SERVICES, label = "Create Picture", description = "Create a Picture document in the input folder. You can initialize the document properties using the 'properties' parameter. The properties are specified as <i>key=value</i> pairs separated by a new line. The key <i>originalPicture</i> is used to reference the JSON representation of the Blob for the original picture. The <i>pictureTemplates</i> parameter can be used to define the size of the different views to be generated, each line must be a JSONObject { title=\"title\", description=\"description\", maxsize=maxsize}. Returns the created document.")
055public class CreatePicture {
056
057    public static final String ID = "Picture.Create";
058
059    public static final String PICTURE_FIELD = "originalPicture";
060
061    @Context
062    protected CoreSession session;
063
064    @Param(name = "name", required = false)
065    protected String name;
066
067    @Param(name = "properties", required = false)
068    protected Properties content;
069
070    @Param(name = "pictureTemplates", required = false)
071    protected Properties pictureTemplates;
072
073    protected static final Log log = LogFactory.getLog(CreatePicture.class);
074
075    protected ArrayList<Map<String, Object>> computePictureTemplates() {
076        if (pictureTemplates == null || pictureTemplates.size() == 0) {
077            return null;
078        }
079        ArrayList<Map<String, Object>> templates = new ArrayList<>();
080
081        try {
082            ObjectMapper mapper = new ObjectMapper();
083
084            // for (String templateDef : pictureTemplates) {
085            for (String name : pictureTemplates.keySet()) {
086                String templateDef = pictureTemplates.get(name);
087                JsonNode node = mapper.readTree(templateDef);
088                Map<String, Object> map = new HashMap<>();
089                map.put("tag", name);
090                Iterator<Entry<String, JsonNode>> it = node.fields();
091                while (it.hasNext()) {
092                    Entry<String, JsonNode> entry = it.next();
093                    if (entry.getValue().isInt() || entry.getValue().isLong()) {
094                        map.put(entry.getKey(), entry.getValue().longValue());
095                    } else {
096                        map.put(entry.getKey(), entry.getValue().asText());
097                    }
098                }
099                templates.add(map);
100            }
101        } catch (IOException e) {
102            log.error("Error while parsing picture templates", e);
103        }
104
105        return templates;
106    }
107
108    @OperationMethod(collector = DocumentModelCollector.class)
109    public DocumentModel run(DocumentModel doc) throws IOException {
110        if (name == null) {
111            name = "Untitled";
112        }
113        String jsonBlob = content.get(PICTURE_FIELD);
114        content.remove(PICTURE_FIELD);
115
116        ArrayList<Map<String, Object>> templates = computePictureTemplates();
117
118        DocumentModel newDoc = session.createDocumentModel(doc.getPathAsString(), name, "Picture");
119        DocumentHelper.setProperties(session, newDoc, content);
120        DocumentModel picture = session.createDocument(newDoc);
121
122        if (jsonBlob == null) {
123            log.warn("Properties does not contains originalPicture field");
124        } else {
125            Blob blob = (Blob) ComplexTypeJSONDecoder.decode(null, jsonBlob);
126            if (blob == null) {
127                log.warn("Unable to read Blob from properties");
128            } else {
129                picture.setPropertyValue("file:content", (Serializable) blob);
130                PictureResourceAdapter adapter = picture.getAdapter(PictureResourceAdapter.class);
131                adapter.fillPictureViews(blob, blob.getFilename(), picture.getTitle(), templates);
132                picture = session.saveDocument(picture);
133            }
134        }
135        return picture;
136    }
137
138    @OperationMethod(collector = DocumentModelCollector.class)
139    public DocumentModel run(DocumentRef doc) throws IOException {
140        return run(session.getDocument(doc));
141    }
142
143}