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