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_1_1;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.util.FileUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    
022    import java.sql.Connection;
023    import java.sql.PreparedStatement;
024    import java.sql.ResultSet;
025    
026    /**
027     * @author Sergio Gonz??lez
028     */
029    public class UpgradeDocumentLibrary extends UpgradeProcess {
030    
031            @Override
032            protected void doUpgrade() throws Exception {
033                    updateFileEntries();
034            }
035    
036            protected boolean hasFileEntry(long groupId, long folderId, String title)
037                    throws Exception {
038    
039                    Connection con = null;
040                    PreparedStatement ps = null;
041                    ResultSet rs = null;
042    
043                    try {
044                            con = DataAccess.getUpgradeOptimizedConnection();
045    
046                            ps = con.prepareStatement(
047                                    "select count(*) from DLFileEntry where groupId = ? and " +
048                                            "folderId = ? and title = ?");
049    
050                            ps.setLong(1, groupId);
051                            ps.setLong(2, folderId);
052                            ps.setString(3, title);
053    
054                            rs = ps.executeQuery();
055    
056                            while (rs.next()) {
057                                    int count = rs.getInt(1);
058    
059                                    if (count > 0) {
060                                            return true;
061                                    }
062                            }
063    
064                            return false;
065                    }
066                    finally {
067                            DataAccess.cleanUp(con, ps, rs);
068                    }
069            }
070    
071            protected void updateFileEntries() throws Exception {
072                    Connection con = null;
073                    PreparedStatement ps = null;
074                    ResultSet rs = null;
075    
076                    try {
077                            con = DataAccess.getUpgradeOptimizedConnection();
078    
079                            ps = con.prepareStatement(
080                                    "select fileEntryId, groupId, folderId, title, extension, " +
081                                            "version from DLFileEntry");
082    
083                            rs = ps.executeQuery();
084    
085                            while (rs.next()) {
086                                    long fileEntryId = rs.getLong("fileEntryId");
087                                    long groupId = rs.getLong("groupId");
088                                    long folderId = rs.getLong("folderId");
089                                    String title = rs.getString("title");
090                                    String extension = rs.getString("extension");
091                                    String version = rs.getString("version");
092    
093                                    String periodAndExtension = StringPool.PERIOD + extension;
094    
095                                    if (!title.endsWith(periodAndExtension)) {
096                                            continue;
097                                    }
098    
099                                    title = FileUtil.stripExtension(title);
100    
101                                    String uniqueTitle = title;
102    
103                                    int count = 0;
104    
105                                    while (hasFileEntry(groupId, folderId, uniqueTitle) ||
106                                               ((count != 0) &&
107                                                    hasFileEntry(
108                                                            groupId, folderId,
109                                                            uniqueTitle + periodAndExtension))) {
110    
111                                            count++;
112    
113                                            uniqueTitle = title + String.valueOf(count);
114                                    }
115    
116                                    if (count <= 0) {
117                                            continue;
118                                    }
119    
120                                    uniqueTitle += periodAndExtension;
121    
122                                    runSQL(
123                                            "update DLFileEntry set title = '" + uniqueTitle +
124                                                    "' where fileEntryId = " + fileEntryId);
125                                    runSQL(
126                                            "update DLFileVersion set title = '" + uniqueTitle +
127                                                    "' where fileEntryId = " + fileEntryId +
128                                                            " and DLFileVersion.version = '" + version + "'");
129                            }
130                    }
131                    finally {
132                            DataAccess.cleanUp(con, ps, rs);
133                    }
134            }
135    
136    }