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