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_1_5;
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.upgrade.v5_1_5.util.TagsAssetTable;
21  import com.liferay.portal.upgrade.v5_1_5.util.TagsPropertyTable;
22  import com.liferay.portal.upgrade.v5_1_5.util.TagsPropertyValueUpgradeColumnImpl;
23  import com.liferay.portal.util.PortalUtil;
24  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
25  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
26  
27  /**
28   * <a href="UpgradeTags.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   * @author Samuel Kong
32   */
33  public class UpgradeTags extends UpgradeProcess {
34  
35      protected void doUpgrade() throws Exception {
36          try {
37              runSQL("alter_column_type TagsAsset title VARCHAR(255) null");
38          }
39          catch (Exception e) {
40  
41              // TagsAsset
42  
43              UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
44                  TagsAssetTable.TABLE_NAME, TagsAssetTable.TABLE_COLUMNS);
45  
46              upgradeTable.setCreateSQL(TagsAssetTable.TABLE_SQL_CREATE);
47  
48              upgradeTable.updateTable();
49          }
50  
51          updateAssetViewCount();
52  
53          // TagsProperty
54  
55          UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
56              TagsPropertyTable.TABLE_NAME, TagsPropertyTable.TABLE_COLUMNS,
57              new TagsPropertyValueUpgradeColumnImpl("value"));
58  
59          upgradeTable.setCreateSQL(TagsPropertyTable.TABLE_SQL_CREATE);
60  
61          upgradeTable.updateTable();
62      }
63  
64      protected void updateAssetViewCount() throws Exception {
65          updateAssetViewCount(
66              BookmarksEntry.class.getName(), "BookmarksEntry", "entryId",
67              "visits");
68  
69          updateAssetViewCount(
70              DLFileEntry.class.getName(), "DLFileEntry", "fileEntryId",
71              "readCount");
72      }
73  
74      protected void updateAssetViewCount(
75              String className, String tableName, String columnClassPK,
76              String columnViewCount)
77          throws Exception {
78  
79          long classNameId = PortalUtil.getClassNameId(className);
80  
81          StringBuilder sb = new StringBuilder();
82  
83          sb.append("update TagsAsset set viewCount = (select ");
84          sb.append(tableName);
85          sb.append(".");
86          sb.append(columnViewCount);
87          sb.append(" from ");
88          sb.append(tableName);
89          sb.append(" where TagsAsset.classPK = ");
90          sb.append(tableName);
91          sb.append(".");
92          sb.append(columnClassPK);
93          sb.append(") where TagsAsset.classNameId = ");
94          sb.append(classNameId);
95  
96          runSQL(sb.toString());
97      }
98  
99  }