001
014
015 package com.liferay.portal.upgrade.v6_1_1;
016
017 import com.liferay.portal.kernel.upgrade.UpgradeProcess;
018 import com.liferay.portal.kernel.util.FileUtil;
019 import com.liferay.portal.kernel.util.GetterUtil;
020 import com.liferay.portal.kernel.util.LoggingTimer;
021 import com.liferay.portal.kernel.util.StringPool;
022
023 import java.sql.PreparedStatement;
024 import java.sql.ResultSet;
025 import java.sql.SQLException;
026
027
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 try (PreparedStatement ps = connection.prepareStatement(
041 "select count(*) from DLFileEntry where groupId = ? and " +
042 "folderId = ? and title = ?")) {
043
044 ps.setLong(1, groupId);
045 ps.setLong(2, folderId);
046 ps.setString(3, title);
047
048 try (ResultSet rs = ps.executeQuery()) {
049 while (rs.next()) {
050 int count = rs.getInt(1);
051
052 if (count > 0) {
053 return true;
054 }
055 }
056
057 return false;
058 }
059 }
060 }
061
062 protected void updateFileEntries() throws Exception {
063 try (LoggingTimer loggingTimer = new LoggingTimer();
064 PreparedStatement ps = connection.prepareStatement(
065 "select fileEntryId, groupId, folderId, title, extension, " +
066 "version from DLFileEntry");
067 ResultSet rs = ps.executeQuery()) {
068
069 while (rs.next()) {
070 long fileEntryId = rs.getLong("fileEntryId");
071 long groupId = rs.getLong("groupId");
072 long folderId = rs.getLong("folderId");
073 String title = rs.getString("title");
074 String extension = GetterUtil.getString(
075 rs.getString("extension"));
076 String version = rs.getString("version");
077
078 String periodAndExtension = StringPool.PERIOD.concat(extension);
079
080 if (!title.endsWith(periodAndExtension)) {
081 continue;
082 }
083
084 title = FileUtil.stripExtension(title);
085
086 String uniqueTitle = title;
087
088 int count = 0;
089
090 while (hasFileEntry(groupId, folderId, uniqueTitle) ||
091 ((count != 0) &&
092 hasFileEntry(
093 groupId, folderId,
094 uniqueTitle + periodAndExtension))) {
095
096 count++;
097
098 uniqueTitle = title + String.valueOf(count);
099 }
100
101 if (count <= 0) {
102 continue;
103 }
104
105 uniqueTitle += periodAndExtension;
106
107 updateFileEntry(fileEntryId, version, uniqueTitle);
108 }
109 }
110 }
111
112 protected void updateFileEntry(
113 long fileEntryId, String version, String newTitle)
114 throws SQLException {
115
116 try (PreparedStatement ps1 = connection.prepareStatement(
117 "update DLFileEntry set title = ? where fileEntryId = ?")) {
118
119 ps1.setString(1, newTitle);
120 ps1.setLong(2, fileEntryId);
121
122 ps1.executeUpdate();
123
124 try (PreparedStatement ps2 = connection.prepareStatement(
125 "update DLFileVersion set title = ? where fileEntryId = " +
126 "? and version = ?")) {
127
128 ps2.setString(1, newTitle);
129 ps2.setLong(2, fileEntryId);
130 ps2.setString(3, version);
131
132 ps2.executeUpdate();
133 }
134 }
135 }
136
137 }