001/*
002 * (C) Copyright 2006-2012 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 *     Thomas Roger <troger@nuxeo.com>
018 */
019
020package org.nuxeo.ecm.automation.core.operations.services.directory;
021
022import java.io.IOException;
023import java.io.StringWriter;
024import java.util.ArrayList;
025import java.util.List;
026import java.util.Map;
027
028import org.codehaus.jackson.map.ObjectMapper;
029import org.codehaus.jackson.type.TypeReference;
030import org.nuxeo.ecm.automation.OperationContext;
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.core.api.Blob;
037import org.nuxeo.ecm.core.api.Blobs;
038import org.nuxeo.ecm.core.api.DocumentModel;
039import org.nuxeo.ecm.directory.Session;
040import org.nuxeo.ecm.directory.api.DirectoryService;
041
042/**
043 * Creates entries for a given {@link org.nuxeo.ecm.directory.Directory}.
044 * <p>
045 * Entries to create are sent as a JSON array.
046 * <p>
047 * Returns the created entries ids as a JSON array.
048 *
049 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
050 * @since 5.7
051 */
052@Operation(id = CreateDirectoryEntries.ID, category = Constants.CAT_SERVICES, label = "Creates directory entries", description = "Creates directory entries. Entries are sent as a JSON array. Returning the created entries ids as a JSON array.", addToStudio = false)
053public class CreateDirectoryEntries extends AbstractDirectoryOperation {
054
055    public static final String ID = "Directory.CreateEntries";
056
057    @Context
058    protected OperationContext ctx;
059
060    @Context
061    protected DirectoryService directoryService;
062
063    @Param(name = "directoryName", required = true)
064    protected String directoryName;
065
066    @Param(name = "entries", required = true)
067    protected String jsonEntries;
068
069    @OperationMethod
070    public Blob run() throws IOException {
071        validateCanManageDirectories(ctx);
072
073        ObjectMapper mapper = new ObjectMapper();
074
075        List<Map<String, Object>> entries = mapper.readValue(jsonEntries,
076                new TypeReference<List<Map<String, Object>>>() {
077                });
078        List<String> ids = new ArrayList<String>();
079        try (Session session = directoryService.open(directoryName)) {
080            for (Map<String, Object> entry : entries) {
081                DocumentModel doc = session.createEntry(entry);
082                ids.add(doc.getId());
083            }
084        }
085
086        StringWriter writer = new StringWriter();
087        mapper.writeValue(writer, ids);
088        return Blobs.createBlob(writer.toString(), "application/json");
089    }
090
091}