001
014
015 package com.liferay.portal.upgrade.v7_0_0;
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 import com.liferay.portal.kernel.util.Validator;
023 import com.liferay.portlet.documentlibrary.util.DLUtil;
024
025 import java.sql.Connection;
026 import java.sql.PreparedStatement;
027 import java.sql.ResultSet;
028
029
032 public class UpgradeDocumentLibrary extends UpgradeProcess {
033
034 @Override
035 protected void doUpgrade() throws Exception {
036
037
038
039 runSQL("alter table DLFileEntry add fileName VARCHAR(255) null");
040
041 updateFileEntryFileNames();
042
043
044
045 runSQL("alter table DLFileVersion add fileName VARCHAR(255) null");
046
047 updateFileVersionFileNames();
048 }
049
050 protected boolean hasFileEntry(long groupId, long folderId, String fileName)
051 throws Exception {
052
053 Connection con = null;
054 PreparedStatement ps = null;
055 ResultSet rs = null;
056
057 try {
058 con = DataAccess.getUpgradeOptimizedConnection();
059
060 ps = con.prepareStatement(
061 "select count(*) from DLFileEntry where groupId = ? and " +
062 "folderId = ? and fileName = ?");
063
064 ps.setLong(1, groupId);
065 ps.setLong(2, folderId);
066 ps.setString(3, fileName);
067
068 rs = ps.executeQuery();
069
070 while (rs.next()) {
071 int count = rs.getInt(1);
072
073 if (count > 0) {
074 return true;
075 }
076 }
077
078 return false;
079 }
080 finally {
081 DataAccess.cleanUp(con, ps, rs);
082 }
083 }
084
085 protected void updateFileEntryFileName(long fileEntryId, String fileName)
086 throws Exception {
087
088 Connection con = null;
089 PreparedStatement ps = null;
090
091 try {
092 con = DataAccess.getUpgradeOptimizedConnection();
093
094 ps = con.prepareStatement(
095 "update DLFileEntry set fileName = ? where fileEntryId = ?");
096
097 ps.setString(1, fileName);
098 ps.setLong(2, fileEntryId);
099
100 ps.executeUpdate();
101 }
102 finally {
103 DataAccess.cleanUp(con, ps);
104 }
105 }
106
107 protected void updateFileEntryFileNames() throws Exception {
108 Connection con = null;
109 PreparedStatement ps = null;
110 ResultSet rs = null;
111
112 try {
113 con = DataAccess.getUpgradeOptimizedConnection();
114
115 ps = con.prepareStatement(
116 "select fileEntryId, groupId, folderId, extension, title, " +
117 "version from DLFileEntry");
118
119 rs = ps.executeQuery();
120
121 while (rs.next()) {
122 long fileEntryId = rs.getLong("fileEntryId");
123 long groupId = rs.getLong("groupId");
124 long folderId = rs.getLong("folderId");
125 String extension = GetterUtil.getString(
126 rs.getString("extension"));
127 String title = GetterUtil.getString(rs.getString("title"));
128 String version = rs.getString("version");
129
130 String uniqueFileName = DLUtil.getSanitizedFileName(
131 title, extension);
132
133 String titleExtension = StringPool.BLANK;
134 String titleWithoutExtension = title;
135
136 if (title.endsWith(StringPool.PERIOD + extension)) {
137 titleExtension = extension;
138 titleWithoutExtension = FileUtil.stripExtension(title);
139 }
140
141 String uniqueTitle = StringPool.BLANK;
142
143 for (int i = 1;; i++) {
144 if (!hasFileEntry(groupId, folderId, uniqueFileName)) {
145 break;
146 }
147
148 uniqueTitle =
149 titleWithoutExtension + StringPool.UNDERLINE +
150 String.valueOf(i);
151
152 if (Validator.isNotNull(titleExtension)) {
153 uniqueTitle += StringPool.PERIOD.concat(titleExtension);
154 }
155
156 uniqueFileName = DLUtil.getSanitizedFileName(
157 uniqueTitle, extension);
158 }
159
160 updateFileEntryFileName(fileEntryId, uniqueFileName);
161
162 if (Validator.isNotNull(uniqueTitle)) {
163 updateFileEntryTitle(fileEntryId, uniqueTitle, version);
164 }
165 }
166 }
167 finally {
168 DataAccess.cleanUp(con, ps, rs);
169 }
170 }
171
172 protected void updateFileEntryTitle(
173 long fileEntryId, String title, String version)
174 throws Exception {
175
176 Connection con = null;
177 PreparedStatement ps = null;
178
179 try {
180 con = DataAccess.getUpgradeOptimizedConnection();
181
182 ps = con.prepareStatement(
183 "update DLFileEntry set title = ? where fileEntryId = ?");
184
185 ps.setString(1, title);
186 ps.setLong(2, fileEntryId);
187
188 ps.executeUpdate();
189
190 ps = con.prepareStatement(
191 "update DLFileVersion set title = ? where fileEntryId = " +
192 "? and version = ?");
193
194 ps.setString(1, title);
195 ps.setLong(2, fileEntryId);
196 ps.setString(3, version);
197
198 ps.executeUpdate();
199 }
200 finally {
201 DataAccess.cleanUp(con, ps);
202 }
203 }
204
205 protected void updateFileVersionFileName(
206 long fileVersionId, String fileName)
207 throws Exception {
208
209 Connection con = null;
210 PreparedStatement ps = null;
211
212 try {
213 con = DataAccess.getUpgradeOptimizedConnection();
214
215 ps = con.prepareStatement(
216 "update DLFileVersion set fileName = ? where " +
217 "fileVersionId = ?");
218
219 ps.setString(1, fileName);
220 ps.setLong(2, fileVersionId);
221
222 ps.executeUpdate();
223 }
224 finally {
225 DataAccess.cleanUp(con, ps);
226 }
227 }
228
229 protected void updateFileVersionFileNames() throws Exception {
230 Connection con = null;
231 PreparedStatement ps = null;
232 ResultSet rs = null;
233
234 try {
235 con = DataAccess.getUpgradeOptimizedConnection();
236
237 ps = con.prepareStatement(
238 "select fileVersionId, extension, title from DLFileVersion");
239
240 rs = ps.executeQuery();
241
242 while (rs.next()) {
243 long fileVersionId = rs.getLong("fileVersionId");
244 String extension = GetterUtil.getString(
245 rs.getString("extension"));
246 String title = GetterUtil.getString(rs.getString("title"));
247
248 String fileName = DLUtil.getSanitizedFileName(title, extension);
249
250 updateFileVersionFileName(fileVersionId, fileName);
251 }
252 }
253 finally {
254 DataAccess.cleanUp(con, ps, rs);
255 }
256 }
257
258 }