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_4;
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.model.Group;
26  import com.liferay.portal.model.Layout;
27  import com.liferay.portal.service.GroupLocalServiceUtil;
28  import com.liferay.portal.service.LayoutLocalServiceUtil;
29  import com.liferay.portal.upgrade.UpgradeException;
30  import com.liferay.portal.upgrade.UpgradeProcess;
31  
32  import java.sql.Connection;
33  import java.sql.PreparedStatement;
34  import java.sql.ResultSet;
35  
36  /**
37   * <a href="UpgradeSocial.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Amos Fong
40   *
41   */
42  public class UpgradeSocial extends UpgradeProcess {
43  
44      public void upgrade() throws UpgradeException {
45          _log.info("Upgrading");
46  
47          try {
48              doUpgrade();
49          }
50          catch (Exception e) {
51              throw new UpgradeException(e);
52          }
53      }
54  
55      protected void doUpgrade() throws Exception {
56          Connection con = null;
57          PreparedStatement ps = null;
58          ResultSet rs = null;
59  
60          try {
61              con = DataAccess.getConnection();
62  
63              ps = con.prepareStatement(
64                  "select distinct(groupId) from SocialActivity");
65  
66              rs = ps.executeQuery();
67  
68              while (rs.next()) {
69                  long groupId = rs.getLong("groupId");
70  
71                  try {
72                      updateGroupId(groupId);
73                  }
74                  catch (Exception e) {
75                      if (_log.isWarnEnabled()) {
76                          _log.warn(e);
77                      }
78                  }
79              }
80          }
81          finally {
82              DataAccess.cleanUp(con, ps);
83          }
84      }
85  
86      protected void updateGroupId(long groupId) throws Exception {
87          Group group = GroupLocalServiceUtil.getGroup(groupId);
88  
89          if (group.isLayout()) {
90              Layout layout = LayoutLocalServiceUtil.getLayout(
91                  group.getClassPK());
92  
93              long layoutGroupId = layout.getGroupId();
94  
95              runSQL(
96                  "update SocialActivity set groupId = " + layoutGroupId +
97                      " where groupId = " + groupId);
98          }
99      }
100 
101     private static Log _log = LogFactoryUtil.getLog(UpgradeSocial.class);
102 
103 }