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_1_1;
016    
017    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
018    import com.liferay.portal.kernel.util.LoggingTimer;
019    import com.liferay.portal.kernel.util.StringBundler;
020    
021    import java.sql.PreparedStatement;
022    import java.sql.ResultSet;
023    
024    /**
025     * @author Jorge Ferrer
026     */
027    public class UpgradeLayout extends UpgradeProcess {
028    
029            @Override
030            protected void doUpgrade() throws Exception {
031                    updateSourcePrototypeLayoutUuid();
032            }
033    
034            protected long getLayoutPrototypeGroupId(String layoutPrototypeUuid)
035                    throws Exception {
036    
037                    try (PreparedStatement ps = connection.prepareStatement(
038                                    "select groupId from Group_ where classPK = (select " +
039                                            "layoutPrototypeId from LayoutPrototype where uuid_ = " +
040                                                    "?)")) {
041    
042                            ps.setString(1, layoutPrototypeUuid);
043    
044                            try (ResultSet rs = ps.executeQuery()) {
045                                    while (rs.next()) {
046                                            long groupId = rs.getLong("groupId");
047    
048                                            return groupId;
049                                    }
050                            }
051                    }
052    
053                    return 0;
054            }
055    
056            protected boolean isGroupPrivateLayout(
057                            long groupId, String sourcePrototypeLayoutUuid)
058                    throws Exception {
059    
060                    try (PreparedStatement ps = connection.prepareStatement(
061                                    "select count(*) from Layout where uuid_ = ? and groupId = ? " +
062                                            "and privateLayout = ?")) {
063    
064                            ps.setString(1, sourcePrototypeLayoutUuid);
065                            ps.setLong(2, groupId);
066                            ps.setBoolean(3, true);
067    
068                            try (ResultSet rs = ps.executeQuery()) {
069                                    while (rs.next()) {
070                                            int count = rs.getInt(1);
071    
072                                            if (count > 0) {
073                                                    return true;
074                                            }
075                                            else {
076                                                    return false;
077                                            }
078                                    }
079                            }
080                    }
081    
082                    return false;
083            }
084    
085            protected void updateSourcePrototypeLayoutUuid() throws Exception {
086                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
087                            StringBundler sb = new StringBundler(4);
088    
089                            sb.append("select plid, layoutPrototypeUuid, ");
090                            sb.append("sourcePrototypeLayoutUuid from Layout where ");
091                            sb.append("layoutPrototypeUuid != '' and ");
092                            sb.append("sourcePrototypeLayoutUuid != ''");
093    
094                            try (PreparedStatement ps = connection.prepareStatement(
095                                            sb.toString());
096                                    ResultSet rs = ps.executeQuery()) {
097    
098                                    // Get pages with a sourcePrototypeLayoutUuid that have a page
099                                    // template. If the layoutUuid points to a page template, remove
100                                    // it. Otherwise, it points to a site template page, so leave
101                                    // it.
102    
103                                    while (rs.next()) {
104                                            long plid = rs.getLong("plid");
105                                            String layoutPrototypeUuid = rs.getString(
106                                                    "layoutPrototypeUuid");
107                                            String sourcePrototypeLayoutUuid = rs.getString(
108                                                    "sourcePrototypeLayoutUuid");
109    
110                                            long groupId = getLayoutPrototypeGroupId(
111                                                    layoutPrototypeUuid);
112    
113                                            if (groupId == 0) {
114                                                    continue;
115                                            }
116    
117                                            if (isGroupPrivateLayout(
118                                                            groupId, sourcePrototypeLayoutUuid)) {
119    
120                                                    runSQL(
121                                                            "update Layout set sourcePrototypeLayoutUuid = " +
122                                                                    "null where plid = " + plid);
123                                            }
124                                    }
125                            }
126                    }
127            }
128    
129    }