001/*
002 *  Copyright 2021 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.members;
017
018import java.io.IOException;
019import java.util.Arrays;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023import java.util.stream.Collectors;
024
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.cocoon.ProcessingException;
028import org.apache.cocoon.environment.ObjectModelHelper;
029import org.apache.cocoon.environment.Request;
030import org.apache.cocoon.generation.ServiceableGenerator;
031import org.apache.cocoon.xml.XMLUtils;
032import org.apache.commons.lang3.StringUtils;
033import org.xml.sax.SAXException;
034
035import org.ametys.cms.repository.Content;
036import org.ametys.core.user.CurrentUserProvider;
037import org.ametys.core.user.UserIdentity;
038import org.ametys.plugins.workspaces.members.JCRProjectMember.MemberType;
039import org.ametys.plugins.workspaces.project.ProjectManager;
040import org.ametys.plugins.workspaces.project.ProjectsCatalogueManager;
041import org.ametys.plugins.workspaces.project.favorites.FavoritesHelper;
042import org.ametys.plugins.workspaces.project.objects.Project;
043
044/**
045 * Sax the public and moderated projects of current member content
046 *
047 */
048public class MemberProjectsGenerator extends ServiceableGenerator
049{
050    /** The project manager */
051    protected ProjectManager _projectManager;
052    /** The project catalog manager */
053    protected ProjectsCatalogueManager _projectCatalogManager;
054    /** The current user provider */
055    protected CurrentUserProvider _currentUserProvider;
056    /** To handle favorites */
057    protected FavoritesHelper _favoritesHelper;
058
059    @Override
060    public void service(ServiceManager smanager) throws ServiceException
061    {
062        super.service(smanager);
063        _projectManager = (ProjectManager) smanager.lookup(ProjectManager.ROLE);
064        _projectCatalogManager = (ProjectsCatalogueManager) smanager.lookup(ProjectsCatalogueManager.ROLE);
065        _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
066        _favoritesHelper = (FavoritesHelper) manager.lookup(FavoritesHelper.ROLE);
067    }
068
069    public void generate() throws IOException, SAXException, ProcessingException
070    {
071        contentHandler.startDocument();
072        
073        XMLUtils.startElement(contentHandler, "projects");
074        
075        Request request = ObjectModelHelper.getRequest(objectModel);
076        UserIdentity user = _getUser(request);
077        
078        if (user != null)
079        {
080            // The filtered projects' categories
081            Set<String> filteredCategories = _getFilteredCategories(request);
082            
083            // Pattern filter
084            String pattern = request.getParameter("pattern");
085            
086            boolean excludePrivate = parameters.getParameterAsBoolean("excludePrivate", false);
087            
088            Map<Project, MemberType> userProjects = _projectManager.getUserProjects(user, filteredCategories, null, pattern, false, excludePrivate);
089            
090            Set<String> favorites = _favoritesHelper.getFavorites(user);
091            
092            List<Project> projects = userProjects.keySet()
093                    .stream()
094                    .collect(Collectors.toList());
095            
096            int maxMembers = parameters.getParameterAsInteger("maxMembers", Integer.MAX_VALUE);
097            boolean withMembers = parameters.getParameterAsBoolean("withMembers", false);
098            boolean expandGroup = parameters.getParameterAsBoolean("expandGroup", false);
099            
100            for (Project project : projects)
101            {
102                _projectCatalogManager.saxProject(contentHandler, project, withMembers, expandGroup, maxMembers, favorites.contains(project.getName()));
103            }
104        }
105        
106        XMLUtils.endElement(contentHandler, "projects");
107        contentHandler.endDocument();
108    }
109    
110    private UserIdentity _getUser(Request request)
111    {
112        boolean userContent = parameters.getParameterAsBoolean("userContent", false);
113        if (userContent)
114        {
115            // Get members of user content
116            Content content = (Content) request.getAttribute(Content.class.getName());
117            return content.getValue("user");
118        }
119        else
120        {
121            // Get members of connected users
122            return _currentUserProvider.getUser();
123        }
124    }
125    
126    private Set<String> _getFilteredCategories(Request request)
127    {
128        String[] categoryIds = request.getParameterValues("categories");
129        if (categoryIds != null)
130        {
131            return Arrays.stream(categoryIds)
132                    .filter(StringUtils::isNotEmpty)
133                    .collect(Collectors.toSet());
134        }
135        else
136        {
137            return Set.of();
138        }
139    }
140}