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.Session;
037import org.nuxeo.ecm.directory.api.DirectoryService;
038import org.nuxeo.runtime.api.Framework;
039
040public class MultiReference extends AbstractReference implements Cloneable {
041
042    private static final Log log = LogFactory.getLog(MultiReference.class);
043
044    final MultiDirectory dir;
045
046    MultiReference(MultiDirectory dir, String fieldName) {
047        super(fieldName, null);
048        this.dir = dir;
049    }
050
051    @Override
052    public void addLinks(String sourceId, List<String> targetIds) throws DirectoryException {
053        throw new UnsupportedOperationException();
054    }
055
056    @Override
057    public void addLinks(String sourceId, List<String> targetIds, Session session) throws DirectoryException {
058        throw new UnsupportedOperationException();
059    }
060
061    @Override
062    public void addLinks(List<String> sourceIds, String targetId, Session session) throws DirectoryException {
063        throw new UnsupportedOperationException();
064    }
065
066    @Override
067    public void addLinks(List<String> sourceIds, String targetId) throws DirectoryException {
068        throw new UnsupportedOperationException();
069    }
070
071    protected interface Collector {
072        List<String> collect(List<Reference> dir) throws DirectoryException;
073    }
074
075    protected List<String> doCollect(Collector extractor) throws DirectoryException {
076        DirectoryService dirService = Framework.getService(DirectoryService.class);
077        Set<String> ids = new HashSet<>();
078        for (SourceDescriptor src : dir.getDescriptor().sources) {
079            for (SubDirectoryDescriptor sub : src.subDirectories) {
080                Directory dir = dirService.getDirectory(sub.name);
081                if (dir == null) {
082                    continue;
083                }
084                List<Reference> ref = dir.getReferences(fieldName);
085                if (ref == null) {
086                    continue;
087                }
088                try {
089                    ids.addAll(extractor.collect(ref));
090                } catch (DirectoryEntryNotFoundException e) {
091                    log.debug(e.getMessage());
092                }
093            }
094        }
095        List<String> x = new ArrayList<>(ids.size());
096        x.addAll(ids);
097        return x;
098    }
099
100    @Override
101    public List<String> getSourceIdsForTarget(final String targetId) throws DirectoryException {
102        return doCollect(refs -> {
103            List<String> sourceIds = new ArrayList<>(1);
104            for (Reference ref : refs) {
105                sourceIds.addAll(ref.getSourceIdsForTarget(targetId));
106            }
107            return sourceIds;
108        });
109    }
110
111    @Override
112    public List<String> getTargetIdsForSource(final String sourceId) throws DirectoryException {
113        return doCollect(refs -> {
114            List<String> targetIds = new ArrayList<>(1);
115            for (Reference ref : refs) {
116                targetIds.addAll(ref.getSourceIdsForTarget(sourceId));
117            }
118            return targetIds;
119        });
120    }
121
122    @Override
123    public void removeLinksForSource(String sourceId) throws DirectoryException {
124        throw new UnsupportedOperationException();
125    }
126
127    @Override
128    public void removeLinksForSource(String sourceId, Session session) throws DirectoryException {
129        throw new UnsupportedOperationException();
130    }
131
132    @Override
133    public void removeLinksForTarget(String targetId) throws DirectoryException {
134        throw new UnsupportedOperationException();
135    }
136
137    @Override
138    public void removeLinksForTarget(String targetId, Session session) throws DirectoryException {
139        throw new UnsupportedOperationException();
140    }
141
142    @Override
143    public void setSourceIdsForTarget(String targetId, List<String> sourceIds) throws DirectoryException {
144        throw new UnsupportedOperationException();
145    }
146
147    @Override
148    public void setSourceIdsForTarget(String targetId, List<String> sourceIds, Session session)
149            throws DirectoryException {
150        throw new UnsupportedOperationException();
151    }
152
153    @Override
154    public void setTargetIdsForSource(String sourceId, List<String> targetIds) throws DirectoryException {
155        throw new UnsupportedOperationException();
156    }
157
158    @Override
159    public void setTargetIdsForSource(String sourceId, List<String> targetIds, Session session)
160            throws DirectoryException {
161        throw new UnsupportedOperationException();
162    }
163
164    /**
165     * @since 5.6
166     */
167    @Override
168    public MultiReference clone() {
169        // basic fields are already copied by super.clone()
170        try {
171            return (MultiReference) super.clone();
172        } catch (CloneNotSupportedException e) {
173            throw new AssertionError(e);
174        }
175    }
176}