001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.upgrade.v5_2_5_to_6_0_0;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.model.Layout;
020    import com.liferay.portal.util.PortalUtil;
021    
022    import java.sql.Connection;
023    import java.sql.PreparedStatement;
024    import java.sql.ResultSet;
025    
026    /**
027     * @author Wesley Gong
028     */
029    public class UpgradeGroup extends UpgradeProcess {
030    
031            protected void doUpgrade() throws Exception {
032                    updateParentGroupId();
033            }
034    
035            protected Object[] getLayout(long plid) throws Exception {
036                    Object[] layout = null;
037    
038                    Connection con = null;
039                    PreparedStatement ps = null;
040                    ResultSet rs = null;
041    
042                    try {
043                            con = DataAccess.getConnection();
044    
045                            ps = con.prepareStatement(_GET_LAYOUT);
046    
047                            ps.setLong(1, plid);
048    
049                            rs = ps.executeQuery();
050    
051                            while (rs.next()) {
052                                    long groupId = rs.getLong("groupId");
053    
054                                    layout = new Object[] {groupId};
055                            }
056                    }
057                    finally {
058                            DataAccess.cleanUp(con, ps, rs);
059                    }
060    
061                    return layout;
062            }
063    
064            protected void updateParentGroupId() throws Exception {
065                    Connection con = null;
066                    PreparedStatement ps = null;
067                    ResultSet rs = null;
068    
069                    try {
070                            con = DataAccess.getConnection();
071    
072                            long classNameId = PortalUtil.getClassNameId(
073                                    Layout.class.getName());
074    
075                            ps = con.prepareStatement(
076                                    "select groupId, classPK from Group_ where classNameId = " +
077                                            classNameId);
078    
079                            rs = ps.executeQuery();
080    
081                            while (rs.next()) {
082                                    long groupId = rs.getLong("groupId");
083                                    long classPK = rs.getLong("classPK");
084    
085                                    Object[] layout = getLayout(classPK);
086    
087                                    if (layout != null) {
088                                            long layoutGroupId = (Long)layout[0];
089    
090                                            runSQL(
091                                                    "update Group_ set parentGroupId = " + layoutGroupId +
092                                                            " where groupId = " + groupId);
093                                    }
094                            }
095                    }
096                    finally {
097                            DataAccess.cleanUp(con, ps, rs);
098                    }
099            }
100    
101            private static final String _GET_LAYOUT =
102                    "select groupId from Layout where plid = ?";
103    
104    }