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    @Override
053    public void addLinks(String sourceId, List<String> targetIds) throws DirectoryException {
054        throw new UnsupportedOperationException();
055    }
056
057    @Override
058    public void addLinks(List<String> sourceIds, String targetId) throws DirectoryException {
059        throw new UnsupportedOperationException();
060    }
061
062    protected interface Collector {
063        List<String> collect(List<Reference> dir) throws DirectoryException;
064    }
065
066    protected List<String> doCollect(Collector extractor) throws DirectoryException {
067        DirectoryService dirService = Framework.getService(DirectoryService.class);
068        Set<String> ids = new HashSet<String>();
069        for (SourceDescriptor src : dir.getDescriptor().sources) {
070            for (SubDirectoryDescriptor sub : src.subDirectories) {
071                Directory dir = dirService.getDirectory(sub.name);
072                if (dir == null) {
073                    continue;
074                }
075                List<Reference> ref = dir.getReferences(fieldName);
076                if (ref == null) {
077                    continue;
078                }
079                try {
080                    ids.addAll(extractor.collect(ref));
081                } catch (DirectoryEntryNotFoundException e) {
082                    log.debug(e.getMessage());
083                }
084            }
085        }
086        List<String> x = new ArrayList<String>(ids.size());
087        x.addAll(ids);
088        return x;
089    }
090
091    @Override
092    public List<String> getSourceIdsForTarget(final String targetId) throws DirectoryException {
093        return doCollect(new Collector() {
094            @Override
095            public List<String> collect(List<Reference> refs) throws DirectoryException {
096                List<String> sourceIds = new ArrayList<>(1);
097                for (Reference ref : refs) {
098                    sourceIds.addAll(ref.getSourceIdsForTarget(targetId));
099                }
100                return sourceIds;
101            }
102        });
103    }
104
105    @Override
106    public List<String> getTargetIdsForSource(final String sourceId) throws DirectoryException {
107        return doCollect(new Collector() {
108            @Override
109            public List<String> collect(List<Reference> refs) throws DirectoryException {
110                List<String> targetIds = new ArrayList<>(1);
111                for (Reference ref : refs) {
112                    targetIds.addAll(ref.getSourceIdsForTarget(sourceId));
113                }
114                return targetIds;
115            }
116        });
117    }
118
119    @Override
120    public void removeLinksForSource(String sourceId) throws DirectoryException {
121        throw new UnsupportedOperationException();
122    }
123
124    @Override
125    public void removeLinksForTarget(String targetId) throws DirectoryException {
126        throw new UnsupportedOperationException();
127    }
128
129    @Override
130    public void setSourceIdsForTarget(String targetId, List<String> sourceIds) throws DirectoryException {
131        throw new UnsupportedOperationException();
132    }
133
134    @Override
135    public void setTargetIdsForSource(String sourceId, List<String> targetIds) throws DirectoryException {
136        throw new UnsupportedOperationException();
137    }
138
139    /**
140     * @since 5.6
141     */
142    @Override
143    public MultiReference clone() {
144        MultiReference clone = (MultiReference) super.clone();
145        // basic fields are already copied by super.clone()
146        return clone;
147    }
148
149}