001/* 002 * Copyright 2017 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.odfweb.observation.solr; 017 018import java.util.Map; 019 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.avalon.framework.service.ServiceManager; 022 023import org.ametys.cms.content.indexing.solr.observation.ObserverHelper; 024import org.ametys.core.observation.Event; 025import org.ametys.core.observation.Observer; 026import org.ametys.plugins.odfweb.repository.OdfPageHandler; 027import org.ametys.plugins.repository.RepositoryConstants; 028import org.ametys.web.ObservationConstants; 029import org.ametys.web.WebConstants; 030import org.ametys.web.indexing.observation.AbstractSolrObserver; 031import org.ametys.web.repository.page.Page; 032import org.ametys.web.repository.site.Site; 033import org.ametys.web.repository.sitemap.Sitemap; 034 035/** 036 * {@link Observer} for observing site configuration modifications 037 * in order to synchronize Solr index. 038 */ 039public abstract class AbstractSolrOnSiteConfModifiedObserver extends AbstractSolrObserver 040{ 041 private OdfPageHandler _odfPageHandler; 042 043 @Override 044 public void service(ServiceManager manager) throws ServiceException 045 { 046 super.service(manager); 047 _odfPageHandler = (OdfPageHandler) manager.lookup(OdfPageHandler.ROLE); 048 } 049 050 @Override 051 public boolean supports(Event event) 052 { 053 return event.getId().equals(ObservationConstants.EVENT_SITE_UPDATED); 054 } 055 056 @Override 057 public void observe(Event event, Map<String, Object> transientVars) throws Exception 058 { 059 if (ObserverHelper.isNotSuspendedObservationForIndexation()) 060 { 061 Site site = (Site) event.getArguments().get(ObservationConstants.ARGS_SITE); 062 if (site != null) 063 { 064 _updateOdfRootIndex(site); 065 } 066 } 067 } 068 069 private void _updateOdfRootIndex(Site site) throws Exception 070 { 071 boolean indexModified = false; 072 String siteName = site.getName(); 073 for (Sitemap sitemap : site.getSitemaps()) 074 { 075 for (Page odfRootPage : _odfPageHandler.getOdfRootPages(siteName, sitemap.getSitemapName())) 076 { 077 _updateIndex(odfRootPage); 078 indexModified = true; 079 } 080 } 081 082 if (indexModified) 083 { 084 _solrIndexer.commit(RepositoryConstants.DEFAULT_WORKSPACE); 085 _solrIndexer.commit(WebConstants.LIVE_WORKSPACE); 086 } 087 } 088 089 /** 090 * Update the index from the observed event. 091 * @param odfRootPage the odf root page 092 * @throws Exception if an error occurs. 093 */ 094 protected abstract void _updateIndex(Page odfRootPage) throws Exception; 095}