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