1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.upgrade.v5_2_0;
21  
22  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.ArrayUtil;
26  import com.liferay.portal.model.ResourceConstants;
27  import com.liferay.portal.model.impl.OrganizationImpl;
28  import com.liferay.portal.upgrade.UpgradeException;
29  import com.liferay.portal.upgrade.UpgradeProcess;
30  import com.liferay.portal.upgrade.util.DefaultUpgradeTableImpl;
31  import com.liferay.portal.upgrade.util.TempUpgradeColumnImpl;
32  import com.liferay.portal.upgrade.util.UpgradeColumn;
33  import com.liferay.portal.upgrade.util.UpgradeTable;
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   *
46   */
47  public class UpgradeOrganization extends UpgradeProcess {
48  
49      public void upgrade() throws UpgradeException {
50          _log.info("Upgrading");
51  
52          try {
53              doUpgrade();
54          }
55          catch (Exception e) {
56              throw new UpgradeException(e);
57          }
58      }
59  
60      protected void doUpgrade() throws Exception {
61          UpgradeColumn locationColumn = new TempUpgradeColumnImpl(
62              "location", new Integer(Types.BOOLEAN));
63  
64          UpgradeColumn typeColumn = new OrganizationTypeUpgradeColumnImpl(
65              locationColumn);
66  
67          Object[][] organizationColumns1 =
68              {{"location", new Integer(Types.BOOLEAN)}};
69          Object[][] organizationColumns2 =
70              OrganizationImpl.TABLE_COLUMNS.clone();
71  
72          Object[][] organizationColumns = ArrayUtil.append(
73              organizationColumns1, organizationColumns2);
74  
75          UpgradeTable upgradeTable = new DefaultUpgradeTableImpl(
76              OrganizationImpl.TABLE_NAME, organizationColumns,
77              locationColumn, typeColumn);
78  
79          upgradeTable.updateTable();
80  
81          updateLocationResources();
82      }
83  
84      protected void updateCodeId(long companyId, int scope) throws Exception {
85          Connection con = null;
86          PreparedStatement ps = null;
87          ResultSet rs = null;
88  
89          try {
90              con = DataAccess.getConnection();
91  
92              ps = con.prepareStatement(
93                  "select codeId from ResourceCode where companyId = ? and " +
94                      "name = ? and scope = ?");
95  
96              ps.setLong(1, companyId);
97              ps.setString(2, "com.liferay.portal.model.Location");
98              ps.setInt(3, scope);
99  
100             rs = ps.executeQuery();
101 
102             long oldCodeId = 0;
103 
104             if (rs.next()) {
105                 oldCodeId = rs.getLong("codeId");
106             }
107 
108             ps.setString(2, "com.liferay.portal.model.Organization");
109 
110             rs = ps.executeQuery();
111 
112             long newCodeId = 0;
113 
114             if (rs.next()) {
115                 newCodeId = rs.getLong("codeId");
116             }
117 
118             ps.close();
119 
120             ps = con.prepareStatement(
121                 "update Resource_ set codeId = ? where codeId = ?");
122 
123             ps.setLong(1, oldCodeId);
124             ps.setLong(2, newCodeId);
125 
126             ps.executeUpdate();
127 
128             ps.close();
129 
130             ps = con.prepareStatement(
131                 "delete from ResourceCode where codeId = " + oldCodeId);
132 
133             ps.executeUpdate();
134         }
135         finally {
136             DataAccess.cleanUp(con, ps, rs);
137         }
138     }
139 
140     protected void updateLocationResources() throws Exception {
141         Connection con = null;
142         PreparedStatement ps = null;
143         ResultSet rs = null;
144 
145         try {
146             con = DataAccess.getConnection();
147 
148             ps = con.prepareStatement(_GET_COMPANY_IDS);
149 
150             rs = ps.executeQuery();
151 
152             while (rs.next()) {
153                 long companyId = rs.getLong("companyId");
154 
155                 for (int scope : ResourceConstants.SCOPES) {
156                     updateCodeId(companyId, scope);
157                 }
158             }
159         }
160         finally {
161             DataAccess.cleanUp(con, ps, rs);
162         }
163     }
164 
165     private static final String _GET_COMPANY_IDS =
166         "select companyId from Company";
167 
168     private static Log _log = LogFactoryUtil.getLog(UpgradeOrganization.class);
169 
170 }