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_1_0;
016    
017    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
018    import com.liferay.portal.kernel.util.LoggingTimer;
019    
020    import java.sql.PreparedStatement;
021    import java.sql.ResultSet;
022    
023    /**
024     * @author Alexander Chow
025     */
026    public class UpgradeVirtualHost extends UpgradeProcess {
027    
028            protected void addVirtualHost(
029                            long virtualHostId, long companyId, long layoutSetId,
030                            String hostname)
031                    throws Exception {
032    
033                    if (hostname == null) {
034                            return;
035                    }
036    
037                    runSQL(
038                            "insert into VirtualHost (virtualHostId, companyId, layoutSetId, " +
039                                    "hostname) values (" + virtualHostId + ", " + companyId + ", " +
040                                            layoutSetId + ", '" + hostname + "')");
041            }
042    
043            @Override
044            protected void doUpgrade() throws Exception {
045                    updateCompany();
046                    updateLayoutSet();
047            }
048    
049            protected void updateCompany() throws Exception {
050                    try (LoggingTimer loggingTimer = new LoggingTimer();
051                            PreparedStatement ps = connection.prepareStatement(
052                                    "select companyId, virtualHost from Company where " +
053                                            "virtualHost != ''");
054                            ResultSet rs = ps.executeQuery()) {
055    
056                            while (rs.next()) {
057                                    long companyId = rs.getLong("companyId");
058                                    String hostname = rs.getString("virtualHost");
059    
060                                    long virtualHostId = increment();
061    
062                                    addVirtualHost(virtualHostId, companyId, 0, hostname);
063                            }
064    
065                            runSQL("alter table Company drop column virtualHost");
066                    }
067            }
068    
069            protected void updateLayoutSet() throws Exception {
070                    try (LoggingTimer loggingTimer = new LoggingTimer();
071                            PreparedStatement ps = connection.prepareStatement(
072                                    "select layoutSetId, companyId, virtualHost from LayoutSet " +
073                                            "where virtualHost != ''");
074                            ResultSet rs = ps.executeQuery()) {
075    
076                            while (rs.next()) {
077                                    long layoutSetId = rs.getLong("layoutSetId");
078                                    long companyId = rs.getLong("companyId");
079                                    String hostname = rs.getString("virtualHost");
080    
081                                    long virtualHostId = increment();
082    
083                                    addVirtualHost(virtualHostId, companyId, layoutSetId, hostname);
084                            }
085    
086                            runSQL("alter table LayoutSet drop column virtualHost");
087                    }
088            }
089    
090    }