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