1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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_6;
24  
25  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
26  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
27  import com.liferay.portal.model.Layout;
28  import com.liferay.portal.util.PortalUtil;
29  
30  import java.sql.Connection;
31  import java.sql.PreparedStatement;
32  import java.sql.ResultSet;
33  
34  /**
35   * <a href="UpgradeGroup.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Wesley Gong
38   */
39  public class UpgradeGroup extends UpgradeProcess {
40  
41      protected void doUpgrade() throws Exception {
42          updateParentGroupId();
43      }
44  
45      protected Object[] getLayout(long plid) throws Exception {
46          Object[] layout = null;
47  
48          Connection con = null;
49          PreparedStatement ps = null;
50          ResultSet rs = null;
51  
52          try {
53              con = DataAccess.getConnection();
54  
55              ps = con.prepareStatement(_GET_LAYOUT);
56  
57              ps.setLong(1, plid);
58  
59              rs = ps.executeQuery();
60  
61              while (rs.next()) {
62                  long groupId = rs.getLong("groupId");
63  
64                  layout = new Object[] {groupId};
65              }
66          }
67          finally {
68              DataAccess.cleanUp(con, ps, rs);
69          }
70  
71          return layout;
72      }
73  
74      protected void updateParentGroupId() throws Exception {
75          Connection con = null;
76          PreparedStatement ps = null;
77          ResultSet rs = null;
78  
79          try {
80              con = DataAccess.getConnection();
81  
82              long classNameId = PortalUtil.getClassNameId(
83                  Layout.class.getName());
84  
85              ps = con.prepareStatement(
86                  "select groupId, classPK from Group_ where classNameId = " +
87                      classNameId);
88  
89              rs = ps.executeQuery();
90  
91              while (rs.next()) {
92                  long groupId = rs.getLong("groupId");
93                  long classPK = rs.getLong("classPK");
94  
95                  Object[] layout = getLayout(classPK);
96  
97                  if (layout != null) {
98                      long layoutGroupId = (Long)layout[0];
99  
100                     runSQL(
101                         "update Group_ set parentGroupId = " + layoutGroupId +
102                             " where groupId = " + groupId);
103                 }
104             }
105         }
106         finally {
107             DataAccess.cleanUp(con, ps, rs);
108         }
109     }
110 
111     private static final String _GET_LAYOUT =
112         "select groupId from Layout where plid = ?";
113 
114 }