Package: SimpleCompositePresentable$SCPFinder
SimpleCompositePresentable$SCPFinder
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
SimpleCompositePresentable.SCPFinder(SimpleCompositePresentable.SCPFinder, Object) |
|
|
|
|
|
||||||||||||||||||||
computeResults() |
|
|
|
|
|
Coverage
1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * SteelBlue: DCI User Interfaces
5: * http://tidalwave.it/projects/steelblue
6: *
7: * Copyright (C) 2015 - 2025 by 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/steelblue-src
22: * git clone https://github.com/tidalwave-it/steelblue-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.ui.core.role.spi;
27:
28: import jakarta.annotation.Nonnull;
29: import java.util.Collection;
30: import java.util.List;
31: import it.tidalwave.ui.core.role.Presentable;
32: import it.tidalwave.ui.core.role.PresentationModel;
33: import it.tidalwave.ui.core.role.PresentationModelFactory;
34: import it.tidalwave.util.As;
35: import it.tidalwave.util.RoleFactory;
36: import it.tidalwave.util.Task;
37: import it.tidalwave.util.spi.ContextSnapshot;
38: import it.tidalwave.util.spi.SimpleFinderSupport;
39: import it.tidalwave.role.SimpleComposite;
40: import lombok.RequiredArgsConstructor;
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.util.ShortNames.*;
45: import static it.tidalwave.role.SimpleComposite._SimpleComposite_;
46:
47: /***************************************************************************************************************************************************************
48: *
49: * An implementation of {@link Presentable} for datum instances having the {@link SimpleComposite} role.
50: *
51: * @stereotype Role
52: * @since 2.0-ALPHA-1
53: * @author Fabrizio Giudici
54: *
55: **************************************************************************************************************************************************************/
56: @Slf4j
57: public class SimpleCompositePresentable implements Presentable
58: {
59: private static final As.Type<SimpleComposite<As>> _SimpleAsComposite_ = As.type(SimpleComposite.class);
60:
61: @RequiredArgsConstructor
62: public static class SCPFinder extends SimpleFinderSupport<PresentationModel>
63: {
64: // private static final long serialVersionUID = -3235827383866946732L;
65:
66: @Nonnull
67: private final SimpleCompositePresentable scp;
68:
69: @Nonnull
70: private final Collection<Object> roles;
71:
72: public SCPFinder (@Nonnull final SCPFinder other, @Nonnull final Object override)
73: {
74: super(other, override);
75: final var source = getSource(SCPFinder.class, other, override);
76: this.scp = source.scp;
77: this.roles = source.roles;
78: }
79:
80: @Override @Nonnull
81: protected List<PresentationModel> computeResults()
82: {
83: return scp.contextSnapshot.runWithContexts(new Task<>()
84: {
85: @Override @Nonnull
86: public List<PresentationModel> run()
87: {
88: final List<As> children = scp.datum.maybeAs(_SimpleAsComposite_).map(c -> c.findChildren().results()).orElse(emptyList());
89: return children.stream()
90: .map(child -> child.maybeAs(_Presentable_).orElseGet(() -> new SimpleCompositePresentable(child)))
91: .map(presentable -> presentable.createPresentationModel(roles))
92: .collect(toList());
93: }
94: });
95: }
96: }
97:
98: @Nonnull
99: private final As datum;
100:
101: // This is not @Injected to avoid a dependency on Spring AOP
102: @Nonnull
103: private final PresentationModelFactory defaultPresentationModelFactory;
104:
105: private final ContextSnapshot contextSnapshot;
106:
107: /***********************************************************************************************************************************************************
108: * @param datum the owner
109: **********************************************************************************************************************************************************/
110: public SimpleCompositePresentable (@Nonnull final As datum)
111: {
112: this(datum, new DefaultPresentationModelFactory());
113: }
114:
115: /***********************************************************************************************************************************************************
116: * @param datum the owner
117: * @param defaultPresentationModelFactory the {@code PresentationModelFactory}
118: **********************************************************************************************************************************************************/
119: public SimpleCompositePresentable (@Nonnull final As datum,
120: @Nonnull final PresentationModelFactory defaultPresentationModelFactory)
121: {
122: this.datum = datum;
123: this.defaultPresentationModelFactory = defaultPresentationModelFactory;
124: contextSnapshot = new ContextSnapshot(datum);
125: }
126:
127: /***********************************************************************************************************************************************************
128: * {@inheritDoc}
129: **********************************************************************************************************************************************************/
130: @Override @Nonnull
131: public PresentationModel createPresentationModel (@Nonnull final Collection<Object> roles)
132: {
133: return internalCreatePresentationModel(datum, roles);
134: }
135:
136: /***********************************************************************************************************************************************************
137: *
138: **********************************************************************************************************************************************************/
139: @Nonnull
140: private PresentationModel internalCreatePresentationModel (@Nonnull final As datum,
141: @Nonnull final Collection<Object> roles)
142: {
143: final var pmFinder = new SCPFinder(this, roles);
144:
145: return contextSnapshot.runWithContexts(new Task<>()
146: {
147: @Override @Nonnull
148: public PresentationModel run()
149: {
150: final var r = RoleFactory.resolveFactories(datum, roles);
151:
152: if (datum.maybeAs(_SimpleComposite_).isPresent())
153: {
154: r.add(SimpleComposite.of(pmFinder));
155: }
156:
157: log.trace(">>>> r for {}: {}", shortId(datum), shortIds(r));
158:
159: return defaultPresentationModelFactory.createPresentationModel(datum, r);
160: }
161: });
162: }
163: }