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_4;
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.model.Group;
29  import com.liferay.portal.model.Layout;
30  import com.liferay.portal.service.GroupLocalServiceUtil;
31  import com.liferay.portal.service.LayoutLocalServiceUtil;
32  import com.liferay.portal.upgrade.UpgradeException;
33  import com.liferay.portal.upgrade.UpgradeProcess;
34  import com.liferay.portal.upgrade.util.DateUpgradeColumnImpl;
35  import com.liferay.portal.upgrade.util.DefaultUpgradeTableImpl;
36  import com.liferay.portal.upgrade.util.UpgradeColumn;
37  import com.liferay.portal.upgrade.util.UpgradeTable;
38  import com.liferay.portlet.social.model.impl.SocialActivityImpl;
39  import com.liferay.portlet.social.model.impl.SocialRelationImpl;
40  import com.liferay.portlet.social.model.impl.SocialRequestImpl;
41  
42  import java.sql.Connection;
43  import java.sql.PreparedStatement;
44  import java.sql.ResultSet;
45  
46  /**
47   * <a href="UpgradeSocial.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Amos Fong
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class UpgradeSocial extends UpgradeProcess {
54  
55      public void upgrade() throws UpgradeException {
56          _log.info("Upgrading");
57  
58          try {
59              doUpgrade();
60          }
61          catch (Exception e) {
62              throw new UpgradeException(e);
63          }
64      }
65  
66      protected void doUpgrade() throws Exception {
67          updateGroupId();
68  
69          // SocialActivity
70  
71          UpgradeColumn createDateColumn = new DateUpgradeColumnImpl(
72              "createDate");
73          UpgradeColumn modifiedDateColumn = new DateUpgradeColumnImpl(
74              "modifiedDate");
75  
76          UpgradeTable upgradeTable = new DefaultUpgradeTableImpl(
77              SocialActivityImpl.TABLE_NAME, SocialActivityImpl.TABLE_COLUMNS,
78              createDateColumn);
79  
80          upgradeTable.setCreateSQL(SocialActivityImpl.TABLE_SQL_CREATE);
81  
82          upgradeTable.updateTable();
83  
84          // SocialRelation
85  
86          upgradeTable = new DefaultUpgradeTableImpl(
87              SocialRelationImpl.TABLE_NAME, SocialRelationImpl.TABLE_COLUMNS,
88              createDateColumn);
89  
90          upgradeTable.setCreateSQL(SocialRelationImpl.TABLE_SQL_CREATE);
91  
92          upgradeTable.updateTable();
93  
94          // SocialRequest
95  
96          upgradeTable = new DefaultUpgradeTableImpl(
97              SocialRequestImpl.TABLE_NAME, SocialRequestImpl.TABLE_COLUMNS,
98              createDateColumn, modifiedDateColumn);
99  
100         upgradeTable.setCreateSQL(SocialRequestImpl.TABLE_SQL_CREATE);
101 
102         upgradeTable.updateTable();
103     }
104 
105     protected void updateGroupId() throws Exception {
106         Connection con = null;
107         PreparedStatement ps = null;
108         ResultSet rs = null;
109 
110         try {
111             con = DataAccess.getConnection();
112 
113             ps = con.prepareStatement(
114                 "select distinct(groupId) from SocialActivity where groupId " +
115                     "> 0");
116 
117             rs = ps.executeQuery();
118 
119             while (rs.next()) {
120                 long groupId = rs.getLong("groupId");
121 
122                 try {
123                     updateGroupId(groupId);
124                 }
125                 catch (Exception e) {
126                     if (_log.isWarnEnabled()) {
127                         _log.warn(e);
128                     }
129                 }
130             }
131         }
132         finally {
133             DataAccess.cleanUp(con, ps);
134         }
135     }
136 
137     protected void updateGroupId(long groupId) throws Exception {
138         Group group = GroupLocalServiceUtil.getGroup(groupId);
139 
140         if (group.isLayout()) {
141             Layout layout = LayoutLocalServiceUtil.getLayout(
142                 group.getClassPK());
143 
144             long layoutGroupId = layout.getGroupId();
145 
146             runSQL(
147                 "update SocialActivity set groupId = " + layoutGroupId +
148                     " where groupId = " + groupId);
149         }
150     }
151 
152     private static Log _log = LogFactoryUtil.getLog(UpgradeSocial.class);
153 
154 }