001/* 002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Thomas Roger <troger@nuxeo.com> 016 */ 017 018package org.nuxeo.ecm.automation.core.operations.services.directory; 019 020import java.io.IOException; 021import java.io.StringWriter; 022import java.util.ArrayList; 023import java.util.List; 024import java.util.Map; 025 026import org.codehaus.jackson.map.ObjectMapper; 027import org.codehaus.jackson.type.TypeReference; 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 041/** 042 * Updates entries for a given {@link org.nuxeo.ecm.directory.Directory}. 043 * <p> 044 * Entries to update are sent as a JSON array. 045 * <p> 046 * Returns the updated entries ids as a JSON array of JSON objects containing all fields 047 * 048 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 049 * @since 5.7 050 */ 051@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) 052public class UpdateDirectoryEntries extends AbstractDirectoryOperation { 053 054 public static final String ID = "Directory.UpdateEntries"; 055 056 @Context 057 protected OperationContext ctx; 058 059 @Context 060 protected DirectoryService directoryService; 061 062 @Param(name = "directoryName", required = true) 063 protected String directoryName; 064 065 @Param(name = "entries", required = true) 066 protected String jsonEntries; 067 068 @OperationMethod 069 public Blob run() throws IOException { 070 validateCanManageDirectories(ctx); 071 072 ObjectMapper mapper = new ObjectMapper(); 073 074 List<Map<String, Object>> entries = mapper.readValue(jsonEntries, 075 new TypeReference<List<Map<String, Object>>>() { 076 }); 077 List<String> ids = new ArrayList<String>(); 078 079 Directory directory = directoryService.getDirectory(directoryName); 080 String idField = directory.getIdField(); 081 082 try (Session session = directoryService.open(directoryName)) { 083 for (Map<String, Object> entry : entries) { 084 if (entry.containsKey(idField)) { 085 DocumentModel doc = session.getEntry((String) entry.get(idField)); 086 if (doc != null) { 087 doc.getDataModel(directory.getSchema()).setMap(entry); 088 session.updateEntry(doc); 089 ids.add(doc.getId()); 090 } 091 } 092 } 093 } 094 095 StringWriter writer = new StringWriter(); 096 mapper.writeValue(writer, ids); 097 return Blobs.createBlob(writer.toString(), "application/json"); 098 } 099}