1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.upgrade.v5_2_0;
24  
25  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
26  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
27  import com.liferay.portal.kernel.upgrade.util.TempUpgradeColumnImpl;
28  import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
29  import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
30  import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
31  import com.liferay.portal.kernel.util.ArrayUtil;
32  import com.liferay.portal.model.ResourceConstants;
33  import com.liferay.portal.upgrade.v5_2_0.util.OrganizationTable;
34  import com.liferay.portal.upgrade.v5_2_0.util.OrganizationTypeUpgradeColumnImpl;
35  
36  import java.sql.Connection;
37  import java.sql.PreparedStatement;
38  import java.sql.ResultSet;
39  import java.sql.Types;
40  
41  /**
42   * <a href="UpgradeOrganization.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Jorge Ferrer
45   * @author Edward Shin
46   */
47  public class UpgradeOrganization extends UpgradeProcess {
48  
49      protected void doUpgrade() throws Exception {
50          UpgradeColumn locationColumn = new TempUpgradeColumnImpl(
51              "location", new Integer(Types.BOOLEAN));
52  
53          UpgradeColumn typeColumn = new OrganizationTypeUpgradeColumnImpl(
54              locationColumn);
55  
56          Object[][] organizationColumns1 =
57              {{"location", new Integer(Types.BOOLEAN)}};
58          Object[][] organizationColumns2 =
59              OrganizationTable.TABLE_COLUMNS.clone();
60  
61          Object[][] organizationColumns = ArrayUtil.append(
62              organizationColumns1, organizationColumns2);
63  
64          UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
65              OrganizationTable.TABLE_NAME, organizationColumns, locationColumn,
66              typeColumn);
67  
68          upgradeTable.updateTable();
69  
70          updateLocationResources();
71      }
72  
73      protected void updateCodeId(long companyId, int scope) throws Exception {
74          Connection con = null;
75          PreparedStatement ps = null;
76          ResultSet rs = null;
77  
78          try {
79              con = DataAccess.getConnection();
80  
81              ps = con.prepareStatement(
82                  "select codeId from ResourceCode where companyId = ? and " +
83                      "name = ? and scope = ?");
84  
85              ps.setLong(1, companyId);
86              ps.setString(2, "com.liferay.portal.model.Location");
87              ps.setInt(3, scope);
88  
89              rs = ps.executeQuery();
90  
91              long oldCodeId = 0;
92  
93              if (rs.next()) {
94                  oldCodeId = rs.getLong("codeId");
95              }
96  
97              ps.setString(2, "com.liferay.portal.model.Organization");
98  
99              rs = ps.executeQuery();
100 
101             long newCodeId = 0;
102 
103             if (rs.next()) {
104                 newCodeId = rs.getLong("codeId");
105             }
106 
107             ps.close();
108 
109             ps = con.prepareStatement(
110                 "update Resource_ set codeId = ? where codeId = ?");
111 
112             ps.setLong(1, newCodeId);
113             ps.setLong(2, oldCodeId);
114 
115             ps.executeUpdate();
116 
117             ps.close();
118 
119             ps = con.prepareStatement(
120                 "delete from ResourceCode where codeId = " + oldCodeId);
121 
122             ps.executeUpdate();
123         }
124         finally {
125             DataAccess.cleanUp(con, ps, rs);
126         }
127     }
128 
129     protected void updateLocationResources() throws Exception {
130         Connection con = null;
131         PreparedStatement ps = null;
132         ResultSet rs = null;
133 
134         try {
135             con = DataAccess.getConnection();
136 
137             ps = con.prepareStatement(_GET_COMPANY_IDS);
138 
139             rs = ps.executeQuery();
140 
141             while (rs.next()) {
142                 long companyId = rs.getLong("companyId");
143 
144                 for (int scope : ResourceConstants.SCOPES) {
145                     updateCodeId(companyId, scope);
146                 }
147             }
148         }
149         finally {
150             DataAccess.cleanUp(con, ps, rs);
151         }
152     }
153 
154     private static final String _GET_COMPANY_IDS =
155         "select companyId from Company";
156 
157 }