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.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            @Override
041            protected void doUpgrade() throws Exception {
042                    UpgradeColumn locationColumn = new TempUpgradeColumnImpl(
043                            "location", new Integer(Types.BOOLEAN));
044    
045                    UpgradeColumn typeColumn = new OrganizationTypeUpgradeColumnImpl(
046                            locationColumn);
047    
048                    Object[][] organizationColumns1 =
049                            {{"location", new Integer(Types.BOOLEAN)}};
050                    Object[][] organizationColumns2 =
051                            OrganizationTable.TABLE_COLUMNS.clone();
052    
053                    Object[][] organizationColumns = ArrayUtil.append(
054                            organizationColumns1, organizationColumns2);
055    
056                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
057                            OrganizationTable.TABLE_NAME, organizationColumns, locationColumn,
058                            typeColumn);
059    
060                    upgradeTable.updateTable();
061    
062                    updateLocationResources();
063            }
064    
065            protected long getCodeId(long companyId, String name, int scope)
066                    throws Exception {
067    
068                    Connection con = null;
069                    PreparedStatement ps = null;
070                    ResultSet rs = null;
071    
072                    try {
073                            con = DataAccess.getConnection();
074    
075                            ps = con.prepareStatement(
076                                    "select codeId from ResourceCode where companyId = ? and " +
077                                            "name = ? and scope = ?");
078    
079                            ps.setLong(1, companyId);
080                            ps.setString(2, name);
081                            ps.setInt(3, scope);
082    
083                            rs = ps.executeQuery();
084    
085                            if (rs.next()) {
086                                    return rs.getLong("codeId");
087                            }
088    
089                            return 0;
090                    }
091                    finally {
092                            DataAccess.cleanUp(con, ps, rs);
093                    }
094            }
095    
096            protected void updateCodeId(long companyId, int scope) throws Exception {
097                    long oldCodeId = getCodeId(
098                            companyId, "com.liferay.portal.model.Location", scope);
099                    long newCodeId = getCodeId(
100                            companyId, "com.liferay.portal.model.Organization", scope);
101    
102                    runSQL(
103                            "update Resource_ set codeId = " + newCodeId + " where codeId = " +
104                                    oldCodeId);
105    
106                    runSQL("delete from ResourceCode where codeId = " + oldCodeId);
107            }
108    
109            protected void updateLocationResources() throws Exception {
110                    long[] companyIds = PortalInstances.getCompanyIdsBySQL();
111    
112                    for (long companyId : companyIds) {
113                            for (int scope : ResourceConstants.SCOPES) {
114                                    updateCodeId(companyId, scope);
115                            }
116                    }
117            }
118    
119    }