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