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.Session; 038import org.nuxeo.ecm.directory.api.DirectoryService; 039 040/** 041 * Creates entries for a given {@link org.nuxeo.ecm.directory.Directory}. 042 * <p> 043 * Entries to create are sent as a JSON array. 044 * <p> 045 * Returns the created entries ids as a JSON array. 046 * 047 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 048 * @since 5.7 049 */ 050@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) 051public class CreateDirectoryEntries extends AbstractDirectoryOperation { 052 053 public static final String ID = "Directory.CreateEntries"; 054 055 @Context 056 protected OperationContext ctx; 057 058 @Context 059 protected DirectoryService directoryService; 060 061 @Param(name = "directoryName", required = true) 062 protected String directoryName; 063 064 @Param(name = "entries", required = true) 065 protected String jsonEntries; 066 067 @OperationMethod 068 public Blob run() throws IOException { 069 validateCanManageDirectories(ctx); 070 071 ObjectMapper mapper = new ObjectMapper(); 072 073 List<Map<String, Object>> entries = mapper.readValue(jsonEntries, 074 new TypeReference<List<Map<String, Object>>>() { 075 }); 076 List<String> ids = new ArrayList<String>(); 077 try (Session session = directoryService.open(directoryName)) { 078 for (Map<String, Object> entry : entries) { 079 DocumentModel doc = session.createEntry(entry); 080 ids.add(doc.getId()); 081 } 082 } 083 084 StringWriter writer = new StringWriter(); 085 mapper.writeValue(writer, ids); 086 return Blobs.createBlob(writer.toString(), "application/json"); 087 } 088 089}