001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    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            @Override
031            protected void doUpgrade() throws Exception {
032                    updateCompany();
033                    updateLayoutSet();
034            }
035    
036            protected void addVirtualHost(
037                            long virtualHostId, long companyId, long layoutSetId,
038                            String hostname)
039                    throws Exception {
040    
041                    runSQL(
042                            "insert into VirtualHost (virtualHostId, companyId, layoutSetId, " +
043                                    "hostname) values (" + virtualHostId + ", " + companyId +
044                                            ", " + layoutSetId + ", '" + hostname + "')");
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.getConnection();
054    
055                            ps = con.prepareStatement(
056                                    "select companyId, virtualHost from Company where " +
057                                            "virtualHost != ? or 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.getConnection();
086    
087                            ps = con.prepareStatement(
088                                    "select layoutSetId, companyId, virtualHost from LayoutSet " +
089                                            "where virtualHost != ? or 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    }