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.events.activitystream;
017
018import java.util.ArrayList;
019import java.util.Date;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023import java.util.Set;
024import java.util.stream.Collectors;
025
026import javax.jcr.RepositoryException;
027
028import org.apache.avalon.framework.parameters.Parameters;
029import org.apache.avalon.framework.service.ServiceException;
030import org.apache.avalon.framework.service.ServiceManager;
031import org.apache.cocoon.ProcessingException;
032import org.apache.cocoon.acting.ServiceableAction;
033import org.apache.cocoon.environment.ObjectModelHelper;
034import org.apache.cocoon.environment.Redirector;
035import org.apache.cocoon.environment.Request;
036import org.apache.cocoon.environment.SourceResolver;
037
038import org.ametys.core.cocoon.ActionResultGenerator;
039import org.ametys.core.user.CurrentUserProvider;
040import org.ametys.core.user.UserIdentity;
041import org.ametys.plugins.workspaces.project.ProjectManager;
042import org.ametys.plugins.workspaces.project.objects.Project;
043import org.ametys.runtime.parameter.ParameterHelper;
044
045/**
046 * Get the events for activity stream
047 *
048 */
049public class ActivityStreamServiceAction extends ServiceableAction
050{
051    private ProjectManager _projectManager;
052    private CurrentUserProvider _currentUserProvider;
053    private ActivityStreamClientInteraction _activityStream;
054    
055    @Override
056    public void service(ServiceManager smanager) throws ServiceException
057    {
058        super.service(smanager);
059        _projectManager = (ProjectManager) smanager.lookup(ProjectManager.ROLE);
060        _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
061        _activityStream = (ActivityStreamClientInteraction) smanager.lookup(ActivityStreamClientInteraction.ROLE);
062    }
063    
064    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
065    {
066        int limit = parameters.getParameterAsInteger("max-results", Integer.MAX_VALUE);
067        
068        UserIdentity user = _currentUserProvider.getUser();
069        List<Project> userProjects = _projectManager.getUserProjects(user);
070        
071        List<String> projectNames = userProjects.stream().map(p -> p.getName()).collect(Collectors.toList());
072        
073        try
074        {
075            Set<String> allowedEventTypes = _activityStream.getAllowedEventTypes(userProjects);
076            
077            List<Map<String, Object>> events = _activityStream.getEvents(projectNames, new ArrayList<>(allowedEventTypes), limit);
078            
079            // Add a parameter representing the date in the ISO 8601 format
080            events.stream().forEach(event -> 
081            {
082                // start date
083                event.put("date-iso", ParameterHelper.valueToString(event.get("date")));
084                
085                // optional end date
086                Date endDate = (Date) event.get("endDate");
087                if (endDate != null)
088                {
089                    event.put("end-date-iso", ParameterHelper.valueToString(endDate));
090                }
091            });
092            
093            Map<String, Object> result = new HashMap<>();
094            result.put("events", events);
095            
096            Request request = ObjectModelHelper.getRequest(objectModel);
097            request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, result);
098            
099            return EMPTY_MAP;
100        }
101        catch (RepositoryException e)
102        {
103            throw new ProcessingException("Failed to get events", e);
104        }
105    }
106
107}