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.preferences;
017
018import java.util.Map;
019import java.util.Map.Entry;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.xml.AttributesImpl;
025import org.apache.cocoon.xml.XMLUtils;
026import org.xml.sax.SAXException;
027
028import org.ametys.core.user.UserIdentity;
029import org.ametys.core.userpref.UserPreferencesException;
030import org.ametys.plugins.workspaces.members.JCRProjectMember.MemberType;
031import org.ametys.plugins.workspaces.project.ProjectManager;
032import org.ametys.plugins.workspaces.project.objects.Project;
033import org.ametys.web.userpref.FOUserPreferencesGenerator;
034
035/**
036 * Generator for the workspace preference service
037 * 
038 * This generator retrieve the service parameters in the userPref and sax them
039 */
040public class PreferenceServiceGenerator extends FOUserPreferencesGenerator
041{
042    private ProjectManager _projectManager;
043    
044    @Override
045    public void service(ServiceManager smanager) throws ServiceException
046    {
047        super.service(smanager);
048        _projectManager = (ProjectManager) smanager.lookup(ProjectManager.ROLE);
049    }
050    
051    @Override
052    protected void _saxPreferences(String storageContext, Map<String, String> contextVars, UserIdentity user, boolean excludePrivate)
053            throws ProcessingException, SAXException, UserPreferencesException
054    {
055        super._saxPreferences(storageContext, contextVars, user, excludePrivate);
056        _saxProjects(user);
057    }
058    
059    private void _saxProjects(UserIdentity user) throws SAXException
060    {
061        Map<Project, MemberType> userProjects = _projectManager.getUserProjects(user);
062        XMLUtils.startElement(contentHandler, "userProjects");
063        for (Entry<Project, MemberType> entry : userProjects.entrySet())
064        {
065            Project project = entry.getKey();
066            AttributesImpl attrs = new AttributesImpl();
067            attrs.addCDATAAttribute("id", project.getId());
068            attrs.addCDATAAttribute("name", project.getName());
069            
070            XMLUtils.startElement(contentHandler, "project", attrs);
071            
072            XMLUtils.createElement(contentHandler, "title", project.getTitle());
073            
074            String description = project.getDescription();
075            if (description != null)
076            {
077                XMLUtils.createElement(contentHandler, "description", description);
078            }
079            XMLUtils.endElement(contentHandler, "project");
080        }
081        XMLUtils.endElement(contentHandler, "userProjects");
082    }
083
084}