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 UpgradeWikiAttachments 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 nodeFolderId = getFolderId(
055                            groupId, companyId, userId, userName, createDate, repositoryId,
056                            repositoryFolderId, String.valueOf(containerModelId), false);
057    
058                    long pageFolderId = getFolderId(
059                            groupId, companyId, userId, userName, createDate, repositoryId,
060                            nodeFolderId, String.valueOf(resourcePrimKey), false);
061    
062                    return pageFolderId;
063            }
064    
065            @Override
066            protected String getDirName(long containerModelId, long resourcePrimKey) {
067                    return "wiki/" + resourcePrimKey;
068            }
069    
070            @Override
071            protected String getPortletId() {
072                    return PortletKeys.WIKI;
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 resourcePrimKey, groupId, companyId, userId, " +
086                                            "userName, nodeId from WikiPage group by resourcePrimKey");
087    
088                            rs = ps.executeQuery();
089    
090                            while (rs.next()) {
091                                    long resourcePrimKey = rs.getLong("resourcePrimKey");
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 nodeId = rs.getLong("nodeId");
097    
098                                    updateEntryAttachments(
099                                            companyId, groupId, resourcePrimKey, nodeId, userId,
100                                            userName);
101                            }
102                    }
103                    finally {
104                            DataAccess.cleanUp(con, ps, rs);
105                    }
106            }
107    
108    }