1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.upgrade.v5_2_6;
16  
17  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
18  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
19  import com.liferay.portal.model.Layout;
20  import com.liferay.portal.util.PortalUtil;
21  
22  import java.sql.Connection;
23  import java.sql.PreparedStatement;
24  import java.sql.ResultSet;
25  
26  /**
27   * <a href="UpgradeGroup.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Wesley Gong
30   */
31  public class UpgradeGroup extends UpgradeProcess {
32  
33      protected void doUpgrade() throws Exception {
34          updateParentGroupId();
35      }
36  
37      protected Object[] getLayout(long plid) throws Exception {
38          Object[] layout = null;
39  
40          Connection con = null;
41          PreparedStatement ps = null;
42          ResultSet rs = null;
43  
44          try {
45              con = DataAccess.getConnection();
46  
47              ps = con.prepareStatement(_GET_LAYOUT);
48  
49              ps.setLong(1, plid);
50  
51              rs = ps.executeQuery();
52  
53              while (rs.next()) {
54                  long groupId = rs.getLong("groupId");
55  
56                  layout = new Object[] {groupId};
57              }
58          }
59          finally {
60              DataAccess.cleanUp(con, ps, rs);
61          }
62  
63          return layout;
64      }
65  
66      protected void updateParentGroupId() throws Exception {
67          Connection con = null;
68          PreparedStatement ps = null;
69          ResultSet rs = null;
70  
71          try {
72              con = DataAccess.getConnection();
73  
74              long classNameId = PortalUtil.getClassNameId(
75                  Layout.class.getName());
76  
77              ps = con.prepareStatement(
78                  "select groupId, classPK from Group_ where classNameId = " +
79                      classNameId);
80  
81              rs = ps.executeQuery();
82  
83              while (rs.next()) {
84                  long groupId = rs.getLong("groupId");
85                  long classPK = rs.getLong("classPK");
86  
87                  Object[] layout = getLayout(classPK);
88  
89                  if (layout != null) {
90                      long layoutGroupId = (Long)layout[0];
91  
92                      runSQL(
93                          "update Group_ set parentGroupId = " + layoutGroupId +
94                              " where groupId = " + groupId);
95                  }
96              }
97          }
98          finally {
99              DataAccess.cleanUp(con, ps, rs);
100         }
101     }
102 
103     private static final String _GET_LAYOUT =
104         "select groupId from Layout where plid = ?";
105 
106 }