001    /**
002     * Copyright (c) 2000-2013 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.portal.util.PortletKeys;
021    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
022    import com.liferay.portlet.wiki.model.WikiPage;
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 String getClassName() {
036                    return WikiPage.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                            StringBundler sb = new StringBundler(4);
085    
086                            sb.append("select resourcePrimKey, groupId, companyId, ");
087                            sb.append("MIN(userId) as userId, MIN(userName) as userName, ");
088                            sb.append("nodeId from WikiPage group by resourcePrimKey, ");
089                            sb.append("groupId, companyId, nodeId");
090    
091                            ps = con.prepareStatement(sb.toString());
092    
093                            rs = ps.executeQuery();
094    
095                            while (rs.next()) {
096                                    long resourcePrimKey = rs.getLong("resourcePrimKey");
097                                    long groupId = rs.getLong("groupId");
098                                    long companyId = rs.getLong("companyId");
099                                    long userId = rs.getLong("userId");
100                                    String userName = rs.getString("userName");
101                                    long nodeId = rs.getLong("nodeId");
102    
103                                    updateEntryAttachments(
104                                            companyId, groupId, resourcePrimKey, nodeId, userId,
105                                            userName);
106                            }
107                    }
108                    finally {
109                            DataAccess.cleanUp(con, ps, rs);
110                    }
111            }
112    
113    }