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