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