001    /**
002     * Copyright (c) 2000-2012 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    
021    import java.sql.Connection;
022    import java.sql.PreparedStatement;
023    import java.sql.ResultSet;
024    
025    /**
026     * @author Shuyang Zhou
027     */
028    public class UpgradeMessageBoards extends UpgradeProcess {
029    
030            @Override
031            protected void doUpgrade() throws Exception {
032                    updateMessage();
033                    updateThread();
034            }
035    
036            protected void updateMessage() throws Exception {
037                    Connection con = null;
038                    PreparedStatement ps = null;
039                    ResultSet rs = null;
040    
041                    try {
042                            con = DataAccess.getConnection();
043    
044                            ps = con.prepareStatement(
045                                    "select messageId, body from MBMessage where (body like " +
046                                            "'%<3%') or (body like '%>_>%') or (body like '%<_<%')");
047    
048                            rs = ps.executeQuery();
049    
050                            while (rs.next()) {
051                                    long messageId = rs.getLong("messageId");
052                                    String body = rs.getString("body");
053    
054                                    body = StringUtil.replace(
055                                            body,
056                                            new String[] {"<3", ">_>", "<_<"},
057                                            new String[] {":love:", ":glare:", ":dry:"});
058    
059                                    updateMessage(messageId, body);
060                            }
061                    }
062                    finally {
063                            DataAccess.cleanUp(con, ps, rs);
064                    }
065            }
066    
067            protected void updateMessage(long messageId, String body)
068                    throws Exception {
069    
070                    Connection con = null;
071                    PreparedStatement ps = null;
072    
073                    try {
074                            con = DataAccess.getConnection();
075    
076                            ps = con.prepareStatement(
077                                    "update MBMessage set body = ? where messageId = " + messageId);
078    
079                            ps.setString(1, body);
080    
081                            ps.executeUpdate();
082                    }
083                    finally {
084                            DataAccess.cleanUp(con, ps);
085                    }
086            }
087    
088            protected void updateThread() throws Exception {
089                    Connection con = null;
090                    PreparedStatement ps = null;
091                    ResultSet rs = null;
092    
093                    try {
094                            con = DataAccess.getConnection();
095    
096                            ps = con.prepareStatement(
097                                    "select MBThread.threadId, MBMessage.companyId, " +
098                                            "MBMessage.userId from MBThread inner join MBMessage on " +
099                                                    "MBThread.rootMessageId = MBMessage.messageId");
100    
101                            rs = ps.executeQuery();
102    
103                            while (rs.next()) {
104                                    long threadId = rs.getLong("threadId");
105                                    long companyId = rs.getLong("companyId");
106                                    long userId = rs.getLong("userId");
107    
108                                    runSQL(
109                                            "update MBThread set companyId = " + companyId +
110                                                    ", rootMessageUserId = " + userId +
111                                                            " where threadId = " + threadId);
112                            }
113                    }
114                    finally {
115                            DataAccess.cleanUp(con, ps, rs);
116                    }
117            }
118    
119    }