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