Package: HtmlTemplateBlogViewController
HtmlTemplateBlogViewController
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
HtmlTemplateBlogViewController(SiteNode, HtmlTemplateBlogView, RequestLocaleManager) |
|
|
|
|
|
||||||||||||||||||||
lambda$renderPosts$0(Content) |
|
|
|
|
|
||||||||||||||||||||
lambda$renderPosts$1(Content) |
|
|
|
|
|
||||||||||||||||||||
lambda$renderPosts$2(Content) |
|
|
|
|
|
||||||||||||||||||||
lambda$toAggregate$3(String) |
|
|
|
|
|
||||||||||||||||||||
renderPosts(List, List, List) |
|
|
|
|
|
||||||||||||||||||||
renderTagCloud(Collection) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
toAggregate(Content, Key) |
|
|
|
|
|
||||||||||||||||||||
toAggregate(DefaultBlogViewController.TagAndCount) |
|
|
|
|
|
||||||||||||||||||||
toAggregate(String) |
|
|
|
|
|
Coverage
1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * NorthernWind - lightweight CMS
5: * http://tidalwave.it/projects/northernwind
6: *
7: * Copyright (C) 2011 - 2025 Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/northernwind-src
22: * git clone https://github.com/tidalwave-it/northernwind-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.northernwind.frontend.ui.component.blog.htmltemplate;
27:
28: import javax.annotation.Nonnull;
29: import java.util.Collection;
30: import java.util.List;
31: import it.tidalwave.util.Key;
32: import it.tidalwave.northernwind.core.model.Content;
33: import it.tidalwave.northernwind.core.model.RequestLocaleManager;
34: import it.tidalwave.northernwind.core.model.ResourcePath;
35: import it.tidalwave.northernwind.core.model.SiteNode;
36: import it.tidalwave.northernwind.core.model.Template.Aggregate;
37: import it.tidalwave.northernwind.core.model.Template.Aggregates;
38: import it.tidalwave.northernwind.frontend.ui.component.blog.BlogViewController;
39: import it.tidalwave.northernwind.frontend.ui.component.blog.DefaultBlogViewController;
40: import lombok.extern.slf4j.Slf4j;
41: import static java.util.Collections.emptyList;
42: import static java.util.stream.Collectors.*;
43: import static it.tidalwave.northernwind.core.model.Content.*;
44: import static it.tidalwave.northernwind.core.model.Template.Aggregates.toAggregates;
45:
46: /***************************************************************************************************************************************************************
47: *
48: * <p>An implementation of {@link BlogViewController} based on HTML templates.</p>
49: *
50: * <p>The templates for rendering the page can be specified by means of the following properties:</p>
51: *
52: * <ul>
53: * <li>{@code P_TEMPLATE_POSTS_PATH}: the template for rendering posts;</li>
54: * <li>{@code P_TEMPLATE_TAG_CLOUD_PATH}: the template for rendering the tag cloud.</li>
55: * </ul>
56: *
57: * <p>This controller calls render methods to the view by passing {@link Aggregates} to be used with templates.</p>
58: * <p>In case of post rendering, the following aggregates are defined:</p>
59: *
60: * <ul>
61: * <li>{@code fullPosts}: the posts to be rendered in full;</li>
62: * <li>{@code leadinPosts}: the posts to be rendered as lead-in text;</li>
63: * <li>{@code linkedPosts}: the posts to be rendered as links.</li>
64: * </ul>
65: *
66: * <p>Each item is an {@link Aggregate} of the following fields:</p>
67: *
68: * <ul>
69: * <li>{@code title}: the title of the post;</li>
70: * <li>{@code text}: the text of the post;</li>
71: * <li>{@code link}: the URL of the post;</li>
72: * <li>{@code id}: the unique id of the post;</li>
73: * <li>{@code publishDate}: the publishing date of the post;</li>
74: * <li>{@code category}: the category of the post;</li>
75: * <li>{@code tags}: a list of tags of the post.</li>
76: * </ul>
77: *
78: * <p>Each tag is an {@code Aggregate} of two attributes:</p>
79: *
80: * <ul>
81: * <li>{@code name}: the name of the tag;</li>
82: * <li>{@code link}: the target URL.</li>
83: * </ul>
84: *
85: * <p>In case of tag cloud rendering, the {@code Aggregate} is named {@code tags} and each contained tag, in addition to the
86: * previous attributes, has got:</p>
87: *
88: * <ul>
89: * <li>{@code rank}: the rank of the tag;</li>
90: * <li>{@code count}: the count of the tagged posts.</li>
91: * </ul>
92: *
93: * @see HtmlTemplateBlogView
94: * @author Fabrizio Giudici
95: *
96: **************************************************************************************************************************************************************/
97: @Slf4j
98: public class HtmlTemplateBlogViewController extends DefaultBlogViewController
99: {
100: /** The relative path to the {@link Content} that contains a template for rendering posts. */
101: public static final Key<ResourcePath> P_TEMPLATE_POSTS_PATH = Key.of("postsTemplate", ResourcePath.class);
102:
103: /** The relative path to the {@link Content} that contains a template for rendering the tag cloud. */
104: public static final Key<ResourcePath> P_TEMPLATE_TAG_CLOUD_PATH = Key.of("tagCloudTemplate", ResourcePath.class);
105:
106: @Nonnull
107: private final HtmlTemplateBlogView view;
108:
109: /***********************************************************************************************************************************************************
110: * Creates an instance,
111: *
112: * @param siteNode the {@link SiteNode} for which the view is to be rendered
113: * @param view the view to render
114: * @param requestLocaleManager the {@link RequestLocaleManager}
115: **********************************************************************************************************************************************************/
116: public HtmlTemplateBlogViewController (@Nonnull final SiteNode siteNode,
117: @Nonnull final HtmlTemplateBlogView view,
118: @Nonnull final RequestLocaleManager requestLocaleManager)
119: {
120: super(siteNode, view, requestLocaleManager);
121: this.view = view;
122: }
123:
124: /***********************************************************************************************************************************************************
125: * {@inheritDoc}
126: **********************************************************************************************************************************************************/
127: @Override
128: protected void renderPosts (@Nonnull final List<? extends Content> fullPosts,
129: @Nonnull final List<? extends Content> leadinPosts,
130: @Nonnull final List<? extends Content> linkedPosts)
131: {
132: view.renderPosts(getViewProperties().getProperty(P_TEMPLATE_POSTS_PATH),
133: fullPosts .stream().map(p -> toAggregate(p, P_FULL_TEXT)).collect(toAggregates("fullPosts")),
134: leadinPosts.stream().map(p -> toAggregate(p, P_LEADIN_TEXT)).collect(toAggregates("leadinPosts")),
135: linkedPosts.stream().map(p -> toAggregate(p, P_LEADIN_TEXT)).collect(toAggregates("linkedPosts")));
136: }
137:
138: /***********************************************************************************************************************************************************
139: * {@inheritDoc}
140: **********************************************************************************************************************************************************/
141: @Override
142: protected void renderTagCloud (@Nonnull final Collection<? extends TagAndCount> tagsAndCount)
143: {
144: view.renderTagCloud(getViewProperties().getProperty(P_TEMPLATE_TAG_CLOUD_PATH),
145: tagsAndCount.stream().map(this::toAggregate).collect(toAggregates("tags")));
146: }
147:
148: /***********************************************************************************************************************************************************
149: * Transforms a post into an {@link Aggregate}.
150: *
151: * @param post the post
152: * @param textProperty the property to extract the text from
153: * @return the {@code Aggregate}
154: **********************************************************************************************************************************************************/
155: @Nonnull
156: private Aggregate toAggregate (@Nonnull final Content post, @Nonnull final Key<String> textProperty)
157: {
158: @SuppressWarnings("squid:S3655")
159: final var dateTime = post.getProperty(DATE_KEYS).get();
160: final var id = String.format("nw-%s-blogpost-%s", view.getId(), dateTime.toInstant().toEpochMilli());
161: final var tags = post.getProperty(P_TAGS).orElse(emptyList());
162:
163: return Aggregate.of( "title", post.getProperty(P_TITLE))
164: .with("text" , post.getProperty(textProperty))
165: .with("link", post.getExposedUri().map(this::createLink))
166: .with("id", id)
167: .with("publishDate", formatDateTime(dateTime))
168: .with("category", post.getProperty(P_CATEGORY))
169: .with("tags", tags.stream()
170: .sorted()
171: .map(tag -> toAggregate(tag).getMap())
172: .collect(toList()));
173: }
174:
175: /***********************************************************************************************************************************************************
176: * Transforms a {@link TagAndCount} into an {@link Aggregate}.
177: *
178: * @param tagAndCount the tag info
179: * @return the {@code Aggregate}
180: **********************************************************************************************************************************************************/
181: @Nonnull
182: private Aggregate toAggregate (@Nonnull final TagAndCount tagAndCount)
183: {
184: return toAggregate(tagAndCount.tag).with("rank", tagAndCount.rank).with("count", tagAndCount.count);
185: }
186:
187: /***********************************************************************************************************************************************************
188: * Transforms a tag into an {@link Aggregate}.
189: *
190: * @param tag the tag info
191: * @return the {@code Aggregate}
192: **********************************************************************************************************************************************************/
193: @Nonnull
194: private Aggregate toAggregate (@Nonnull final String tag)
195: {
196: return Aggregate.of("name", tag).with("link", createTagLink(tag));
197: }
198: }