Skip to content

Package: DefaultHtmlTextWithTitleViewController$TextWithTitle

DefaultHtmlTextWithTitleViewController$TextWithTitle

nameinstructionbranchcomplexitylinemethod
DefaultHtmlTextWithTitleViewController.TextWithTitle(Optional, Optional, int)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

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.htmltextwithtitle;
28:
29: import javax.annotation.Nonnull;
30: import java.util.List;
31: import java.util.Optional;
32: import it.tidalwave.northernwind.core.model.Content;
33: import it.tidalwave.northernwind.core.model.SiteNode;
34: import it.tidalwave.northernwind.frontend.ui.RenderContext;
35: import lombok.RequiredArgsConstructor;
36: import lombok.extern.slf4j.Slf4j;
37: import static java.util.Collections.emptyList;
38: import static java.util.stream.Collectors.*;
39: import static it.tidalwave.northernwind.core.model.Content.*;
40: import static it.tidalwave.northernwind.frontend.ui.component.Properties.*;
41:
42: /***********************************************************************************************************************
43: *
44: * <p>A default implementation of the {@link HtmlTextWithTitleViewController} that is independent of the presentation
45: * technology. This class is capable to render a sequence of texts with their titles.</p>
46: *
47: * <p>Supported properties of the {@link SiteNode}:</p>
48: *
49: * <ul>
50: * <li>{@code P_CONTENT_PATHS}: a set of paths to {@link Content}s;</li>
51: * <li>{@code P_CLASS}: an optional CSS class name for the wrapping {@code <div>}.</li>
52: * </ul>
53: *
54: * <p>For each {@code Content} the following properties are used:</p>
55: *
56: * <ul>
57: * <li>{@code P_TITLE}: for rendering the title;</li>
58: * <li>{@code P_FULL_TEXT}: for rendering the text.</li>
59: * </ul>
60: *
61: * <p>Concrete implementations must provide the following method:</p>
62: *
63: * <ul>
64: * <li>{@link #render(java.util.List)}</li>
65: * </ul>
66: *
67: * @author Fabrizio Giudici
68: *
69: **********************************************************************************************************************/
70: @RequiredArgsConstructor @Slf4j
71: public abstract class DefaultHtmlTextWithTitleViewController implements HtmlTextWithTitleViewController
72: {
73: @RequiredArgsConstructor
74: public static class TextWithTitle
75: {
76: public final Optional<String> title;
77: public final Optional<String> text;
78: public final int level;
79: }
80:
81: @Nonnull
82: private final HtmlTextWithTitleView view;
83:
84: @Nonnull
85: private final SiteNode siteNode;
86:
87: /*******************************************************************************************************************
88: *
89: * {@inheritDoc}
90: *
91: ******************************************************************************************************************/
92: @Override
93: public void renderView (@Nonnull final RenderContext context)
94: {
95: final var viewProperties = siteNode.getPropertyGroup(view.getId());
96: final int titleLevel = viewProperties.getProperty(P_LEVEL).orElse(2);
97: view.setClassName(viewProperties.getProperty(P_CLASS).orElse("nw-" + view.getId()));
98: render(viewProperties.getProperty(P_CONTENT_PATHS).orElse(emptyList())
99: .stream()
100: .flatMap(path -> siteNode.getSite().find(_Content_).withRelativePath(path).stream())
101: .map(c -> new TextWithTitle(c.getProperty(P_TITLE), c.getProperty(P_FULL_TEXT), titleLevel))
102: .collect(toList()));
103: }
104:
105: /*******************************************************************************************************************
106: *
107: * Renders the collection of texts with their titles.
108: *
109: * @param contents the contents to render
110: *
111: ******************************************************************************************************************/
112: protected abstract void render (@Nonnull List<? extends TextWithTitle> contents);
113: }