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.nuxeo.ecm.automation.OperationContext;
029import org.nuxeo.ecm.automation.core.Constants;
030import org.nuxeo.ecm.automation.core.annotations.Context;
031import org.nuxeo.ecm.automation.core.annotations.Operation;
032import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
033import org.nuxeo.ecm.automation.core.annotations.Param;
034import org.nuxeo.ecm.core.api.Blob;
035import org.nuxeo.ecm.core.api.Blobs;
036import org.nuxeo.ecm.core.api.DocumentModel;
037import org.nuxeo.ecm.directory.Directory;
038import org.nuxeo.ecm.directory.Session;
039import org.nuxeo.ecm.directory.api.DirectoryService;
040
041import com.fasterxml.jackson.core.type.TypeReference;
042import com.fasterxml.jackson.databind.ObjectMapper;
043
044/**
045 * Updates entries for a given {@link org.nuxeo.ecm.directory.Directory}.
046 * <p>
047 * Entries to update are sent as a JSON array.
048 * <p>
049 * Returns the updated entries ids as a JSON array of JSON objects containing all fields
050 *
051 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
052 * @since 5.7
053 */
054@Operation(id = UpdateDirectoryEntries.ID, category = Constants.CAT_SERVICES, label = "Updates directory entries", description = "Updates directory entries. Entries to update are sent as a JSON array. Returns the updated entries ids as a JSON array of JSON objects containing all fields", addToStudio = false)
055public class UpdateDirectoryEntries extends AbstractDirectoryOperation {
056
057    public static final String ID = "Directory.UpdateEntries";
058
059    @Context
060    protected OperationContext ctx;
061
062    @Context
063    protected DirectoryService directoryService;
064
065    @Param(name = "directoryName", required = true)
066    protected String directoryName;
067
068    @Param(name = "entries", required = true)
069    protected String jsonEntries;
070
071    @OperationMethod
072    public Blob run() throws IOException {
073        validateCanManageDirectories(ctx);
074
075        ObjectMapper mapper = new ObjectMapper();
076
077        List<Map<String, Object>> entries = mapper.readValue(jsonEntries,
078                new TypeReference<List<Map<String, Object>>>() {
079                });
080        List<String> ids = new ArrayList<String>();
081
082        Directory directory = directoryService.getDirectory(directoryName);
083        String idField = directory.getIdField();
084
085        try (Session session = directoryService.open(directoryName)) {
086            for (Map<String, Object> entry : entries) {
087                if (entry.containsKey(idField)) {
088                    DocumentModel doc = session.getEntry((String) entry.get(idField));
089                    if (doc != null) {
090                        doc.setProperties(directory.getSchema(), entry);
091                        session.updateEntry(doc);
092                        ids.add(doc.getId());
093                    }
094                }
095            }
096        }
097
098        StringWriter writer = new StringWriter();
099        mapper.writeValue(writer, ids);
100        return Blobs.createJSONBlob(writer.toString());
101    }
102}