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 long getCodeId(long companyId, String name, int scope)
74          throws Exception {
75  
76          long codeId = 0;
77  
78          Connection con = null;
79          PreparedStatement ps = null;
80          ResultSet rs = null;
81  
82          try {
83              con = DataAccess.getConnection();
84  
85              ps = con.prepareStatement(
86                  "select codeId from ResourceCode where companyId = ? and " +
87                      "name = ? and scope = ?");
88  
89              ps.setLong(1, companyId);
90              ps.setString(2, name);
91              ps.setInt(3, scope);
92  
93              rs = ps.executeQuery();
94  
95              if (rs.next()) {
96                  codeId = rs.getLong("codeId");
97              }
98          }
99          finally {
100             DataAccess.cleanUp(con, ps, rs);
101         }
102 
103         return codeId;
104     }
105 
106     protected void updateCodeId(long companyId, int scope) throws Exception {
107         long oldCodeId = getCodeId(
108             companyId, "com.liferay.portal.model.Location", scope);
109         long newCodeId = getCodeId(
110             companyId, "com.liferay.portal.model.Organization", scope);
111 
112         runSQL(
113             "update Resource_ set codeId = " + newCodeId + " where codeId = " +
114                 oldCodeId);
115 
116         runSQL("delete from ResourceCode where codeId = " + oldCodeId);
117     }
118 
119     protected void updateLocationResources() throws Exception {
120         Connection con = null;
121         PreparedStatement ps = null;
122         ResultSet rs = null;
123 
124         try {
125             con = DataAccess.getConnection();
126 
127             ps = con.prepareStatement(_GET_COMPANY_IDS);
128 
129             rs = ps.executeQuery();
130 
131             while (rs.next()) {
132                 long companyId = rs.getLong("companyId");
133 
134                 for (int scope : ResourceConstants.SCOPES) {
135                     updateCodeId(companyId, scope);
136                 }
137             }
138         }
139         finally {
140             DataAccess.cleanUp(con, ps, rs);
141         }
142     }
143 
144     private static final String _GET_COMPANY_IDS =
145         "select companyId from Company";
146 
147 }