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