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.HashMap;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.core.observation.Event;
026import org.ametys.core.observation.Observer;
027import org.ametys.plugins.repository.AmetysObjectResolver;
028import org.ametys.plugins.workspaces.project.objects.Project;
029import org.ametys.web.ObservationConstants;
030import org.ametys.web.repository.page.Page;
031import org.ametys.web.repository.site.Site;
032
033/**
034 * This {@link Observer} converts Page events to minisite page events if needed
035 */
036public class ConvertPageEvent2MinisitePageEventObserver extends AbstractConvertMinisiteEventObserver
037{
038    /** Map for events id conversion */
039    protected static final Map<String, String> _EVENTS_ID_CONVERSION = Map.of(
040            org.ametys.web.ObservationConstants.EVENT_PAGE_ADDED, org.ametys.plugins.workspaces.ObservationConstants.EVENT_MINISITE_PAGE_CREATED,
041            org.ametys.web.ObservationConstants.EVENT_PAGE_UPDATED, org.ametys.plugins.workspaces.ObservationConstants.EVENT_MINISITE_PAGE_UPDATED,
042            org.ametys.web.ObservationConstants.EVENT_PAGE_RENAMED, org.ametys.plugins.workspaces.ObservationConstants.EVENT_MINISITE_PAGE_RENAMED,
043            org.ametys.web.ObservationConstants.EVENT_PAGE_DELETING, org.ametys.plugins.workspaces.ObservationConstants.EVENT_MINISITE_PAGE_DELETED);
044    
045    private AmetysObjectResolver _resolver;
046    
047    @Override
048    public void service(ServiceManager serviceManager) throws ServiceException
049    {
050        super.service(serviceManager);
051        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
052    }
053    
054    public boolean supports(Event event)
055    {
056        String eventId = event.getId();
057        return _EVENTS_ID_CONVERSION.containsKey(eventId);
058    }
059
060    public void observe(Event event, Map<String, Object> transientVars) throws Exception
061    {
062        Page page = (Page) event.getArguments().get(org.ametys.web.ObservationConstants.ARGS_PAGE);
063        if (page == null)
064        {
065            // in case of page deletion
066            String pageId = (String) event.getArguments().get(org.ametys.web.ObservationConstants.ARGS_PAGE_ID);
067            page = _resolver.resolveById(pageId);
068        }
069        
070        Project project = _getProject(page);
071        
072        if (project != null && isMinisitePage(project, page))
073        {
074            Map<String, Object> eventParams = new HashMap<>(event.getArguments());
075            // add project to event parameters
076            eventParams.put(org.ametys.plugins.workspaces.ObservationConstants.ARGS_PROJECT, project);
077            
078            if (event.getId().equals(org.ametys.web.ObservationConstants.EVENT_PAGE_DELETING))
079            {
080                eventParams.remove(ObservationConstants.ARGS_PAGE);
081                eventParams.put(ObservationConstants.ARGS_PAGE_ID, page.getId());
082                eventParams.put(org.ametys.plugins.workspaces.ObservationConstants.ARGS_PAGE_TITLE, page.getTitle());
083            }
084            
085            _observationManager.notify(new Event(_EVENTS_ID_CONVERSION.get(event.getId()), event.getIssuer(), eventParams));
086        }
087    }
088    
089    /**
090     * Get the project the page belongs
091     * @param page the page
092     * @return the project or null if the page does not belong to a project
093     */
094    protected Project _getProject(Page page)
095    {
096        Site site = page.getSite();
097        List<Project> projectsForSite = _projectManager.getProjectsForSite(site);
098        if (!projectsForSite.isEmpty())
099        {
100            return projectsForSite.get(0);
101        }
102        
103        return null;
104    }
105}