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.Company;
30  import com.liferay.portal.model.ResourceCode;
31  import com.liferay.portal.model.ResourceConstants;
32  import com.liferay.portal.model.impl.OrganizationImpl;
33  import com.liferay.portal.service.CompanyLocalServiceUtil;
34  import com.liferay.portal.service.ResourceCodeLocalServiceUtil;
35  import com.liferay.portal.upgrade.UpgradeException;
36  import com.liferay.portal.upgrade.UpgradeProcess;
37  import com.liferay.portal.upgrade.util.DefaultUpgradeTableImpl;
38  import com.liferay.portal.upgrade.util.TempUpgradeColumnImpl;
39  import com.liferay.portal.upgrade.util.UpgradeColumn;
40  import com.liferay.portal.upgrade.util.UpgradeTable;
41  import com.liferay.portal.upgrade.v5_2_0.util.OrganizationTypeUpgradeColumnImpl;
42  
43  import java.sql.Connection;
44  import java.sql.PreparedStatement;
45  import java.sql.ResultSet;
46  import java.sql.Types;
47  
48  import java.util.List;
49  
50  /**
51   * <a href="UpgradeOrganization.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Jorge Ferrer
54   *
55   */
56  public class UpgradeOrganization extends UpgradeProcess {
57  
58      public void upgrade() throws UpgradeException {
59          _log.info("Upgrading");
60  
61          try {
62              doUpgrade();
63          }
64          catch (Exception e) {
65              throw new UpgradeException(e);
66          }
67      }
68  
69      protected void doUpgrade() throws Exception {
70          UpgradeColumn locationColumn = new TempUpgradeColumnImpl(
71              "location", new Integer(Types.BOOLEAN));
72  
73          UpgradeColumn typeColumn = new OrganizationTypeUpgradeColumnImpl(
74              locationColumn);
75  
76          Object[][] organizationColumns1 =
77              {{"location", new Integer(Types.BOOLEAN)}};
78          Object[][] organizationColumns2 =
79              OrganizationImpl.TABLE_COLUMNS.clone();
80  
81          Object[][] organizationColumns = ArrayUtil.append(
82              organizationColumns1, organizationColumns2);
83  
84          UpgradeTable upgradeTable = new DefaultUpgradeTableImpl(
85              OrganizationImpl.TABLE_NAME, organizationColumns,
86              locationColumn, typeColumn);
87  
88          upgradeTable.updateTable();
89  
90          updateLocationResources();
91      }
92  
93      protected void updateCodeId(
94              ResourceCode oldResourceCode, ResourceCode newResourceCode)
95          throws Exception {
96  
97          Connection con = null;
98          PreparedStatement ps = null;
99          ResultSet rs = null;
100 
101         try {
102             con = DataAccess.getConnection();
103 
104             ps = con.prepareStatement(
105                 "update Resource_ set codeId = ? where codeId = ?");
106 
107             ps.setLong(1, newResourceCode.getCodeId());
108             ps.setLong(2, oldResourceCode.getCodeId());
109 
110             ps.executeUpdate();
111         }
112         finally {
113             DataAccess.cleanUp(con, ps, rs);
114         }
115 
116         ResourceCodeLocalServiceUtil.deleteResourceCode(oldResourceCode);
117     }
118 
119     protected void updateLocationResources() throws Exception {
120         List<Company> companies = CompanyLocalServiceUtil.getCompanies();
121 
122         for (Company company : companies) {
123             for (int scope : ResourceConstants.SCOPES) {
124                 ResourceCode oldResourceCode =
125                     ResourceCodeLocalServiceUtil.getResourceCode(
126                         company.getCompanyId(),
127                         "com.liferay.portal.model.Location", scope);
128 
129                 ResourceCode newResourceCode =
130                     ResourceCodeLocalServiceUtil.getResourceCode(
131                         company.getCompanyId(),
132                         "com.liferay.portal.model.Organization", scope);
133 
134                 updateCodeId(oldResourceCode, newResourceCode);
135 
136                 ResourceCodeLocalServiceUtil.deleteResourceCode(
137                     oldResourceCode);
138             }
139         }
140     }
141 
142     private static Log _log = LogFactoryUtil.getLog(UpgradeOrganization.class);
143 
144 }