/**
 * Copyright  2003 The JA-SIG Collaborative.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the JA-SIG Collaborative
 *    (http://www.jasig.org/)."
 *
 * THIS SOFTWARE IS PROVIDED BY THE JA-SIG COLLABORATIVE "AS IS" AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE JA-SIG COLLABORATIVE OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

package org.jasig.portal.layout.utils;

import java.util.Enumeration;
import java.util.Hashtable;
import java.sql.Connection;
import java.sql.Statement;

import org.jasig.portal.IUserLayoutStore;
import org.jasig.portal.UserLayoutStoreFactory;
import org.jasig.portal.UserProfile;
import org.jasig.portal.RDBMServices;

/**
 * The aggregated layout migration utility
 * @author <a href="mailto:mvi@immagic.com">Michael Ivanov</a>
 * @version $Revision$
 */
public class ALMigrationUtil {

  private static final int ADD = 1;
  private static final int DELETE = 2;
  private static final int DEFAULT_SS_ID = 1;
  private static IUserLayoutStore ulsdb = null;

  public ALMigrationUtil() {
    if ( ulsdb == null )
     ulsdb = UserLayoutStoreFactory.getUserLayoutStoreImpl();
  }

  public void registerStylesheet ( String stylesheetURI, String stylesheetDescriptionURI, int stylesheetId, boolean isTheme, int command ) {
        try {
            setInitialStructureId( command );
            switch ( command ) {
             case DELETE:
               if ( stylesheetId > 1 ) {
                 updateUserProfile ( DEFAULT_SS_ID );
                 if (isTheme)
                    ulsdb.removeThemeStylesheetDescription(stylesheetId);
                 else
                    ulsdb.removeStructureStylesheetDescription(stylesheetId);
               }
                break;
             case ADD:
                Integer id = null;
                if (isTheme)
                    id = ulsdb.addThemeStylesheetDescription(stylesheetDescriptionURI, stylesheetURI);
                else
                    id = ulsdb.addStructureStylesheetDescription(stylesheetDescriptionURI, stylesheetURI);

                if (id == null) {
                    System.out.println("Save failed: stylsheet ID = null");
                    return;
                } else {
                    stylesheetId = id.intValue();
                    System.out.println("Save successfull! The new " + ((isTheme)?"theme":"structure") + " stylesheet was assigned with Id="+id);
                  }
               updateUserProfile ( stylesheetId );
               break;
               default:
            }

        } catch (Exception e) {
            System.out.println("registerStylesheet: An error has been encountered:");
            e.printStackTrace();
        }

  }


  public synchronized void setInitialStructureId( int command ) throws Exception {
        Connection con = RDBMServices.getConnection();
        try {
            RDBMServices.setAutoCommit(con, false);
            Statement stmt = con.createStatement();
            System.out.println( "Updating the initial structure ID...");
            try {
               String sql = null;
               switch ( command ) {
                case ADD:
                 sql = "UPDATE UP_USER_LAYOUT SET INIT_STRUCT_ID = 1 WHERE USER_ID IN (1,2)";
                 stmt.executeUpdate(sql);
                 break;
                case DELETE:
                 sql = "UPDATE UP_USER_LAYOUT SET INIT_STRUCT_ID = 20 WHERE USER_ID = 1";
                 stmt.executeUpdate(sql);
                 sql = "UPDATE UP_USER_LAYOUT SET INIT_STRUCT_ID = 9 WHERE USER_ID = 2";
                 stmt.executeUpdate(sql);
                 break;
               }
                 RDBMServices.commit(con);
                 System.out.println( "The initial structure ID successfully updated.");
            } catch (Exception e) {
                RDBMServices.rollback(con);
                e.printStackTrace();
            } finally {
                stmt.close();
            }
        } finally {
            RDBMServices.releaseConnection(con);
        }
    }



