001/*
002 * Copyright (c) 2006-2011 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 Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     qlamerand
011 */
012package org.nuxeo.ecm.automation.core.operations.services;
013
014import java.io.Serializable;
015import java.util.Locale;
016
017import net.sf.json.JSONArray;
018import net.sf.json.JSONObject;
019
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022import org.nuxeo.common.utils.i18n.I18NUtils;
023import org.nuxeo.ecm.automation.OperationContext;
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Context;
026import org.nuxeo.ecm.automation.core.annotations.Operation;
027import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
028import org.nuxeo.ecm.automation.core.annotations.Param;
029import org.nuxeo.ecm.core.api.Blob;
030import org.nuxeo.ecm.core.api.Blobs;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.DocumentModelList;
033import org.nuxeo.ecm.core.schema.SchemaManager;
034import org.nuxeo.ecm.core.schema.types.Field;
035import org.nuxeo.ecm.core.schema.types.QName;
036import org.nuxeo.ecm.core.schema.types.Schema;
037import org.nuxeo.ecm.directory.Directory;
038import org.nuxeo.ecm.directory.Session;
039import org.nuxeo.ecm.directory.api.DirectoryService;
040
041/**
042 * Return the content of a {@link Directory} as a JSON StringBlob
043 *
044 * @author <a href="mailto:qlamerand@nuxeo.com">Quentin Lamerand</a>
045 */
046@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)
047public class GetDirectoryEntries {
048
049    private static final Log log = LogFactory.getLog(GetDirectoryEntries.class);
050
051    public static final String ID = "Directory.Entries";
052
053    @Context
054    protected OperationContext ctx;
055
056    @Context
057    protected DirectoryService directoryService;
058
059    @Context
060    protected SchemaManager schemaManager;
061
062    @Param(name = "directoryName", required = true)
063    protected String directoryName;
064
065    @Param(name = "translateLabels", required = false)
066    protected boolean translateLabels;
067
068    @Param(name = "lang", required = false)
069    protected String lang;
070
071    @OperationMethod
072    public Blob run(DocumentModel doc) {
073        Directory directory = directoryService.getDirectory(directoryName, doc);
074        try (Session session = directory.getSession()) {
075            DocumentModelList entries = session.getEntries();
076            String schemaName = directory.getSchema();
077            Schema schema = schemaManager.getSchema(schemaName);
078            JSONArray rows = new JSONArray();
079            for (DocumentModel entry : entries) {
080                JSONObject obj = new JSONObject();
081                for (Field field : schema.getFields()) {
082                    QName fieldName = field.getName();
083                    String key = fieldName.getLocalName();
084                    Serializable value = entry.getPropertyValue(fieldName.getPrefixedName());
085                    if (translateLabels && "label".equals(key)) {
086                        value = translate((String) value);
087                    }
088                    obj.element(key, value);
089                }
090                rows.add(obj);
091            }
092            return Blobs.createBlob(rows.toString(), "application/json");
093        }
094    }
095
096    @OperationMethod
097    public Blob run() {
098        return run(null);
099    }
100
101    protected Locale getLocale() {
102        if (lang == null) {
103            lang = (String) ctx.get("lang");
104        }
105        if (lang == null) {
106            lang = "en";
107        }
108        return new Locale(lang);
109    }
110
111    protected String translate(String key) {
112        if (key == null) {
113            return "";
114        }
115        return I18NUtils.getMessageString("messages", key, new Object[0], getLocale());
116    }
117
118}