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.Collection;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.commons.lang3.ArrayUtils;
024
025import org.ametys.cms.ObservationConstants;
026import org.ametys.cms.repository.Content;
027import org.ametys.core.observation.Event;
028import org.ametys.core.observation.Observer;
029import org.ametys.plugins.workspaces.WorkspacesConstants;
030import org.ametys.plugins.workspaces.project.objects.Project;
031import org.ametys.web.repository.content.WebContent;
032import org.ametys.web.repository.page.Page;
033
034/**
035 * This {@link Observer} throws a minisite page event updated when a content has been validated
036 */
037public class ConvertContentValidatedEvent2MinisitePageEventObserver extends AbstractConvertMinisiteEventObserver
038{
039    public boolean supports(Event event)
040    {
041        return event.getId().equals(ObservationConstants.EVENT_CONTENT_VALIDATED);
042    }
043
044    public void observe(Event event, Map<String, Object> transientVars) throws Exception
045    {
046        Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
047        String[] cTypes = content.getTypes();
048        if (ArrayUtils.contains(cTypes, WorkspacesConstants.PROJECT_ARTICLE_CONTENT_TYPE) && content instanceof WebContent)
049        {
050            Project project = _getProject((WebContent) content);
051            if (project != null)
052            {
053                _notifyPageUpdate(content, project, event);
054            }
055        }
056    }
057
058    private void _notifyPageUpdate(Content content, Project project, Event event)
059    {
060        Collection<Page> referencingPages = ((WebContent) content).getReferencingPages();
061        for (Page page : referencingPages)
062        {
063            Map<String, Object> eventParams = new HashMap<>();
064            eventParams.put(org.ametys.web.ObservationConstants.ARGS_PAGE, page);
065            eventParams.put(org.ametys.plugins.workspaces.ObservationConstants.ARGS_PROJECT, project);
066            
067            _observationManager.notify(new Event(org.ametys.plugins.workspaces.ObservationConstants.EVENT_MINISITE_PAGE_UPDATED, event.getIssuer(), eventParams));
068        }
069    }
070
071    private Project _getProject(WebContent content)
072    {
073        List<String> projects = _projectManager.getProjectsForSite(content.getSiteName());
074        if (projects.size() > 0)
075        {
076            return _projectManager.getProject(projects.get(0));
077        }
078        return null;
079    }
080}