001
014
015 package com.liferay.portal.kernel.upgrade.v6_2_0;
016
017 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.upgrade.UpgradeProcess;
021 import com.liferay.portal.kernel.util.FileUtil;
022 import com.liferay.portal.kernel.util.MimeTypesUtil;
023 import com.liferay.portal.kernel.util.StringBundler;
024 import com.liferay.portal.kernel.util.StringPool;
025 import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
026 import com.liferay.portal.model.CompanyConstants;
027 import com.liferay.portal.util.PortalUtil;
028 import com.liferay.portlet.documentlibrary.NoSuchDirectoryException;
029 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
030 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
031 import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
032
033 import java.sql.Connection;
034 import java.sql.PreparedStatement;
035 import java.sql.ResultSet;
036 import java.sql.Timestamp;
037
038
041 public abstract class BaseUpgradeAttachments extends UpgradeProcess {
042
043 protected long addDLFileEntry(
044 long groupId, long companyId, long userId, String className,
045 long classPK, String userName, Timestamp createDate,
046 long repositoryId, long folderId, String name, String extension,
047 String mimeType, String title, long size)
048 throws Exception {
049
050 Connection con = null;
051 PreparedStatement ps = null;
052
053 try {
054 long fileEntryId = increment();
055
056 con = DataAccess.getUpgradeOptimizedConnection();
057
058 StringBundler sb = new StringBundler(10);
059
060 sb.append("insert into DLFileEntry (uuid_, fileEntryId, groupId, ");
061 sb.append("companyId, userId, userName, versionUserId, ");
062 sb.append("versionUserName, createDate, modifiedDate, ");
063 sb.append("classNameId, classPK, repositoryId, folderId, name, ");
064 sb.append("extension, mimeType, title, description, ");
065 sb.append("extraSettings, fileEntryTypeId, version, size_, ");
066 sb.append("readCount, smallImageId, largeImageId, ");
067 sb.append("custom1ImageId, custom2ImageId) values (?, ?, ?, ?, ");
068 sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
069 sb.append("?, ?, ?, ?, ?, ?)");
070
071 String sql = sb.toString();
072
073 ps = con.prepareStatement(sql);
074
075 ps.setString(1, PortalUUIDUtil.generate());
076 ps.setLong(2, fileEntryId);
077 ps.setLong(3, groupId);
078 ps.setLong(4, companyId);
079 ps.setLong(5, userId);
080 ps.setString(6, userName);
081 ps.setLong(7, userId);
082 ps.setString(8, userName);
083 ps.setTimestamp(9, createDate);
084 ps.setTimestamp(10, createDate);
085 ps.setLong(11, PortalUtil.getClassNameId(className));
086 ps.setLong(12, classPK);
087 ps.setLong(13, repositoryId);
088 ps.setLong(14, folderId);
089 ps.setString(15, name);
090 ps.setString(16, extension);
091 ps.setString(17, mimeType);
092 ps.setString(18, title);
093 ps.setString(19, StringPool.BLANK);
094 ps.setString(20, StringPool.BLANK);
095 ps.setLong(21, 0);
096 ps.setString(22, "1.0");
097 ps.setLong(23, size);
098 ps.setInt(24, 0);
099 ps.setLong(25, 0);
100 ps.setLong(26, 0);
101 ps.setLong(27, 0);
102 ps.setLong(28, 0);
103
104 ps.executeUpdate();
105
106 return fileEntryId;
107 }
108 finally {
109 DataAccess.cleanUp(con, ps);
110 }
111 }
112
113 protected void addDLFileVersion(
114 long fileVersionId, long groupId, long companyId, long userId,
115 String userName, Timestamp createDate, long repositoryId,
116 long folderId, long fileEntryId, String extension, String mimeType,
117 String title, long size)
118 throws Exception {
119
120 Connection con = null;
121 PreparedStatement ps = null;
122
123 try {
124 con = DataAccess.getUpgradeOptimizedConnection();
125
126 StringBundler sb = new StringBundler(8);
127
128 sb.append("insert into DLFileVersion (fileVersionId, groupId, ");
129 sb.append("companyId, userId, userName, createDate, ");
130 sb.append("modifiedDate, repositoryId, folderId, fileEntryId, ");
131 sb.append("extension, mimeType, title, description, changeLog, ");
132 sb.append("extraSettings, fileEntryTypeId, version, size_, ");
133 sb.append("status, statusByUserId, statusByUserName, statusDate) ");
134 sb.append("values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
135 sb.append("?, ?, ?, ?, ?, ?, ?, ?)");
136
137 String sql = sb.toString();
138
139 ps = con.prepareStatement(sql);
140
141 ps.setLong(1, fileVersionId);
142 ps.setLong(2, groupId);
143 ps.setLong(3, companyId);
144 ps.setLong(4, userId);
145 ps.setString(5, userName);
146 ps.setTimestamp(6, createDate);
147 ps.setTimestamp(7, createDate);
148 ps.setLong(8, repositoryId);
149 ps.setLong(9, folderId);
150 ps.setLong(10, fileEntryId);
151 ps.setString(11, extension);
152 ps.setString(12, mimeType);
153 ps.setString(13, title);
154 ps.setString(14, StringPool.BLANK);
155 ps.setString(15, StringPool.BLANK);
156 ps.setString(16, StringPool.BLANK);
157 ps.setLong(17, 0);
158 ps.setString(18, "1.0");
159 ps.setLong(19, size);
160 ps.setInt(20, 0);
161 ps.setLong(21, userId);
162 ps.setString(22, userName);
163 ps.setTimestamp(23, createDate);
164
165 ps.executeUpdate();
166 }
167 finally {
168 DataAccess.cleanUp(con, ps);
169 }
170 }
171
172 protected long addDLFolder(
173 long folderId, long groupId, long companyId, long userId,
174 String userName, Timestamp createDate, long repositoryId,
175 boolean mountPoint, long parentFolderId, String name,
176 boolean hidden)
177 throws Exception {
178
179 Connection con = null;
180 PreparedStatement ps = null;
181
182 try {
183 con = DataAccess.getUpgradeOptimizedConnection();
184
185 StringBundler sb = new StringBundler(8);
186
187 sb.append("insert into DLFolder (uuid_, folderId, groupId, ");
188 sb.append("companyId, userId, userName, createDate, ");
189 sb.append("modifiedDate, repositoryId, mountPoint, ");
190 sb.append("parentFolderId, name, description, lastPostDate, ");
191 sb.append("defaultFileEntryTypeId, hidden_, ");
192 sb.append("overrideFileEntryTypes, status, statusByUserId, ");
193 sb.append("statusByUserName, statusDate) values (?, ?, ?, ?, ?, ");
194 sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
195
196 String sql = sb.toString();
197
198 ps = con.prepareStatement(sql);
199
200 ps.setString(1, PortalUUIDUtil.generate());
201 ps.setLong(2, folderId);
202 ps.setLong(3, groupId);
203 ps.setLong(4, companyId);
204 ps.setLong(5, userId);
205 ps.setString(6, userName);
206 ps.setTimestamp(7, createDate);
207 ps.setTimestamp(8, createDate);
208 ps.setLong(9, repositoryId);
209 ps.setBoolean(10, mountPoint);
210 ps.setLong(11, parentFolderId);
211 ps.setString(12, name);
212 ps.setString(13, StringPool.BLANK);
213 ps.setTimestamp(14, createDate);
214 ps.setLong(15, 0);
215 ps.setBoolean(16, hidden);
216 ps.setBoolean(17, false);
217 ps.setLong(18, 0);
218 ps.setLong(19, userId);
219 ps.setString(20, userName);
220 ps.setTimestamp(21, createDate);
221
222 ps.executeUpdate();
223
224 return folderId;
225 }
226 finally {
227 DataAccess.cleanUp(con, ps);
228 }
229 }
230
231 protected long addRepository(
232 long groupId, long companyId, long userId, String userName,
233 Timestamp createDate, long classNameId, String portletId)
234 throws Exception {
235
236 long repositoryId = increment();
237
238 long folderId = addDLFolder(
239 increment(), groupId, companyId, userId, userName, createDate,
240 repositoryId, true, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
241 portletId, true);
242
243 Connection con = null;
244 PreparedStatement ps = null;
245
246 try {
247 con = DataAccess.getUpgradeOptimizedConnection();
248
249 StringBundler sb = new StringBundler(8);
250
251 sb.append("insert into Repository (uuid_, repositoryId, groupId, ");
252 sb.append("companyId, userId, userName, createDate, ");
253 sb.append("modifiedDate, classNameId, name, description, ");
254 sb.append("portletId, typeSettings, dlFolderId) values (?, ?, ?, ");
255 sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
256
257 ps = con.prepareStatement(sb.toString());
258
259 ps.setString(1, PortalUUIDUtil.generate());
260 ps.setLong(2, repositoryId);
261 ps.setLong(3, groupId);
262 ps.setLong(4, companyId);
263 ps.setLong(5, userId);
264 ps.setString(6, userName);
265 ps.setTimestamp(7, createDate);
266 ps.setTimestamp(8, createDate);
267 ps.setLong(9, classNameId);
268 ps.setString(10, portletId);
269 ps.setString(11, StringPool.BLANK);
270 ps.setString(12, portletId);
271 ps.setString(13, StringPool.BLANK);
272 ps.setLong(14, folderId);
273
274 ps.executeUpdate();
275
276 return repositoryId;
277 }
278 finally {
279 DataAccess.cleanUp(con, ps);
280 }
281 }
282
283 @Override
284 protected void doUpgrade() throws Exception {
285 updateAttachments();
286 }
287
288 protected String[] getAttachments(
289 long companyId, long containerModelId, long resourcePrimKey)
290 throws Exception {
291
292 String dirName = getDirName(containerModelId, resourcePrimKey);
293
294 String[] attachments = null;
295
296 try {
297 attachments = DLStoreUtil.getFileNames(
298 companyId, CompanyConstants.SYSTEM, dirName);
299 }
300 catch (NoSuchDirectoryException nsde) {
301 }
302
303 return attachments;
304 }
305
306 protected abstract String getClassName();
307
308 protected long getClassNameId() {
309 return PortalUtil.getClassNameId(getClassName());
310 }
311
312 protected long getContainerModelFolderId(
313 long groupId, long companyId, long resourcePrimKey,
314 long containerModelId, long userId, String userName,
315 Timestamp createDate)
316 throws Exception {
317
318 return DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
319 }
320
321 protected abstract String getDirName(
322 long containerModelId, long resourcePrimKey);
323
324 protected long getFolderId(
325 long groupId, long companyId, long userId, String userName,
326 Timestamp createDate, long repositoryId, long parentFolderId,
327 String name, boolean hidden)
328 throws Exception {
329
330 Connection con = null;
331 PreparedStatement ps = null;
332 ResultSet rs = null;
333
334 try {
335 con = DataAccess.getUpgradeOptimizedConnection();
336
337 ps = con.prepareStatement(
338 "select folderId from DLFolder where repositoryId = ? and " +
339 "parentFolderId = ? and name = ?");
340
341 ps.setLong(1, repositoryId);
342 ps.setLong(2, parentFolderId);
343 ps.setString(3, name);
344
345 rs = ps.executeQuery();
346
347 while (rs.next()) {
348 int folderId = rs.getInt(1);
349
350 return folderId;
351 }
352 }
353 finally {
354 DataAccess.cleanUp(con, ps);
355 }
356
357 return addDLFolder(
358 increment(), groupId, companyId, userId, userName, createDate,
359 repositoryId, false, parentFolderId, name, hidden);
360 }
361
362 protected abstract String getPortletId();
363
364 protected long getRepositoryId(
365 long groupId, long companyId, long userId, String userName,
366 Timestamp createDate, long classNameId, String portletId)
367 throws Exception {
368
369 Connection con = null;
370 PreparedStatement ps = null;
371 ResultSet rs = null;
372
373 try {
374 con = DataAccess.getUpgradeOptimizedConnection();
375
376 ps = con.prepareStatement(
377 "select repositoryId from Repository where groupId = ? and " +
378 "name = ? and portletId = ?");
379
380 ps.setLong(1, groupId);
381 ps.setString(2, portletId);
382 ps.setString(3, portletId);
383
384 rs = ps.executeQuery();
385
386 while (rs.next()) {
387 int repositoryId = rs.getInt(1);
388
389 return repositoryId;
390 }
391 }
392 finally {
393 DataAccess.cleanUp(con, ps);
394 }
395
396 return addRepository(
397 groupId, companyId, userId, userName, createDate, classNameId,
398 portletId);
399 }
400
401 protected abstract void updateAttachments() throws Exception;
402
403 protected void updateEntryAttachments(
404 long companyId, long groupId, long resourcePrimKey,
405 long containerModelId, long userId, String userName)
406 throws Exception {
407
408 String[] attachments = getAttachments(
409 companyId, containerModelId, resourcePrimKey);
410
411 if ((attachments == null) || (attachments.length == 0)) {
412 return;
413 }
414
415 Timestamp createDate = new Timestamp(System.currentTimeMillis());
416
417 long repositoryId = getRepositoryId(
418 groupId, companyId, userId, userName, createDate,
419 PortalUtil.getClassNameId(_LIFERAY_REPOSITORY_CLASS_NAME),
420 getPortletId());
421 long containerModelFolderId = getContainerModelFolderId(
422 groupId, companyId, resourcePrimKey, containerModelId, userId,
423 userName, createDate);
424
425 for (String attachment : attachments) {
426 String name = String.valueOf(
427 increment(DLFileEntry.class.getName()));
428
429 String title = FileUtil.getShortFileName(attachment);
430
431 String extension = FileUtil.getExtension(title);
432
433 String mimeType = MimeTypesUtil.getExtensionContentType(extension);
434
435 long size = DLStoreUtil.getFileSize(
436 companyId, CompanyConstants.SYSTEM, attachment);
437
438 long fileEntryId = addDLFileEntry(
439 groupId, companyId, userId, getClassName(), resourcePrimKey,
440 userName, createDate, repositoryId, containerModelFolderId,
441 name, extension, mimeType, title, size);
442
443 addDLFileVersion(
444 increment(), groupId, companyId, userId, userName, createDate,
445 repositoryId, containerModelFolderId, fileEntryId, extension,
446 mimeType, title, size);
447
448 byte[] bytes = DLStoreUtil.getFileAsBytes(
449 companyId, CompanyConstants.SYSTEM, attachment);
450
451 DLStoreUtil.addFile(
452 companyId, containerModelFolderId, name, false, bytes);
453
454 try {
455 DLStoreUtil.deleteFile(
456 companyId, CompanyConstants.SYSTEM, attachment);
457 }
458 catch (Exception e) {
459 if (_log.isWarnEnabled()) {
460 _log.warn(
461 "Unable to delete the attachment " + attachment, e);
462 }
463 }
464 }
465 }
466
467 private static final String _LIFERAY_REPOSITORY_CLASS_NAME =
468 "com.liferay.portal.repository.liferayrepository.LiferayRepository";
469
470 private static Log _log = LogFactoryUtil.getLog(
471 BaseUpgradeAttachments.class);
472
473 }