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_2_0;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.upgrade.v6_2_0.BaseUpgradeAttachments;
019    import com.liferay.portal.util.PortletKeys;
020    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
021    import com.liferay.portlet.messageboards.model.MBMessage;
022    
023    import java.sql.Connection;
024    import java.sql.PreparedStatement;
025    import java.sql.ResultSet;
026    import java.sql.Timestamp;
027    
028    /**
029     * @author Eudaldo Alonso
030     */
031    public class UpgradeMessageBoardsAttachments extends BaseUpgradeAttachments {
032    
033            @Override
034            protected String getClassName() {
035                    return MBMessage.class.getName();
036            }
037    
038            @Override
039            protected long getContainerModelFolderId(
040                            long groupId, long companyId, long resourcePrimKey,
041                            long containerModelId, long userId, String userName,
042                            Timestamp createDate)
043                    throws Exception {
044    
045                    long repositoryId = getRepositoryId(
046                            groupId, companyId, userId, userName, createDate, getClassNameId(),
047                            getPortletId());
048    
049                    long repositoryFolderId = getFolderId(
050                            groupId, companyId, userId, userName, createDate, repositoryId,
051                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, getPortletId(), false);
052    
053                    long threadFolderId = getFolderId(
054                            groupId, companyId, userId, userName, createDate, repositoryId,
055                            repositoryFolderId, String.valueOf(containerModelId), false);
056    
057                    long messageFolderId = getFolderId(
058                            groupId, companyId, userId, userName, createDate, repositoryId,
059                            threadFolderId, String.valueOf(resourcePrimKey), false);
060    
061                    return messageFolderId;
062            }
063    
064            @Override
065            protected String getDirName(long containerModelId, long resourcePrimKey) {
066                    return "messageboards/" + containerModelId + "/" + resourcePrimKey;
067            }
068    
069            @Override
070            protected String getPortletId() {
071                    return PortletKeys.MESSAGE_BOARDS;
072            }
073    
074            @Override
075            protected void updateAttachments() throws Exception {
076                    Connection con = null;
077                    PreparedStatement ps = null;
078                    ResultSet rs = null;
079    
080                    try {
081                            con = DataAccess.getUpgradeOptimizedConnection();
082    
083                            ps = con.prepareStatement(
084                                    "select messageId, groupId, companyId, userId, userName, " +
085                                            "threadId from MBMessage where classNameId = 0 and " +
086                                                    "classPK = 0");
087    
088                            rs = ps.executeQuery();
089    
090                            while (rs.next()) {
091                                    long messageId = rs.getLong("messageId");
092                                    long groupId = rs.getLong("groupId");
093                                    long companyId = rs.getLong("companyId");
094                                    long userId = rs.getLong("userId");
095                                    String userName = rs.getString("userName");
096                                    long threadId = rs.getLong("threadId");
097    
098                                    updateEntryAttachments(
099                                            companyId, groupId, messageId, threadId, userId, userName);
100                            }
101                    }
102                    finally {
103                            DataAccess.cleanUp(con, ps, rs);
104                    }
105            }
106    
107    }