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.activitystream;
017
018import java.util.Collections;
019import java.util.Date;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.avalon.framework.parameters.Parameters;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.cocoon.acting.ServiceableAction;
028import org.apache.cocoon.environment.ObjectModelHelper;
029import org.apache.cocoon.environment.Redirector;
030import org.apache.cocoon.environment.Request;
031import org.apache.cocoon.environment.SourceResolver;
032
033import org.ametys.core.cocoon.ActionResultGenerator;
034import org.ametys.core.user.CurrentUserProvider;
035import org.ametys.core.user.UserIdentity;
036import org.ametys.core.userpref.UserPreferencesManager;
037import org.ametys.core.util.DateUtils;
038import org.ametys.plugins.workspaces.project.ProjectManager;
039import org.ametys.plugins.workspaces.project.objects.Project;
040import org.ametys.web.WebHelper;
041
042/**
043 * Get the activities for activity stream
044 *
045 */
046public class ActivityStreamServiceAction extends ServiceableAction
047{
048    private ActivityStreamClientInteraction _activityStream;
049    private ProjectManager _projectManager;
050    private UserPreferencesManager _userPrefManager;
051    private CurrentUserProvider _currentUserProvider;
052    
053    @Override
054    public void service(ServiceManager smanager) throws ServiceException
055    {
056        super.service(smanager);
057        _activityStream = (ActivityStreamClientInteraction) smanager.lookup(ActivityStreamClientInteraction.ROLE);
058        _projectManager = (ProjectManager) smanager.lookup(ProjectManager.ROLE);
059        _userPrefManager = (UserPreferencesManager) smanager.lookup(UserPreferencesManager.ROLE);
060        _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
061    }
062    
063    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
064    {
065        int limit = parameters.getParameterAsInteger("max-results", Integer.MAX_VALUE);
066        
067        boolean allActivities = parameters.getParameterAsBoolean("all", false);
068        boolean markAsRead = parameters.getParameterAsBoolean("mark-as-read", false);
069        
070        Request request = ObjectModelHelper.getRequest(objectModel);
071        String siteName = WebHelper.getSiteName(request);
072        
073        List<String> projects = _projectManager.getProjectsForSite(siteName);
074        
075        List<Map<String, Object>> activities;
076        if (projects.isEmpty() || allActivities)
077        {
078            // Get activity stream of all current user's projects
079            activities = _activityStream.getActivitiesForCurrentUser(limit);
080        }
081        else
082        {
083            // Get activity stream  of current project only
084            Project project = _projectManager.getProject(projects.get(0));
085            activities = _activityStream.getActivitiesForCurrentUser(Collections.singleton(project), limit);
086        }
087        
088        UserIdentity user = _currentUserProvider.getUser();
089        if (markAsRead && user != null)
090        {
091            // Mark as read in user prefs
092            _userPrefManager.setUserPreferences(user, ActivityStreamClientInteraction.ACTIVITY_STREAM_USER_PREF_CONTEXT, Map.of(), Map.of(ActivityStreamClientInteraction.ACTIVITY_STREAM_USER_PREF_LAST_UPDATE, DateUtils.dateToString(new Date())));
093        }
094        
095        Map<String, Object> result = new HashMap<>();
096        // FIXME this should be rename activities
097        result.put("events", activities);
098        
099        request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, result);
100        
101        return EMPTY_MAP;
102    }
103
104}