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