001/*
002 * (C) Copyright 2006-2013 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     ldoguin
016 *
017 */
018package org.nuxeo.ecm.automation.core.operations.services.directory;
019
020import java.io.Serializable;
021import java.util.HashMap;
022import java.util.HashSet;
023import java.util.List;
024import java.util.Map;
025import java.util.Set;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.automation.OperationContext;
030import org.nuxeo.ecm.automation.core.Constants;
031import org.nuxeo.ecm.automation.core.annotations.Context;
032import org.nuxeo.ecm.automation.core.annotations.Operation;
033import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
034import org.nuxeo.ecm.automation.core.annotations.Param;
035import org.nuxeo.ecm.automation.core.util.Properties;
036import org.nuxeo.ecm.automation.core.util.StringList;
037import org.nuxeo.ecm.directory.Directory;
038import org.nuxeo.ecm.directory.Session;
039import org.nuxeo.ecm.directory.api.DirectoryService;
040
041/**
042 * @author Laurent Doguin (ldoguin@nuxeo.com)
043 * @since 5.7.2
044 */
045@Operation(id = DirectoryProjection.ID, category = Constants.CAT_SERVICES, label = "Get a Directory Projection", since = "5.7.2", description = "Executes a query using given filter and return only the column *<b>columnName</b>*. The result is assigned to the context variable *<b>variableName</b>*. The filters are specified as <i>key=value</i> pairs separated by a new line. The key used for a filter is the column name of the directory. To specify multi-line values you can use a \\ character followed by a new line. <p>Example:<pre>firstName=John<br>lastName=doe</pre>By default, the search filters use exact match. You can do a fulltext search on some specific columns using the fulltextFields. it's specified as comma separated columnName, for instance : <p>Example:<pre>firstName,lastName</pre>", addToStudio = false)
046public class DirectoryProjection {
047
048    public static final String ID = "Directory.Projection";
049
050    private static final Log log = LogFactory.getLog(DirectoryProjection.class);
051
052    @Context
053    protected OperationContext ctx;
054
055    @Context
056    protected DirectoryService directoryService;
057
058    @Param(name = "directoryName", required = true)
059    protected String directoryName;
060
061    @Param(name = "columnName", required = true)
062    protected String columnName;
063
064    @Param(name = "variableName", required = true)
065    protected String variableName;
066
067    @Param(name = "filters", required = false)
068    protected Properties filterProperties;
069
070    @Param(name = "fulltextFields", required = false)
071    protected StringList fulltextFields;
072
073    @OperationMethod
074    public void run() {
075        try (Session session = directoryService.open(directoryName)) {
076            Map<String, Serializable> filter = new HashMap<String, Serializable>();
077            Set<String> fulltext = new HashSet<String>();
078            if (filterProperties != null) {
079                filter.putAll(filterProperties);
080                if (fulltextFields != null) {
081                    fulltext.addAll(fulltextFields);
082                }
083            }
084            List<String> uids = session.getProjection(filter, fulltext, columnName);
085            ctx.put(variableName, uids);
086        }
087    }
088
089}