001/*
002 * (C) Copyright 2006-2013 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 *     ldoguin
018 *
019 */
020package org.nuxeo.ecm.automation.core.operations.services.directory;
021
022import java.io.Serializable;
023import java.util.HashMap;
024import java.util.HashSet;
025import java.util.List;
026import java.util.Map;
027import java.util.Set;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.ecm.automation.OperationContext;
032import org.nuxeo.ecm.automation.core.Constants;
033import org.nuxeo.ecm.automation.core.annotations.Context;
034import org.nuxeo.ecm.automation.core.annotations.Operation;
035import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
036import org.nuxeo.ecm.automation.core.annotations.Param;
037import org.nuxeo.ecm.automation.core.util.Properties;
038import org.nuxeo.ecm.automation.core.util.StringList;
039import org.nuxeo.ecm.directory.Session;
040import org.nuxeo.ecm.directory.api.DirectoryService;
041
042/**
043 * @author Laurent Doguin (ldoguin@nuxeo.com)
044 * @since 5.7.2
045 */
046@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)
047public class DirectoryProjection {
048
049    public static final String ID = "Directory.Projection";
050
051    private static final Log log = LogFactory.getLog(DirectoryProjection.class);
052
053    @Context
054    protected OperationContext ctx;
055
056    @Context
057    protected DirectoryService directoryService;
058
059    @Param(name = "directoryName", required = true)
060    protected String directoryName;
061
062    @Param(name = "columnName", required = true)
063    protected String columnName;
064
065    @Param(name = "variableName", required = true)
066    protected String variableName;
067
068    @Param(name = "filters", required = false)
069    protected Properties filterProperties;
070
071    @Param(name = "fulltextFields", required = false)
072    protected StringList fulltextFields;
073
074    @OperationMethod
075    public void run() {
076        try (Session session = directoryService.open(directoryName)) {
077            Map<String, Serializable> filter = new HashMap<>();
078            Set<String> fulltext = new HashSet<>();
079            if (filterProperties != null) {
080                filter.putAll(filterProperties);
081                if (fulltextFields != null) {
082                    fulltext.addAll(fulltextFields);
083                }
084            }
085            List<String> uids = session.getProjection(filter, fulltext, columnName);
086            ctx.put(variableName, uids);
087        }
088    }
089
090}