001/*
002 *  Copyright 2020 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.plugins.workspaces.members.solr;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020import org.quartz.JobExecutionContext;
021
022import org.ametys.cms.content.indexing.solr.SolrIndexer;
023import org.ametys.cms.repository.Content;
024import org.ametys.cms.repository.ContentQueryHelper;
025import org.ametys.cms.repository.ContentTypeExpression;
026import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable;
027import org.ametys.plugins.repository.AmetysObjectIterable;
028import org.ametys.plugins.repository.AmetysObjectResolver;
029import org.ametys.plugins.repository.query.expression.Expression.Operator;
030import org.ametys.plugins.workspaces.WorkspacesConstants;
031
032/**
033 * Schedulable to re-index all existing members
034 */
035public class ReindexMembersSchedulable extends AbstractStaticSchedulable
036{
037    /** The Solr indexer */
038    protected SolrIndexer _solrIndexer;
039
040    /** The ametys object resolver */
041    protected AmetysObjectResolver _resolver;
042
043    @Override
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        super.service(manager);
047        _solrIndexer = (SolrIndexer) manager.lookup(SolrIndexer.ROLE);
048        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
049    }
050    
051    @Override
052    public void execute(JobExecutionContext context) throws Exception
053    {
054        String xpathQuery = ContentQueryHelper.getContentXPathQuery(new ContentTypeExpression(Operator.EQ, WorkspacesConstants.MEMBER_CONTENT_TYPE_ID));
055        AmetysObjectIterable<Content> searchResults = _resolver.query(xpathQuery);
056        _solrIndexer.indexContents(searchResults); // This index on all workspaces
057    }
058}