1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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_5;
24  
25  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
29  import com.liferay.portal.kernel.upgrade.util.DateUpgradeColumnImpl;
30  import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
31  import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
32  import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
33  import com.liferay.portal.model.Group;
34  import com.liferay.portal.model.Layout;
35  import com.liferay.portal.service.GroupLocalServiceUtil;
36  import com.liferay.portal.service.LayoutLocalServiceUtil;
37  import com.liferay.portal.upgrade.v5_2_5.util.SocialActivityTable;
38  import com.liferay.portal.upgrade.v5_2_5.util.SocialRelationTable;
39  import com.liferay.portal.upgrade.v5_2_5.util.SocialRequestTable;
40  
41  import java.sql.Connection;
42  import java.sql.PreparedStatement;
43  import java.sql.ResultSet;
44  
45  /**
46   * <a href="UpgradeSocial.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Amos Fong
49   * @author Brian Wing Shun Chan
50   */
51  public class UpgradeSocial extends UpgradeProcess {
52  
53      protected void doUpgrade() throws Exception {
54          updateGroupId();
55  
56          // SocialActivity
57  
58          UpgradeColumn createDateColumn = new DateUpgradeColumnImpl(
59              "createDate");
60          UpgradeColumn modifiedDateColumn = new DateUpgradeColumnImpl(
61              "modifiedDate");
62  
63          UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
64              SocialActivityTable.TABLE_NAME, SocialActivityTable.TABLE_COLUMNS,
65              createDateColumn);
66  
67          upgradeTable.setCreateSQL(SocialActivityTable.TABLE_SQL_CREATE);
68  
69          upgradeTable.updateTable();
70  
71          // SocialRelation
72  
73          upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
74              SocialRelationTable.TABLE_NAME, SocialRelationTable.TABLE_COLUMNS,
75              createDateColumn);
76  
77          upgradeTable.setCreateSQL(SocialRelationTable.TABLE_SQL_CREATE);
78  
79          upgradeTable.updateTable();
80  
81          // SocialRequest
82  
83          upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
84              SocialRequestTable.TABLE_NAME, SocialRequestTable.TABLE_COLUMNS,
85              createDateColumn, modifiedDateColumn);
86  
87          upgradeTable.setCreateSQL(SocialRequestTable.TABLE_SQL_CREATE);
88  
89          upgradeTable.updateTable();
90      }
91  
92      protected void updateGroupId() throws Exception {
93          Connection con = null;
94          PreparedStatement ps = null;
95          ResultSet rs = null;
96  
97          try {
98              con = DataAccess.getConnection();
99  
100             ps = con.prepareStatement(
101                 "select distinct(groupId) from SocialActivity where groupId " +
102                     "> 0");
103 
104             rs = ps.executeQuery();
105 
106             while (rs.next()) {
107                 long groupId = rs.getLong("groupId");
108 
109                 try {
110                     updateGroupId(groupId);
111                 }
112                 catch (Exception e) {
113                     if (_log.isWarnEnabled()) {
114                         _log.warn(e);
115                     }
116                 }
117             }
118         }
119         finally {
120             DataAccess.cleanUp(con, ps);
121         }
122     }
123 
124     protected void updateGroupId(long groupId) throws Exception {
125         Group group = GroupLocalServiceUtil.getGroup(groupId);
126 
127         if (group.isLayout()) {
128             Layout layout = LayoutLocalServiceUtil.getLayout(
129                 group.getClassPK());
130 
131             long layoutGroupId = layout.getGroupId();
132 
133             runSQL(
134                 "update SocialActivity set groupId = " + layoutGroupId +
135                     " where groupId = " + groupId);
136         }
137     }
138 
139     private static Log _log = LogFactoryUtil.getLog(UpgradeSocial.class);
140 
141 }