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