001/*
002 *  Copyright 2011 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.web.userpref;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.xml.AttributesImpl;
027import org.apache.cocoon.xml.XMLUtils;
028import org.apache.commons.lang.StringUtils;
029import org.xml.sax.SAXException;
030
031import org.ametys.core.user.UserIdentity;
032import org.ametys.core.userpref.UserPreference;
033import org.ametys.core.userpref.UserPreferencesException;
034import org.ametys.core.userpref.UserPreferencesExtensionPoint;
035import org.ametys.core.userpref.UserPreferencesManager;
036import org.ametys.plugins.core.userpref.UserPreferencesGenerator;
037import org.ametys.web.repository.page.Page;
038
039/**
040 * Generates front-office user preferences and the current page.
041 */
042public class FOUserPreferencesGenerator extends UserPreferencesGenerator
043{
044    @Override
045    public void service(ServiceManager serviceManager) throws ServiceException
046    {
047        super.service(serviceManager);
048        _userPrefEP = (UserPreferencesExtensionPoint) serviceManager.lookup(UserPreferencesExtensionPoint.ROLE + ".FO");
049        _userPrefManager = (UserPreferencesManager) serviceManager.lookup(UserPreferencesManager.ROLE + ".FO");
050    }
051    
052    @Override
053    protected UserIdentity getUser()
054    {
055        return  _currentUserProvider.getUser();
056    }
057    
058    @Override
059    protected Map<String, String> getContextVars(Request request)
060    {
061        Map<String, String> contextVars = new HashMap<>(super.getContextVars(request));
062        
063        String siteName = (String) request.getAttribute("site");
064        String language = (String) request.getAttribute("sitemapLanguage");
065        
066        contextVars.put(FOUserPreferencesConstants.CONTEXT_VAR_SITENAME, siteName);
067        contextVars.put(FOUserPreferencesConstants.CONTEXT_VAR_LANGUAGE, language);
068        
069        return contextVars;
070    }
071    
072    @Override
073    protected void _saxPreferences(String storageContext, Map<String, String> contextVars, UserIdentity user, boolean excludePrivate) throws ProcessingException, SAXException, UserPreferencesException
074    {
075        Request request = ObjectModelHelper.getRequest(objectModel);
076        Page page = (Page) request.getAttribute(Page.class.getName());
077        
078        // Generate page information to redirect back to it.
079        if (page != null)
080        {
081            AttributesImpl atts = new AttributesImpl();
082            atts.addCDATAAttribute("sitemap", page.getSitemapName());
083            atts.addCDATAAttribute("path", page.getSitemapName() + "/" + page.getPathInSitemap());
084            atts.addCDATAAttribute("name", page.getName());
085            atts.addCDATAAttribute("title", page.getTitle());
086            
087            XMLUtils.createElement(contentHandler, "page", atts);
088        }
089        
090        // Generate the user input to initialize the form.
091        _saxUserInput(contextVars);
092        
093        super._saxPreferences(storageContext, contextVars, user, excludePrivate);
094    }
095    
096    /**
097     * Generate the user input.
098     * @param contextVars The context variables including environment elements  
099     * @throws SAXException if an error occurs while saxing
100     * @throws ProcessingException if failed to get user preferences
101     */
102    protected void _saxUserInput(Map<String, String> contextVars) throws ProcessingException, SAXException
103    {
104        Request request = ObjectModelHelper.getRequest(objectModel);
105        XMLUtils.startElement(contentHandler, "UserInput");
106        
107        for (UserPreference preference : _userPrefEP.getUserPreferences(contextVars).values())
108        {
109            String id = preference.getId();
110            String[] values = request.getParameterValues(id);
111            if (values != null)
112            {
113                String value = StringUtils.join(values, ',');
114                
115                AttributesImpl atts = new AttributesImpl();
116                atts.addCDATAAttribute("id", id);
117                XMLUtils.createElement(contentHandler, "preference", atts, value);
118            }
119        }
120        
121        XMLUtils.endElement(contentHandler, "UserInput");
122    }
123    
124}