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_5;
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.kernel.upgrade.util.DateUpgradeColumnImpl;
22  import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
23  import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
24  import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
25  import com.liferay.portal.model.Layout;
26  import com.liferay.portal.upgrade.v5_2_5.util.SocialActivityTable;
27  import com.liferay.portal.upgrade.v5_2_5.util.SocialRelationTable;
28  import com.liferay.portal.upgrade.v5_2_5.util.SocialRequestTable;
29  import com.liferay.portal.util.PortalUtil;
30  
31  import java.sql.Connection;
32  import java.sql.PreparedStatement;
33  import java.sql.ResultSet;
34  
35  /**
36   * <a href="UpgradeSocial.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Amos Fong
39   * @author Brian Wing Shun Chan
40   */
41  public class UpgradeSocial extends UpgradeProcess {
42  
43      protected void doUpgrade() throws Exception {
44          updateGroupId();
45  
46          // SocialActivity
47  
48          UpgradeColumn createDateColumn = new DateUpgradeColumnImpl(
49              "createDate");
50          UpgradeColumn modifiedDateColumn = new DateUpgradeColumnImpl(
51              "modifiedDate");
52  
53          UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
54              SocialActivityTable.TABLE_NAME, SocialActivityTable.TABLE_COLUMNS,
55              createDateColumn);
56  
57          upgradeTable.setCreateSQL(SocialActivityTable.TABLE_SQL_CREATE);
58  
59          upgradeTable.updateTable();
60  
61          // SocialRelation
62  
63          upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
64              SocialRelationTable.TABLE_NAME, SocialRelationTable.TABLE_COLUMNS,
65              createDateColumn);
66  
67          upgradeTable.setCreateSQL(SocialRelationTable.TABLE_SQL_CREATE);
68  
69          upgradeTable.updateTable();
70  
71          // SocialRequest
72  
73          upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
74              SocialRequestTable.TABLE_NAME, SocialRequestTable.TABLE_COLUMNS,
75              createDateColumn, modifiedDateColumn);
76  
77          upgradeTable.setCreateSQL(SocialRequestTable.TABLE_SQL_CREATE);
78  
79          upgradeTable.updateTable();
80      }
81  
82      protected Object[] getGroup(long groupId) throws Exception {
83          Object[] group = null;
84  
85          Connection con = null;
86          PreparedStatement ps = null;
87          ResultSet rs = null;
88  
89          try {
90              con = DataAccess.getConnection();
91  
92              ps = con.prepareStatement(_GET_GROUP);
93  
94              ps.setLong(1, groupId);
95  
96              rs = ps.executeQuery();
97  
98              while (rs.next()) {
99                  long classNameId = rs.getLong("classNameId");
100                 long classPK = rs.getLong("classPK");
101 
102                 group = new Object[] {classNameId, classPK};
103             }
104         }
105         finally {
106             DataAccess.cleanUp(con, ps, rs);
107         }
108 
109         return group;
110     }
111 
112     protected Object[] getLayout(long plid) throws Exception {
113         Object[] layout = null;
114 
115         Connection con = null;
116         PreparedStatement ps = null;
117         ResultSet rs = null;
118 
119         try {
120             con = DataAccess.getConnection();
121 
122             ps = con.prepareStatement(_GET_LAYOUT);
123 
124             ps.setLong(1, plid);
125 
126             rs = ps.executeQuery();
127 
128             while (rs.next()) {
129                 long groupId = rs.getLong("groupId");
130 
131                 layout = new Object[] {groupId};
132             }
133         }
134         finally {
135             DataAccess.cleanUp(con, ps, rs);
136         }
137 
138         return layout;
139     }
140 
141     protected void updateGroupId() throws Exception {
142         Connection con = null;
143         PreparedStatement ps = null;
144         ResultSet rs = null;
145 
146         try {
147             con = DataAccess.getConnection();
148 
149             ps = con.prepareStatement(
150                 "select distinct(groupId) from SocialActivity where groupId " +
151                     "> 0");
152 
153             rs = ps.executeQuery();
154 
155             while (rs.next()) {
156                 long groupId = rs.getLong("groupId");
157 
158                 try {
159                     updateGroupId(groupId);
160                 }
161                 catch (Exception e) {
162                     if (_log.isWarnEnabled()) {
163                         _log.warn(e);
164                     }
165                 }
166             }
167         }
168         finally {
169             DataAccess.cleanUp(con, ps, rs);
170         }
171     }
172 
173     protected void updateGroupId(long groupId) throws Exception {
174         Object[] group = getGroup(groupId);
175 
176         if (group == null) {
177             return;
178         }
179 
180         long classNameId = (Long)group[0];
181 
182         if (classNameId != PortalUtil.getClassNameId(Layout.class.getName())) {
183             return;
184         }
185 
186         long classPK = (Long)group[1];
187 
188         Object[] layout = getLayout(classPK);
189 
190         if (layout == null) {
191             return;
192         }
193 
194         long layoutGroupId = (Long)layout[0];
195 
196         runSQL(
197             "update SocialActivity set groupId = " + layoutGroupId +
198                 " where groupId = " + groupId);
199     }
200 
201     private static final String _GET_GROUP =
202         "select * from Group_ where groupId = ?";
203 
204     private static final String _GET_LAYOUT =
205         "select * from Layout where plid = ?";
206 
207     private static Log _log = LogFactoryUtil.getLog(UpgradeSocial.class);
208 
209 }