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.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 Alexander Chow
026     */
027    public class UpgradeVirtualHost extends UpgradeProcess {
028    
029            protected void addVirtualHost(
030                            long virtualHostId, long companyId, long layoutSetId,
031                            String hostname)
032                    throws Exception {
033    
034                    if (hostname == null) {
035                            return;
036                    }
037    
038                    runSQL(
039                            "insert into VirtualHost (virtualHostId, companyId, layoutSetId, " +
040                                    "hostname) values (" + virtualHostId + ", " + companyId +
041                                            ", " + layoutSetId + ", '" + hostname + "')");
042            }
043    
044            @Override
045            protected void doUpgrade() throws Exception {
046                    updateCompany();
047                    updateLayoutSet();
048            }
049    
050            protected void updateCompany() throws Exception {
051                    Connection con = null;
052                    PreparedStatement ps = null;
053                    ResultSet rs = null;
054    
055                    try {
056                            con = DataAccess.getUpgradeOptimizedConnection();
057    
058                            ps = con.prepareStatement(
059                                    "select companyId, virtualHost from Company where " +
060                                            "virtualHost != ''");
061    
062                            rs = ps.executeQuery();
063    
064                            while (rs.next()) {
065                                    long companyId = rs.getLong("companyId");
066                                    String hostname = rs.getString("virtualHost");
067    
068                                    long virtualHostId = increment();
069    
070                                    addVirtualHost(virtualHostId, companyId, 0, hostname);
071                            }
072                    }
073                    finally {
074                            DataAccess.cleanUp(con, ps, rs);
075                    }
076    
077                    runSQL("alter table Company drop column virtualHost");
078            }
079    
080            protected void updateLayoutSet() throws Exception {
081                    Connection con = null;
082                    PreparedStatement ps = null;
083                    ResultSet rs = null;
084    
085                    try {
086                            con = DataAccess.getUpgradeOptimizedConnection();
087    
088                            ps = con.prepareStatement(
089                                    "select layoutSetId, companyId, virtualHost from LayoutSet " +
090                                            "where virtualHost != ''");
091    
092                            rs = ps.executeQuery();
093    
094                            while (rs.next()) {
095                                    long layoutSetId = rs.getLong("layoutSetId");
096                                    long companyId = rs.getLong("companyId");
097                                    String hostname = rs.getString("virtualHost");
098    
099                                    long virtualHostId = increment();
100    
101                                    addVirtualHost(virtualHostId, companyId, layoutSetId, hostname);
102                            }
103                    }
104                    finally {
105                            DataAccess.cleanUp(con, ps, rs);
106                    }
107    
108                    runSQL("alter table LayoutSet drop column virtualHost");
109            }
110    
111    }