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.Serializable; 022import java.io.StringWriter; 023import java.util.ArrayList; 024import java.util.HashMap; 025import java.util.List; 026import java.util.Locale; 027import java.util.Map; 028 029import org.codehaus.jackson.map.ObjectMapper; 030import org.codehaus.jackson.type.TypeReference; 031import org.nuxeo.common.utils.i18n.I18NUtils; 032import org.nuxeo.ecm.automation.OperationContext; 033import org.nuxeo.ecm.automation.core.Constants; 034import org.nuxeo.ecm.automation.core.annotations.Context; 035import org.nuxeo.ecm.automation.core.annotations.Operation; 036import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 037import org.nuxeo.ecm.automation.core.annotations.Param; 038import org.nuxeo.ecm.core.api.Blob; 039import org.nuxeo.ecm.core.api.Blobs; 040import org.nuxeo.ecm.core.api.DocumentModel; 041import org.nuxeo.ecm.core.schema.SchemaManager; 042import org.nuxeo.ecm.core.schema.types.Field; 043import org.nuxeo.ecm.core.schema.types.QName; 044import org.nuxeo.ecm.core.schema.types.Schema; 045import org.nuxeo.ecm.directory.Directory; 046import org.nuxeo.ecm.directory.Session; 047import org.nuxeo.ecm.directory.api.DirectoryService; 048 049/** 050 * Reads entries for a given {@link org.nuxeo.ecm.directory.Directory}. 051 * <p> 052 * Entries ids to read are sent as a JSON array. 053 * <p> 054 * Returns the entries as a JSON array of JSON objects containing all fields. 055 * 056 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 057 * @since 5.7 058 */ 059@Operation(id = ReadDirectoryEntries.ID, category = Constants.CAT_SERVICES, label = "Reads directory entries", description = "Reads directory entries. Entries ids to read are sent as a JSON array. Returns the entries as a JSON array of JSON objects containing all fields.", addToStudio = false) 060public class ReadDirectoryEntries extends AbstractDirectoryOperation { 061 062 public static final String ID = "Directory.ReadEntries"; 063 064 @Context 065 protected OperationContext ctx; 066 067 @Context 068 protected DirectoryService directoryService; 069 070 @Context 071 protected SchemaManager schemaManager; 072 073 @Param(name = "directoryName", required = true) 074 protected String directoryName; 075 076 @Param(name = "entries", required = true) 077 protected String jsonEntries; 078 079 @Param(name = "translateLabels", required = false) 080 protected boolean translateLabels; 081 082 @Param(name = "lang", required = false) 083 protected String lang; 084 085 @OperationMethod 086 public Blob run() throws IOException { 087 ObjectMapper mapper = new ObjectMapper(); 088 089 List<String> ids = mapper.readValue(jsonEntries, new TypeReference<List<String>>() { 090 }); 091 List<Map<String, Object>> entries = new ArrayList<Map<String, Object>>(); 092 093 Directory directory = directoryService.getDirectory(directoryName); 094 String schemaName = directory.getSchema(); 095 Schema schema = schemaManager.getSchema(schemaName); 096 try (Session session = directoryService.open(directoryName)) { 097 for (String id : ids) { 098 DocumentModel entry = session.getEntry(id); 099 if (entry != null) { 100 Map<String, Object> m = new HashMap<String, Object>(); 101 for (Field field : schema.getFields()) { 102 QName fieldName = field.getName(); 103 String key = fieldName.getLocalName(); 104 Serializable value = entry.getPropertyValue(fieldName.getPrefixedName()); 105 if (translateLabels && "label".equals(key)) { 106 value = translate((String) value); 107 } 108 m.put(key, value); 109 } 110 entries.add(m); 111 } 112 } 113 } 114 115 StringWriter writer = new StringWriter(); 116 mapper.writeValue(writer, entries); 117 return Blobs.createBlob(writer.toString(), "application/json"); 118 } 119 120 protected Locale getLocale() { 121 if (lang == null) { 122 lang = (String) ctx.get("lang"); 123 } 124 if (lang == null) { 125 lang = "en"; 126 } 127 return new Locale(lang); 128 } 129 130 protected String translate(String key) { 131 if (key == null) { 132 return ""; 133 } 134 return I18NUtils.getMessageString("messages", key, new Object[0], getLocale()); 135 } 136 137}