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.List;
020import java.util.Map;
021import java.util.stream.Collectors;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.ProcessingException;
026import org.apache.cocoon.environment.ObjectModelHelper;
027import org.apache.cocoon.environment.Request;
028import org.apache.cocoon.generation.ServiceableGenerator;
029import org.apache.cocoon.xml.XMLUtils;
030import org.xml.sax.SAXException;
031
032import org.ametys.cms.repository.Content;
033import org.ametys.core.user.UserIdentity;
034import org.ametys.plugins.workspaces.members.JCRProjectMember.MemberType;
035import org.ametys.plugins.workspaces.project.ProjectManager;
036import org.ametys.plugins.workspaces.project.ProjectsCatalogueManager;
037import org.ametys.plugins.workspaces.project.objects.Project;
038import org.ametys.plugins.workspaces.project.objects.Project.InscriptionStatus;
039
040/**
041 * Sax the public and moderated projects of current member content
042 *
043 */
044public class MemberProjectsGenerator extends ServiceableGenerator
045{
046    /** The project manager */
047    protected ProjectManager _projectManager;
048    /** The project catalog manager */
049    protected ProjectsCatalogueManager _projectCatalogManager;
050
051    @Override
052    public void service(ServiceManager smanager) throws ServiceException
053    {
054        super.service(smanager);
055        _projectManager = (ProjectManager) smanager.lookup(ProjectManager.ROLE);
056        _projectCatalogManager = (ProjectsCatalogueManager) smanager.lookup(ProjectsCatalogueManager.ROLE);
057    }
058
059    public void generate() throws IOException, SAXException, ProcessingException
060    {
061        contentHandler.startDocument();
062        
063        XMLUtils.startElement(contentHandler, "projects");
064        
065        Request request = ObjectModelHelper.getRequest(objectModel);
066        Content content = (Content) request.getAttribute(Content.class.getName());
067        
068        UserIdentity user = content.getValue("user");
069        
070        if (user != null)
071        {
072            Map<Project, MemberType> userProjects = _projectManager.getUserProjects(user);
073            
074            List<Project> publicProjects = userProjects.keySet()
075                    .stream()
076                    .filter(p -> p.getInscriptionStatus() != InscriptionStatus.PRIVATE)
077                    .collect(Collectors.toList());
078            
079            for (Project project : publicProjects)
080            {
081                _projectCatalogManager.saxProject(contentHandler, project);
082            }
083        }
084        
085        XMLUtils.endElement(contentHandler, "projects");
086        contentHandler.endDocument();
087
088    }
089}