001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.upgrade.v6_0_12;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.util.PortalUtil;
021    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
022    import com.liferay.portlet.messageboards.model.MBDiscussion;
023    import com.liferay.portlet.messageboards.model.MBMessage;
024    
025    import java.sql.Connection;
026    import java.sql.PreparedStatement;
027    import java.sql.ResultSet;
028    
029    /**
030     * @author Shuyang Zhou
031     * @author Kalman Vincze
032     */
033    public class UpgradeMessageBoards extends UpgradeProcess {
034    
035            @Override
036            protected void doUpgrade() throws Exception {
037                    updateMessage();
038                    updateRatings();
039                    updateThread();
040            }
041    
042            protected void updateMessage() throws Exception {
043                    Connection con = null;
044                    PreparedStatement ps = null;
045                    ResultSet rs = null;
046    
047                    try {
048                            con = DataAccess.getUpgradeOptimizedConnection();
049    
050                            ps = con.prepareStatement(
051                                    "select messageId, body from MBMessage where (body like " +
052                                            "'%<3%') or (body like '%>_>%') or (body like '%<_<%')");
053    
054                            rs = ps.executeQuery();
055    
056                            while (rs.next()) {
057                                    long messageId = rs.getLong("messageId");
058                                    String body = rs.getString("body");
059    
060                                    body = StringUtil.replace(
061                                            body, new String[] {"<3", ">_>", "<_<"},
062                                            new String[] {":love:", ":glare:", ":dry:"});
063    
064                                    updateMessage(messageId, body);
065                            }
066                    }
067                    finally {
068                            DataAccess.cleanUp(con, ps, rs);
069                    }
070            }
071    
072            protected void updateMessage(long messageId, String body) throws Exception {
073                    Connection con = null;
074                    PreparedStatement ps = null;
075    
076                    try {
077                            con = DataAccess.getUpgradeOptimizedConnection();
078    
079                            ps = con.prepareStatement(
080                                    "update MBMessage set body = ? where messageId = " + messageId);
081    
082                            ps.setString(1, body);
083    
084                            ps.executeUpdate();
085                    }
086                    finally {
087                            DataAccess.cleanUp(con, ps);
088                    }
089            }
090    
091            protected void updateRatings() throws Exception {
092                    Connection con = null;
093                    PreparedStatement ps = null;
094                    ResultSet rs = null;
095    
096                    try {
097                            con = DataAccess.getUpgradeOptimizedConnection();
098    
099                            ps = con.prepareStatement(
100                                    "select MBMessage.messageId from MBMessage where " +
101                                            "MBMessage.categoryId = " +
102                                                    MBCategoryConstants.DISCUSSION_CATEGORY_ID);
103    
104                            rs = ps.executeQuery();
105    
106                            while (rs.next()) {
107                                    long messageId = rs.getLong("messageId");
108    
109                                    updateRatings(messageId);
110                            }
111                    }
112                    finally {
113                            DataAccess.cleanUp(con, ps, rs);
114                    }
115            }
116    
117            protected void updateRatings(long classPK) throws Exception {
118                    long discussionClassNameId = PortalUtil.getClassNameId(
119                            MBDiscussion.class);
120                    long messageClassNameId = PortalUtil.getClassNameId(MBMessage.class);
121    
122                    runSQL(
123                            "update RatingsStats set classNameId = " + discussionClassNameId +
124                                    " where classNameId = " + messageClassNameId +
125                                            " and classPK = " + classPK);
126    
127                    runSQL(
128                            "update RatingsEntry set classNameId = " + discussionClassNameId +
129                                    " where classNameId = " + messageClassNameId +
130                                            " and classPK = " + classPK);
131            }
132    
133            protected void updateThread() throws Exception {
134                    Connection con = null;
135                    PreparedStatement ps = null;
136                    ResultSet rs = null;
137    
138                    try {
139                            con = DataAccess.getUpgradeOptimizedConnection();
140    
141                            ps = con.prepareStatement(
142                                    "select MBThread.threadId, MBMessage.companyId, " +
143                                            "MBMessage.userId from MBThread inner join MBMessage on " +
144                                                    "MBThread.rootMessageId = MBMessage.messageId");
145    
146                            rs = ps.executeQuery();
147    
148                            while (rs.next()) {
149                                    long threadId = rs.getLong("threadId");
150                                    long companyId = rs.getLong("companyId");
151                                    long userId = rs.getLong("userId");
152    
153                                    runSQL(
154                                            "update MBThread set companyId = " + companyId +
155                                                    ", rootMessageUserId = " + userId +
156                                                            " where threadId = " + threadId);
157                            }
158                    }
159                    finally {
160                            DataAccess.cleanUp(con, ps, rs);
161                    }
162            }
163    
164    }