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