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