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; 022import org.apache.commons.lang3.ArrayUtils; 023 024import org.ametys.cms.ObservationConstants; 025import org.ametys.cms.content.indexing.solr.observation.ObserverHelper; 026import org.ametys.core.observation.Event; 027import org.ametys.core.observation.Observer; 028import org.ametys.odf.ProgramItem; 029import org.ametys.odf.orgunit.OrgUnit; 030import org.ametys.plugins.odfweb.repository.OdfPageHandler; 031import org.ametys.plugins.odfweb.restrictions.OdfProgramRestrictionManager; 032import org.ametys.web.indexing.observation.AbstractSolrObserver; 033import org.ametys.web.repository.page.Page; 034import org.ametys.web.repository.site.Site; 035import org.ametys.web.repository.site.SiteManager; 036import org.ametys.web.repository.sitemap.Sitemap; 037import org.ametys.web.site.SiteConfigurationExtensionPoint; 038 039/** 040 * {@link Observer} for observing orgunit modifications 041 * in order to synchronize Solr index. 042 */ 043public abstract class AbstractSolrOrgUnitObserver extends AbstractSolrObserver 044{ 045 /** The site manager. */ 046 protected SiteManager _siteManager; 047 /** The site configuration. */ 048 protected SiteConfigurationExtensionPoint _siteConf; 049 /** The ODF page handler. */ 050 protected OdfPageHandler _odfPageHandler; 051 /** Odf program restriction manager */ 052 protected OdfProgramRestrictionManager _odfProgramRestrictionManager; 053 054 @Override 055 public void service(ServiceManager serviceManager) throws ServiceException 056 { 057 super.service(serviceManager); 058 _siteManager = (SiteManager) serviceManager.lookup(SiteManager.ROLE); 059 _odfPageHandler = (OdfPageHandler) serviceManager.lookup(OdfPageHandler.ROLE); 060 _siteConf = (SiteConfigurationExtensionPoint) serviceManager.lookup(SiteConfigurationExtensionPoint.ROLE); 061 _odfProgramRestrictionManager = (OdfProgramRestrictionManager) serviceManager.lookup(OdfProgramRestrictionManager.ROLE); 062 } 063 064 @Override 065 public boolean supports(Event event) 066 { 067 return event.getArguments().get(ObservationConstants.ARGS_CONTENT) instanceof OrgUnit && ArrayUtils.contains(_supportedEventIds(), event.getId()); 068 } 069 070 /** 071 * Gets the supported event ids for this Observer to look for the {@link OrgUnit}s 072 * @return the supported event ids for this Observer 073 */ 074 protected abstract String[] _supportedEventIds(); 075 076 @Override 077 public void observe(Event event, Map<String, Object> transientVars) throws Exception 078 { 079 if (ObserverHelper.isNotSuspendedObservationForIndexation()) 080 { 081 for (Site site : _siteManager.getSites()) 082 { 083 String siteName = site.getName(); 084 if (_odfPageHandler.hasOdfRootPage(site)) 085 { 086 for (Sitemap sitemap : site.getSitemaps()) 087 { 088 for (Page odfRootPage : _odfPageHandler.getOdfRootPages(siteName, sitemap.getSitemapName())) 089 { 090 if (_odfProgramRestrictionManager.hasOrgunitRestrictions(odfRootPage) 091 || ProgramItem.ORG_UNITS_REFERENCES.equals(_odfPageHandler.getLevel1Metadata(odfRootPage)) 092 || ProgramItem.ORG_UNITS_REFERENCES.equals(_odfPageHandler.getLevel2Metadata(odfRootPage))) 093 { 094 _updateIndex(odfRootPage); 095 } 096 } 097 } 098 } 099 } 100 } 101 } 102 103 /** 104 * Update the index from the observed event. 105 * @param odfRootPage the odf root page 106 * @throws Exception if an error occurs. 107 */ 108 protected abstract void _updateIndex(Page odfRootPage) throws Exception; 109}