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