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.components.ContextHelper;
024import org.apache.cocoon.environment.Request;
025
026import org.ametys.core.userpref.UserPreferencesEnumerator;
027import org.ametys.core.userpref.UserPreferencesExtensionPoint;
028import org.ametys.plugins.repository.AmetysObjectResolver;
029import org.ametys.web.WebConstants;
030import org.ametys.web.repository.page.Page;
031
032/**
033 * Enumerates front-office user preferences.
034 */
035public class FOUserPreferencesEnumerator extends UserPreferencesEnumerator
036{
037    
038    /** The ametys object resolver. */
039    protected AmetysObjectResolver _resolver;
040    
041    @Override
042    public void service(ServiceManager serviceManager) throws ServiceException
043    {
044        super.service(serviceManager);
045        _userPrefEP = (UserPreferencesExtensionPoint) serviceManager.lookup(UserPreferencesExtensionPoint.ROLE + ".FO");
046        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
047    }
048    
049    @Override
050    protected Map<String, String> getContextVars()
051    {
052        Map<String, String> contextVars = new HashMap<>(super.getContextVars());
053        
054        Request request = ContextHelper.getRequest(_context);
055        
056        String pageId = (String) request.getAttribute(WebConstants.REQUEST_ATTR_PAGE_ID);
057        Page page = _resolver.resolveById(pageId);
058        
059        String siteName = (String) request.getAttribute("siteName");
060        String language = page.getSitemapName();
061        
062        contextVars.put(FOUserPreferencesConstants.CONTEXT_VAR_SITENAME, siteName);
063        contextVars.put(FOUserPreferencesConstants.CONTEXT_VAR_LANGUAGE, language);
064        
065        return contextVars;
066    }
067    
068}