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