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