001/*
002 *  Copyright 2019 Anyware Services
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 */
016package org.ametys.cms.content.indexing.solr.content.attachment.impl;
017
018import java.util.Collection;
019import java.util.List;
020import java.util.Map.Entry;
021import java.util.Set;
022import java.util.stream.Collectors;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027
028import org.ametys.cms.content.indexing.solr.content.attachment.ContentVisibleAttachmentIndexer;
029import org.ametys.cms.content.references.OutgoingReferences;
030import org.ametys.cms.content.references.OutgoingReferencesExtractor;
031import org.ametys.cms.repository.Content;
032
033/**
034 * A {@link ContentVisibleAttachmentIndexer} for attachments extracted from the {@link OutgoingReferencesExtractor} component.
035 */
036public class OutgoingRefsContentAttachmentIndexer implements ContentVisibleAttachmentIndexer, Serviceable
037{
038    /** The outgoing references extractor */
039    protected OutgoingReferencesExtractor _outgoingReferencesExtractor;
040    
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        _outgoingReferencesExtractor = (OutgoingReferencesExtractor) manager.lookup(OutgoingReferencesExtractor.ROLE);
045    }
046    
047    @Override
048    public Collection<String> getVisibleAttachmentIds(Content content)
049    {
050        return _outgoingReferencesExtractor.getOutgoingReferences(content)
051                // key of #getOutgoingReferences() is the data path, we do not care what data it comes from
052                .values()
053                .parallelStream()
054                .map(OutgoingReferences::entrySet)
055                .flatMap(Set::parallelStream)
056                .filter(this::_filterReferences)
057                .map(Entry::getValue)
058                // flat resource ids
059                .flatMap(List::parallelStream)
060                .collect(Collectors.toList());
061    }
062    
063    private boolean _filterReferences(Entry<String, List<String>> outgoingRefsEntry)
064    {
065        // only references of 'explorer' type or 'attachment-content' type
066        String outgoingRefsType = outgoingRefsEntry.getKey();
067        return "explorer".equals(outgoingRefsType) || "attachment-content".equals(outgoingRefsType);
068    }
069}
070