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