001    /**
002     * Copyright (c) 2000-2011 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            @Override
035            protected void doUpgrade() throws Exception {
036                    try {
037                            runSQL("alter_column_type TagsAsset title VARCHAR(255) null");
038                    }
039                    catch (Exception e) {
040    
041                            // TagsAsset
042    
043                            UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
044                                    TagsAssetTable.TABLE_NAME, TagsAssetTable.TABLE_COLUMNS);
045    
046                            upgradeTable.setCreateSQL(TagsAssetTable.TABLE_SQL_CREATE);
047                            upgradeTable.setIndexesSQL(TagsAssetTable.TABLE_SQL_ADD_INDEXES);
048    
049                            upgradeTable.updateTable();
050                    }
051    
052                    updateAssetViewCount();
053    
054                    // TagsProperty
055    
056                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
057                            TagsPropertyTable.TABLE_NAME, TagsPropertyTable.TABLE_COLUMNS,
058                            new TagsPropertyValueUpgradeColumnImpl("value"));
059    
060                    upgradeTable.setCreateSQL(TagsPropertyTable.TABLE_SQL_CREATE);
061                    upgradeTable.setIndexesSQL(TagsPropertyTable.TABLE_SQL_ADD_INDEXES);
062    
063                    upgradeTable.updateTable();
064            }
065    
066            protected void updateAssetViewCount() throws Exception {
067                    updateAssetViewCount(
068                            BookmarksEntry.class.getName(), "BookmarksEntry", "entryId",
069                            "visits");
070    
071                    updateAssetViewCount(
072                            DLFileEntry.class.getName(), "DLFileEntry", "fileEntryId",
073                            "readCount");
074            }
075    
076            protected void updateAssetViewCount(
077                            String className, String tableName, String columnClassPK,
078                            String columnViewCount)
079                    throws Exception {
080    
081                    long classNameId = PortalUtil.getClassNameId(className);
082    
083                    StringBundler sb = new StringBundler(12);
084    
085                    sb.append("update TagsAsset set viewCount = (select ");
086                    sb.append(tableName);
087                    sb.append(".");
088                    sb.append(columnViewCount);
089                    sb.append(" from ");
090                    sb.append(tableName);
091                    sb.append(" where TagsAsset.classPK = ");
092                    sb.append(tableName);
093                    sb.append(".");
094                    sb.append(columnClassPK);
095                    sb.append(") where TagsAsset.classNameId = ");
096                    sb.append(classNameId);
097    
098                    runSQL(sb.toString());
099            }
100    
101    }