001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }