001/*
002 * (C) Copyright 2006-2009 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$
020 */
021
022package org.nuxeo.ecm.directory.multi;
023
024import java.util.ArrayList;
025import java.util.HashSet;
026import java.util.List;
027import java.util.Set;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.ecm.directory.AbstractReference;
032import org.nuxeo.ecm.directory.Directory;
033import org.nuxeo.ecm.directory.DirectoryEntryNotFoundException;
034import org.nuxeo.ecm.directory.DirectoryException;
035import org.nuxeo.ecm.directory.Reference;
036import org.nuxeo.ecm.directory.api.DirectoryService;
037import org.nuxeo.runtime.api.Framework;
038
039public class MultiReference extends AbstractReference {
040
041    private static final Log log = LogFactory.getLog(MultiReference.class);
042
043    final MultiDirectory dir;
044
045    final String fieldName;
046
047    MultiReference(MultiDirectory dir, String fieldName) {
048        this.dir = dir;
049        this.fieldName = fieldName;
050    }
051
052    public void addLinks(String sourceId, List<String> targetIds) throws DirectoryException {
053        throw new UnsupportedOperationException();
054    }
055
056    public void addLinks(List<String> sourceIds, String targetId) throws DirectoryException {
057        throw new UnsupportedOperationException();
058    }
059
060    protected interface Collector {
061        List<String> collect(List<Reference> dir) throws DirectoryException;
062    }
063
064    protected List<String> doCollect(Collector extractor) throws DirectoryException {
065        DirectoryService dirService = Framework.getService(DirectoryService.class);
066        Set<String> ids = new HashSet<String>();
067        for (SourceDescriptor src : dir.getDescriptor().sources) {
068            for (SubDirectoryDescriptor sub : src.subDirectories) {
069                Directory dir = dirService.getDirectory(sub.name);
070                if (dir == null) {
071                    continue;
072                }
073                List<Reference> ref = dir.getReferences(fieldName);
074                if (ref == null) {
075                    continue;
076                }
077                try {
078                    ids.addAll(extractor.collect(ref));
079                } catch (DirectoryEntryNotFoundException e) {
080                    log.debug(e.getMessage());
081                }
082            }
083        }
084        List<String> x = new ArrayList<String>(ids.size());
085        x.addAll(ids);
086        return x;
087    }
088
089    public List<String> getSourceIdsForTarget(final String targetId) throws DirectoryException {
090        return doCollect(new Collector() {
091            public List<String> collect(List<Reference> refs) throws DirectoryException {
092                List<String> sourceIds = new ArrayList<>(1);
093                for (Reference ref : refs) {
094                    sourceIds.addAll(ref.getSourceIdsForTarget(targetId));
095                }
096                return sourceIds;
097            }
098        });
099    }
100
101    public List<String> getTargetIdsForSource(final String sourceId) throws DirectoryException {
102        return doCollect(new Collector() {
103            public List<String> collect(List<Reference> refs) throws DirectoryException {
104                List<String> targetIds = new ArrayList<>(1);
105                for (Reference ref : refs) {
106                    targetIds.addAll(ref.getSourceIdsForTarget(sourceId));
107                }
108                return targetIds;
109            }
110        });
111    }
112
113    public void removeLinksForSource(String sourceId) throws DirectoryException {
114        throw new UnsupportedOperationException();
115    }
116
117    public void removeLinksForTarget(String targetId) throws DirectoryException {
118        throw new UnsupportedOperationException();
119    }
120
121    public void setSourceIdsForTarget(String targetId, List<String> sourceIds) throws DirectoryException {
122        throw new UnsupportedOperationException();
123    }
124
125    public void setTargetIdsForSource(String sourceId, List<String> targetIds) throws DirectoryException {
126        throw new UnsupportedOperationException();
127    }
128
129    /**
130     * @since 5.6
131     */
132    @Override
133    protected AbstractReference newInstance() {
134        return new MultiReference(dir, fieldName);
135    }
136
137    /**
138     * @since 5.6
139     */
140    @Override
141    public AbstractReference clone() {
142        return super.clone();
143    }
144
145}