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