001
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 import java.sql.SQLException;
027
028
031 public class UpgradeDocumentLibrary extends UpgradeProcess {
032
033 @Override
034 protected void doUpgrade() throws Exception {
035 updateFileEntries();
036 }
037
038 protected boolean hasFileEntry(long groupId, long folderId, String title)
039 throws Exception {
040
041 Connection con = null;
042 PreparedStatement ps = null;
043 ResultSet rs = null;
044
045 try {
046 con = DataAccess.getUpgradeOptimizedConnection();
047
048 ps = con.prepareStatement(
049 "select count(*) from DLFileEntry where groupId = ? and " +
050 "folderId = ? and title = ?");
051
052 ps.setLong(1, groupId);
053 ps.setLong(2, folderId);
054 ps.setString(3, title);
055
056 rs = ps.executeQuery();
057
058 while (rs.next()) {
059 int count = rs.getInt(1);
060
061 if (count > 0) {
062 return true;
063 }
064 }
065
066 return false;
067 }
068 finally {
069 DataAccess.cleanUp(con, ps, rs);
070 }
071 }
072
073 protected void updateFileEntries() throws Exception {
074 Connection con = null;
075 PreparedStatement ps = null;
076 ResultSet rs = null;
077
078 try {
079 con = DataAccess.getUpgradeOptimizedConnection();
080
081 ps = con.prepareStatement(
082 "select fileEntryId, groupId, folderId, title, extension, " +
083 "version from DLFileEntry");
084
085 rs = ps.executeQuery();
086
087 while (rs.next()) {
088 long fileEntryId = rs.getLong("fileEntryId");
089 long groupId = rs.getLong("groupId");
090 long folderId = rs.getLong("folderId");
091 String title = rs.getString("title");
092 String extension = GetterUtil.getString(
093 rs.getString("extension"));
094 String version = rs.getString("version");
095
096 String periodAndExtension = StringPool.PERIOD.concat(extension);
097
098 if (!title.endsWith(periodAndExtension)) {
099 continue;
100 }
101
102 title = FileUtil.stripExtension(title);
103
104 String uniqueTitle = title;
105
106 int count = 0;
107
108 while (hasFileEntry(groupId, folderId, uniqueTitle) ||
109 ((count != 0) &&
110 hasFileEntry(
111 groupId, folderId,
112 uniqueTitle + periodAndExtension))) {
113
114 count++;
115
116 uniqueTitle = title + String.valueOf(count);
117 }
118
119 if (count <= 0) {
120 continue;
121 }
122
123 uniqueTitle += periodAndExtension;
124
125 updateFileEntry(fileEntryId, version, uniqueTitle);
126 }
127 }
128 finally {
129 DataAccess.cleanUp(con, ps, rs);
130 }
131 }
132
133 protected void updateFileEntry(
134 long fileEntryId, String version, String newTitle)
135 throws SQLException {
136
137 Connection con = null;
138 PreparedStatement ps = null;
139
140 try {
141 con = DataAccess.getUpgradeOptimizedConnection();
142
143 ps = con.prepareStatement(
144 "update DLFileEntry set title = ? where fileEntryId = ?");
145
146 ps.setString(1, newTitle);
147 ps.setLong(2, fileEntryId);
148
149 ps.executeUpdate();
150
151 DataAccess.cleanUp(ps);
152
153 ps = con.prepareStatement(
154 "update DLFileVersion set title = ? where fileEntryId = " +
155 "? and version = ?");
156
157 ps.setString(1, newTitle);
158 ps.setLong(2, fileEntryId);
159 ps.setString(3, version);
160
161 ps.executeUpdate();
162 }
163 finally {
164 DataAccess.cleanUp(con, ps);
165 }
166 }
167
168 }