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_1_7_to_5_2_7;
16  
17  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
21  import com.liferay.portal.model.Layout;
22  import com.liferay.portal.util.PortalUtil;
23  
24  import java.sql.Connection;
25  import java.sql.PreparedStatement;
26  import java.sql.ResultSet;
27  
28  /**
29   * <a href="UpgradeSocial.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Amos Fong
32   * @author Brian Wing Shun Chan
33   */
34  public class UpgradeSocial extends UpgradeProcess {
35  
36      protected void doUpgrade() throws Exception {
37          updateGroupId();
38      }
39  
40      protected Object[] getGroup(long groupId) throws Exception {
41          Object[] group = null;
42  
43          Connection con = null;
44          PreparedStatement ps = null;
45          ResultSet rs = null;
46  
47          try {
48              con = DataAccess.getConnection();
49  
50              ps = con.prepareStatement(_GET_GROUP);
51  
52              ps.setLong(1, groupId);
53  
54              rs = ps.executeQuery();
55  
56              while (rs.next()) {
57                  long classNameId = rs.getLong("classNameId");
58                  long classPK = rs.getLong("classPK");
59  
60                  group = new Object[] {classNameId, classPK};
61              }
62          }
63          finally {
64              DataAccess.cleanUp(con, ps, rs);
65          }
66  
67          return group;
68      }
69  
70      protected Object[] getLayout(long plid) throws Exception {
71          Object[] layout = null;
72  
73          Connection con = null;
74          PreparedStatement ps = null;
75          ResultSet rs = null;
76  
77          try {
78              con = DataAccess.getConnection();
79  
80              ps = con.prepareStatement(_GET_LAYOUT);
81  
82              ps.setLong(1, plid);
83  
84              rs = ps.executeQuery();
85  
86              while (rs.next()) {
87                  long groupId = rs.getLong("groupId");
88  
89                  layout = new Object[] {groupId};
90              }
91          }
92          finally {
93              DataAccess.cleanUp(con, ps, rs);
94          }
95  
96          return layout;
97      }
98  
99      protected void updateGroupId() throws Exception {
100         Connection con = null;
101         PreparedStatement ps = null;
102         ResultSet rs = null;
103 
104         try {
105             con = DataAccess.getConnection();
106 
107             ps = con.prepareStatement(
108                 "select distinct(groupId) from SocialActivity where groupId " +
109                     "> 0");
110 
111             rs = ps.executeQuery();
112 
113             while (rs.next()) {
114                 long groupId = rs.getLong("groupId");
115 
116                 try {
117                     updateGroupId(groupId);
118                 }
119                 catch (Exception e) {
120                     if (_log.isWarnEnabled()) {
121                         _log.warn(e);
122                     }
123                 }
124             }
125         }
126         finally {
127             DataAccess.cleanUp(con, ps, rs);
128         }
129     }
130 
131     protected void updateGroupId(long groupId) throws Exception {
132         Object[] group = getGroup(groupId);
133 
134         if (group == null) {
135             return;
136         }
137 
138         long classNameId = (Long)group[0];
139 
140         if (classNameId != PortalUtil.getClassNameId(Layout.class.getName())) {
141             return;
142         }
143 
144         long classPK = (Long)group[1];
145 
146         Object[] layout = getLayout(classPK);
147 
148         if (layout == null) {
149             return;
150         }
151 
152         long layoutGroupId = (Long)layout[0];
153 
154         runSQL(
155             "update SocialActivity set groupId = " + layoutGroupId +
156                 " where groupId = " + groupId);
157     }
158 
159     private static final String _GET_GROUP =
160         "select * from Group_ where groupId = ?";
161 
162     private static final String _GET_LAYOUT =
163         "select * from Layout where plid = ?";
164 
165     private static Log _log = LogFactoryUtil.getLog(UpgradeSocial.class);
166 
167 }