/**
 * Licensed to Jasig under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Jasig licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a
 * copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.jasig.portal.layout.profile;

import java.util.Collections;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.jasig.portal.security.IPerson;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.util.Assert;

/**
 * 
 * @author Jen Bourey, jennifer.bourey@gmail.com
 * @version $Revision$
 */
public class SessionAttributeProfileMapperImpl
    implements IProfileMapper, ApplicationListener<ProfileSelectionEvent> {

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * Since uPortal 4.2, instead of externally relying upon this key and hoping that a runtime
     * SessionAttributeProfileMapperImpl is not configured to use a different key,
     * consider instead firing a ProfileSelectionEvent and let this class store its data itself into the session.
     */
    public static final String DEFAULT_SESSION_ATTRIBUTE_NAME = "profileKey";

    private Map<String,String> mappings = Collections.<String,String>emptyMap();
    private String defaultProfileName = null;
    private String attributeName = DEFAULT_SESSION_ATTRIBUTE_NAME;
    
    /**
     * Session profile key to database profile fname mappings.
     */
    public void setMappings(Map<String,String> mappings) {
        this.mappings = mappings;
    }
    
    /**
     * Default profile name to return if no match is found, defaults to <code>null</code>.
     */
    public void setDefaultProfileName(String defaultProfileName) {
        this.defaultProfileName = defaultProfileName;
    }

    /**
     * 
     * @param attributeName
     */
    public void setAttributeName(String attributeName) {
        this.attributeName = attributeName;
    }
    
    @Override
    public String getProfileFname(IPerson person, HttpServletRequest request) {
        final HttpSession session = request.getSession(false);
        if (session == null) {
            logger.debug("Cannot get a session-stored profile fname from a null session, so returning null.");
            return null;
        }
        
        final String requestedProfileKey = (String) session.getAttribute(attributeName);
        if (requestedProfileKey != null) {
            final String profileName = mappings.get(requestedProfileKey);
            if (profileName != null) {
                logger.debug("The stored requested profile key {} mapped to profile fname {}.",
                        requestedProfileKey, profileName);
                return profileName;
            } else {
                logger.warn("The stored requested profile key {} does not map to any profile fname.",
                        requestedProfileKey);
            }
        } else {
            logger.trace("There is no requested profile key stored at session attribute {}.",
                    attributeName);
        }

        logger.trace("Falling back on default profile name {} .", defaultProfileName);
        return defaultProfileName;
    }

    /*
     * Store the requested profile key into the user Session so that this SessionAttributeProfileMapperImpl
     * can subsequently find it and use it to determine a profile mapping.
     */
    @Override
    public void onApplicationEvent(final ProfileSelectionEvent event) {
        final HttpSession session = event.getRequest().getSession(false);

        Assert.notNull(session, "Cannot store a profile selection into a null session.");

        session.setAttribute(this.attributeName, event.getRequestedProfileKey());

        logger.trace("Stored desired profile key [{}] into session (at attribute [{}]).",
                event.getRequestedProfileKey(), attributeName);

        final String fnameTheKeyMapsTo = this.mappings.get( event.getRequestedProfileKey() );

        if (null == fnameTheKeyMapsTo) {
            logger.warn("The desired profile key {} has no mapping so will have no effect.",
                    event.getRequestedProfileKey());
        }

    }

    @Override
    public String toString() {
        return getClass().getSimpleName() + " which considers session attribute [" + this.attributeName +
                "] as key to mappings [" + this.mappings +
                "], falling back on default [" + this.defaultProfileName + "] .";
    }

}

@@ -941,24 +941,136 @@
 tpSession;%0A%0A
+import org.apache.commons.lang3.builder.ToStringBuilder;%0Aimport org.apache.commons.lang3.builder.ToStringStyle;%0A
 import org.j
@@ -5006,63 +5006,98 @@
 urn 
-getClass().getSimpleName() + %22 which considers session 
+new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)%0A                     .append(%22
 attr
@@ -5101,21 +5101,22 @@
 ttribute
- %5B%22 +
+Name%22,
  this.at
@@ -5126,19 +5126,19 @@
 buteName
- +%0A
+)%0A 
         
@@ -5149,29 +5149,29 @@
     
-%22%5D as key to 
+    .append(%22
 mappings
  %5B%22 
@@ -5166,21 +5166,18 @@
 mappings
- %5B%22 +
+%22,
  this.ma
@@ -5186,11 +5186,13 @@
 ings
- +%0A
+)%0A   
     
@@ -5207,40 +5207,39 @@
     
-%22%5D, falling back on default %5B%22 +
+  .append(%22defaultProfileName%22,
  thi
@@ -5262,16 +5262,20 @@
 Name
- + %22%5D .%22
+).toString()
 ;%0A  

Change SessionAttributeProfileMapperImpl toString() to use ToStringBuilder().