001/* 002 * (C) Copyright 2006-2011 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 * qlamerand 018 */ 019package org.nuxeo.ecm.automation.core.operations.services; 020 021import java.io.Serializable; 022import java.util.Locale; 023 024import net.sf.json.JSONArray; 025import net.sf.json.JSONObject; 026 027import org.apache.commons.logging.Log; 028import org.apache.commons.logging.LogFactory; 029import org.nuxeo.common.utils.i18n.I18NUtils; 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.core.api.DocumentModelList; 040import org.nuxeo.ecm.core.schema.SchemaManager; 041import org.nuxeo.ecm.core.schema.types.Field; 042import org.nuxeo.ecm.core.schema.types.QName; 043import org.nuxeo.ecm.core.schema.types.Schema; 044import org.nuxeo.ecm.directory.Directory; 045import org.nuxeo.ecm.directory.Session; 046import org.nuxeo.ecm.directory.api.DirectoryService; 047 048/** 049 * Return the content of a {@link Directory} as a JSON StringBlob 050 * 051 * @author <a href="mailto:qlamerand@nuxeo.com">Quentin Lamerand</a> 052 */ 053@Operation(id = GetDirectoryEntries.ID, category = Constants.CAT_SERVICES, label = "Get directory entries", description = "Get the entries of a directory. This is returning a blob containing a serialized JSON array. The input document, if specified, is used as a context for a potential local configuration of the directory.", addToStudio = false) 054public class GetDirectoryEntries { 055 056 private static final Log log = LogFactory.getLog(GetDirectoryEntries.class); 057 058 public static final String ID = "Directory.Entries"; 059 060 @Context 061 protected OperationContext ctx; 062 063 @Context 064 protected DirectoryService directoryService; 065 066 @Context 067 protected SchemaManager schemaManager; 068 069 @Param(name = "directoryName", required = true) 070 protected String directoryName; 071 072 @Param(name = "translateLabels", required = false) 073 protected boolean translateLabels; 074 075 @Param(name = "lang", required = false) 076 protected String lang; 077 078 @OperationMethod 079 public Blob run(DocumentModel doc) { 080 Directory directory = directoryService.getDirectory(directoryName, doc); 081 try (Session session = directory.getSession()) { 082 DocumentModelList entries = session.getEntries(); 083 String schemaName = directory.getSchema(); 084 Schema schema = schemaManager.getSchema(schemaName); 085 JSONArray rows = new JSONArray(); 086 for (DocumentModel entry : entries) { 087 JSONObject obj = new JSONObject(); 088 for (Field field : schema.getFields()) { 089 QName fieldName = field.getName(); 090 String key = fieldName.getLocalName(); 091 Serializable value = entry.getPropertyValue(fieldName.getPrefixedName()); 092 if (translateLabels && "label".equals(key)) { 093 value = translate((String) value); 094 } 095 obj.element(key, value); 096 } 097 rows.add(obj); 098 } 099 return Blobs.createBlob(rows.toString(), "application/json"); 100 } 101 } 102 103 @OperationMethod 104 public Blob run() { 105 return run(null); 106 } 107 108 protected Locale getLocale() { 109 if (lang == null) { 110 lang = (String) ctx.get("lang"); 111 } 112 if (lang == null) { 113 lang = "en"; 114 } 115 return new Locale(lang); 116 } 117 118 protected String translate(String key) { 119 if (key == null) { 120 return ""; 121 } 122 return I18NUtils.getMessageString("messages", key, new Object[0], getLocale()); 123 } 124 125}