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