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_1_5;
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.upgrade.v5_1_5.util.TagsAssetTable;
021    import com.liferay.portal.upgrade.v5_1_5.util.TagsPropertyTable;
022    import com.liferay.portal.upgrade.v5_1_5.util.TagsPropertyValueUpgradeColumnImpl;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
025    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     * @author Samuel Kong
030     */
031    public class UpgradeTags extends UpgradeProcess {
032    
033            @Override
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                            upgradeTable.setIndexesSQL(TagsAssetTable.TABLE_SQL_ADD_INDEXES);
047    
048                            upgradeTable.updateTable();
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                    StringBuilder sb = new StringBuilder();
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    }