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