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.v5_2_3;
016    
017    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
018    import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
019    import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.upgrade.v5_2_3.util.TagsAssetTable;
022    import com.liferay.portal.upgrade.v5_2_3.util.TagsPropertyTable;
023    import com.liferay.portal.upgrade.v5_2_3.util.TagsPropertyValueUpgradeColumnImpl;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
026    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     * @author Samuel Kong
031     */
032    public class UpgradeTags extends UpgradeProcess {
033    
034            protected void doUpgrade() throws Exception {
035                    try {
036                            runSQL("alter_column_type TagsAsset title VARCHAR(255) null");
037                    }
038                    catch (Exception e) {
039    
040                            // TagsAsset
041    
042                            UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
043                                    TagsAssetTable.TABLE_NAME, TagsAssetTable.TABLE_COLUMNS);
044    
045                            upgradeTable.setCreateSQL(TagsAssetTable.TABLE_SQL_CREATE);
046    
047                            upgradeTable.updateTable();
048                    }
049    
050                    updateAssetViewCount();
051    
052                    // TagsProperty
053    
054                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
055                            TagsPropertyTable.TABLE_NAME, TagsPropertyTable.TABLE_COLUMNS,
056                            new TagsPropertyValueUpgradeColumnImpl("value"));
057    
058                    upgradeTable.setCreateSQL(TagsPropertyTable.TABLE_SQL_CREATE);
059    
060                    upgradeTable.updateTable();
061            }
062    
063            protected void updateAssetViewCount() throws Exception {
064                    updateAssetViewCount(
065                            BookmarksEntry.class.getName(), "BookmarksEntry", "entryId",
066                            "visits");
067    
068                    updateAssetViewCount(
069                            DLFileEntry.class.getName(), "DLFileEntry", "fileEntryId",
070                            "readCount");
071            }
072    
073            protected void updateAssetViewCount(
074                            String className, String tableName, String columnClassPK,
075                            String columnViewCount)
076                    throws Exception {
077    
078                    long classNameId = PortalUtil.getClassNameId(className);
079    
080                    StringBundler sb = new StringBundler(12);
081    
082                    sb.append("update TagsAsset set viewCount = (select ");
083                    sb.append(tableName);
084                    sb.append(".");
085                    sb.append(columnViewCount);
086                    sb.append(" from ");
087                    sb.append(tableName);
088                    sb.append(" where TagsAsset.classPK = ");
089                    sb.append(tableName);
090                    sb.append(".");
091                    sb.append(columnClassPK);
092                    sb.append(") where TagsAsset.classNameId = ");
093                    sb.append(classNameId);
094    
095                    runSQL(sb.toString());
096            }
097    
098    }