001
014
015 package com.liferay.portal.upgrade.v7_0_0;
016
017 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.upgrade.util.UpgradeProcessUtil;
021 import com.liferay.portal.kernel.util.StringBundler;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
024 import com.liferay.portal.model.RoleConstants;
025 import com.liferay.portal.security.permission.ActionKeys;
026 import com.liferay.portal.util.PortalUtil;
027 import com.liferay.portlet.asset.model.AssetCategory;
028 import com.liferay.portlet.asset.model.AssetCategoryConstants;
029 import com.liferay.portlet.asset.model.AssetVocabulary;
030 import com.liferay.portlet.asset.util.AssetVocabularySettingsHelper;
031 import com.liferay.portlet.journal.model.JournalArticle;
032
033 import java.sql.Connection;
034 import java.sql.PreparedStatement;
035 import java.sql.ResultSet;
036 import java.sql.Timestamp;
037
038 import java.util.ArrayList;
039 import java.util.HashMap;
040 import java.util.List;
041 import java.util.Map;
042
043
046 public class UpgradeJournalArticleType extends UpgradeBaseJournal {
047
048 protected void addAssetCategory(
049 long assetCategoryId, long groupId, long companyId, long userId,
050 long rightAssetCategoryId, long leftAssetCategoryId, String name,
051 String title, long assetVocabularyId)
052 throws Exception {
053
054 Timestamp now = new Timestamp(System.currentTimeMillis());
055
056 Connection con = null;
057 PreparedStatement ps = null;
058
059 try {
060 con = DataAccess.getUpgradeOptimizedConnection();
061
062 StringBundler sb = new StringBundler(6);
063
064 sb.append("insert into AssetCategory (uuid_, categoryId, ");
065 sb.append("groupId, companyId, userId, userName, createDate, ");
066 sb.append("modifiedDate, parentCategoryId, leftCategoryId, ");
067 sb.append("rightCategoryId, name, title, description, ");
068 sb.append("vocabularyId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
069 sb.append("?, ?, ?, ?, ?)");
070
071 String sql = sb.toString();
072
073 ps = con.prepareStatement(sql);
074
075 ps.setString(1, PortalUUIDUtil.generate());
076 ps.setLong(2, assetCategoryId);
077 ps.setLong(3, groupId);
078 ps.setLong(4, companyId);
079 ps.setLong(5, userId);
080 ps.setString(6, StringPool.BLANK);
081 ps.setTimestamp(7, now);
082 ps.setTimestamp(8, now);
083 ps.setLong(9, AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
084 ps.setLong(10, rightAssetCategoryId);
085 ps.setLong(11, leftAssetCategoryId);
086 ps.setString(12, name);
087 ps.setString(13, title);
088 ps.setString(14, StringPool.BLANK);
089 ps.setLong(15, assetVocabularyId);
090
091 ps.executeUpdate();
092
093 Map<String, Long> bitwiseValues = getBitwiseValues(
094 AssetCategory.class.getName());
095
096 List<String> actionIds = new ArrayList<>();
097
098 actionIds.add(ActionKeys.VIEW);
099
100 long bitwiseValue = getBitwiseValue(bitwiseValues, actionIds);
101
102 addResourcePermission(
103 companyId, AssetCategory.class.getName(), assetCategoryId,
104 getRoleId(companyId, RoleConstants.GUEST), bitwiseValue);
105 addResourcePermission(
106 companyId, AssetCategory.class.getName(), assetCategoryId,
107 getRoleId(companyId, RoleConstants.SITE_MEMBER), bitwiseValue);
108 }
109 catch (Exception e) {
110 _log.error("Unable to add asset category");
111
112 throw e;
113 }
114 finally {
115 DataAccess.cleanUp(con, ps);
116 }
117 }
118
119 protected void addAssetEntryToAssetCategory(
120 long assetEntryId, long assetCategoryId)
121 throws Exception {
122
123 Connection con = null;
124 PreparedStatement ps = null;
125
126 try {
127 con = DataAccess.getUpgradeOptimizedConnection();
128
129 ps = con.prepareStatement(
130 "insert into AssetEntries_AssetCategories (categoryId, " +
131 "entryId) values (?, ?)");
132
133 ps.setLong(1, assetCategoryId);
134 ps.setLong(2, assetEntryId);
135
136 ps.executeUpdate();
137 }
138 catch (Exception e) {
139 _log.error("Unable to add asset entry to asset category");
140
141 throw e;
142 }
143 finally {
144 DataAccess.cleanUp(con, ps);
145 }
146 }
147
148 protected void addAssetVocabulary(
149 long vocabularyId, long groupId, long companyId, long userId,
150 String name, String title, String settings)
151 throws Exception {
152
153 Timestamp now = new Timestamp(System.currentTimeMillis());
154
155 Connection con = null;
156 PreparedStatement ps = null;
157
158 try {
159 con = DataAccess.getUpgradeOptimizedConnection();
160
161 StringBundler sb = new StringBundler(4);
162
163 sb.append("insert into AssetVocabulary (uuid_, vocabularyId, ");
164 sb.append("groupId, companyId, userId, userName, createDate, ");
165 sb.append("modifiedDate, name, title, description, settings_) ");
166 sb.append("values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
167
168 String sql = sb.toString();
169
170 ps = con.prepareStatement(sql);
171
172 ps.setString(1, PortalUUIDUtil.generate());
173 ps.setLong(2, vocabularyId);
174 ps.setLong(3, groupId);
175 ps.setLong(4, companyId);
176 ps.setLong(5, userId);
177 ps.setString(6, StringPool.BLANK);
178 ps.setTimestamp(7, now);
179 ps.setTimestamp(8, now);
180 ps.setString(9, name);
181 ps.setString(10, title);
182 ps.setString(11, StringPool.BLANK);
183 ps.setString(12, settings);
184
185 ps.executeUpdate();
186
187 Map<String, Long> bitwiseValues = getBitwiseValues(
188 AssetVocabulary.class.getName());
189
190 List<String> actionIds = new ArrayList<>();
191
192 actionIds.add(ActionKeys.VIEW);
193
194 long bitwiseValue = getBitwiseValue(bitwiseValues, actionIds);
195
196 addResourcePermission(
197 companyId, AssetVocabulary.class.getName(), vocabularyId,
198 getRoleId(companyId, RoleConstants.GUEST), bitwiseValue);
199 addResourcePermission(
200 companyId, AssetVocabulary.class.getName(), vocabularyId,
201 getRoleId(companyId, RoleConstants.SITE_MEMBER), bitwiseValue);
202 }
203 catch (Exception e) {
204 _log.error("Unable to add asset vocabulary");
205
206 throw e;
207 }
208 finally {
209 DataAccess.cleanUp(con, ps);
210 }
211 }
212
213 @Override
214 protected void doUpgrade() throws Exception {
215 updateArticleType();
216
217
218
219
220
221 upgrade(UpgradeJournalArticles.class);
222 }
223
224 protected List<String> getArticleTypes() throws Exception {
225 Connection con = null;
226 PreparedStatement ps = null;
227 ResultSet rs = null;
228
229 try {
230 con = DataAccess.getUpgradeOptimizedConnection();
231
232 ps = con.prepareStatement(
233 "select distinct type_ from JournalArticle");
234
235 rs = ps.executeQuery();
236
237 List<String> types = new ArrayList<>();
238
239 while (rs.next()) {
240 types.add(rs.getString("type_"));
241 }
242
243 return types;
244 }
245 finally {
246 DataAccess.cleanUp(con, ps, rs);
247 }
248 }
249
250 protected long getAssetEntryId(long classPK) throws Exception {
251 Connection con = null;
252 PreparedStatement ps = null;
253 ResultSet rs = null;
254
255 try {
256 con = DataAccess.getUpgradeOptimizedConnection();
257
258 ps = con.prepareStatement(
259 "select entryId from AssetEntry where classNameId = ? and " +
260 "classPK = ?");
261
262 ps.setLong(1, PortalUtil.getClassNameId(JournalArticle.class));
263 ps.setLong(2, classPK);
264
265 rs = ps.executeQuery();
266
267 while (rs.next()) {
268 return rs.getLong("entryId");
269 }
270
271 return 0;
272 }
273 finally {
274 DataAccess.cleanUp(con, ps, rs);
275 }
276 }
277
278 protected boolean hasSelectedArticleTypes() throws Exception {
279 Connection con = null;
280 PreparedStatement ps = null;
281 ResultSet rs = null;
282
283 try {
284 con = DataAccess.getUpgradeOptimizedConnection();
285
286 ps = con.prepareStatement(
287 "select count(*) from JournalArticle where type_ != 'general'");
288
289 rs = ps.executeQuery();
290
291 while (rs.next()) {
292 int count = rs.getInt(1);
293
294 if (count > 0) {
295 return true;
296 }
297 }
298
299 return false;
300 }
301 finally {
302 DataAccess.cleanUp(con, ps, rs);
303 }
304 }
305
306 protected void updateArticles(
307 long companyId,
308 Map<String, Long> journalArticleTypesToAssetCategoryIds)
309 throws Exception {
310
311 Connection con = null;
312 PreparedStatement ps = null;
313 ResultSet rs = null;
314
315 try {
316 con = DataAccess.getUpgradeOptimizedConnection();
317
318 StringBundler sb = new StringBundler(10);
319
320 sb.append("select JournalArticle.resourcePrimKey, ");
321 sb.append("JournalArticle.type_ from JournalArticle ");
322 sb.append("left join JournalArticle tempJournalArticle on ");
323 sb.append("(JournalArticle.groupId = tempJournalArticle.groupId) ");
324 sb.append("and (JournalArticle.articleId = ");
325 sb.append("tempJournalArticle.articleId) and ");
326 sb.append(" (JournalArticle.version < ");
327 sb.append("tempJournalArticle.version) where ");
328 sb.append("JournalArticle.companyId = ? and ");
329 sb.append("tempJournalArticle.id_ is null");
330
331 ps = con.prepareStatement(sb.toString());
332
333 ps.setLong(1, companyId);
334
335 rs = ps.executeQuery();
336
337 while (rs.next()) {
338 long resourcePrimKey = rs.getLong("resourcePrimKey");
339 String type = rs.getString("type_");
340
341 long assetEntryId = getAssetEntryId(resourcePrimKey);
342 long assetCategoryId =
343 journalArticleTypesToAssetCategoryIds.get(type);
344
345 if (assetEntryId > 0) {
346 addAssetEntryToAssetCategory(assetEntryId, assetCategoryId);
347 }
348 }
349 }
350 finally {
351 DataAccess.cleanUp(con, ps, rs);
352 }
353 }
354
355 protected void updateArticleType() throws Exception {
356 if (!hasSelectedArticleTypes()) {
357 return;
358 }
359
360 List<String> types = getArticleTypes();
361
362 if (types.size() <= 0) {
363 return;
364 }
365
366 Connection con = null;
367 PreparedStatement ps = null;
368 ResultSet rs = null;
369
370 try {
371 con = DataAccess.getUpgradeOptimizedConnection();
372
373 ps = con.prepareStatement("select companyId from Company");
374
375 rs = ps.executeQuery();
376
377 while (rs.next()) {
378 long vocabularyId = increment();
379
380 long companyId = rs.getLong("companyId");
381
382 long groupId = getCompanyGroupId(companyId);
383 long userId = getDefaultUserId(companyId);
384
385 String defaultLanguageId =
386 UpgradeProcessUtil.getDefaultLanguageId(companyId);
387
388 AssetVocabularySettingsHelper assetVocabularySettingsHelper =
389 new AssetVocabularySettingsHelper();
390
391 assetVocabularySettingsHelper.setMultiValued(false);
392
393 assetVocabularySettingsHelper.setClassNameIdsAndClassTypePKs(
394 new long[] {
395 PortalUtil.getClassNameId(JournalArticle.class)
396 },
397 new long[] {-1}, new boolean[] {false});
398
399 addAssetVocabulary(
400 vocabularyId, groupId, companyId, userId, "type",
401 localize(groupId, "type", defaultLanguageId),
402 assetVocabularySettingsHelper.toString());
403
404 Map<String, Long> journalArticleTypesToAssetCategoryIds =
405 new HashMap<>();
406
407 int i = 1;
408
409 for (String type : types) {
410 long assetCategoryId = increment();
411
412 addAssetCategory(
413 assetCategoryId, groupId, companyId, userId, i++, i++,
414 type, localize(groupId, type, defaultLanguageId),
415 vocabularyId);
416
417 journalArticleTypesToAssetCategoryIds.put(
418 type, assetCategoryId);
419 }
420
421 updateArticles(
422 companyId, journalArticleTypesToAssetCategoryIds);
423 }
424 }
425 finally {
426 DataAccess.cleanUp(con, ps, rs);
427 }
428 }
429
430 private static final Log _log = LogFactoryUtil.getLog(
431 UpgradeJournalArticleType.class);
432
433 }