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