Package: DefaultViewFactory
DefaultViewFactory
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
createViewAndController(String, Id, SiteNode) |
|
|
|
|
|
||||||||||||||||||||
initialize() |
|
|
|
|
|
||||||||||||||||||||
lambda$logConfiguration$0(ViewBuilder) |
|
|
|
|
|
||||||||||||||||||||
logConfiguration() |
|
|
|
|
|
||||||||||||||||||||
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.impl.ui;
27:
28: import javax.annotation.Nonnull;
29: import javax.annotation.PostConstruct;
30: import java.util.Map;
31: import java.util.TreeMap;
32: import it.tidalwave.util.Id;
33: import it.tidalwave.util.NotFoundException;
34: import it.tidalwave.util.spring.ClassScanner;
35: import it.tidalwave.northernwind.core.model.HttpStatusException;
36: import it.tidalwave.northernwind.core.model.SiteNode;
37: import it.tidalwave.northernwind.frontend.ui.ViewFactory;
38: import it.tidalwave.northernwind.frontend.ui.annotation.ViewMetadata;
39: import lombok.Getter;
40: import lombok.RequiredArgsConstructor;
41: import lombok.Setter;
42: import lombok.ToString;
43: import lombok.extern.slf4j.Slf4j;
44: import static it.tidalwave.util.NotFoundException.throwWhenNull;
45:
46: /***************************************************************************************************************************************************************
47: *
48: * The default implementation of {@link ViewFactory}.
49: *
50: * @stereotype Factory
51: *
52: * @author Fabrizio Giudici
53: *
54: **************************************************************************************************************************************************************/
55: @RequiredArgsConstructor @Slf4j @ToString
56: public class DefaultViewFactory implements ViewFactory
57: {
58: /* package */ final Map<String, ViewBuilder> viewBuilderMapByTypeUri = new TreeMap<>();
59:
60: @Getter @Setter
61: private boolean logConfigurationEnabled;
62:
63: /***********************************************************************************************************************************************************
64: * {@inheritDoc}
65: **********************************************************************************************************************************************************/
66: @Override @Nonnull
67: public ViewAndController createViewAndController (@Nonnull final String viewTypeUri,
68: @Nonnull final Id viewId,
69: @Nonnull final SiteNode siteNode)
70: throws NotFoundException, HttpStatusException
71: {
72: final var viewBuilder = throwWhenNull(viewBuilderMapByTypeUri.get(viewTypeUri),
73: String.format("Cannot find %s: available: %s",
74: viewTypeUri, viewBuilderMapByTypeUri.keySet()));
75: return viewBuilder.createViewAndController(viewId, siteNode);
76: }
77:
78: /***********************************************************************************************************************************************************
79: *
80: **********************************************************************************************************************************************************/
81: @PostConstruct
82: /* package */ void initialize() // FIXME: gets called twice
83: throws IllegalArgumentException, SecurityException
84: {
85: final var classScanner = new ClassScanner().withAnnotationFilter(ViewMetadata.class);
86:
87:• for (final var viewClass : classScanner.findClasses())
88: {
89: final var viewMetadata = viewClass.getAnnotation(ViewMetadata.class);
90: final var typeUri = viewMetadata.typeUri();
91: final var viewBuilder = new ViewBuilder(viewClass, viewMetadata.controlledBy());
92: viewBuilderMapByTypeUri.put(typeUri, viewBuilder);
93: }
94:
95:• if (logConfigurationEnabled)
96: {
97: logConfiguration();
98: }
99: }
100:
101: /***********************************************************************************************************************************************************
102: *
103: **********************************************************************************************************************************************************/
104: private void logConfiguration()
105: {
106: log.info("View definitions:");
107: viewBuilderMapByTypeUri.values().forEach(viewBuilder -> log.info(">>>> {}", viewBuilder));
108: }
109: }