1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.upgrade.v5_2_3;
16  
17  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
18  import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
19  import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
20  import com.liferay.portal.kernel.util.StringBundler;
21  import com.liferay.portal.upgrade.v5_2_3.util.TagsAssetTable;
22  import com.liferay.portal.upgrade.v5_2_3.util.TagsPropertyTable;
23  import com.liferay.portal.upgrade.v5_2_3.util.TagsPropertyValueUpgradeColumnImpl;
24  import com.liferay.portal.util.PortalUtil;
25  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
26  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
27  
28  /**
29   * <a href="UpgradeTags.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   * @author Samuel Kong
33   */
34  public class UpgradeTags extends UpgradeProcess {
35  
36      protected void doUpgrade() throws Exception {
37          try {
38              runSQL("alter_column_type TagsAsset title VARCHAR(255) null");
39          }
40          catch (Exception e) {
41  
42              // TagsAsset
43  
44              UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
45                  TagsAssetTable.TABLE_NAME, TagsAssetTable.TABLE_COLUMNS);
46  
47              upgradeTable.setCreateSQL(TagsAssetTable.TABLE_SQL_CREATE);
48  
49              upgradeTable.updateTable();
50          }
51  
52          updateAssetViewCount();
53  
54          // TagsProperty
55  
56          UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
57              TagsPropertyTable.TABLE_NAME, TagsPropertyTable.TABLE_COLUMNS,
58              new TagsPropertyValueUpgradeColumnImpl("value"));
59  
60          upgradeTable.setCreateSQL(TagsPropertyTable.TABLE_SQL_CREATE);
61  
62          upgradeTable.updateTable();
63      }
64  
65      protected void updateAssetViewCount() throws Exception {
66          updateAssetViewCount(
67              BookmarksEntry.class.getName(), "BookmarksEntry", "entryId",
68              "visits");
69  
70          updateAssetViewCount(
71              DLFileEntry.class.getName(), "DLFileEntry", "fileEntryId",
72              "readCount");
73      }
74  
75      protected void updateAssetViewCount(
76              String className, String tableName, String columnClassPK,
77              String columnViewCount)
78          throws Exception {
79  
80          long classNameId = PortalUtil.getClassNameId(className);
81  
82          StringBundler sb = new StringBundler(12);
83  
84          sb.append("update TagsAsset set viewCount = (select ");
85          sb.append(tableName);
86          sb.append(".");
87          sb.append(columnViewCount);
88          sb.append(" from ");
89          sb.append(tableName);
90          sb.append(" where TagsAsset.classPK = ");
91          sb.append(tableName);
92          sb.append(".");
93          sb.append(columnClassPK);
94          sb.append(") where TagsAsset.classNameId = ");
95          sb.append(classNameId);
96  
97          runSQL(sb.toString());
98      }
99  
100 }