001    /**
002     * Copyright (c) 2000-present 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.kernel.util.PortalUtil;
020    
021    import java.sql.Connection;
022    import java.sql.PreparedStatement;
023    import java.sql.ResultSet;
024    
025    /**
026     * @author Wesley Gong
027     */
028    public class UpgradeGroup extends UpgradeProcess {
029    
030            @Override
031            protected void doUpgrade() throws Exception {
032                    updateParentGroupId();
033            }
034    
035            protected Object[] getLayout(long plid) throws Exception {
036                    Connection con = null;
037                    PreparedStatement ps = null;
038                    ResultSet rs = null;
039    
040                    try {
041                            con = DataAccess.getUpgradeOptimizedConnection();
042    
043                            ps = con.prepareStatement(_GET_LAYOUT);
044    
045                            ps.setLong(1, plid);
046    
047                            rs = ps.executeQuery();
048    
049                            if (rs.next()) {
050                                    long groupId = rs.getLong("groupId");
051    
052                                    return new Object[] {groupId};
053                            }
054    
055                            return null;
056                    }
057                    finally {
058                            DataAccess.cleanUp(con, ps, rs);
059                    }
060            }
061    
062            protected void updateParentGroupId() throws Exception {
063                    Connection con = null;
064                    PreparedStatement ps = null;
065                    ResultSet rs = null;
066    
067                    try {
068                            con = DataAccess.getUpgradeOptimizedConnection();
069    
070                            long classNameId = PortalUtil.getClassNameId(
071                                    "com.liferay.portal.model.Layout");
072    
073                            ps = con.prepareStatement(
074                                    "select groupId, classPK from Group_ where classNameId = " +
075                                            classNameId);
076    
077                            rs = ps.executeQuery();
078    
079                            while (rs.next()) {
080                                    long groupId = rs.getLong("groupId");
081                                    long classPK = rs.getLong("classPK");
082    
083                                    Object[] layout = getLayout(classPK);
084    
085                                    if (layout != null) {
086                                            long layoutGroupId = (Long)layout[0];
087    
088                                            runSQL(
089                                                    "update Group_ set parentGroupId = " + layoutGroupId +
090                                                            " where groupId = " + groupId);
091                                    }
092                            }
093                    }
094                    finally {
095                            DataAccess.cleanUp(con, ps, rs);
096                    }
097            }
098    
099            private static final String _GET_LAYOUT =
100                    "select groupId from Layout where plid = ?";
101    
102    }