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.web.indexing.observation; 017 018import java.util.Collection; 019 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.avalon.framework.service.ServiceManager; 022import org.apache.avalon.framework.service.Serviceable; 023 024import org.ametys.cms.content.indexing.solr.SolrIndexer; 025import org.ametys.cms.repository.Content; 026import org.ametys.core.observation.Event; 027import org.ametys.core.observation.Observer; 028import org.ametys.plugins.repository.AmetysObjectResolver; 029import org.ametys.plugins.repository.AmetysRepositoryException; 030import org.ametys.plugins.repository.RepositoryConstants; 031import org.ametys.runtime.plugin.component.AbstractLogEnabled; 032import org.ametys.web.WebConstants; 033import org.ametys.web.indexing.SiteIndexer; 034import org.ametys.web.indexing.solr.SolrPageIndexer; 035import org.ametys.web.repository.content.WebContent; 036import org.ametys.web.repository.page.Page; 037 038/** 039 * Abstract {@link Observer} for synchronizing the Solr index for Web components 040 */ 041public abstract class AbstractSolrObserver extends AbstractLogEnabled implements Observer, Serviceable 042{ 043 /** The site indexer helper */ 044 protected SiteIndexer _siteIndexer; 045 /** The Solr indexer */ 046 protected SolrIndexer _solrIndexer; 047 /** The Solr page indexer. */ 048 protected SolrPageIndexer _solrPageIndexer; 049 /** The Ametys object resolver */ 050 protected AmetysObjectResolver _resolver; 051 052 @Override 053 public void service(ServiceManager manager) throws ServiceException 054 { 055 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 056 _solrIndexer = (SolrIndexer) manager.lookup(SolrIndexer.ROLE); 057 _siteIndexer = (SiteIndexer) manager.lookup(SiteIndexer.ROLE); 058 _solrPageIndexer = (SolrPageIndexer) manager.lookup(SolrPageIndexer.ROLE); 059 } 060 061 public int getPriority(Event event) 062 { 063 // Will be processed after live synchronization observers 064 return MAX_PRIORITY + 3000; 065 } 066 067 /** 068 * Update index of all pages referencing the content for all workspaces if exists 069 * @param contentId The id of content 070 * @param recursively true to index recursively the subpages 071 * @throws AmetysRepositoryException If an error occurred 072 * @throws Exception if an error occurred during indexing 073 */ 074 protected void _updateIndexReferencingPages (String contentId, boolean recursively) throws Exception 075 { 076 Content content = _resolver.resolveById(contentId); 077 078 if (content instanceof WebContent) 079 { 080 Collection<Page> referencingPages = ((WebContent) content).getReferencingPages(); 081 082 for (Page page : referencingPages) 083 { 084 // Reindex page in default workspace (its attachments are still indexed, so no need to index them) 085 _solrPageIndexer.reindexPage(page.getId(), RepositoryConstants.DEFAULT_WORKSPACE, recursively, false, true); 086 // Reindex page in live workspace if exists (if page was not in live before, its attachments were not either, so index them) 087 _solrPageIndexer.reindexPage(page.getId(), WebConstants.LIVE_WORKSPACE, recursively, true, true); 088 } 089 } 090 } 091}