1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.upgrade.v5_1_5;
24  
25  import com.liferay.portal.upgrade.UpgradeProcess;
26  import com.liferay.portal.upgrade.util.DefaultUpgradeTableImpl;
27  import com.liferay.portal.upgrade.util.UpgradeTable;
28  import com.liferay.portal.upgrade.v5_1_5.util.TagsAssetTable;
29  import com.liferay.portal.upgrade.v5_1_5.util.TagsPropertyTable;
30  import com.liferay.portal.upgrade.v5_1_5.util.TagsPropertyValueUpgradeColumnImpl;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
33  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
34  
35  /**
36   * <a href="UpgradeTags.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   * @author Samuel Kong
40   */
41  public class UpgradeTags extends UpgradeProcess {
42  
43      protected void doUpgrade() throws Exception {
44          try {
45              runSQL("alter_column_type TagsAsset title VARCHAR(255) null");
46          }
47          catch (Exception e) {
48  
49              // TagsAsset
50  
51              UpgradeTable upgradeTable = new DefaultUpgradeTableImpl(
52                  TagsAssetTable.TABLE_NAME, TagsAssetTable.TABLE_COLUMNS);
53  
54              upgradeTable.setCreateSQL(TagsAssetTable.TABLE_SQL_CREATE);
55  
56              upgradeTable.updateTable();
57          }
58  
59          updateAssetViewCount();
60  
61          // TagsProperty
62  
63          UpgradeTable upgradeTable = new DefaultUpgradeTableImpl(
64              TagsPropertyTable.TABLE_NAME, TagsPropertyTable.TABLE_COLUMNS,
65              new TagsPropertyValueUpgradeColumnImpl("value"));
66  
67          upgradeTable.setCreateSQL(TagsPropertyTable.TABLE_SQL_CREATE);
68  
69          upgradeTable.updateTable();
70      }
71  
72      protected void updateAssetViewCount() throws Exception {
73          updateAssetViewCount(
74              BookmarksEntry.class.getName(), "BookmarksEntry", "entryId",
75              "visits");
76  
77          updateAssetViewCount(
78              DLFileEntry.class.getName(), "DLFileEntry", "fileEntryId",
79              "readCount");
80      }
81  
82      protected void updateAssetViewCount(
83              String className, String tableName, String columnClassPK,
84              String columnViewCount)
85          throws Exception {
86  
87          long classNameId = PortalUtil.getClassNameId(className);
88  
89          StringBuilder sb = new StringBuilder();
90  
91          sb.append("update TagsAsset set viewCount = (select ");
92          sb.append(tableName);
93          sb.append(".");
94          sb.append(columnViewCount);
95          sb.append(" from ");
96          sb.append(tableName);
97          sb.append(" where TagsAsset.classPK = ");
98          sb.append(tableName);
99          sb.append(".");
100         sb.append(columnClassPK);
101         sb.append(") where TagsAsset.classNameId = ");
102         sb.append(classNameId);
103 
104         runSQL(sb.toString());
105     }
106 
107 }