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_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            protected void addVirtualHost(
032                            long virtualHostId, long companyId, long layoutSetId,
033                            String hostname)
034                    throws Exception {
035    
036                    if (Validator.isNull(hostname)) {
037                            return;
038                    }
039    
040                    runSQL(
041                            "insert into VirtualHost (virtualHostId, companyId, layoutSetId, " +
042                                    "hostname) values (" + virtualHostId + ", " + companyId +
043                                            ", " + layoutSetId + ", '" + hostname + "')");
044            }
045    
046            @Override
047            protected void doUpgrade() throws Exception {
048                    updateCompany();
049                    updateLayoutSet();
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    }