001    /**
002     * Copyright (c) 2000-2010 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.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
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     */
030    public class UpgradeAsset extends UpgradeProcess {
031    
032            protected void doUpgrade() throws Exception {
033                    Connection con = null;
034                    PreparedStatement ps = null;
035                    ResultSet rs = null;
036    
037                    try {
038                            con = DataAccess.getConnection();
039    
040                            ps = con.prepareStatement(
041                                    "select classNameId, classPK from AssetEntry");
042    
043                            rs = ps.executeQuery();
044    
045                            while (rs.next()) {
046                                    long classNameId = rs.getLong("classNameId");
047                                    long classPK = rs.getLong("classPK");
048    
049                                    String className = PortalUtil.getClassName(classNameId);
050    
051                                    String[] tableAndColumn = getTableAndColumnName(className);
052    
053                                    if (Validator.isNull(tableAndColumn[0]) ||
054                                            Validator.isNull(tableAndColumn[1])) {
055    
056                                            continue;
057                                    }
058    
059                                    String uuid = getUuid(
060                                            tableAndColumn[0], tableAndColumn[1], tableAndColumn[2],
061                                            classPK);
062    
063                                    updateAssetEntry(classNameId, classPK, uuid);
064                            }
065                    }
066                    finally {
067                            DataAccess.cleanUp(con, ps, rs);
068                    }
069            }
070    
071            protected String[] getTableAndColumnName(String className) {
072                    String[] tableAndColumn = new String[3];
073    
074                    if (className.equals("com.liferay.portal.model.Group")) {
075                    }
076                    else if (className.equals("com.liferay.portal.model.Organization")) {
077                    }
078                    else if (className.equals("com.liferay.portal.model.User")) {
079                            tableAndColumn[0] = "User_";
080                            tableAndColumn[1] = "userId";
081                            tableAndColumn[2] = "userId";
082                    }
083                    else if (className.equals(
084                                            "com.liferay.portlet.blogs.model.BlogsEntry")) {
085    
086                            tableAndColumn[0] = "BlogsEntry";
087                            tableAndColumn[1] = "entryId";
088                            tableAndColumn[2] = "entryId";
089                    }
090                    else if (className.equals(
091                                            "com.liferay.portlet.bookmarks.model.BookmarksEntry")) {
092    
093                            tableAndColumn[0] = "BookmarksEntry";
094                            tableAndColumn[1] = "entryId";
095                            tableAndColumn[2] = "entryId";
096                    }
097                    else if (className.equals(
098                                            "com.liferay.portlet.calendar.model.CalEvent")) {
099    
100                            tableAndColumn[0] = "CalEvent";
101                            tableAndColumn[1] = "eventId";
102                            tableAndColumn[2] = "eventId";
103                    }
104                    else if (className.equals(
105                                            "com.liferay.portlet.documentlibrary.model.DLFileEntry")) {
106    
107                            tableAndColumn[0] = "DLFileEntry";
108                            tableAndColumn[1] = "fileEntryId";
109                            tableAndColumn[2] = "fileEntryId";
110                    }
111                    else if (className.equals(
112                                            "com.liferay.portlet.documentlibrary.model." +
113                                                    "DLFileShortcut")) {
114    
115                            tableAndColumn[0] = "DLFileShortcut";
116                            tableAndColumn[1] = "fileShortcutId";
117                            tableAndColumn[2] = "fileShortcutId";
118                    }
119                    else if (className.equals(
120                                            "com.liferay.portlet.imagegallery.model.IGImage")) {
121    
122                            tableAndColumn[0] = "IGImage";
123                            tableAndColumn[1] = "imageId";
124                            tableAndColumn[2] = "imageId";
125                    }
126                    else if (className.equals(
127                                            "com.liferay.portlet.journal.model.JournalArticle")) {
128    
129                            tableAndColumn[0] = "JournalArticle";
130                            tableAndColumn[1] = "resourcePrimKey";
131                            tableAndColumn[2] = "id_";
132                    }
133                    else if (className.equals(
134                                            "com.liferay.portlet.messageboards.model.MBMessage")) {
135    
136                            tableAndColumn[0] = "MBMessage";
137                            tableAndColumn[1] = "messageId";
138                            tableAndColumn[2] = "messageId";
139                    }
140                    else if (className.equals(
141                                            "com.liferay.portlet.wiki.model.WikiPage")) {
142    
143                            tableAndColumn[0] = "WikiPage";
144                            tableAndColumn[1] = "resourcePrimKey";
145                            tableAndColumn[2] = "pageId";
146                    }
147    
148                    return tableAndColumn;
149            }
150    
151            protected String getUuid(
152                            String tableName, String columnName1, String columnName2,
153                            long classPK)
154                    throws Exception {
155    
156                    String uuid = StringPool.BLANK;
157    
158                    Connection con = null;
159                    PreparedStatement ps = null;
160                    ResultSet rs = null;
161    
162                    try {
163                            con = DataAccess.getConnection();
164    
165                            ps = con.prepareStatement(
166                                    "select uuid_ from " + tableName + " where " + columnName1 +
167                                            " = ? or " + columnName2 + " = ?");
168    
169                            ps.setLong(1, classPK);
170                            ps.setLong(2, classPK);
171    
172                            rs = ps.executeQuery();
173    
174                            while (rs.next()) {
175                                    uuid = rs.getString("uuid_");
176                            }
177                    }
178                    finally {
179                            DataAccess.cleanUp(con, ps, rs);
180                    }
181    
182                    return uuid;
183            }
184    
185            protected void updateAssetEntry(long classNameId, long classPK, String uuid)
186                    throws Exception {
187    
188                    Connection con = null;
189                    PreparedStatement ps = null;
190    
191                    try {
192                            con = DataAccess.getConnection();
193    
194                            ps = con.prepareStatement(
195                                    "update AssetEntry set classUuid = ? where classNameId = ? " +
196                                            "and classPK = ?");
197    
198                            ps.setString(1, uuid);
199                            ps.setLong(2, classNameId);
200                            ps.setLong(3, classPK);
201    
202                            ps.executeUpdate();
203                    }
204                    finally {
205                            DataAccess.cleanUp(con, ps);
206                    }
207            }
208    
209    }