001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.upgrade.v5_1_5;
016    
017    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
018    
019    /**
020     * @author Brian Wing Shun Chan
021     */
022    public class UpgradeMessageBoards extends UpgradeProcess {
023    
024            @Override
025            protected void doUpgrade() throws Exception {
026                    updateGroupId();
027                    updateMessageClassNameId();
028                    updateMessageFlagThreadId();
029                    updateMessagePriority();
030            }
031    
032            protected void updateGroupId() throws Exception {
033                    StringBuilder sb = new StringBuilder();
034    
035                    sb.append("update MBCategory set threadCount = (select count(*) from ");
036                    sb.append("MBThread where MBThread.categoryId = ");
037                    sb.append("MBCategory.categoryId)");
038    
039                    runSQL(sb.toString());
040    
041                    sb = new StringBuilder();
042    
043                    sb.append("update MBCategory set messageCount = (select count(*) ");
044                    sb.append("from MBMessage where MBMessage.categoryId = ");
045                    sb.append("MBCategory.categoryId)");
046    
047                    runSQL(sb.toString());
048    
049                    sb = new StringBuilder();
050    
051                    sb.append("update MBMessage set groupId = (select groupId from ");
052                    sb.append("MBCategory where MBCategory.categoryId = ");
053                    sb.append("MBMessage.categoryId)");
054    
055                    runSQL(sb.toString());
056    
057                    sb = new StringBuilder();
058    
059                    sb.append("update MBThread set groupId = (select groupId from ");
060                    sb.append("MBCategory where MBCategory.categoryId = ");
061                    sb.append("MBThread.categoryId)");
062    
063                    runSQL(sb.toString());
064            }
065    
066            protected void updateMessageClassNameId() throws Exception {
067                    StringBuilder sb = new StringBuilder();
068    
069                    sb.append("update MBMessage set classNameId = (select classNameId ");
070                    sb.append("from MBDiscussion where MBDiscussion.threadId = ");
071                    sb.append("MBMessage.threadId), classPK = (select classPK from ");
072                    sb.append("MBDiscussion where MBDiscussion.threadId = ");
073                    sb.append("MBMessage.threadId)");
074    
075                    runSQL(sb.toString());
076            }
077    
078            protected void updateMessageFlagThreadId() throws Exception {
079                    StringBuilder sb = new StringBuilder();
080    
081                    sb.append("update MBMessageFlag set threadId = (select threadId from ");
082                    sb.append("MBMessage where MBMessage.messageId = ");
083                    sb.append("MBMessageFlag.messageId)");
084    
085                    runSQL(sb.toString());
086            }
087    
088            protected void updateMessagePriority() throws Exception {
089                    StringBuilder sb = new StringBuilder();
090    
091                    sb.append("update MBMessage set priority = (select priority from ");
092                    sb.append("MBThread where MBThread.threadId = MBMessage.threadId)");
093    
094                    runSQL(sb.toString());
095            }
096    
097    }