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