001
014
015 package com.liferay.portal.upgrade.v7_0_0;
016
017 import com.liferay.portal.kernel.configuration.Filter;
018 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
019 import com.liferay.portal.kernel.upgrade.UpgradeProcess;
020 import com.liferay.portal.kernel.util.ArrayUtil;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.PropsKeys;
023 import com.liferay.portal.kernel.util.StringBundler;
024 import com.liferay.portal.util.PortalUtil;
025 import com.liferay.portal.util.PropsUtil;
026 import com.liferay.portal.util.PropsValues;
027
028 import java.sql.Connection;
029 import java.sql.DatabaseMetaData;
030 import java.sql.PreparedStatement;
031 import java.sql.ResultSet;
032
033
036 public class UpgradeRatings extends UpgradeProcess {
037
038 @Override
039 protected void doUpgrade() throws Exception {
040 upgradeRatingsEntry();
041 upgradeRatingsStats();
042 }
043
044 protected void upgradeRatingsEntry() throws Exception {
045 Connection con = null;
046 PreparedStatement ps = null;
047 ResultSet rs = null;
048
049 try {
050 con = DataAccess.getUpgradeOptimizedConnection();
051
052 ps = con.prepareStatement(
053 "select distinct classNameId from RatingsEntry");
054
055 rs = ps.executeQuery();
056
057 while (rs.next()) {
058 upgradeRatingsEntry(rs.getLong("classNameId"));
059 }
060 }
061 finally {
062 DataAccess.cleanUp(con, ps, rs);
063 }
064 }
065
066 protected void upgradeRatingsEntry(long classNameId) throws Exception {
067 String className = PortalUtil.getClassName(classNameId);
068
069 if (ArrayUtil.contains(
070 PropsValues.RATINGS_UPGRADE_THUMBS_CLASS_NAMES, className)) {
071
072 upgradeRatingsEntryThumbs(classNameId);
073 }
074 else {
075 int defaultRatingsStarsNormalizationFactor = GetterUtil.getInteger(
076 PropsUtil.get(
077 PropsKeys.RATINGS_UPGRADE_STARS_NORMALIZATION_FACTOR,
078 new Filter("default")),
079 5);
080
081 int ratingsStarsNormalizationFactor = GetterUtil.getInteger(
082 PropsUtil.get(
083 PropsKeys.RATINGS_UPGRADE_STARS_NORMALIZATION_FACTOR,
084 new Filter(className)),
085 defaultRatingsStarsNormalizationFactor);
086
087 upgradeRatingsEntryStars(
088 classNameId, ratingsStarsNormalizationFactor);
089 }
090 }
091
092 protected void upgradeRatingsEntryStars(
093 long classNameId, int normalizationFactor)
094 throws Exception {
095
096 Connection con = null;
097 PreparedStatement ps = null;
098
099 try {
100 con = DataAccess.getUpgradeOptimizedConnection();
101
102 ps = con.prepareStatement(
103 "update RatingsEntry set score = score / ? where classNameId " +
104 "= ?");
105
106 ps.setInt(1, normalizationFactor);
107 ps.setLong(2, classNameId);
108
109 ps.executeUpdate();
110 }
111 finally {
112 DataAccess.cleanUp(con, ps);
113 }
114 }
115
116 protected void upgradeRatingsEntryThumbs(long classNameId)
117 throws Exception {
118
119 Connection con = null;
120 PreparedStatement ps = null;
121
122 try {
123 con = DataAccess.getUpgradeOptimizedConnection();
124
125 ps = con.prepareStatement(
126 "update RatingsEntry set score = ? where score = ? and " +
127 "classNameId = ?");
128
129 ps.setDouble(1, 0);
130 ps.setDouble(2, -1);
131 ps.setLong(3, classNameId);
132
133 ps.executeUpdate();
134 }
135 finally {
136 DataAccess.cleanUp(con, ps);
137 }
138 }
139
140 protected void upgradeRatingsStats() throws Exception {
141 Connection con = null;
142 PreparedStatement ps = null;
143 ResultSet rs = null;
144
145 try {
146 con = DataAccess.getUpgradeOptimizedConnection();
147
148 DatabaseMetaData databaseMetaData = con.getMetaData();
149
150 boolean supportsBatchUpdates =
151 databaseMetaData.supportsBatchUpdates();
152
153 StringBundler sb = new StringBundler(4);
154
155 sb.append("select classNameId, classPK, count(1) as ");
156 sb.append("totalEntries, sum(RatingsEntry.score) as totalScore, ");
157 sb.append("sum(RatingsEntry.score) / count(1) as averageScore ");
158 sb.append("from RatingsEntry group by classNameId, classPK");
159
160 ps = con.prepareStatement(sb.toString());
161
162 rs = ps.executeQuery();
163
164 ps = con.prepareStatement(
165 "update RatingsStats set totalEntries = ?, totalScore = ?, " +
166 "averageScore = ? where classNameId = ? and classPK = ?");
167
168 int count = 0;
169
170 while (rs.next()) {
171 ps.setInt(1, rs.getInt("totalEntries"));
172 ps.setDouble(2, rs.getDouble("totalScore"));
173 ps.setDouble(3, rs.getDouble("averageScore"));
174 ps.setLong(4, rs.getLong("classNameId"));
175 ps.setLong(5, rs.getLong("classPK"));
176
177 if (supportsBatchUpdates) {
178 ps.addBatch();
179
180 if (count == PropsValues.HIBERNATE_JDBC_BATCH_SIZE) {
181 ps.executeBatch();
182
183 count = 0;
184 }
185 else {
186 count++;
187 }
188 }
189 else {
190 ps.executeUpdate();
191 }
192 }
193
194 if (supportsBatchUpdates && (count > 0)) {
195 ps.executeBatch();
196 }
197 }
198 finally {
199 DataAccess.cleanUp(con, ps, rs);
200 }
201 }
202
203 }