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