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.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    /**
039     * @author Eudaldo Alonso
040     */
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                    catch (Exception e) {
109                            if (_log.isWarnEnabled()) {
110                                    _log.warn("Unable to add file entry " + name, e);
111                            }
112    
113                            return -1;
114                    }
115                    finally {
116                            DataAccess.cleanUp(con, ps);
117                    }
118            }
119    
120            protected void addDLFileVersion(
121                            long fileVersionId, long groupId, long companyId, long userId,
122                            String userName, Timestamp createDate, long repositoryId,
123                            long folderId, long fileEntryId, String extension, String mimeType,
124                            String title, long size)
125                    throws Exception {
126    
127                    Connection con = null;
128                    PreparedStatement ps = null;
129    
130                    try {
131                            con = DataAccess.getUpgradeOptimizedConnection();
132    
133                            StringBundler sb = new StringBundler(8);
134    
135                            sb.append("insert into DLFileVersion (fileVersionId, groupId, ");
136                            sb.append("companyId, userId, userName, createDate, ");
137                            sb.append("modifiedDate, repositoryId, folderId, fileEntryId, ");
138                            sb.append("extension, mimeType, title, description, changeLog, ");
139                            sb.append("extraSettings, fileEntryTypeId, version, size_, ");
140                            sb.append("status, statusByUserId, statusByUserName, statusDate) ");
141                            sb.append("values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
142                            sb.append("?, ?, ?, ?, ?, ?, ?, ?)");
143    
144                            String sql = sb.toString();
145    
146                            ps = con.prepareStatement(sql);
147    
148                            ps.setLong(1, fileVersionId);
149                            ps.setLong(2, groupId);
150                            ps.setLong(3, companyId);
151                            ps.setLong(4, userId);
152                            ps.setString(5, userName);
153                            ps.setTimestamp(6, createDate);
154                            ps.setTimestamp(7, createDate);
155                            ps.setLong(8, repositoryId);
156                            ps.setLong(9, folderId);
157                            ps.setLong(10, fileEntryId);
158                            ps.setString(11, extension);
159                            ps.setString(12, mimeType);
160                            ps.setString(13, title);
161                            ps.setString(14, StringPool.BLANK);
162                            ps.setString(15, StringPool.BLANK);
163                            ps.setString(16, StringPool.BLANK);
164                            ps.setLong(17, 0);
165                            ps.setString(18, "1.0");
166                            ps.setLong(19, size);
167                            ps.setInt(20, 0);
168                            ps.setLong(21, userId);
169                            ps.setString(22, userName);
170                            ps.setTimestamp(23, createDate);
171    
172                            ps.executeUpdate();
173                    }
174                    catch (Exception e) {
175                            if (_log.isWarnEnabled()) {
176                                    _log.warn(
177                                            "Unable to add file version 1.0 for file entry " + title,
178                                            e);
179                            }
180                    }
181                    finally {
182                            DataAccess.cleanUp(con, ps);
183                    }
184            }
185    
186            protected long addDLFolder(
187                            long folderId, long groupId, long companyId, long userId,
188                            String userName, Timestamp createDate, long repositoryId,
189                            boolean mountPoint, long parentFolderId, String name,
190                            boolean hidden)
191                    throws Exception {
192    
193                    Connection con = null;
194                    PreparedStatement ps = null;
195    
196                    try {
197                            con = DataAccess.getUpgradeOptimizedConnection();
198    
199                            StringBundler sb = new StringBundler(8);
200    
201                            sb.append("insert into DLFolder (uuid_, folderId, groupId, ");
202                            sb.append("companyId, userId, userName, createDate, ");
203                            sb.append("modifiedDate, repositoryId, mountPoint, ");
204                            sb.append("parentFolderId, name, description, lastPostDate, ");
205                            sb.append("defaultFileEntryTypeId, hidden_, ");
206                            sb.append("overrideFileEntryTypes, status, statusByUserId, ");
207                            sb.append("statusByUserName, statusDate) values (?, ?, ?, ?, ?, ");
208                            sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
209    
210                            String sql = sb.toString();
211    
212                            ps = con.prepareStatement(sql);
213    
214                            ps.setString(1, PortalUUIDUtil.generate());
215                            ps.setLong(2, folderId);
216                            ps.setLong(3, groupId);
217                            ps.setLong(4, companyId);
218                            ps.setLong(5, userId);
219                            ps.setString(6, userName);
220                            ps.setTimestamp(7, createDate);
221                            ps.setTimestamp(8, createDate);
222                            ps.setLong(9, repositoryId);
223                            ps.setBoolean(10, mountPoint);
224                            ps.setLong(11, parentFolderId);
225                            ps.setString(12, name);
226                            ps.setString(13, StringPool.BLANK);
227                            ps.setTimestamp(14, createDate);
228                            ps.setLong(15, 0);
229                            ps.setBoolean(16, hidden);
230                            ps.setBoolean(17, false);
231                            ps.setLong(18, 0);
232                            ps.setLong(19, userId);
233                            ps.setString(20, userName);
234                            ps.setTimestamp(21, createDate);
235    
236                            ps.executeUpdate();
237    
238                            return folderId;
239                    }
240                    catch (Exception e) {
241                            if (_log.isWarnEnabled()) {
242                                    _log.warn("Unable to add folder " + name, e);
243                            }
244    
245                            return -1;
246                    }
247                    finally {
248                            DataAccess.cleanUp(con, ps);
249                    }
250            }
251    
252            protected long addRepository(
253                            long groupId, long companyId, long userId, String userName,
254                            Timestamp createDate, long classNameId, String portletId)
255                    throws Exception {
256    
257                    long repositoryId = increment();
258    
259                    long folderId = addDLFolder(
260                            increment(), groupId, companyId, userId, userName, createDate,
261                            repositoryId, true, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
262                            portletId, true);
263    
264                    if (folderId < 0) {
265                            return -1;
266                    }
267    
268                    Connection con = null;
269                    PreparedStatement ps = null;
270    
271                    try {
272                            con = DataAccess.getUpgradeOptimizedConnection();
273    
274                            StringBundler sb = new StringBundler(8);
275    
276                            sb.append("insert into Repository (uuid_, repositoryId, groupId, ");
277                            sb.append("companyId, userId, userName, createDate, ");
278                            sb.append("modifiedDate, classNameId, name, description, ");
279                            sb.append("portletId, typeSettings, dlFolderId) values (?, ?, ?, ");
280                            sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
281    
282                            ps = con.prepareStatement(sb.toString());
283    
284                            ps.setString(1, PortalUUIDUtil.generate());
285                            ps.setLong(2, repositoryId);
286                            ps.setLong(3, groupId);
287                            ps.setLong(4, companyId);
288                            ps.setLong(5, userId);
289                            ps.setString(6, userName);
290                            ps.setTimestamp(7, createDate);
291                            ps.setTimestamp(8, createDate);
292                            ps.setLong(9, classNameId);
293                            ps.setString(10, portletId);
294                            ps.setString(11, StringPool.BLANK);
295                            ps.setString(12, portletId);
296                            ps.setString(13, StringPool.BLANK);
297                            ps.setLong(14, folderId);
298    
299                            ps.executeUpdate();
300    
301                            return repositoryId;
302                    }
303                    catch (Exception e) {
304                            if (_log.isWarnEnabled()) {
305                                    _log.warn(
306                                            "Unable to add repository for portlet " + portletId, e);
307                            }
308    
309                            return -1;
310                    }
311                    finally {
312                            DataAccess.cleanUp(con, ps);
313                    }
314            }
315    
316            @Override
317            protected void doUpgrade() throws Exception {
318                    updateAttachments();
319            }
320    
321            protected String[] getAttachments(
322                            long companyId, long containerModelId, long resourcePrimKey)
323                    throws Exception {
324    
325                    String dirName = getDirName(containerModelId, resourcePrimKey);
326    
327                    String[] attachments = null;
328    
329                    try {
330                            attachments = DLStoreUtil.getFileNames(
331                                    companyId, CompanyConstants.SYSTEM, dirName);
332                    }
333                    catch (NoSuchDirectoryException nsde) {
334                    }
335    
336                    return attachments;
337            }
338    
339            protected abstract String getClassName();
340    
341            protected long getClassNameId() {
342                    return PortalUtil.getClassNameId(getClassName());
343            }
344    
345            protected long getContainerModelFolderId(
346                            long groupId, long companyId, long resourcePrimKey,
347                            long containerModelId, long userId, String userName,
348                            Timestamp createDate)
349                    throws Exception {
350    
351                    return DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
352            }
353    
354            protected abstract String getDirName(
355                    long containerModelId, long resourcePrimKey);
356    
357            protected long getFolderId(
358                            long groupId, long companyId, long userId, String userName,
359                            Timestamp createDate, long repositoryId, long parentFolderId,
360                            String name, boolean hidden)
361                    throws Exception {
362    
363                    if ((repositoryId < 0) || (parentFolderId < 0)) {
364                            return -1;
365                    }
366    
367                    Connection con = null;
368                    PreparedStatement ps = null;
369                    ResultSet rs = null;
370    
371                    try {
372                            con = DataAccess.getUpgradeOptimizedConnection();
373    
374                            ps = con.prepareStatement(
375                                    "select folderId from DLFolder where repositoryId = ? and " +
376                                            "parentFolderId = ? and name = ?");
377    
378                            ps.setLong(1, repositoryId);
379                            ps.setLong(2, parentFolderId);
380                            ps.setString(3, name);
381    
382                            rs = ps.executeQuery();
383    
384                            while (rs.next()) {
385                                    int folderId = rs.getInt(1);
386    
387                                    return folderId;
388                            }
389                    }
390                    finally {
391                            DataAccess.cleanUp(con, ps);
392                    }
393    
394                    return addDLFolder(
395                            increment(), groupId, companyId, userId, userName, createDate,
396                            repositoryId, false, parentFolderId, name, hidden);
397            }
398    
399            protected abstract String getPortletId();
400    
401            protected long getRepositoryId(
402                            long groupId, long companyId, long userId, String userName,
403                            Timestamp createDate, long classNameId, String portletId)
404                    throws Exception {
405    
406                    Connection con = null;
407                    PreparedStatement ps = null;
408                    ResultSet rs = null;
409    
410                    try {
411                            con = DataAccess.getUpgradeOptimizedConnection();
412    
413                            ps = con.prepareStatement(
414                                    "select repositoryId from Repository where groupId = ? and " +
415                                            "name = ? and portletId = ?");
416    
417                            ps.setLong(1, groupId);
418                            ps.setString(2, portletId);
419                            ps.setString(3, portletId);
420    
421                            rs = ps.executeQuery();
422    
423                            while (rs.next()) {
424                                    int repositoryId = rs.getInt(1);
425    
426                                    return repositoryId;
427                            }
428                    }
429                    finally {
430                            DataAccess.cleanUp(con, ps);
431                    }
432    
433                    return addRepository(
434                            groupId, companyId, userId, userName, createDate, classNameId,
435                            portletId);
436            }
437    
438            protected abstract void updateAttachments() throws Exception;
439    
440            protected void updateEntryAttachments(
441                            long companyId, long groupId, long resourcePrimKey,
442                            long containerModelId, long userId, String userName)
443                    throws Exception {
444    
445                    String[] attachments = getAttachments(
446                            companyId, containerModelId, resourcePrimKey);
447    
448                    if ((attachments == null) || (attachments.length == 0)) {
449                            return;
450                    }
451    
452                    Timestamp createDate = new Timestamp(System.currentTimeMillis());
453    
454                    long repositoryId = getRepositoryId(
455                            groupId, companyId, userId, userName, createDate,
456                            PortalUtil.getClassNameId(_LIFERAY_REPOSITORY_CLASS_NAME),
457                            getPortletId());
458    
459                    if (repositoryId < 0) {
460                            return;
461                    }
462    
463                    long containerModelFolderId = getContainerModelFolderId(
464                            groupId, companyId, resourcePrimKey, containerModelId, userId,
465                            userName, createDate);
466    
467                    if (containerModelFolderId < 0) {
468                            return;
469                    }
470    
471                    for (String attachment : attachments) {
472                            String name = String.valueOf(
473                                    increment(DLFileEntry.class.getName()));
474    
475                            String title = FileUtil.getShortFileName(attachment);
476    
477                            String extension = FileUtil.getExtension(title);
478    
479                            String mimeType = MimeTypesUtil.getExtensionContentType(extension);
480    
481                            long size = DLStoreUtil.getFileSize(
482                                    companyId, CompanyConstants.SYSTEM, attachment);
483    
484                            long fileEntryId = addDLFileEntry(
485                                    groupId, companyId, userId, getClassName(), resourcePrimKey,
486                                    userName, createDate, repositoryId, containerModelFolderId,
487                                    name, extension, mimeType, title, size);
488    
489                            if (fileEntryId < 0) {
490                                    continue;
491                            }
492    
493                            addDLFileVersion(
494                                    increment(), groupId, companyId, userId, userName, createDate,
495                                    repositoryId, containerModelFolderId, fileEntryId, extension,
496                                    mimeType, title, size);
497    
498                            byte[] bytes = DLStoreUtil.getFileAsBytes(
499                                    companyId, CompanyConstants.SYSTEM, attachment);
500    
501                            DLStoreUtil.addFile(
502                                    companyId, containerModelFolderId, name, false, bytes);
503    
504                            try {
505                                    DLStoreUtil.deleteFile(
506                                            companyId, CompanyConstants.SYSTEM, attachment);
507                            }
508                            catch (Exception e) {
509                                    if (_log.isWarnEnabled()) {
510                                            _log.warn(
511                                                    "Unable to delete the attachment " + attachment, e);
512                                    }
513                            }
514                    }
515            }
516    
517            private static final String _LIFERAY_REPOSITORY_CLASS_NAME =
518                    "com.liferay.portal.repository.liferayrepository.LiferayRepository";
519    
520            private static Log _log = LogFactoryUtil.getLog(
521                    BaseUpgradeAttachments.class);
522    
523    }