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.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.ArrayUtil;
29  import com.liferay.portal.model.ResourceConstants;
30  import com.liferay.portal.model.impl.OrganizationImpl;
31  import com.liferay.portal.upgrade.UpgradeException;
32  import com.liferay.portal.upgrade.UpgradeProcess;
33  import com.liferay.portal.upgrade.util.DefaultUpgradeTableImpl;
34  import com.liferay.portal.upgrade.util.TempUpgradeColumnImpl;
35  import com.liferay.portal.upgrade.util.UpgradeColumn;
36  import com.liferay.portal.upgrade.util.UpgradeTable;
37  import com.liferay.portal.upgrade.v5_2_0.util.OrganizationTypeUpgradeColumnImpl;
38  
39  import java.sql.Connection;
40  import java.sql.PreparedStatement;
41  import java.sql.ResultSet;
42  import java.sql.Types;
43  
44  /**
45   * <a href="UpgradeOrganization.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Jorge Ferrer
48   *
49   */
50  public class UpgradeOrganization extends UpgradeProcess {
51  
52      public void upgrade() throws UpgradeException {
53          _log.info("Upgrading");
54  
55          try {
56              doUpgrade();
57          }
58          catch (Exception e) {
59              throw new UpgradeException(e);
60          }
61      }
62  
63      protected void doUpgrade() throws Exception {
64          UpgradeColumn locationColumn = new TempUpgradeColumnImpl(
65              "location", new Integer(Types.BOOLEAN));
66  
67          UpgradeColumn typeColumn = new OrganizationTypeUpgradeColumnImpl(
68              locationColumn);
69  
70          Object[][] organizationColumns1 =
71              {{"location", new Integer(Types.BOOLEAN)}};
72          Object[][] organizationColumns2 =
73              OrganizationImpl.TABLE_COLUMNS.clone();
74  
75          Object[][] organizationColumns = ArrayUtil.append(
76              organizationColumns1, organizationColumns2);
77  
78          UpgradeTable upgradeTable = new DefaultUpgradeTableImpl(
79              OrganizationImpl.TABLE_NAME, organizationColumns,
80              locationColumn, typeColumn);
81  
82          upgradeTable.updateTable();
83  
84          updateLocationResources();
85      }
86  
87      protected void updateCodeId(long companyId, int scope) throws Exception {
88          Connection con = null;
89          PreparedStatement ps = null;
90          ResultSet rs = null;
91  
92          try {
93              con = DataAccess.getConnection();
94  
95              ps = con.prepareStatement(
96                  "select codeId from ResourceCode where companyId = ? and " +
97                      "name = ? and scope = ?");
98  
99              ps.setLong(1, companyId);
100             ps.setString(2, "com.liferay.portal.model.Location");
101             ps.setInt(3, scope);
102 
103             rs = ps.executeQuery();
104 
105             long oldCodeId = 0;
106 
107             if (rs.next()) {
108                 oldCodeId = rs.getLong("codeId");
109             }
110 
111             ps.setString(2, "com.liferay.portal.model.Organization");
112 
113             rs = ps.executeQuery();
114 
115             long newCodeId = 0;
116 
117             if (rs.next()) {
118                 newCodeId = rs.getLong("codeId");
119             }
120 
121             ps.close();
122 
123             ps = con.prepareStatement(
124                 "update Resource_ set codeId = ? where codeId = ?");
125 
126             ps.setLong(1, oldCodeId);
127             ps.setLong(2, newCodeId);
128 
129             ps.executeUpdate();
130 
131             ps.close();
132 
133             ps = con.prepareStatement(
134                 "delete from ResourceCode where codeId = " + oldCodeId);
135 
136             ps.executeUpdate();
137         }
138         finally {
139             DataAccess.cleanUp(con, ps, rs);
140         }
141     }
142 
143     protected void updateLocationResources() throws Exception {
144         Connection con = null;
145         PreparedStatement ps = null;
146         ResultSet rs = null;
147 
148         try {
149             con = DataAccess.getConnection();
150 
151             ps = con.prepareStatement(_GET_COMPANY_IDS);
152 
153             rs = ps.executeQuery();
154 
155             while (rs.next()) {
156                 long companyId = rs.getLong("companyId");
157 
158                 for (int scope : ResourceConstants.SCOPES) {
159                     updateCodeId(companyId, scope);
160                 }
161             }
162         }
163         finally {
164             DataAccess.cleanUp(con, ps, rs);
165         }
166     }
167 
168     private static final String _GET_COMPANY_IDS =
169         "select companyId from Company";
170 
171     private static Log _log = LogFactoryUtil.getLog(UpgradeOrganization.class);
172 
173 }