001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.upgrade.v6_1_0;
016    
017    import com.liferay.portal.image.FileSystemHook;
018    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
019    import com.liferay.portal.kernel.image.Hook;
020    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
021    import com.liferay.portal.kernel.util.FileUtil;
022    import com.liferay.portal.kernel.util.InstanceFactory;
023    import com.liferay.portal.kernel.util.MimeTypesUtil;
024    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.model.Image;
029    import com.liferay.portal.service.ImageLocalServiceUtil;
030    import com.liferay.portal.util.PropsValues;
031    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
032    import com.liferay.portlet.documentlibrary.model.DLFolder;
033    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
034    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
035    import com.liferay.portlet.documentlibrary.util.ImageProcessor;
036    
037    import java.io.InputStream;
038    
039    import java.sql.Connection;
040    import java.sql.Date;
041    import java.sql.PreparedStatement;
042    import java.sql.ResultSet;
043    
044    import java.util.HashMap;
045    import java.util.Map;
046    
047    /**
048     * @author Sergio González
049     * @author Miguel Pastor
050     */
051    public class UpgradeImageGallery extends UpgradeProcess {
052    
053            protected void addDLFileEntry(
054                            String uuid, long fileEntryId, long groupId, long companyId,
055                            long userId, String userName, long versionUserId,
056                            String versionUserName, Date createDate, Date modifiedDate,
057                            long repositoryId, long folderId, String name, String extension,
058                            String mimeType, String title, String description,
059                            String extraSettings, String version, long size, int readCount,
060                            long smallImageId, long largeImageId, long custom1ImageId,
061                            long custom2ImageId)
062                    throws Exception {
063    
064                    Connection con = null;
065                    PreparedStatement ps = null;
066    
067                    try {
068                            con = DataAccess.getConnection();
069    
070                            StringBundler sb = new StringBundler(9);
071    
072                            sb.append("insert into DLFileEntry (uuid_, fileEntryId, groupId, ");
073                            sb.append("companyId, userId, userName, versionUserId, ");
074                            sb.append("versionUserName, createDate, modifiedDate, ");
075                            sb.append("repositoryId, folderId, name, extension, mimeType, ");
076                            sb.append("title, description, extraSettings, version, size_, ");
077                            sb.append("readCount, smallImageId, largeImageId, ");
078                            sb.append("custom1ImageId, custom2ImageId) values (");
079                            sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
080                            sb.append("?, ?, ?, ?, ?, ?, ?, ?)");
081    
082                            String sql = sb.toString();
083    
084                            ps = con.prepareStatement(sql);
085    
086                            ps.setString(1, uuid);
087                            ps.setLong(2, fileEntryId);
088                            ps.setLong(3, groupId);
089                            ps.setLong(4, companyId);
090                            ps.setLong(5, userId);
091                            ps.setString(6, userName);
092                            ps.setLong(7, versionUserId);
093                            ps.setString(8, versionUserName);
094                            ps.setDate(9, createDate);
095                            ps.setDate(10, modifiedDate);
096                            ps.setLong(11, repositoryId);
097                            ps.setLong(12, folderId);
098                            ps.setString(13, name);
099                            ps.setString(14, extension);
100                            ps.setString(15, mimeType);
101                            ps.setString(16, title);
102                            ps.setString(17, description);
103                            ps.setString(18, extraSettings);
104                            ps.setString(19, version);
105                            ps.setLong(20, size);
106                            ps.setInt(21, readCount);
107                            ps.setLong(22, smallImageId);
108                            ps.setLong(23, largeImageId);
109                            ps.setLong(24, custom1ImageId);
110                            ps.setLong(25, custom2ImageId);
111    
112                            ps.executeUpdate();
113                    }
114                    finally {
115                            DataAccess.cleanUp(con, ps);
116                    }
117            }
118    
119            protected void addDLFileVersion(
120                            long fileVersionId, long groupId, long companyId, long userId,
121                            String userName, Date createDate, long repositoryId, long folderId,
122                            long fileEntryId, String extension, String mimeType, String title,
123                            String description, String changeLog, String extraSettings,
124                            long fileEntryTypeId, String version, long size, int status,
125                            long statusByUserId, String statusByUserName, Date statusDate)
126                    throws Exception {
127    
128                    Connection con = null;
129                    PreparedStatement ps = null;
130    
131                    try {
132                            con = DataAccess.getConnection();
133    
134                            StringBundler sb = new StringBundler(9);
135    
136                            sb.append("insert into DLFileVersion (fileVersionId, groupId, ");
137                            sb.append("companyId, userId, userName, createDate, ");
138                            sb.append("repositoryId, folderId, fileEntryId, extension, ");
139                            sb.append("mimeType, title, description, changeLog, ");
140                            sb.append("extraSettings, fileEntryTypeId, version, size_, ");
141                            sb.append("status, statusByUserId, statusByUserName, statusDate) ");
142                            sb.append("values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
143                            sb.append("?, ?, ?, ?, ?, ?, ?)");
144    
145                            String sql = sb.toString();
146    
147                            ps = con.prepareStatement(sql);
148    
149                            ps.setLong(1, fileVersionId);
150                            ps.setLong(2, groupId);
151                            ps.setLong(3, companyId);
152                            ps.setLong(4, userId);
153                            ps.setString(5, userName);
154                            ps.setDate(6, createDate);
155                            ps.setLong(7, repositoryId);
156                            ps.setLong(8, folderId);
157                            ps.setLong(9, fileEntryId);
158                            ps.setString(10, extension);
159                            ps.setString(11, mimeType);
160                            ps.setString(12, title);
161                            ps.setString(13, description);
162                            ps.setString(14, changeLog);
163                            ps.setString(15, extraSettings);
164                            ps.setLong(16, fileEntryTypeId);
165                            ps.setString(17, version);
166                            ps.setLong(18, size);
167                            ps.setInt(19, status);
168                            ps.setLong(20, statusByUserId);
169                            ps.setString(21, statusByUserName);
170                            ps.setDate(22, statusDate);
171    
172                            ps.executeUpdate();
173                    }
174                    finally {
175                            DataAccess.cleanUp(con, ps);
176                    }
177            }
178    
179            protected void addDLFolderEntry(
180                            String uuid, long folderId, long groupId, long companyId,
181                            long userId, String userName, Date createDate, Date modifiedDate,
182                            long repositoryId, long parentFolderId, String name,
183                            String description, Date lastPostDate)
184                    throws Exception {
185    
186                    Connection con = null;
187                    PreparedStatement ps = null;
188    
189                    try {
190                            con = DataAccess.getConnection();
191    
192                            StringBundler sb = new StringBundler(5);
193    
194                            sb.append("insert into DLFolder (uuid_, folderId, groupId, ");
195                            sb.append("companyId, userId, userName, createDate, ");
196                            sb.append("modifiedDate, repositoryId, mountPoint, ");
197                            sb.append("parentFolderId, name, description, lastPostDate) ");
198                            sb.append("values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
199    
200                            String sql = sb.toString();
201    
202                            ps = con.prepareStatement(sql);
203    
204                            ps.setString(1, uuid);
205                            ps.setLong(2, folderId);
206                            ps.setLong(3, groupId);
207                            ps.setLong(4, companyId);
208                            ps.setLong(5, userId);
209                            ps.setString(6, userName);
210                            ps.setDate(7, createDate);
211                            ps.setDate(8, modifiedDate);
212                            ps.setLong(9, repositoryId);
213                            ps.setBoolean(10, false);
214                            ps.setLong(11, parentFolderId);
215                            ps.setString(12, name);
216                            ps.setString(13, description);
217                            ps.setDate(14, lastPostDate);
218    
219                            ps.executeUpdate();
220                    }
221                    finally {
222                            DataAccess.cleanUp(con, ps);
223                    }
224            }
225    
226            @Override
227            protected void doUpgrade() throws Exception {
228                    updateIGFolderEntries();
229                    updateIGImageEntries();
230                    updateIGFolderPermissions();
231                    updateIGImagePermissions();
232    
233                    migrateImageFiles();
234    
235                    UpgradeDocumentLibrary upgradeDocumentLibrary =
236                            new UpgradeDocumentLibrary();
237    
238                    upgradeDocumentLibrary.updateSyncs();
239            }
240    
241            protected Object[] getImage(long imageId) throws Exception {
242                    Connection con = null;
243                    PreparedStatement ps = null;
244                    ResultSet rs = null;
245    
246                    try {
247                            con = DataAccess.getConnection();
248    
249                            ps = con.prepareStatement(
250                                    "select type_, size_ from Image where imageId = " + imageId);
251    
252                            rs = ps.executeQuery();
253    
254                            if (rs.next()) {
255                                    String type = rs.getString("type_");
256                                    long size = rs.getLong("size_");
257    
258                                    return new Object[] {type, size};
259                            }
260    
261                            return null;
262                    }
263                    finally {
264                            DataAccess.cleanUp(con, ps, rs);
265                    }
266            }
267    
268            protected void migrateFile(
269                            long repositoryId, long companyId, String name, Image image)
270                    throws Exception {
271    
272                    ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
273    
274                    String sourceHookClassName = FileSystemHook.class.getName();
275    
276                    if (Validator.isNotNull(PropsValues.IMAGE_HOOK_IMPL)) {
277                            sourceHookClassName = PropsValues.IMAGE_HOOK_IMPL;
278                    }
279    
280                    Hook sourceHook = (Hook)classLoader.loadClass(
281                            sourceHookClassName).newInstance();
282    
283                    InputStream is = sourceHook.getImageAsStream(image);
284    
285                    byte[] bytes = FileUtil.getBytes(is);
286    
287                    if (name == null) {
288                            name = image.getImageId() + StringPool.PERIOD + image.getType();
289                    }
290    
291                    if (DLStoreUtil.hasFile(companyId, repositoryId, name)) {
292                            DLStoreUtil.deleteFile(companyId, repositoryId, name);
293                    }
294    
295                    DLStoreUtil.addFile(companyId, repositoryId, name, true, bytes);
296            }
297    
298            protected void migrateImage(long imageId) throws Exception {
299                    Connection con = null;
300                    PreparedStatement ps = null;
301                    ResultSet rs = null;
302    
303                    try {
304                            con = DataAccess.getConnection();
305    
306                            StringBundler sb = new StringBundler(8);
307    
308                            sb.append("select fileVersionId, fileEntry.fileEntryId, ");
309                            sb.append("fileEntry.groupId, fileEntry.companyId, ");
310                            sb.append("fileEntry.folderId, name, largeImageId, smallImageId, ");
311                            sb.append("custom1ImageId, custom2ImageId from DLFileVersion ");
312                            sb.append("fileVersion, DLFileEntry fileEntry where ");
313                            sb.append("fileEntry.fileEntryId = fileVersion.fileEntryId and ");
314                            sb.append("(largeImageId = ? or smallImageId = ? or ");
315                            sb.append("custom1ImageId = ? or custom2ImageId = ?)");
316    
317                            String sql = sb.toString();
318    
319                            ps = con.prepareStatement(sql);
320    
321                            ps.setLong(1, imageId);
322                            ps.setLong(2, imageId);
323                            ps.setLong(3, imageId);
324                            ps.setLong(4, imageId);
325    
326                            rs = ps.executeQuery();
327    
328                            if (rs.next()) {
329                                    long fileVersionId = rs.getLong("fileVersionId");
330                                    long fileEntryId = rs.getLong("fileEntry.fileEntryId");
331                                    long companyId = rs.getLong("fileEntry.companyId");
332                                    long groupId = rs.getLong("fileEntry.groupId");
333                                    long folderId = rs.getLong("fileEntry.folderId");
334                                    String name = rs.getString("name");
335                                    long largeImageId = rs.getLong("largeImageId");
336                                    long custom1ImageId = rs.getLong("custom1ImageId");
337                                    long custom2ImageId = rs.getLong("custom2ImageId");
338    
339                                    Image image = ImageLocalServiceUtil.getImage(imageId);
340    
341                                    if (largeImageId == imageId) {
342                                            long repositoryId = DLFolderConstants.getDataRepositoryId(
343                                                    groupId, folderId);
344    
345                                            try {
346                                                    migrateFile(repositoryId, companyId, name, image);
347                                            }
348                                            catch (Exception e) {
349                                            }
350                                    }
351                                    else {
352                                            ClassLoader classLoader =
353                                                    PortalClassLoaderUtil.getClassLoader();
354    
355                                            String sourceHookClassName = FileSystemHook.class.getName();
356    
357                                            if (Validator.isNotNull(PropsValues.IMAGE_HOOK_IMPL)) {
358                                                    sourceHookClassName = PropsValues.IMAGE_HOOK_IMPL;
359                                            }
360    
361                                            Hook sourceHook = (Hook)InstanceFactory.newInstance(
362                                                    classLoader, sourceHookClassName);
363    
364                                            InputStream is = sourceHook.getImageAsStream(image);
365    
366                                            ImageProcessor.storeThumbnail(
367                                                    companyId, groupId, fileEntryId, fileVersionId,
368                                                    custom1ImageId, custom2ImageId, is, image.getType());
369                                    }
370                            }
371                            else {
372                                    Image image = ImageLocalServiceUtil.getImage(imageId);
373    
374                                    try {
375                                            migrateFile(0, 0, null, image);
376                                    }
377                                    catch (Exception e) {
378                                    }
379                            }
380                    }
381                    finally {
382                            DataAccess.cleanUp(con, ps, rs);
383                    }
384            }
385    
386            protected void migrateImageFiles() throws Exception {
387                    Connection con = null;
388                    PreparedStatement ps = null;
389                    ResultSet rs = null;
390    
391                    try {
392                            con = DataAccess.getConnection();
393    
394                            ps = con.prepareStatement("select imageId from Image");
395    
396                            rs = ps.executeQuery();
397    
398                            while (rs.next()) {
399                                    long imageId = rs.getLong("imageId");
400    
401                                    migrateImage(imageId);
402                            }
403                    }
404                    finally {
405                            DataAccess.cleanUp(con, ps, rs);
406                    }
407            }
408    
409            protected void updateIGFolderEntries() throws Exception {
410                    Connection con = null;
411                    PreparedStatement ps = null;
412                    ResultSet rs = null;
413    
414                    try {
415                            con = DataAccess.getConnection();
416    
417                            ps = con.prepareStatement(
418                                    "select * from IGFolder order by folderId asc");
419    
420                            rs = ps.executeQuery();
421    
422                            Map<Long, Long> folderIds = new HashMap<Long, Long>();
423    
424                            while (rs.next()) {
425                                    String uuid = rs.getString("uuid_");
426                                    long folderId = rs.getLong("folderId");
427                                    long groupId = rs.getLong("groupId");
428                                    long companyId = rs.getLong("companyId");
429                                    long userId = rs.getLong("userId");
430                                    String userName = rs.getString("userName");
431                                    Date createDate = rs.getDate("createDate");
432                                    Date modifiedDate = rs.getDate("modifiedDate");
433                                    long parentFolderId = rs.getLong("parentFolderId");
434                                    String name = rs.getString("name");
435                                    String description = rs.getString("description");
436    
437                                    if (folderIds.containsKey(parentFolderId)) {
438                                            parentFolderId = folderIds.get(parentFolderId);
439                                    }
440    
441                                    boolean update = updateIGImageFolderId(
442                                            groupId, name, parentFolderId, folderId, folderIds);
443    
444                                    if (!update) {
445                                            addDLFolderEntry(
446                                                    uuid, folderId, groupId, companyId, userId, userName,
447                                                    createDate, modifiedDate, groupId, parentFolderId, name,
448                                                    description, modifiedDate);
449                                    }
450                            }
451    
452                            runSQL("drop table IGFolder");
453                    }
454                    finally {
455                            DataAccess.cleanUp(con, ps, rs);
456                    }
457            }
458    
459            protected void updateIGFolderPermissions() throws Exception {
460                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM != 6) {
461                            return;
462                    }
463    
464                    runSQL(
465                            "delete from ResourcePermission where " +
466                                    "name = 'com.liferay.portlet.imagegallery.model.IGFolder' " +
467                                            "and primKey = '0'");
468    
469                    Connection con = null;
470                    PreparedStatement ps = null;
471                    ResultSet rs = null;
472    
473                    try {
474                            con = DataAccess.getConnection();
475    
476                            StringBundler sb = new StringBundler(4);
477    
478                            sb.append("update ResourcePermission set name = '");
479                            sb.append(DLFolder.class.getName());
480                            sb.append("' where name = 'com.liferay.portlet.imagegallery.");
481                            sb.append("model.IGFolder'");
482    
483                            ps = con.prepareStatement(sb.toString());
484    
485                            ps.executeUpdate();
486                    }
487                    finally {
488                            DataAccess.cleanUp(con, ps, rs);
489                    }
490            }
491    
492            protected void updateIGImageEntries() throws Exception {
493                    Connection con = null;
494                    PreparedStatement ps = null;
495                    ResultSet rs = null;
496    
497                    try {
498                            con = DataAccess.getConnection();
499    
500                            ps = con.prepareStatement("select * from IGImage");
501    
502                            rs = ps.executeQuery();
503    
504                            while (rs.next()) {
505                                    String uuid = rs.getString("uuid_");
506                                    long imageId = rs.getLong("imageId");
507                                    long groupId = rs.getLong("groupId");
508                                    long companyId = rs.getLong("companyId");
509                                    long userId = rs.getLong("userId");
510                                    String userName = rs.getString("userName");
511                                    Date createDate = rs.getDate("createDate");
512                                    Date modifiedDate = rs.getDate("modifiedDate");
513                                    long folderId = rs.getLong("folderId");
514                                    String title = rs.getString("name");
515                                    String description = rs.getString("description");
516                                    long smallImageId = rs.getLong("smallImageId");
517                                    long largeImageId = rs.getLong("largeImageId");
518                                    long custom1ImageId = rs.getLong("custom1ImageId");
519                                    long custom2ImageId = rs.getLong("custom2ImageId");
520    
521                                    Object[] image = getImage(largeImageId);
522    
523                                    if (image == null) {
524                                            continue;
525                                    }
526    
527                                    String extension = (String)image[0];
528    
529                                    String mimeType = MimeTypesUtil.getContentType(
530                                            "A." + extension);
531    
532                                    String name = String.valueOf(
533                                            increment(DLFileEntry.class.getName()));
534    
535                                    long size = (Long)image[1];
536    
537                                    addDLFileEntry(
538                                            uuid, imageId, groupId, companyId, userId,
539                                            userName, userId, userName, createDate, modifiedDate,
540                                            groupId, folderId, name, extension, mimeType, title,
541                                            description, StringPool.BLANK, "1.0", size, 0, smallImageId,
542                                            largeImageId, custom1ImageId, custom2ImageId);
543    
544                                    addDLFileVersion(
545                                            increment(), groupId, companyId, userId, userName,
546                                            createDate, groupId, folderId, imageId, extension, mimeType,
547                                            title, description, StringPool.BLANK, StringPool.BLANK, 0,
548                                            "1.0", size, 0, userId, userName, modifiedDate);
549                            }
550    
551                            runSQL("drop table IGImage");
552                    }
553                    finally {
554                            DataAccess.cleanUp(con, ps, rs);
555                    }
556            }
557    
558            protected boolean updateIGImageFolderId(
559                            long groupId, String name, long parentFolderId, long folderId,
560                            Map<Long, Long> folderIds)
561                    throws Exception {
562    
563                    Connection con = null;
564                    PreparedStatement ps = null;
565                    ResultSet rs = null;
566    
567                    try {
568                            con = DataAccess.getConnection();
569    
570                            ps = con.prepareStatement(
571                                    "select folderId from DLFolder where groupId = " + groupId +
572                                            " and parentFolderId = " + parentFolderId +
573                                                    " and name = '" + name + "'");
574    
575                            rs = ps.executeQuery();
576    
577                            if (rs.next()) {
578                                    long newFolderId = rs.getLong("folderId");
579    
580                                    runSQL(
581                                            "update IGImage set folderId = " + newFolderId +
582                                                    " where folderId = " + folderId);
583    
584                                    folderIds.put(folderId, newFolderId);
585    
586                                    return true;
587                            }
588                    }
589                    finally {
590                            DataAccess.cleanUp(con, ps, rs);
591                    }
592    
593                    return false;
594            }
595    
596            protected void updateIGImagePermissions() throws Exception {
597                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM != 6) {
598                            return;
599                    }
600    
601                    runSQL(
602                            "delete from ResourcePermission where name = '" +
603                                    _IG_IMAGE_CLASS_NAME + "' and primKey = '0'");
604    
605                    runSQL(
606                            "update ResourcePermission set name = '" +
607                                    DLFileEntry.class.getName() + "' where name = '" +
608                                            _IG_IMAGE_CLASS_NAME + "'");
609            }
610    
611            private static final String _IG_IMAGE_CLASS_NAME =
612                    "com.liferay.portlet.imagegallery.model.IGImage";
613    
614    }