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_2_0;
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    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
021    import com.liferay.portal.upgrade.UpgradeProcessUtil;
022    
023    import java.sql.Connection;
024    import java.sql.PreparedStatement;
025    import java.sql.ResultSet;
026    import java.sql.Timestamp;
027    
028    /**
029     * @author Sergio Gonz??lez
030     */
031    public class UpgradeLayoutFriendlyURL extends UpgradeProcess {
032    
033            protected void addLayoutFriendlyURL(
034                            long groupId, long companyId, long userId, String userName,
035                            Timestamp createDate, Timestamp modifiedDate, long plid,
036                            boolean privateLayout, String friendlyURL)
037                    throws Exception {
038    
039                    Connection con = null;
040                    PreparedStatement ps = null;
041    
042                    try {
043                            con = DataAccess.getUpgradeOptimizedConnection();
044    
045                            StringBundler sb = new StringBundler(4);
046    
047                            sb.append("insert into LayoutFriendlyURL (uuid_, ");
048                            sb.append("layoutFriendlyURLId, groupId, companyId, userId, ");
049                            sb.append("userName, createDate, modifiedDate, plid, ");
050                            sb.append("privateLayout, friendlyURL, languageId) values (?, ?, ");
051                            sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
052    
053                            ps = con.prepareStatement(sb.toString());
054    
055                            ps.setString(1, PortalUUIDUtil.generate());
056                            ps.setLong(2, increment());
057                            ps.setLong(3, groupId);
058                            ps.setLong(4, companyId);
059                            ps.setLong(5, userId);
060                            ps.setString(6, userName);
061                            ps.setTimestamp(7, createDate);
062                            ps.setTimestamp(8, modifiedDate);
063                            ps.setLong(9, plid);
064                            ps.setBoolean(10, privateLayout);
065                            ps.setString(11, friendlyURL);
066                            ps.setString(
067                                    12, UpgradeProcessUtil.getDefaultLanguageId(companyId));
068    
069                            ps.executeUpdate();
070                    }
071                    finally {
072                            DataAccess.cleanUp(con, ps);
073                    }
074            }
075    
076            @Override
077            protected void doUpgrade() throws Exception {
078                    Connection con = null;
079                    PreparedStatement ps = null;
080                    ResultSet rs = null;
081    
082                    try {
083                            con = DataAccess.getUpgradeOptimizedConnection();
084    
085                            ps = con.prepareStatement(
086                                    "select plid, groupId, companyId, userId, userName, " +
087                                            "createDate, modifiedDate, privateLayout, friendlyURL " +
088                                                    "from Layout");
089    
090                            rs = ps.executeQuery();
091    
092                            while (rs.next()) {
093                                    long plid = rs.getLong("plid");
094                                    long groupId = rs.getLong("groupId");
095                                    long companyId = rs.getLong("companyId");
096                                    long userId = rs.getLong("userId");
097                                    String userName = rs.getString("userName");
098                                    Timestamp createDate = rs.getTimestamp("createDate");
099                                    Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
100                                    boolean privateLayout = rs.getBoolean("privateLayout");
101                                    String friendlyURL = rs.getString("friendlyURL");
102    
103                                    addLayoutFriendlyURL(
104                                            groupId, companyId, userId, userName, createDate,
105                                            modifiedDate, plid, privateLayout, friendlyURL);
106                            }
107                    }
108                    finally {
109                            DataAccess.cleanUp(con, ps, rs);
110                    }
111            }
112    
113    }