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