001/*
002 *  Copyright 2019 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.List;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.cms.ObservationConstants;
025import org.ametys.cms.repository.Content;
026import org.ametys.core.observation.Event;
027import org.ametys.plugins.repository.AmetysObject;
028import org.ametys.plugins.workspaces.project.ProjectManager;
029import org.ametys.plugins.workspaces.project.objects.Project;
030import org.ametys.web.repository.content.WebContent;
031
032/**
033 * Observer for storing event on news publication
034 *
035 */
036public class AddEventOnNewsPublishedObserver extends AbstractWorkspacesEventsObserver
037{
038    private ProjectManager _projectManager;
039
040    @Override
041    public void service(ServiceManager serviceManager) throws ServiceException
042    {
043        super.service(serviceManager);
044        _projectManager = (ProjectManager) serviceManager.lookup(ProjectManager.ROLE);
045    }
046    
047    
048    @Override
049    public boolean supports(Event event)
050    {
051        return event.getId().equals(org.ametys.plugins.workspaces.ObservationConstants.EVENT_PROJECT_NEWS_PUBLISHED);
052    }
053
054    @Override
055    public void observe(Event event, Map<String, Object> transientVars) throws Exception
056    {
057        Map<String, Object> args = event.getArguments();
058        
059        Content content = (Content) args.get(ObservationConstants.ARGS_CONTENT);
060        
061        Project project = getProject(content);
062        storeEvent(event, project, transientVars);
063    }
064    
065    @Override
066    protected Project getProject(AmetysObject object)
067    {
068        // Find project
069        List<Project> projects = _projectManager.getProjectsForSite(((WebContent) object).getSite());
070        return projects.isEmpty() ? null : projects.get(0);
071    }
072    
073}