1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.upgrade.v5_2_0;
16  
17  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
18  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
19  import com.liferay.portal.kernel.upgrade.util.TempUpgradeColumnImpl;
20  import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
21  import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
22  import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
23  import com.liferay.portal.kernel.util.ArrayUtil;
24  import com.liferay.portal.model.ResourceConstants;
25  import com.liferay.portal.upgrade.v5_2_0.util.OrganizationTable;
26  import com.liferay.portal.upgrade.v5_2_0.util.OrganizationTypeUpgradeColumnImpl;
27  import com.liferay.portal.util.PortalInstances;
28  
29  import java.sql.Connection;
30  import java.sql.PreparedStatement;
31  import java.sql.ResultSet;
32  import java.sql.Types;
33  
34  /**
35   * <a href="UpgradeOrganization.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Jorge Ferrer
38   * @author Edward Shin
39   */
40  public class UpgradeOrganization extends UpgradeProcess {
41  
42      protected void doUpgrade() throws Exception {
43          UpgradeColumn locationColumn = new TempUpgradeColumnImpl(
44              "location", new Integer(Types.BOOLEAN));
45  
46          UpgradeColumn typeColumn = new OrganizationTypeUpgradeColumnImpl(
47              locationColumn);
48  
49          Object[][] organizationColumns1 =
50              {{"location", new Integer(Types.BOOLEAN)}};
51          Object[][] organizationColumns2 =
52              OrganizationTable.TABLE_COLUMNS.clone();
53  
54          Object[][] organizationColumns = ArrayUtil.append(
55              organizationColumns1, organizationColumns2);
56  
57          UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
58              OrganizationTable.TABLE_NAME, organizationColumns, locationColumn,
59              typeColumn);
60  
61          upgradeTable.updateTable();
62  
63          updateLocationResources();
64      }
65  
66      protected long getCodeId(long companyId, String name, int scope)
67          throws Exception {
68  
69          long codeId = 0;
70  
71          Connection con = null;
72          PreparedStatement ps = null;
73          ResultSet rs = null;
74  
75          try {
76              con = DataAccess.getConnection();
77  
78              ps = con.prepareStatement(
79                  "select codeId from ResourceCode where companyId = ? and " +
80                      "name = ? and scope = ?");
81  
82              ps.setLong(1, companyId);
83              ps.setString(2, name);
84              ps.setInt(3, scope);
85  
86              rs = ps.executeQuery();
87  
88              if (rs.next()) {
89                  codeId = rs.getLong("codeId");
90              }
91          }
92          finally {
93              DataAccess.cleanUp(con, ps, rs);
94          }
95  
96          return codeId;
97      }
98  
99      protected void updateCodeId(long companyId, int scope) throws Exception {
100         long oldCodeId = getCodeId(
101             companyId, "com.liferay.portal.model.Location", scope);
102         long newCodeId = getCodeId(
103             companyId, "com.liferay.portal.model.Organization", scope);
104 
105         runSQL(
106             "update Resource_ set codeId = " + newCodeId + " where codeId = " +
107                 oldCodeId);
108 
109         runSQL("delete from ResourceCode where codeId = " + oldCodeId);
110     }
111 
112     protected void updateLocationResources() throws Exception {
113         long[] companyIds = PortalInstances.getCompanyIdsBySQL();
114 
115         for (long companyId : companyIds) {
116             for (int scope : ResourceConstants.SCOPES) {
117                 updateCodeId(companyId, scope);
118             }
119         }
120     }
121 
122 }