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_2_3;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.upgrade.UpgradeException;
28  import com.liferay.portal.upgrade.UpgradeProcess;
29  import com.liferay.portal.upgrade.util.DefaultUpgradeTableImpl;
30  import com.liferay.portal.upgrade.util.UpgradeTable;
31  import com.liferay.portal.upgrade.v5_2_3.util.TagsPropertyValueUpgradeColumnImpl;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
34  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
35  import com.liferay.portlet.tags.model.impl.TagsAssetImpl;
36  import com.liferay.portlet.tags.model.impl.TagsPropertyImpl;
37  
38  /**
39   * <a href="UpgradeTags.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   * @author Samuel Kong
43   *
44   */
45  public class UpgradeTags extends UpgradeProcess {
46  
47      public void upgrade() throws UpgradeException {
48          _log.info("Upgrading");
49  
50          try {
51              doUpgrade();
52          }
53          catch (Exception e) {
54              throw new UpgradeException(e);
55          }
56      }
57  
58      protected void doUpgrade() throws Exception {
59          if (isSupportsAlterColumnName()) {
60              runSQL("alter_column_type TagsAsset title VARCHAR(255) null");
61          }
62          else {
63  
64              // TagsAsset
65  
66              UpgradeTable upgradeTable = new DefaultUpgradeTableImpl(
67                  TagsAssetImpl.TABLE_NAME, TagsAssetImpl.TABLE_COLUMNS);
68  
69              upgradeTable.setCreateSQL(TagsAssetImpl.TABLE_SQL_CREATE);
70  
71              upgradeTable.updateTable();
72          }
73  
74          updateAssetViewCount();
75  
76          // TagsProperty
77  
78          UpgradeTable upgradeTable = new DefaultUpgradeTableImpl(
79              TagsPropertyImpl.TABLE_NAME, TagsPropertyImpl.TABLE_COLUMNS,
80              new TagsPropertyValueUpgradeColumnImpl("value"));
81  
82          upgradeTable.setCreateSQL(TagsPropertyImpl.TABLE_SQL_CREATE);
83  
84          upgradeTable.updateTable();
85      }
86  
87      protected void updateAssetViewCount() throws Exception {
88          updateAssetViewCount(
89              BookmarksEntry.class.getName(), "BookmarksEntry", "entryId",
90              "visits");
91  
92          updateAssetViewCount(
93              DLFileEntry.class.getName(), "DLFileEntry", "fileEntryId",
94              "readCount");
95      }
96  
97      protected void updateAssetViewCount(
98              String className, String tableName, String columnClassPK,
99              String columnViewCount)
100         throws Exception {
101 
102         long classNameId = PortalUtil.getClassNameId(className);
103 
104         StringBuilder sb = new StringBuilder();
105 
106         if (isSupportsUpdateWithInnerJoin()) {
107             sb.append("update TagsAsset inner join ");
108             sb.append(tableName);
109             sb.append(" on ");
110             sb.append(tableName);
111             sb.append(".");
112             sb.append(columnClassPK);
113             sb.append(" = TagsAsset.classPK set TagsAsset.viewCount = ");
114             sb.append(tableName);
115             sb.append(".");
116             sb.append(columnViewCount);
117             sb.append(" where TagsAsset.classNameId = ");
118             sb.append(classNameId);
119         }
120         else {
121             sb.append("update TagsAsset set viewCount = (select ");
122             sb.append(tableName);
123             sb.append(".");
124             sb.append(columnViewCount);
125             sb.append(" from ");
126             sb.append(tableName);
127             sb.append(" where TagsAsset.classPK = ");
128             sb.append(tableName);
129             sb.append(".");
130             sb.append(columnClassPK);
131             sb.append(") where TagsAsset.classNameId = ");
132             sb.append(classNameId);
133         }
134 
135         runSQL(sb.toString());
136     }
137 
138     private static Log _log = LogFactoryUtil.getLog(UpgradeTags.class);
139 
140 }