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.v6_0_5;
016    
017    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
018    import com.liferay.portal.kernel.util.LoggingTimer;
019    
020    import java.sql.PreparedStatement;
021    import java.sql.ResultSet;
022    
023    /**
024     * @author Julio Camarero
025     * @author Brett Swaim
026     */
027    public class UpgradeLayout extends UpgradeProcess {
028    
029            @Override
030            protected void doUpgrade() throws Exception {
031                    updateUUIDs();
032            }
033    
034            protected void updateUUID(long groupId, long liveGroupId) throws Exception {
035                    try (PreparedStatement ps = connection.prepareStatement(
036                                    "select plid, privateLayout, layoutId, friendlyURL from " +
037                                            "Layout where groupId = ?")) {
038    
039                            ps.setLong(1, groupId);
040    
041                            try (ResultSet rs = ps.executeQuery()) {
042                                    while (rs.next()) {
043                                            long plid = rs.getLong("plid");
044                                            boolean privateLayout = rs.getBoolean("privateLayout");
045                                            long layoutId = rs.getLong("layoutId");
046                                            String friendlyURL = rs.getString("friendlyURL");
047    
048                                            updateUUID(
049                                                    plid, liveGroupId, privateLayout, layoutId,
050                                                    friendlyURL);
051                                    }
052                            }
053                    }
054            }
055    
056            protected void updateUUID(
057                            long plid, long groupId, boolean privateLayout, long layoutId,
058                            String friendlyURL)
059                    throws Exception {
060    
061                    try (PreparedStatement ps = connection.prepareStatement(
062                                    "select uuid_ from Layout where groupId = ? and friendlyURL " +
063                                            "= ?")) {
064    
065                            ps.setLong(1, groupId);
066                            ps.setString(2, friendlyURL);
067    
068                            try (ResultSet rs = ps.executeQuery()) {
069                                    if (!rs.next()) {
070                                            try (PreparedStatement ps2 = connection.prepareStatement(
071                                                            "select uuid_ from Layout where groupId = ? and " +
072                                                                    "privateLayout = ? and layoutId = ?")) {
073    
074                                                    ps2.setLong(1, groupId);
075                                                    ps2.setBoolean(2, privateLayout);
076                                                    ps2.setLong(3, layoutId);
077    
078                                                    try (ResultSet rs2 = ps2.executeQuery()) {
079                                                            if (!rs2.next()) {
080                                                                    return;
081                                                            }
082                                                    }
083                                            }
084                                    }
085    
086                                    String uuid = rs.getString("uuid_");
087    
088                                    runSQL(
089                                            "update Layout set uuid_ = '" + uuid + "' where plid = " +
090                                                    plid);
091                            }
092                    }
093            }
094    
095            protected void updateUUIDs() throws Exception {
096                    try (LoggingTimer loggingTimer = new LoggingTimer();
097                            PreparedStatement ps = connection.prepareStatement(
098                                    "select groupId, liveGroupId from Group_ where liveGroupId " +
099                                            "!= 0");
100                            ResultSet rs = ps.executeQuery()) {
101    
102                            while (rs.next()) {
103                                    long groupId = rs.getLong("groupId");
104                                    long liveGroupId = rs.getLong("liveGroupId");
105    
106                                    updateUUID(groupId, liveGroupId);
107                            }
108                    }
109            }
110    
111    }