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