/
JournalArticleAssetRenderer.java
657 lines (518 loc) · 17.9 KB
/
JournalArticleAssetRenderer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/**
* SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/
package com.liferay.journal.web.internal.asset.model;
import com.liferay.asset.display.page.portlet.AssetDisplayPageFriendlyURLProvider;
import com.liferay.asset.display.page.util.AssetDisplayPageUtil;
import com.liferay.asset.kernel.model.AssetEntry;
import com.liferay.asset.kernel.model.AssetRendererFactory;
import com.liferay.asset.kernel.model.BaseJSPAssetRenderer;
import com.liferay.asset.kernel.model.DDMFormValuesReader;
import com.liferay.info.item.ClassPKInfoItemIdentifier;
import com.liferay.info.item.InfoItemReference;
import com.liferay.journal.configuration.JournalServiceConfiguration;
import com.liferay.journal.constants.JournalArticleConstants;
import com.liferay.journal.constants.JournalPortletKeys;
import com.liferay.journal.model.JournalArticle;
import com.liferay.journal.model.JournalArticleDisplay;
import com.liferay.journal.service.JournalArticleLocalServiceUtil;
import com.liferay.journal.util.JournalContent;
import com.liferay.journal.util.JournalHelper;
import com.liferay.journal.web.internal.asset.JournalArticleDDMFormValuesReader;
import com.liferay.journal.web.internal.security.permission.resource.JournalArticlePermission;
import com.liferay.layout.model.LayoutClassedModelUsage;
import com.liferay.layout.service.LayoutClassedModelUsageLocalServiceUtil;
import com.liferay.portal.configuration.module.configuration.ConfigurationProviderUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.model.Group;
import com.liferay.portal.kernel.model.Layout;
import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
import com.liferay.portal.kernel.portlet.LiferayPortletURL;
import com.liferay.portal.kernel.portlet.PortletRequestModel;
import com.liferay.portal.kernel.portlet.PortletURLFactoryUtil;
import com.liferay.portal.kernel.portlet.url.builder.PortletURLBuilder;
import com.liferay.portal.kernel.security.permission.ActionKeys;
import com.liferay.portal.kernel.security.permission.PermissionChecker;
import com.liferay.portal.kernel.service.GroupLocalServiceUtil;
import com.liferay.portal.kernel.service.LayoutLocalServiceUtil;
import com.liferay.portal.kernel.service.permission.LayoutPermissionUtil;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.trash.TrashRenderer;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.HtmlParser;
import com.liferay.portal.kernel.util.HtmlUtil;
import com.liferay.portal.kernel.util.HttpComponentsUtil;
import com.liferay.portal.kernel.util.JavaConstants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PortalUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.portlet.PortletURL;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Julio Camarero
* @author Juan Fernández
* @author Sergio González
* @author Raymond Augé
*/
public class JournalArticleAssetRenderer
extends BaseJSPAssetRenderer<JournalArticle> implements TrashRenderer {
public static final String TYPE = "journal_article";
public static long getClassPK(JournalArticle article) {
if ((article.isDraft() || article.isPending()) &&
(article.getVersion() != JournalArticleConstants.VERSION_DEFAULT)) {
return article.getPrimaryKey();
}
return article.getResourcePrimKey();
}
public JournalArticleAssetRenderer(
JournalArticle article, HtmlParser htmlParser,
JournalHelper journalHelper) {
_article = article;
_htmlParser = htmlParser;
_journalHelper = journalHelper;
}
public JournalArticle getArticle() {
return _article;
}
@Override
public JournalArticle getAssetObject() {
return _article;
}
@Override
public JournalArticle getAssetObject(long versionClassPK) {
return JournalArticleLocalServiceUtil.fetchArticle(versionClassPK);
}
@Override
public String[] getAvailableLanguageIds() {
return _article.getAvailableLanguageIds();
}
@Override
public String getClassName() {
return JournalArticle.class.getName();
}
@Override
public long getClassPK() {
return getClassPK(_article);
}
@Override
public DDMFormValuesReader getDDMFormValuesReader() {
return new JournalArticleDDMFormValuesReader(_article);
}
@Override
public String getDefaultLanguageId() throws Exception {
return _article.getDefaultLanguageId();
}
@Override
public String getDiscussionPath() {
if (_journalServiceConfiguration == null) {
try {
_journalServiceConfiguration =
ConfigurationProviderUtil.getCompanyConfiguration(
JournalServiceConfiguration.class,
_article.getCompanyId());
}
catch (Exception exception) {
_log.error(exception);
return null;
}
}
if (_journalServiceConfiguration.articleCommentsEnabled()) {
return "edit_article_discussion";
}
return null;
}
@Override
public long getGroupId() {
return _article.getGroupId();
}
@Override
public String getJspPath(
HttpServletRequest httpServletRequest, String template) {
if (_article.isInTrash() && template.equals(TEMPLATE_FULL_CONTENT)) {
return "/trash/" + template + ".jsp";
}
if (template.equals(TEMPLATE_ABSTRACT) ||
template.equals(TEMPLATE_FULL_CONTENT)) {
return "/asset/" + template + ".jsp";
}
return null;
}
@Override
public String getPortletId() {
return JournalPortletKeys.JOURNAL;
}
@Override
public int getStatus() {
return _article.getStatus();
}
@Override
public String getSummary(
PortletRequest portletRequest, PortletResponse portletResponse) {
String summary = _article.getDescription(getLocale(portletRequest));
if (Validator.isNotNull(summary)) {
return _htmlParser.render(HtmlUtil.stripHtml(summary));
}
return summary;
}
@Override
public String getThumbnailPath(PortletRequest portletRequest)
throws Exception {
ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
WebKeys.THEME_DISPLAY);
String thumbnailSrc = _article.getArticleImageURL(themeDisplay);
if (Validator.isNotNull(thumbnailSrc)) {
return thumbnailSrc;
}
return super.getThumbnailPath(portletRequest);
}
@Override
public String getTitle(Locale locale) {
return _article.getTitle(locale);
}
@Override
public String getType() {
return TYPE;
}
@Override
public PortletURL getURLEdit(HttpServletRequest httpServletRequest)
throws Exception {
Group group = GroupLocalServiceUtil.fetchGroup(_article.getGroupId());
if (group.isCompany()) {
ThemeDisplay themeDisplay =
(ThemeDisplay)httpServletRequest.getAttribute(
WebKeys.THEME_DISPLAY);
group = themeDisplay.getScopeGroup();
}
return PortletURLBuilder.create(
PortalUtil.getControlPanelPortletURL(
httpServletRequest, group, JournalPortletKeys.JOURNAL, 0, 0,
PortletRequest.RENDER_PHASE)
).setMVCRenderCommandName(
"/journal/edit_article"
).setParameter(
"articleId", _article.getArticleId()
).setParameter(
"groupId", _article.getGroupId()
).setParameter(
"version", _article.getVersion()
).buildPortletURL();
}
@Override
public PortletURL getURLEdit(
LiferayPortletRequest liferayPortletRequest,
LiferayPortletResponse liferayPortletResponse)
throws Exception {
return getURLEdit(
PortalUtil.getHttpServletRequest(liferayPortletRequest));
}
@Override
public PortletURL getURLExport(
LiferayPortletRequest liferayPortletRequest,
LiferayPortletResponse liferayPortletResponse)
throws Exception {
ThemeDisplay themeDisplay =
(ThemeDisplay)liferayPortletRequest.getAttribute(
WebKeys.THEME_DISPLAY);
PortletURL portletURL = PortletURLFactoryUtil.create(
liferayPortletRequest, JournalPortletKeys.JOURNAL,
themeDisplay.getPlid(), PortletRequest.RESOURCE_PHASE);
LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
liferayPortletURL.setParameter(
"groupId", String.valueOf(_article.getGroupId()));
liferayPortletURL.setParameter("articleId", _article.getArticleId());
liferayPortletURL.setResourceID("/journal/export_article");
return liferayPortletURL;
}
@Override
public String getUrlTitle() {
return _article.getUrlTitle();
}
@Override
public String getUrlTitle(Locale locale) {
try {
return _article.getUrlTitle(locale);
}
catch (Exception exception) {
_log.error(exception);
}
return getUrlTitle();
}
@Override
public PortletURL getURLViewDiffs(
LiferayPortletRequest liferayPortletRequest,
LiferayPortletResponse liferayPortletResponse)
throws Exception {
JournalArticle previousApprovedArticle =
JournalArticleLocalServiceUtil.getPreviousApprovedArticle(_article);
if ((previousApprovedArticle.getVersion() == _article.getVersion()) ||
(_article.getVersion() ==
JournalArticleConstants.VERSION_DEFAULT)) {
return null;
}
return PortletURLBuilder.create(
PortalUtil.getControlPanelPortletURL(
liferayPortletRequest, JournalPortletKeys.JOURNAL,
PortletRequest.RENDER_PHASE)
).setMVCRenderCommandName(
"/journal/compare_versions"
).setParameter(
"articleId", _article.getArticleId()
).setParameter(
"groupId", _article.getGroupId()
).setParameter(
"sourceVersion", previousApprovedArticle.getVersion()
).setParameter(
"targetVersion", _article.getVersion()
).buildPortletURL();
}
@Override
public String getURLViewInContext(
LiferayPortletRequest liferayPortletRequest,
LiferayPortletResponse liferayPortletResponse,
String noSuchEntryRedirect)
throws PortalException {
ThemeDisplay themeDisplay =
(ThemeDisplay)liferayPortletRequest.getAttribute(
WebKeys.THEME_DISPLAY);
return getURLViewInContext(themeDisplay, noSuchEntryRedirect);
}
@Override
public String getURLViewInContext(
ThemeDisplay themeDisplay, String noSuchEntryRedirect)
throws PortalException {
if (!_isShowDisplayPage(themeDisplay.getScopeGroupId(), _article)) {
return _getHitLayoutURL(noSuchEntryRedirect, themeDisplay);
}
Layout layout = _article.getLayout();
if (layout == null) {
AssetRendererFactory<JournalArticle> assetRendererFactory =
getAssetRendererFactory();
AssetEntry assetEntry = assetRendererFactory.getAssetEntry(
getClassName(), getClassPK());
layout = _getArticleLayout(
assetEntry.getLayoutUuid(), assetEntry.getGroupId());
}
if (layout != null) {
String friendlyURL = _journalHelper.createURLPattern(
_article, themeDisplay.getLocale(), layout.isPrivateLayout(),
JournalArticleConstants.CANONICAL_URL_SEPARATOR, themeDisplay);
if (!_article.isApproved()) {
friendlyURL = HttpComponentsUtil.addParameter(
friendlyURL, "version", _article.getVersion());
}
return PortalUtil.addPreservedParameters(themeDisplay, friendlyURL);
}
if (_assetDisplayPageFriendlyURLProvider != null) {
String friendlyURL =
_assetDisplayPageFriendlyURLProvider.getFriendlyURL(
new InfoItemReference(
getClassName(),
new ClassPKInfoItemIdentifier(
_article.getResourcePrimKey())),
themeDisplay);
if (Validator.isNotNull(friendlyURL)) {
if (!_article.isApproved()) {
friendlyURL = HttpComponentsUtil.addParameter(
friendlyURL, "version", _article.getVersion());
}
return friendlyURL;
}
}
return noSuchEntryRedirect;
}
@Override
public long getUserId() {
return _article.getStatusByUserId();
}
@Override
public String getUserName() {
return _article.getStatusByUserName();
}
@Override
public String getUuid() {
return _article.getUuid();
}
@Override
public String getViewInContextMessage() {
return "view[action]";
}
@Override
public boolean hasEditPermission(PermissionChecker permissionChecker)
throws PortalException {
return JournalArticlePermission.contains(
permissionChecker, _article, ActionKeys.UPDATE);
}
@Override
public boolean hasViewPermission(PermissionChecker permissionChecker)
throws PortalException {
return JournalArticlePermission.contains(
permissionChecker, _article, ActionKeys.VIEW);
}
@Override
public boolean include(
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, String template)
throws Exception {
httpServletRequest.setAttribute(WebKeys.JOURNAL_ARTICLE, _article);
httpServletRequest.setAttribute(
WebKeys.JOURNAL_ARTICLE_DISPLAY,
_getArticleDisplay(httpServletRequest));
return super.include(httpServletRequest, httpServletResponse, template);
}
@Override
public boolean isConvertible() {
return true;
}
@Override
public boolean isDisplayable() {
Date date = new Date();
Date displayDate = _article.getDisplayDate();
if ((displayDate != null) && displayDate.after(date)) {
return false;
}
Date expirationDate = _article.getExpirationDate();
if ((expirationDate != null) && expirationDate.before(date)) {
return false;
}
return true;
}
@Override
public boolean isLocalizable() {
return true;
}
@Override
public boolean isPrintable() {
return true;
}
public void setAssetDisplayPageFriendlyURLProvider(
AssetDisplayPageFriendlyURLProvider
assetDisplayPageFriendlyURLProvider) {
_assetDisplayPageFriendlyURLProvider =
assetDisplayPageFriendlyURLProvider;
}
public void setJournalContent(JournalContent journalContent) {
_journalContent = journalContent;
}
private JournalArticleDisplay _getArticleDisplay(
HttpServletRequest httpServletRequest)
throws Exception {
boolean workflowAssetPreview = GetterUtil.getBoolean(
httpServletRequest.getAttribute(WebKeys.WORKFLOW_ASSET_PREVIEW));
String ddmTemplateKey = (String)httpServletRequest.getAttribute(
WebKeys.JOURNAL_TEMPLATE_ID);
if (Validator.isNull(ddmTemplateKey)) {
ddmTemplateKey = ParamUtil.getString(
httpServletRequest, "ddmTemplateKey");
}
String viewMode = ParamUtil.getString(
httpServletRequest, "viewMode", Constants.VIEW);
String languageId = ParamUtil.getString(
httpServletRequest, "languageId");
ThemeDisplay themeDisplay =
(ThemeDisplay)httpServletRequest.getAttribute(
WebKeys.THEME_DISPLAY);
if (Validator.isNull(languageId) && (themeDisplay != null) &&
Validator.isNotNull(themeDisplay.getLanguageId())) {
languageId = themeDisplay.getLanguageId();
}
else {
languageId = LanguageUtil.getLanguageId(httpServletRequest);
}
int articlePage = ParamUtil.getInteger(httpServletRequest, "page", 1);
PortletRequestModel portletRequestModel = _getPortletRequestModel(
httpServletRequest);
if (!workflowAssetPreview && _article.isApproved()) {
return _journalContent.getDisplay(
_article.getGroupId(), _article.getArticleId(),
_article.getVersion(), ddmTemplateKey, viewMode, languageId,
articlePage, portletRequestModel, themeDisplay);
}
return JournalArticleLocalServiceUtil.getArticleDisplay(
_article, ddmTemplateKey, viewMode, languageId, articlePage,
portletRequestModel, themeDisplay);
}
private Layout _getArticleLayout(String layoutUuid, long groupId) {
if (Validator.isNull(layoutUuid)) {
return null;
}
Layout layout = LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
layoutUuid, groupId, false);
if (layout == null) {
layout = LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
layoutUuid, groupId, true);
}
return layout;
}
private String _getHitLayoutURL(
String noSuchEntryRedirect, ThemeDisplay themeDisplay)
throws PortalException {
List<LayoutClassedModelUsage> layoutClassedModelUsages =
LayoutClassedModelUsageLocalServiceUtil.getLayoutClassedModelUsages(
PortalUtil.getClassNameId(JournalArticle.class),
_article.getResourcePrimKey());
for (LayoutClassedModelUsage layoutClassedModelUsage :
layoutClassedModelUsages) {
Layout layout = LayoutLocalServiceUtil.fetchLayout(
layoutClassedModelUsage.getPlid());
if ((layout != null) && !layout.isSystem() &&
LayoutPermissionUtil.contains(
themeDisplay.getPermissionChecker(), layout,
ActionKeys.VIEW)) {
return PortalUtil.getLayoutURL(layout, themeDisplay);
}
}
return noSuchEntryRedirect;
}
private PortletRequestModel _getPortletRequestModel(
HttpServletRequest httpServletRequest) {
PortletRequest portletRequest =
(PortletRequest)httpServletRequest.getAttribute(
JavaConstants.JAVAX_PORTLET_REQUEST);
PortletResponse portletResponse =
(PortletResponse)httpServletRequest.getAttribute(
JavaConstants.JAVAX_PORTLET_RESPONSE);
if ((portletRequest == null) || (portletResponse == null)) {
return null;
}
return new PortletRequestModel(portletRequest, portletResponse);
}
private boolean _isShowDisplayPage(long groupId, JournalArticle article)
throws PortalException {
AssetRendererFactory<JournalArticle> assetRendererFactory =
getAssetRendererFactory();
AssetEntry assetEntry = assetRendererFactory.getAssetEntry(
getClassName(), getClassPK());
if (Validator.isNull(article.getLayoutUuid()) &&
Validator.isNull(assetEntry.getLayoutUuid()) &&
!AssetDisplayPageUtil.hasAssetDisplayPage(
groupId,
assetRendererFactory.getAssetEntry(
JournalArticle.class.getName(),
article.getResourcePrimKey()))) {
return false;
}
return true;
}
private static final Log _log = LogFactoryUtil.getLog(
JournalArticleAssetRenderer.class);
private final JournalArticle _article;
private AssetDisplayPageFriendlyURLProvider
_assetDisplayPageFriendlyURLProvider;
private final HtmlParser _htmlParser;
private JournalContent _journalContent;
private final JournalHelper _journalHelper;
private JournalServiceConfiguration _journalServiceConfiguration;
}