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.IOException;
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.LinkedHashMap;
025import java.util.List;
026import java.util.Locale;
027import java.util.Map;
028
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    public static final String ID = "Directory.Entries";
057
058    @Context
059    protected OperationContext ctx;
060
061    @Context
062    protected DirectoryService directoryService;
063
064    @Context
065    protected SchemaManager schemaManager;
066
067    @Param(name = "directoryName", required = true)
068    protected String directoryName;
069
070    @Param(name = "translateLabels", required = false)
071    protected boolean translateLabels;
072
073    @Param(name = "lang", required = false)
074    protected String lang;
075
076    @OperationMethod
077    public Blob run(DocumentModel doc) throws IOException {
078        Directory directory = directoryService.getDirectory(directoryName, doc);
079        try (Session session = directory.getSession()) {
080            DocumentModelList entries = session.getEntries();
081            String schemaName = directory.getSchema();
082            Schema schema = schemaManager.getSchema(schemaName);
083            List<Map<String, Object>> rows = new ArrayList<>();
084            for (DocumentModel entry : entries) {
085                Map<String, Object> obj = new LinkedHashMap<>();
086                for (Field field : schema.getFields()) {
087                    QName fieldName = field.getName();
088                    String key = fieldName.getLocalName();
089                    Serializable value = entry.getPropertyValue(fieldName.getPrefixedName());
090                    if (translateLabels && "label".equals(key)) {
091                        value = translate((String) value);
092                    }
093                    obj.put(key, value);
094                }
095                rows.add(obj);
096            }
097            return Blobs.createJSONBlobFromValue(rows);
098        }
099    }
100
101    @OperationMethod
102    public Blob run() throws IOException {
103        return run(null);
104    }
105
106    protected Locale getLocale() {
107        if (lang == null) {
108            lang = (String) ctx.get("lang");
109        }
110        if (lang == null) {
111            lang = "en";
112        }
113        return new Locale(lang);
114    }
115
116    protected String translate(String key) {
117        if (key == null) {
118            return "";
119        }
120        return I18NUtils.getMessageString("messages", key, new Object[0], getLocale());
121    }
122
123}