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.Directory;
040import org.nuxeo.ecm.directory.Session;
041import org.nuxeo.ecm.directory.api.DirectoryService;
042
043/**
044 * @author Laurent Doguin (ldoguin@nuxeo.com)
045 * @since 5.7.2
046 */
047@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)
048public class DirectoryProjection {
049
050    public static final String ID = "Directory.Projection";
051
052    private static final Log log = LogFactory.getLog(DirectoryProjection.class);
053
054    @Context
055    protected OperationContext ctx;
056
057    @Context
058    protected DirectoryService directoryService;
059
060    @Param(name = "directoryName", required = true)
061    protected String directoryName;
062
063    @Param(name = "columnName", required = true)
064    protected String columnName;
065
066    @Param(name = "variableName", required = true)
067    protected String variableName;
068
069    @Param(name = "filters", required = false)
070    protected Properties filterProperties;
071
072    @Param(name = "fulltextFields", required = false)
073    protected StringList fulltextFields;
074
075    @OperationMethod
076    public void run() {
077        try (Session session = directoryService.open(directoryName)) {
078            Map<String, Serializable> filter = new HashMap<String, Serializable>();
079            Set<String> fulltext = new HashSet<String>();
080            if (filterProperties != null) {
081                filter.putAll(filterProperties);
082                if (fulltextFields != null) {
083                    fulltext.addAll(fulltextFields);
084                }
085            }
086            List<String> uids = session.getProjection(filter, fulltext, columnName);
087            ctx.put(variableName, uids);
088        }
089    }
090
091}