UserProfileChanger

I don't know that this is the right approach for everyone, but I wrote this ServletContextListener to apply the user profile changes necessary when trying out different layout managers.

The source code

UserProfileChanger
/* Copyright 2005 The JA-SIG Collaborative.  All rights reserved.
*  See license distributed with this file and
*  available online at http://www.uportal.org/license.html
*/

package org.jasig.portal.layout;

import java.sql.Types;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.sql.DataSource;

import org.jasig.portal.RDBMServices;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.SqlUpdate;

/**
 * A ContextListener that at Context initialization updates the
 * UP_USER_PROFILE table to set the appropriate structure and theme
 * stylesheets for the layout management scheme.
 * @version $Revision:$ $Date:$
 */
public class UserProfileChanger implements ServletContextListener {

    /**
     * The name of the servlet context parameter the value of which should be the Integer keying to the
     * structure stylesheet we would like to use.
     */
    public String DESIRED_STRUC_SS_ID_PARAM = "org.jasig.portal.layout.UserProfileChanger.STRUCT_SS_ID";
    
    /**
     * The name of the servlet context parameter the value of which should be the Integer keying to the
     * theme stylesheet we would like to use.
     */
    public String DESIRED_THEME_SS_ID_PARAM = "org.jasig.portal.layout.UserProfileChanger.DESIRED_THEME_SS_ID";
    
    
    /**
     * The name of the servlet context parameter the value of which should be the Integer keying to the
     * theme stylesheet we are replacing.
     */
    public String CURRENT_THEME_SS_ID_PARAM = "org.jasig.portal.layout.UserProfileChanger.CURRENT_THEME_SS_ID";
    
    public void contextInitialized(ServletContextEvent sce) {
        
        DataSource ds = RDBMServices.getDataSource();
        
        SqlUpdate update = new SqlUpdate();
        update.setDataSource(ds);
        update.setSql("UPDATE up_user_profile " +
                "SET structure_ss_id = ?, " +
                "theme_ss_id = ?" +
                "WHERE theme_ss_id = ?");
        update.declareParameter(new SqlParameter("structure_ss_id", Types.INTEGER));
        update.declareParameter(new SqlParameter("theme_ss_id", Types.INTEGER));
        update.declareParameter(new SqlParameter("theme_ss_id", Types.INTEGER));
        update.compile();
        
        Integer[] arguments = new Integer[3];
        
        Integer desiredStructStylesheetId = Integer.valueOf(sce.getServletContext().getInitParameter(DESIRED_STRUC_SS_ID_PARAM));
        Integer desiredThemeStylesheetId = Integer.valueOf(sce.getServletContext().getInitParameter(DESIRED_THEME_SS_ID_PARAM));
        Integer currentThemeStylesheetId = Integer.valueOf(sce.getServletContext().getInitParameter(CURRENT_THEME_SS_ID_PARAM));
        
        arguments[0] = desiredStructStylesheetId;
        arguments[1] = desiredThemeStylesheetId;
        arguments[2] = currentThemeStylesheetId;
        
        int rowsModified = update.update(arguments);
        
        System.out.println("Updated " + rowsModified + 
                " rows of uP_user_profile to change from theme " + 
                currentThemeStylesheetId + " to theme " + 
                desiredThemeStylesheetId + " and structure " + 
                desiredStructStylesheetId);
    }

    public void contextDestroyed(ServletContextEvent sce) {
        // do nothing
    }
}

Mapping and configuring

Then I map the listener in web.xml and configure it for the change I'm looking to make, according to the recipe Mark posted.:

	<context-param>
		<param-name>org.jasig.portal.layout.UserProfileChanger.STRUCT_SS_ID</param-name>
		<param-value>1</param-value>
	</context-param>
	
	<context-param>
		<param-name>org.jasig.portal.layout.UserProfileChanger.DESIRED_THEME_SS_ID</param-name>
		<param-value>1</param-value>
	</context-param>
	
	<context-param>
		<param-name>org.jasig.portal.layout.UserProfileChanger.CURRENT_THEME_SS_ID</param-name>
		<param-value>11</param-value>
	</context-param>
	
	<listener>
		<listener-class>org.jasig.portal.layout.UserProfileChanger</listener-class>
	</listener>