  public void updateUserProfile ( int stylesheetId ) {
   try {
      System.out.println("Updating the system profile...");
      Hashtable profileList = ulsdb.getSystemProfileList();
      if ( profileList != null )
       for ( Enumeration profiles = profileList.elements(); profiles.hasMoreElements(); ) {
         UserProfile profile = (UserProfile) profiles.nextElement();
         profile.setStructureStylesheetId(stylesheetId);
         profile.setThemeStylesheetId(stylesheetId);
         ulsdb.updateSystemProfile(profile);
       }
      System.out.println("The profile is succesfully updated");
   } catch (Exception e) {
         System.out.println("updateUserProfile: An error has been encountered:");
         e.printStackTrace();
        }
  }

  public int getSystemStylesheetId() {
    try {
      Hashtable profileList = ulsdb.getSystemProfileList();
      if ( profileList != null )
       for ( Enumeration profiles = profileList.elements(); profiles.hasMoreElements(); ) {
         UserProfile profile = (UserProfile) profiles.nextElement();
         return profile.getStructureStylesheetId();
       }
    } catch (Exception e) {
         System.out.println("getSystemStylesheetId: An error has been encountered:");
         e.printStackTrace();
        }
      return -1;
  }

  public static void main(String[] args) {

    if ( args.length != 1 ) {
     System.out.println("Usage: ALMigrationUtil -Daction=[add|delete]");
     return;
    }

    String themeURI = "/org/jasig/portal/layout/AL_TabColumn/integratedModes/integratedModes.xsl";
    String themeDescriptionURI = "/org/jasig/portal/layout/AL_TabColumn/integratedModes/integratedModes.sdf";
    String structureURI = "/org/jasig/portal/layout/AL_TabColumn/AL_TabColumn.xsl";
    String structureDescriptionURI = "/org/jasig/portal/layout/AL_TabColumn/AL_TabColumn.sdf";
    int command = ("delete".equalsIgnoreCase(args[0]))?DELETE:ADD;

    ALMigrationUtil al = new ALMigrationUtil();
    int stylesheetId = al.getSystemStylesheetId();
    al.registerStylesheet( structureURI, structureDescriptionURI, stylesheetId, false, command );
    al.registerStylesheet( themeURI, themeDescriptionURI, stylesheetId, true, command );

  }
}



@@ -1694,63 +1694,8 @@
 ble;
-%0Aimport java.sql.Connection;%0Aimport java.sql.Statement;
 %0A%0Aim
@@ -1822,46 +1822,8 @@
 ile;
-%0Aimport org.jasig.portal.RDBMServices;
 %0A%0A/*
@@ -2439,54 +2439,8 @@
 y %7B%0A
-            setInitialStructureId( command );%0A
     
@@ -3808,1354 +3808,13 @@
  %7D%0A%0A
+
   %7D%0A%0A
-%0A  public synchronized void setInitialStructureId( int command ) throws Exception %7B%0A        Connection con = RDBMServices.getConnection();%0A        try %7B%0A            RDBMServices.setAutoCommit(con, false);%0A            Statement stmt = con.createStatement();%0A            System.out.println( %22Updating the initial structure ID...%22);%0A            try %7B%0A               String sql = null;%0A               switch ( command ) %7B%0A                case ADD:%0A                 sql = %22UPDATE UP_USER_LAYOUT SET INIT_STRUCT_ID = 1 WHERE USER_ID IN (1,2)%22;%0A                 stmt.executeUpdate(sql);%0A                 break;%0A                case DELETE:%0A                 sql = %22UPDATE UP_USER_LAYOUT SET INIT_STRUCT_ID = 20 WHERE USER_ID = 1%22;%0A                 stmt.executeUpdate(sql);%0A                 sql = %22UPDATE UP_USER_LAYOUT SET INIT_STRUCT_ID = 9 WHERE USER_ID = 2%22;%0A                 stmt.executeUpdate(sql);%0A                 break;%0A               %7D%0A                 RDBMServices.commit(con);%0A                 System.out.println( %22The initial structure ID successfully updated.%22);%0A            %7D catch (Exception e) %7B%0A                RDBMServices.rollback(con);%0A                e.printStackTrace();%0A            %7D finally %7B%0A                stmt.close();%0A            %7D%0A        %7D finally %7B%0A            RDBMServices.releaseConnection(con);%0A        %7D%0A    %7D%0A%0A%0A%0A
   pu

Undo addition of method that changes initial structure IDs. The default layout data now has the right initial structure IDs of 1 for each default user.