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