001    /**
002     * Copyright (c) 2000-present 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.upgrade.v6_0_3;
016    
017    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
018    import com.liferay.portal.kernel.util.LoggingTimer;
019    import com.liferay.portal.kernel.util.PortalUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    
023    import java.sql.PreparedStatement;
024    import java.sql.ResultSet;
025    
026    /**
027     * @author Julio Camarero
028     * @author Amos Fong
029     */
030    public class UpgradeAsset extends UpgradeProcess {
031    
032            protected void createIndex() {
033                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
034                            runSQL(
035                                    "create unique index IX_1E9D371D on AssetEntry (classNameId, " +
036                                            "classPK)");
037                    }
038                    catch (Exception e) {
039                    }
040            }
041    
042            @Override
043            protected void doUpgrade() throws Exception {
044                    createIndex();
045    
046                    updateAssetEntries();
047            }
048    
049            protected String getUuid(
050                            String tableName, String columnName1, String columnName2,
051                            long classPK)
052                    throws Exception {
053    
054                    String uuid = StringPool.BLANK;
055    
056                    try (PreparedStatement ps = connection.prepareStatement(
057                                    "select uuid_ from " + tableName + " where " + columnName1 +
058                                            " = ? or " + columnName2 + " = ?")) {
059    
060                            ps.setLong(1, classPK);
061                            ps.setLong(2, classPK);
062    
063                            try (ResultSet rs = ps.executeQuery()) {
064                                    while (rs.next()) {
065                                            uuid = rs.getString("uuid_");
066                                    }
067                            }
068                    }
069    
070                    return uuid;
071            }
072    
073            protected void updateAssetEntries() throws Exception {
074                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
075                            updateAssetEntry(
076                                    "com.liferay.portal.model.User", "User_", "userId");
077                            updateAssetEntry(
078                                    "com.liferay.portlet.blogs.model.BlogsEntry", "BlogsEntry",
079                                    "entryId");
080                            updateAssetEntry(
081                                    "com.liferay.portlet.bookmarks.model.BookmarksEntry",
082                                    "BookmarksEntry", "entryId");
083                            updateAssetEntry(
084                                    "com.liferay.portlet.calendar.model.CalEvent", "CalEvent",
085                                    "eventId");
086                            updateAssetEntry(
087                                    "com.liferay.portlet.documentlibrary.model.DLFileEntry",
088                                    "DLFileEntry", "fileEntryId");
089                            updateAssetEntry(
090                                    "com.liferay.portlet.documentlibrary.model.DLFileShortcut",
091                                    "DLFileShortcut", "fileShortcutId");
092                            updateAssetEntry(
093                                    "com.liferay.portlet.imagegallery.model.IGImage", "IGImage",
094                                    "imageId");
095                            updateAssetEntry(
096                                    "com.liferay.portlet.journal.model.JournalArticle",
097                                    "JournalArticle", "resourcePrimKey", "id_");
098                            updateAssetEntry(
099                                    "com.liferay.portlet.messageboards.model.MBMessage",
100                                    "MBMessage", "messageId");
101                            updateAssetEntry(
102                                    "com.liferay.portlet.wiki.model.WikiPage", "WikiPage",
103                                    "resourcePrimKey", "pageId");
104                    }
105            }
106    
107            protected void updateAssetEntry(long classNameId, long classPK, String uuid)
108                    throws Exception {
109    
110                    try (PreparedStatement ps = connection.prepareStatement(
111                                    "update AssetEntry set classUuid = ? where classNameId = ? " +
112                                            "and classPK = ?")) {
113    
114                            ps.setString(1, uuid);
115                            ps.setLong(2, classNameId);
116                            ps.setLong(3, classPK);
117    
118                            ps.executeUpdate();
119                    }
120            }
121    
122            protected void updateAssetEntry(
123                            String className, String tableName, String columnName)
124                    throws Exception {
125    
126                    long classNameId = PortalUtil.getClassNameId(className);
127    
128                    StringBundler sb = new StringBundler(11);
129    
130                    sb.append("update AssetEntry set classUuid = (select ");
131                    sb.append(tableName);
132                    sb.append(".uuid_ from ");
133                    sb.append(tableName);
134                    sb.append(" where ");
135                    sb.append(tableName);
136                    sb.append(".");
137                    sb.append(columnName);
138                    sb.append(" = AssetEntry.classPK) where (AssetEntry.classNameId = ");
139                    sb.append(classNameId);
140                    sb.append(StringPool.CLOSE_PARENTHESIS);
141    
142                    runSQL(sb.toString());
143            }
144    
145            protected void updateAssetEntry(
146                            String className, String tableName, String columnName1,
147                            String columnName2)
148                    throws Exception {
149    
150                    try (LoggingTimer loggingTimer = new LoggingTimer(className)) {
151                            long classNameId = PortalUtil.getClassNameId(className);
152    
153                            try (PreparedStatement ps = connection.prepareStatement(
154                                            "select classPK from AssetEntry where classNameId = ?")) {
155    
156                                    ps.setLong(1, classNameId);
157    
158                                    try (ResultSet rs = ps.executeQuery()) {
159                                            while (rs.next()) {
160                                                    long classPK = rs.getLong("classPK");
161    
162                                                    String uuid = getUuid(
163                                                            tableName, columnName1, columnName2, classPK);
164    
165                                                    updateAssetEntry(classNameId, classPK, uuid);
166                                            }
167                                    }
168                            }
169                    }
170            }
171    
172    }