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