001/*
002 *  Copyright 2022 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.events;
017
018import java.util.Set;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.avalon.framework.service.Serviceable;
023
024import org.ametys.core.observation.Event;
025import org.ametys.core.observation.ObservationManager;
026import org.ametys.core.observation.Observer;
027import org.ametys.plugins.workspaces.minisite.MiniSiteWorkspaceModule;
028import org.ametys.plugins.workspaces.project.ProjectManager;
029import org.ametys.plugins.workspaces.project.objects.Project;
030import org.ametys.runtime.plugin.component.AbstractLogEnabled;
031import org.ametys.web.repository.page.Page;
032import org.ametys.web.repository.page.PagesContainer;
033
034/**
035 * Abstract observer for events which have to be convert to minisite page event
036 *
037 */
038public abstract class AbstractConvertMinisiteEventObserver extends AbstractLogEnabled implements Observer, Serviceable
039{
040    /** The project manager */
041    protected ProjectManager _projectManager;
042    /** The observation manager */
043    protected ObservationManager _observationManager;
044
045    @Override
046    public void service(ServiceManager serviceManager) throws ServiceException
047    {
048        _projectManager = (ProjectManager) serviceManager.lookup(ProjectManager.ROLE);
049        _observationManager = (ObservationManager) serviceManager.lookup(ObservationManager.ROLE);
050    }
051    
052    public int getPriority(Event event)
053    {
054        return MAX_PRIORITY;
055    }
056    
057    /**
058     * Determines if the page is part of minisite project module (root page excluded)
059     * @param project the project. Can not be null
060     * @param page the page
061     * @return true if the page is a page of minisite
062     */
063    protected boolean isMinisitePage(Project project, PagesContainer page)
064    {
065        Set<Page> modulePages = _projectManager.getModulePages(project, MiniSiteWorkspaceModule.MINISITE_MODULE_ID);
066        if (!modulePages.isEmpty())
067        {
068            Page minisiteRootPage = modulePages.iterator().next();
069            return page.getPathInSitemap().startsWith(minisiteRootPage.getPathInSitemap() + "/");
070        }
071        
072        return false;
073    }
074}