001    /**
002     * Copyright (c) 2000-2012 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.getConnection();
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)
058                    throws Exception {
059    
060                    Connection con = null;
061                    PreparedStatement ps = null;
062                    ResultSet rs = null;
063    
064                    try {
065                            con = DataAccess.getConnection();
066    
067                            ps = con.prepareStatement(
068                                    "select plid, privateLayout, layoutId, friendlyURL from " +
069                                            "Layout where groupId = ?");
070    
071                            ps.setLong(1, groupId);
072    
073                            rs = ps.executeQuery();
074    
075                            while (rs.next()) {
076                                    long plid = rs.getLong("plid");
077                                    boolean privateLayout = rs.getBoolean("privateLayout");
078                                    long layoutId = rs.getLong("layoutId");
079                                    String friendlyURL = rs.getString("friendlyURL");
080    
081                                    updateUUID(
082                                            plid, liveGroupId, privateLayout, layoutId, friendlyURL);
083                            }
084                    }
085                    finally {
086                            DataAccess.cleanUp(con, ps, rs);
087                    }
088            }
089    
090            protected void updateUUID(
091                            long plid, long groupId, boolean privateLayout, long layoutId,
092                            String friendlyURL)
093                    throws Exception {
094    
095                    Connection con = null;
096                    PreparedStatement ps = null;
097                    ResultSet rs = null;
098    
099                    try {
100                            con = DataAccess.getConnection();
101    
102                            ps = con.prepareStatement(
103                                    "select uuid_ from Layout where groupId = ? and friendlyURL " +
104                                            "= ?");
105    
106                            ps.setLong(1, groupId);
107                            ps.setString(2, friendlyURL);
108    
109                            rs = ps.executeQuery();
110    
111                            if (!rs.next()) {
112                                    ps = con.prepareStatement(
113                                            "select uuid_ from Layout where groupId = ? and " +
114                                                    "privateLayout = ? and layoutId = ?");
115    
116                                    ps.setLong(1, groupId);
117                                    ps.setBoolean(2, privateLayout);
118                                    ps.setLong(3, layoutId);
119    
120                                    rs = ps.executeQuery();
121    
122                                    if (!rs.next()) {
123                                            return;
124                                    }
125                            }
126    
127                            String uuid = rs.getString("uuid_");
128    
129                            runSQL(
130                                    "update Layout set uuid_ = '" + uuid + "' where plid = " +
131                                            plid);
132                    }
133                    finally {
134                            DataAccess.cleanUp(con, ps, rs);
135                    }
136            }
137    
138    }