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