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