/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package com.xpn.xwiki.plugin.activitystream.migration.hibernate;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.inject.Named;
import javax.inject.Singleton;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.jdbc.Work;
import org.xwiki.component.annotation.Component;

import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback;
import com.xpn.xwiki.store.migration.hibernate.AbstractHibernateDataMigration;
import com.xpn.xwiki.store.migration.DataMigrationException;
import com.xpn.xwiki.store.migration.XWikiDBVersion;

/**
 * Migration for XWIKI-6691: Change the mapping of the event requestId field to varchar(48). Since the even group ID is
 * always a GUID of length at most 36, it doesn't make sense to allocate 2000 characters for it. Since Hibernate cannot
 * alter existing columns to change their types, and we don't want to lose all the existing data in case of a migration,
 * we create a new column with the right type, and manually copy data between columns. After that we try to drop the old
 * column to free up some space.
 * 
 * @version $Id$
 * @since 3.5M1
 */
@Component
@Named("R35000XWIKI6691")
@Singleton
public class R35000XWIKI6691DataMigration extends AbstractHibernateDataMigration
{
    @Override
    public String getDescription()
    {
        return "See http://jira.xwiki.org/browse/XWIKI-6691";
    }

    @Override
    public XWikiDBVersion getVersion()
    {
        return new XWikiDBVersion(35000);
    }

    @Override
    public void hibernateMigrate() throws DataMigrationException, XWikiException
    {
        getStore().executeWrite(getXWikiContext(), true, new HibernateCallback<Object>()
        {
            @Override
            public Object doInHibernate(Session session) throws HibernateException, XWikiException
            {
                session.doWork(new Work()
                {
                    @Override
                    public void execute(Connection connection) throws SQLException
                    {
                        Statement stmt = connection.createStatement();
                        stmt.addBatch("UPDATE activitystream_events SET ase_groupid = ase_requestid");
                        stmt.addBatch("ALTER TABLE activitystream_events DROP COLUMN ase_requestid");
                        stmt.executeBatch();
                    }
                });
                return Boolean.TRUE;
            }
        });
    }
}

@@ -1036,24 +1036,52 @@
 Statement;%0A%0A
+import javax.inject.Inject;%0A
 import javax
@@ -1230,16 +1230,41 @@
 c.Work;%0A
+import org.slf4j.Logger;%0A
 import o
@@ -1416,87 +1416,8 @@
 ck;%0A
-import com.xpn.xwiki.store.migration.hibernate.AbstractHibernateDataMigration;%0A
 impo
@@ -1525,16 +1525,95 @@
 Version;
+%0Aimport com.xpn.xwiki.store.migration.hibernate.AbstractHibernateDataMigration;
 %0A%0A/**%0A *
@@ -2297,16 +2297,97 @@
 ation%0A%7B%0A
+    /** Logging helper object. */%0A    @Inject%0A    private static Logger logger;%0A%0A
     @Ove
@@ -2984,14 +2984,36 @@
 new 
+RequestToGroupRename
 Work()
+);
 %0A   
@@ -3017,33 +3017,52 @@
                 
-%7B
+return Boolean.TRUE;
 %0A               
@@ -3055,24 +3055,29 @@
         
+   %7D%0A
         
    @Over
@@ -3072,33 +3072,280 @@
     
-   @Override
+%7D);%0A    %7D%0A%0A    /**%0A     * Hibernate %22Work%22 class responsible for moving data from the old ase_requestid column to the new ase_groupid%0A     * column.%0A     * %0A     * @version $Id$%0A     */%0A    private static final class RequestToGroupRenameWork implements Work
 %0A    
+%7B%0A
         
     
@@ -3332,32 +3332,42 @@
 k%0A    %7B%0A        
+@Override%0A
         public v
@@ -3433,34 +3433,10 @@
     
-      
-      %7B%0A            
+%7B%0A
     
@@ -3498,36 +3498,24 @@
             
-  
-          
 stmt.addBatc
@@ -3589,36 +3589,24 @@
             
-           
- 
 stmt.addBatc
@@ -3671,32 +3671,42 @@
 stid%22);%0A        
+    try %7B%0A
                 
@@ -3742,73 +3742,351 @@
     
-        %7D%0A                %7D);%0A                return Boolean.TRUE
+%7D catch (SQLException ex) %7B%0A                // Ignore, probably the database doesn't need this migration.%0A                // Anyway, in case it really can't be performed, report it in the logs.%0A                R35000XWIKI6691DataMigration.logger.warn(%0A                    %22Failed to apply R35000XWIKI6691 Data Migration : %7B%7D%22, ex.getMessage())
 ;%0A  
@@ -4102,23 +4102,21 @@
  %7D%0A        %7D
-);
 %0A    %7D%0A%7D%0A

Change the mapping of the event requestId field to varchar(48) Fixed migrator error when running on a new HSQLDB database