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