001    /**
002     * Copyright (c) 2000-2010 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.v5_2_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.upgrade.util.TempUpgradeColumnImpl;
020    import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
021    import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
022    import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
023    import com.liferay.portal.kernel.util.ArrayUtil;
024    import com.liferay.portal.model.ResourceConstants;
025    import com.liferay.portal.upgrade.v5_2_0.util.OrganizationTable;
026    import com.liferay.portal.upgrade.v5_2_0.util.OrganizationTypeUpgradeColumnImpl;
027    import com.liferay.portal.util.PortalInstances;
028    
029    import java.sql.Connection;
030    import java.sql.PreparedStatement;
031    import java.sql.ResultSet;
032    import java.sql.Types;
033    
034    /**
035     * @author Jorge Ferrer
036     * @author Edward Shin
037     */
038    public class UpgradeOrganization extends UpgradeProcess {
039    
040            protected void doUpgrade() throws Exception {
041                    UpgradeColumn locationColumn = new TempUpgradeColumnImpl(
042                            "location", new Integer(Types.BOOLEAN));
043    
044                    UpgradeColumn typeColumn = new OrganizationTypeUpgradeColumnImpl(
045                            locationColumn);
046    
047                    Object[][] organizationColumns1 =
048                            {{"location", new Integer(Types.BOOLEAN)}};
049                    Object[][] organizationColumns2 =
050                            OrganizationTable.TABLE_COLUMNS.clone();
051    
052                    Object[][] organizationColumns = ArrayUtil.append(
053                            organizationColumns1, organizationColumns2);
054    
055                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
056                            OrganizationTable.TABLE_NAME, organizationColumns, locationColumn,
057                            typeColumn);
058    
059                    upgradeTable.updateTable();
060    
061                    updateLocationResources();
062            }
063    
064            protected long getCodeId(long companyId, String name, int scope)
065                    throws Exception {
066    
067                    long codeId = 0;
068    
069                    Connection con = null;
070                    PreparedStatement ps = null;
071                    ResultSet rs = null;
072    
073                    try {
074                            con = DataAccess.getConnection();
075    
076                            ps = con.prepareStatement(
077                                    "select codeId from ResourceCode where companyId = ? and " +
078                                            "name = ? and scope = ?");
079    
080                            ps.setLong(1, companyId);
081                            ps.setString(2, name);
082                            ps.setInt(3, scope);
083    
084                            rs = ps.executeQuery();
085    
086                            if (rs.next()) {
087                                    codeId = rs.getLong("codeId");
088                            }
089                    }
090                    finally {
091                            DataAccess.cleanUp(con, ps, rs);
092                    }
093    
094                    return codeId;
095            }
096    
097            protected void updateCodeId(long companyId, int scope) throws Exception {
098                    long oldCodeId = getCodeId(
099                            companyId, "com.liferay.portal.model.Location", scope);
100                    long newCodeId = getCodeId(
101                            companyId, "com.liferay.portal.model.Organization", scope);
102    
103                    runSQL(
104                            "update Resource_ set codeId = " + newCodeId + " where codeId = " +
105                                    oldCodeId);
106    
107                    runSQL("delete from ResourceCode where codeId = " + oldCodeId);
108            }
109    
110            protected void updateLocationResources() throws Exception {
111                    long[] companyIds = PortalInstances.getCompanyIdsBySQL();
112    
113                    for (long companyId : companyIds) {
114                            for (int scope : ResourceConstants.SCOPES) {
115                                    updateCodeId(companyId, scope);
116                            }
117                    }
118            }
119    
120    }