Skip to content

Package: ViewAndControllerLayoutBuilder

ViewAndControllerLayoutBuilder

nameinstructionbranchcomplexitylinemethod
ViewAndControllerLayoutBuilder(SiteNode, RenderContext, BiFunction)
M: 29 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
createComponent(Layout)
M: 22 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
createErrorView(Layout, Throwable)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getErrorViewSupplier()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getValue()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getViewAndControllerFor(Layout)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
visit(Layout)
M: 27 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%

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.spi;
28:
29: import javax.annotation.Nonnull;
30: import javax.annotation.concurrent.NotThreadSafe;
31: import java.util.IdentityHashMap;
32: import java.util.Map;
33: import java.util.Optional;
34: import java.util.function.BiFunction;
35: import it.tidalwave.util.NotFoundException;
36: import it.tidalwave.role.Composite;
37: import it.tidalwave.northernwind.core.model.SiteNode;
38: import it.tidalwave.northernwind.frontend.ui.Layout;
39: import it.tidalwave.northernwind.frontend.ui.RenderContext;
40: import it.tidalwave.northernwind.frontend.ui.ViewController;
41: import it.tidalwave.northernwind.frontend.ui.ViewFactory.ViewAndController;
42: import lombok.Getter;
43: import lombok.RequiredArgsConstructor;
44:
45: /***********************************************************************************************************************
46: *
47: * A {@link Layout} visitor that instantiate views and controllers for a given {@code Layout}. The whole {@code Layout}
48: * tree is visited and for each child an instance of {@link ViewAndController} is created, the controller being called
49: * with the {@link ViewController#prepareRendering(it.tidalwave.northernwind.frontend.ui.ViewController.RenderContext)}
50: * method. No rendering is performed, but instances of {@code ViewAndController} are stored for later retrieval by means
51: * of the method {@link #getViewAndControllerFor(it.tidalwave.northernwind.frontend.ui.Layout)}.
52: *
53: * @stereotype Visitor
54: * @author Fabrizio Giudici
55: *
56: **********************************************************************************************************************/
57: @NotThreadSafe @RequiredArgsConstructor
58: public class ViewAndControllerLayoutBuilder implements Composite.Visitor<Layout, ViewAndControllerLayoutBuilder>
59: {
60: private static final ViewController VOID_CONTROLLER = new ViewController() {};
61:
62: @Nonnull
63: private final SiteNode siteNode;
64:
65: @Nonnull
66: private final RenderContext renderContext;
67:
68: @Getter @Nonnull
69: private final BiFunction<Layout, Throwable, Object> errorViewSupplier;
70:
71: private final Map<Layout, ViewAndController> viewAndControllerMapByLayout = new IdentityHashMap<>();
72:
73: /*******************************************************************************************************************
74: *
75: ******************************************************************************************************************/
76: @Override
77: public void visit (@Nonnull final Layout layout)
78: {
79: try
80: {
81: final var vac = createComponent(layout);
82: vac.getController().prepareRendering(renderContext);
83: viewAndControllerMapByLayout.put(layout, vac);
84: }
85: catch (Exception e)
86: {
87: viewAndControllerMapByLayout.put(layout, createErrorView(layout, e));
88: }
89: }
90:
91: /*******************************************************************************************************************
92: *
93: ******************************************************************************************************************/
94: @Override @Nonnull
95: public Optional<ViewAndControllerLayoutBuilder> getValue()
96: {
97: return Optional.of(this);
98: }
99:
100: /*******************************************************************************************************************
101: *
102: ******************************************************************************************************************/
103: @Nonnull
104: public Optional<ViewAndController> getViewAndControllerFor (@Nonnull final Layout layout)
105: {
106: return Optional.ofNullable(viewAndControllerMapByLayout.get(layout));
107: }
108:
109: /*******************************************************************************************************************
110: *
111: ******************************************************************************************************************/
112: @Nonnull
113: private ViewAndController createComponent (@Nonnull final Layout layout)
114: {
115: try
116: {
117: return layout.createViewAndController(siteNode);
118: }
119: catch (NotFoundException e)
120: {
121: return createErrorView(layout, new NotFoundException("Missing component for: " + layout.getTypeUri()));
122: }
123: catch (Throwable e)
124: {
125: return createErrorView(layout, e);
126: }
127: }
128:
129: /*******************************************************************************************************************
130: *
131: ******************************************************************************************************************/
132: @Nonnull
133: private ViewAndController createErrorView (@Nonnull final Layout layout, @Nonnull final Throwable e)
134: {
135: return new ViewAndController(errorViewSupplier.apply(layout, e), VOID_CONTROLLER);
136: }
137: }