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.portal.kernel.util.StringBundler;
020    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
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 UpgradeWikiAttachments extends BaseUpgradeAttachments {
031    
032            @Override
033            protected String getClassName() {
034                    return "com.liferay.wiki.model.WikiPage";
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 nodeFolderId = getFolderId(
053                            groupId, companyId, userId, userName, createDate, repositoryId,
054                            repositoryFolderId, String.valueOf(containerModelId), false);
055    
056                    long pageFolderId = getFolderId(
057                            groupId, companyId, userId, userName, createDate, repositoryId,
058                            nodeFolderId, String.valueOf(resourcePrimKey), false);
059    
060                    return pageFolderId;
061            }
062    
063            @Override
064            protected String getDirName(long containerModelId, long resourcePrimKey) {
065                    return "wiki/" + resourcePrimKey;
066            }
067    
068            @Override
069            protected String getPortletId() {
070                    return "36";
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                            StringBundler sb = new StringBundler(4);
083    
084                            sb.append("select resourcePrimKey, groupId, companyId, ");
085                            sb.append("MIN(userId) as userId, MIN(userName) as userName, ");
086                            sb.append("nodeId from WikiPage group by resourcePrimKey, ");
087                            sb.append("groupId, companyId, nodeId");
088    
089                            ps = con.prepareStatement(sb.toString());
090    
091                            rs = ps.executeQuery();
092    
093                            while (rs.next()) {
094                                    long resourcePrimKey = rs.getLong("resourcePrimKey");
095                                    long groupId = rs.getLong("groupId");
096                                    long companyId = rs.getLong("companyId");
097                                    long userId = rs.getLong("userId");
098                                    String userName = rs.getString("userName");
099                                    long nodeId = rs.getLong("nodeId");
100    
101                                    updateEntryAttachments(
102                                            companyId, groupId, resourcePrimKey, nodeId, userId,
103                                            userName);
104                            }
105                    }
106                    finally {
107                            DataAccess.cleanUp(con, ps, rs);
108                    }
109            }
110    
111    }