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