001/*
002 *  Copyright 2020 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.workspaces.project.observers;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024
025import org.ametys.core.observation.Event;
026import org.ametys.core.observation.Observer;
027import org.ametys.plugins.workspaces.project.ProjectManager;
028import org.ametys.plugins.workspaces.project.objects.Project;
029import org.ametys.runtime.plugin.component.AbstractLogEnabled;
030import org.ametys.runtime.plugin.component.PluginAware;
031import org.ametys.web.ObservationConstants;
032import org.ametys.web.repository.site.Site;
033
034/**
035 * Abstract observer to have code executed when a sitemap is updated in a project
036 */
037public abstract class AbstractInitializeProjectObserver extends AbstractLogEnabled implements Observer, Serviceable, PluginAware
038{
039
040    /** Project Manager */
041    protected ProjectManager _projectManager;
042    
043    @Override
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        _projectManager = (ProjectManager) manager.lookup(ProjectManager.ROLE);
047    }
048    
049    @Override
050    public boolean supports(Event event)
051    {
052        // Listen on site updated because it is too early to listen on sitemap added (we need the site be configured and synchronized in live)
053        return event.getId().equals(ObservationConstants.EVENT_SITE_UPDATED);
054    }
055
056    @Override
057    public int getPriority(Event event)
058    {
059        // Will be processed after live synchronization observers
060        return MAX_PRIORITY + 2000;
061    }
062
063    @Override
064    public void observe(Event event, Map<String, Object> transientVars) throws Exception
065    {
066        Site site = (Site) event.getArguments().get(ObservationConstants.ARGS_SITE);
067        if (site != null)
068        {
069            List<Project> projects = _projectManager.getProjectsForSite(site);
070            if (!projects.isEmpty())
071            {
072                Project project = projects.get(0);
073                
074                if (project != null)
075                {
076                    doObserve(event, transientVars, site, project);
077                }
078            }
079        }
080    }
081    
082    /**
083     * This method will be called by {@link Observer#observe(Event, Map)} when there is a project on this site
084     * @param event the event
085     * @param transientVars transientVars passed from one Observer to another when processing a single Event. 
086     * This may allow optimizations between observers.
087     * @param site the site impacted
088     * @param project the project impacted
089     * @throws Exception something went wrong
090     */
091    protected abstract void doObserve(Event event, Map<String, Object> transientVars, Site site, Project project) throws Exception;
092}