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