001/*
002 * (C) Copyright 2007 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 *     Nuxeo - initial API and implementation
018 *
019 * $Id: Functions.java 19475 2007-05-27 10:33:53Z sfermigier $
020 */
021package org.nuxeo.ecm.platform.ui.web.directory;
022
023import java.util.Collection;
024
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.DocumentModelList;
027import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
028import org.nuxeo.ecm.directory.Session;
029import org.nuxeo.ecm.directory.api.DirectoryService;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * Utility functions (directory related) to be used from jsf via nxu: tags.
034 *
035 * @author <a href="mailto:dm@nuxeo.com">Dragos Mihalache</a>
036 * @author Anahide Tchertchian
037 */
038public final class DirectoryFunctions {
039
040    /**
041     * Utility classes should not have a public or default constructor.
042     */
043    private DirectoryFunctions() {
044    }
045
046    public static DocumentModel getDirectoryEntry(String directoryName, String entryId) {
047        if (entryId == null) {
048            return null;
049        }
050        DirectoryService dirService = Framework.getService(DirectoryService.class);
051        try (Session session = dirService.open(directoryName)) {
052            return session.getEntry(entryId);
053        }
054    }
055
056    public static DocumentModelList getDirectoryEntries(String directoryName, String... entryIds) {
057        if (entryIds == null) {
058            return null;
059        }
060        DirectoryService dirService = Framework.getService(DirectoryService.class);
061        try (Session session = dirService.open(directoryName)) {
062            DocumentModelList result = new DocumentModelListImpl();
063            for (String entryId : entryIds) {
064                DocumentModel entry = session.getEntry(entryId);
065                if (entry != null) {
066                    result.add(entry);
067                }
068            }
069            return result;
070        }
071    }
072
073    public static DocumentModelList getDirectoryListEntries(String directoryName, Collection<String> entryIds) {
074        if (entryIds == null) {
075            return null;
076        }
077        return getDirectoryEntries(directoryName, entryIds.toArray(new String[] {}));
078    }
079
080}