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