001/*
002 *  Copyright 2015 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;
017
018import java.util.ArrayList;
019import java.util.Collection;
020import java.util.List;
021import java.util.stream.Stream;
022
023import org.apache.avalon.framework.component.Component;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027import org.apache.solr.common.SolrInputDocument;
028
029import org.ametys.cms.indexing.solr.AdditionalDataIndexer;
030import org.ametys.cms.indexing.solr.AdditionalDataIndexerExtensionPoint;
031import org.ametys.cms.model.CMSDataContext;
032import org.ametys.cms.trash.element.DefaultTrashElement;
033import org.ametys.runtime.plugin.component.AbstractLogEnabled;
034
035/**
036 * Component for {@link DefaultTrashElement} indexing into a Solr server.
037 */
038public class SolrTrashElementIndexer extends AbstractLogEnabled implements Component, SolrFieldNames, Serviceable
039{
040    /** The component role. */
041    public static final String ROLE = SolrTrashElementIndexer.class.getName();
042    /** The additional property indexer extension point. */
043    private AdditionalDataIndexerExtensionPoint _additionalDataIndexerEP;
044    
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        _additionalDataIndexerEP = (AdditionalDataIndexerExtensionPoint) manager.lookup(AdditionalDataIndexerExtensionPoint.ROLE);
048    }
049    
050    /**
051     * Populate a solr input document by adding fields to index into it.
052     * @param trashElement The trash element to index
053     * @param document The main solr document to index into
054     * @return Additional documents generated during the indexing
055     * @throws Exception if an error occurred while indexing
056     */
057    public List<SolrInputDocument> indexTrashElement(DefaultTrashElement trashElement, SolrInputDocument document) throws Exception
058    {
059        // Properties specific to a stand-alone indexation.
060        document.addField(ID, trashElement.getId());
061        document.addField(DOCUMENT_TYPE, TYPE_TRASH_ELEMENT);
062        
063        // Index trash element properties and attributes
064        List<SolrInputDocument> trashElementIndexData = trashElement.indexData(document, CMSDataContext.newInstance());
065        
066        List<SolrInputDocument> additionalDocuments = _populateAdditionalData(trashElement, document);
067        
068        return Stream.concat(trashElementIndexData.stream(), additionalDocuments.stream())
069                .toList();
070    }
071    
072    /**
073     * Populate the solr input document by adding fields to index.
074     * @param trashElement the trashElement to index.
075     * @param document the solr input document
076     * @return Additional documents created by additional property indexers
077     * @throws Exception if something goes wrong when processing the indexation of the trash element
078     */
079    protected List<SolrInputDocument> _populateAdditionalData(DefaultTrashElement trashElement, SolrInputDocument document) throws Exception
080    {
081        List<SolrInputDocument> additionnalDocs = new ArrayList<>();
082        
083        Collection<AdditionalDataIndexer> indexers = _additionalDataIndexerEP.getIndexers(AdditionalDataIndexer.TYPE_TRASH_ELEMENT);
084        for (AdditionalDataIndexer indexer : indexers)
085        {
086            additionnalDocs.addAll(indexer.indexAdditionalDocuments(trashElement, document));
087        }
088        
089        return additionnalDocs;
090    }
091}