001/*
002 * (C) Copyright 2006-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 *     Florent Guillaume
016 *
017 * $Id: MultiDirectory.java 25713 2007-10-05 16:06:58Z fguillaume $
018 */
019
020package org.nuxeo.ecm.directory.multi;
021
022import java.util.Collections;
023import java.util.List;
024
025import org.nuxeo.ecm.directory.AbstractDirectory;
026import org.nuxeo.ecm.directory.Directory;
027import org.nuxeo.ecm.directory.DirectoryException;
028import org.nuxeo.ecm.directory.Reference;
029import org.nuxeo.ecm.directory.Session;
030import org.nuxeo.ecm.directory.api.DirectoryService;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * @author Florent Guillaume
035 */
036public class MultiDirectory extends AbstractDirectory {
037
038    private final MultiDirectoryDescriptor descriptor;
039
040    public MultiDirectory(MultiDirectoryDescriptor descriptor) {
041        super(descriptor.name);
042        this.descriptor = descriptor;
043    }
044
045    protected MultiDirectoryDescriptor getDescriptor() {
046        return descriptor;
047    }
048
049    @Override
050    public String getName() {
051        return descriptor.name;
052    }
053
054    @Override
055    public String getSchema() {
056        return descriptor.schemaName;
057    }
058
059    @Override
060    public String getParentDirectory() {
061        return null; // no parent directories are specified for multi
062    }
063
064    @Override
065    public String getIdField() {
066        return descriptor.idField;
067    }
068
069    @Override
070    public String getPasswordField() {
071        return descriptor.passwordField;
072    }
073
074    @Override
075    public Session getSession() throws DirectoryException {
076        MultiDirectorySession session = new MultiDirectorySession(this);
077        addSession(session);
078        return session;
079    }
080
081    @Override
082    public List<Reference> getReferences(String referenceFieldName) {
083        Reference reference = new MultiReference(this, referenceFieldName);
084        return Collections.singletonList(reference);
085    }
086
087    @Override
088    public void invalidateDirectoryCache() throws DirectoryException {
089        DirectoryService dirService = Framework.getService(DirectoryService.class);
090        getCache().invalidateAll();
091        // and also invalidates the cache from the source directories
092        for (SourceDescriptor src : descriptor.sources) {
093            for (SubDirectoryDescriptor sub : src.subDirectories) {
094                Directory dir = dirService.getDirectory(sub.name);
095                if (dir != null) {
096                    dir.invalidateDirectoryCache();
097                }
098            }
099        }
100    }
101
102}