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; 019import java.util.Set; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.commons.lang.StringUtils; 024 025import org.ametys.cms.content.indexing.solr.SolrIndexer; 026import org.ametys.cms.content.indexing.solr.observation.ObserverHelper; 027import org.ametys.core.observation.Event; 028import org.ametys.core.observation.Observer; 029import org.ametys.odf.course.Course; 030import org.ametys.odf.program.Program; 031import org.ametys.odf.program.SubProgram; 032import org.ametys.plugins.odfweb.observation.AbstractODFObserver; 033import org.ametys.plugins.odfweb.repository.OdfPageResolver; 034import org.ametys.plugins.repository.RepositoryConstants; 035import org.ametys.web.WebConstants; 036import org.ametys.web.indexing.solr.SolrPageIndexer; 037import org.ametys.web.repository.page.Page; 038import org.ametys.web.repository.sitemap.Sitemap; 039 040/** 041 * Abstract {@link Observer} for synchronizing the Solr indexes. 042 */ 043public abstract class AbstractSolrODFObserver extends AbstractODFObserver 044{ 045 /** The Solr indexer */ 046 protected SolrIndexer _solrIndexer; 047 /** The page indexer. */ 048 protected SolrPageIndexer _solrPageIndexer; 049 /** ODF page resolver */ 050 protected OdfPageResolver _odfPageResolver; 051 052 @Override 053 public void service(ServiceManager manager) throws ServiceException 054 { 055 super.service(manager); 056 _solrIndexer = (SolrIndexer) manager.lookup(SolrIndexer.ROLE); 057 _solrPageIndexer = (SolrPageIndexer) manager.lookup(SolrPageIndexer.ROLE); 058 _odfPageResolver = (OdfPageResolver) manager.lookup(OdfPageResolver.ROLE); 059 } 060 061 @Override 062 public int getPriority(Event event) 063 { 064 // Will be processed after live synchronization observers 065 return MAX_PRIORITY + 3000; 066 } 067 068 @Override 069 protected final void _internalObserve(Event event, Map<String, Object> transientVars, Page odfRootPage, Set<Program> programs, SubProgram subProgram, Course course) throws Exception 070 { 071 if (ObserverHelper.isNotSuspendedObservationForIndexation()) 072 { 073 boolean indexModified = false; 074 Sitemap sitemap = odfRootPage.getSitemap(); 075 String sitemapName = sitemap.getSitemapName(); 076 077 for (Program program : programs) 078 { 079 if (StringUtils.equals(sitemapName, program.getLanguage())) 080 { 081 _updateIndex(event, transientVars, odfRootPage, program, subProgram, course); 082 indexModified = true; 083 } 084 } 085 086 if (indexModified) 087 { 088 _solrIndexer.commit(RepositoryConstants.DEFAULT_WORKSPACE); 089 _solrIndexer.commit(WebConstants.LIVE_WORKSPACE); 090 } 091 } 092 } 093 094 /** 095 * Update the index from the observed event. 096 * @param event the event. 097 * @param transientVars transientVars passed from one Observer to another when processing a single Event. 098 * This may allow optimizations between observers. 099 * @param odfRootPage the odf root page 100 * @param program the target program 101 * @param subProgram The subprogram. Can be null 102 * @param course the course. Can be null. 103 * @throws Exception if an error occurs. 104 */ 105 protected abstract void _updateIndex(Event event, Map<String, Object> transientVars, Page odfRootPage, Program program, SubProgram subProgram, Course course) throws Exception; 106 107}