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.upgrade.UpgradeProcess;
018    import com.liferay.portal.kernel.util.LoggingTimer;
019    import com.liferay.portal.kernel.util.PortalUtil;
020    
021    import java.sql.PreparedStatement;
022    import java.sql.ResultSet;
023    
024    /**
025     * @author Wesley Gong
026     */
027    public class UpgradeGroup extends UpgradeProcess {
028    
029            @Override
030            protected void doUpgrade() throws Exception {
031                    updateParentGroupId();
032            }
033    
034            protected Object[] getLayout(long plid) throws Exception {
035                    try (PreparedStatement ps = connection.prepareStatement(_GET_LAYOUT)) {
036                            ps.setLong(1, plid);
037    
038                            try (ResultSet rs = ps.executeQuery()) {
039                                    if (rs.next()) {
040                                            long groupId = rs.getLong("groupId");
041    
042                                            return new Object[] {groupId};
043                                    }
044    
045                                    return null;
046                            }
047                    }
048            }
049    
050            protected void updateParentGroupId() throws Exception {
051                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
052                            long classNameId = PortalUtil.getClassNameId(
053                                    "com.liferay.portal.model.Layout");
054    
055                            try (PreparedStatement ps = connection.prepareStatement(
056                                            "select groupId, classPK from Group_ where classNameId = " +
057                                                    classNameId);
058                                    ResultSet rs = ps.executeQuery()) {
059    
060                                    while (rs.next()) {
061                                            long groupId = rs.getLong("groupId");
062                                            long classPK = rs.getLong("classPK");
063    
064                                            Object[] layout = getLayout(classPK);
065    
066                                            if (layout != null) {
067                                                    long layoutGroupId = (Long)layout[0];
068    
069                                                    runSQL(
070                                                            "update Group_ set parentGroupId = " +
071                                                                    layoutGroupId + " where groupId = " + groupId);
072                                            }
073                                    }
074                            }
075                    }
076            }
077    
078            private static final String _GET_LAYOUT =
079                    "select groupId from Layout where plid = ?";
080    
081    }