001/*
002 *  Copyright 2016 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.activities;
017
018import java.util.Map;
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.Observer;
026import org.ametys.plugins.repository.AmetysObjectResolver;
027import org.ametys.plugins.repository.activities.Activity;
028import org.ametys.plugins.repository.activities.ActivityType;
029import org.ametys.plugins.repository.activities.ActivityTypeExtensionPoint;
030import org.ametys.plugins.repository.activities.ActivityTypeProcessingException;
031import org.ametys.plugins.workspaces.project.ProjectManager;
032import org.ametys.plugins.workspaces.project.notification.preferences.NotificationPreferencesHelper;
033import org.ametys.plugins.workspaces.project.objects.Project;
034import org.ametys.runtime.plugin.component.AbstractLogEnabled;
035
036/**
037 * {@link Observer} implementation for workspaces' activities
038 */
039public class WorkspacesActivityObserver extends AbstractLogEnabled implements Observer, Serviceable
040{
041    /** Node id stored as a transient var */
042    public static final String ACTIVITY_ID_TRANSIENT_VAR = "workspaces.activity.node.id";
043    /** Event type extension point */
044    protected ActivityTypeExtensionPoint _activityTypeExtensionPoint;
045    /** the ametys object resolver */
046    protected AmetysObjectResolver _resolver;
047    /** the notification preference helper */
048    protected NotificationPreferencesHelper _notificationPreferenceHelper;
049    /** the project manager */
050    protected ProjectManager _projectManager;
051
052    @Override
053    public void service(ServiceManager serviceManager) throws ServiceException
054    {
055        _activityTypeExtensionPoint = (ActivityTypeExtensionPoint) serviceManager.lookup(ActivityTypeExtensionPoint.ROLE);
056        _notificationPreferenceHelper = (NotificationPreferencesHelper) serviceManager.lookup(NotificationPreferencesHelper.ROLE);
057        _projectManager = (ProjectManager) serviceManager.lookup(ProjectManager.ROLE);
058        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
059    }
060    
061    public boolean supports(Event event)
062    {
063        try
064        {
065            ActivityType activityType = _activityTypeExtensionPoint.getActivityType(event.getId());
066            if (activityType instanceof AbstractWorkspacesActivityType workspaceActivityType)
067            {
068                // check specific cases of support
069                return workspaceActivityType.support(event);
070            }
071        }
072        catch (ActivityTypeProcessingException e)
073        {
074            // Event is not linked to a existing activity type, just ignore it, nothing to do
075        }
076        return false;
077    }
078    
079    @Override
080    public int getPriority(Event event)
081    {
082        return MAX_PRIORITY;
083    }
084    
085    @Override
086    public void observe(Event event, Map<String, Object> transientVars) throws Exception
087    {
088        String eventId = event.getId();
089        AbstractWorkspacesActivityType activityType = (AbstractWorkspacesActivityType) _activityTypeExtensionPoint.getActivityType(eventId);
090        Project project = activityType.getProjectFromEvent(event);
091        if (project == null)
092        {
093            // the event can not be link to a project so we ignore it
094            return;
095        }
096        Map<String, Object> activityParams = getActivityParameters(event, project);
097        
098        // use the event issuer to determine the author. Some activity (like adding a member) can happened with no user authenticated
099        Activity activity = project.addActivity(activityType, activityParams, event.getIssuer(), eventId);
100        
101        if (transientVars != null)
102        {
103            transientVars.put(ACTIVITY_ID_TRANSIENT_VAR, activity.getId());
104        }
105    }
106
107    /**
108     * Get the activity's parameters
109     * @param event The event use to create the activity
110     * @param project The project. Can not be null
111     * @return The event's parameters
112     */
113    protected Map<String, Object> getActivityParameters(Event event, Project project)
114    {
115        Map<String, Object> params = event.getArguments();
116        
117        params.put("projectName", project.getName());
118        params.put("projectTitle", project.getTitle());
119        
120        return params;
121    }
122}