Skip to contentMethod: computeResults()
1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 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
12: * the License. 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
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.role.ui.spi;
28:
29: import javax.annotation.Nonnull;
30: import java.util.ArrayList;
31: import java.util.Collection;
32: import java.util.List;
33: import it.tidalwave.util.As;
34: import it.tidalwave.util.RoleFactory;
35: import it.tidalwave.util.Task;
36: import it.tidalwave.util.spi.SimpleFinderSupport;
37: import it.tidalwave.role.SimpleComposite;
38: import it.tidalwave.role.spi.ContextSampler;
39: import it.tidalwave.role.ui.Presentable;
40: import it.tidalwave.role.ui.PresentationModel;
41: import it.tidalwave.role.ui.PresentationModelFactory;
42: import lombok.RequiredArgsConstructor;
43: import lombok.extern.slf4j.Slf4j;
44: import static java.util.Collections.emptyList;
45: import static java.util.stream.Collectors.*;
46: import static it.tidalwave.role.SimpleComposite._SimpleComposite_;
47: import static it.tidalwave.role.spi.impl.LogUtil.*;
48:
49: /***********************************************************************************************************************
50: *
51: * An implementation of {@link Presentable} for datum instances having the {@link SimpleComposite} role.
52: *
53: * @stereotype Role
54: *
55: * @author Fabrizio Giudici
56: *
57: **********************************************************************************************************************/
58: @Slf4j
59: public class SimpleCompositePresentable implements Presentable
60: {
61: @RequiredArgsConstructor
62: 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 SCPFinder source = getSource(SCPFinder.class, other, override);
76: this.scp = source.scp;
77: this.roles = source.roles;
78: }
79:
80: @Override @Nonnull
81: protected List<? extends PresentationModel> computeResults()
82: {
83: return scp.contextSampler.runWithContexts(new Task<List<? extends PresentationModel>, RuntimeException>()
84: {
85: @Override @Nonnull
86: public List<? extends PresentationModel> run()
87: {
88: final List<As> children = scp.datum.maybeAs(_SimpleComposite_)
89: .map(c -> c.findChildren().results()).orElse(emptyList());
90: return children.stream()
91: .map(child -> child.maybeAs(_Presentable_)
92: .orElseGet(() -> new SimpleCompositePresentable(child)))
93: .map(presentable -> presentable.createPresentationModel(roles))
94: .collect(toList());
95: }
96: });
97: }
98: }
99:
100: private static final long serialVersionUID = 324646965695684L;
101:
102: @Nonnull
103: private final As datum;
104:
105: // This is not @Injected to avoid a dependency on Spring AOP
106: @Nonnull
107: private final PresentationModelFactory defaultPresentationModelFactory;
108:
109: private final ContextSampler contextSampler;
110:
111: /*******************************************************************************************************************
112: *
113: * @param datum the owner
114: *
115: ******************************************************************************************************************/
116: public SimpleCompositePresentable (@Nonnull final As datum)
117: {
118: this(datum, new DefaultPresentationModelFactory());
119: }
120:
121: /*******************************************************************************************************************
122: *
123: * @param datum the owner
124: * @param defaultPresentationModelFactory the {@code PresentationModelFactory}
125: *
126: ******************************************************************************************************************/
127: public SimpleCompositePresentable (@Nonnull final As datum,
128: @Nonnull final PresentationModelFactory defaultPresentationModelFactory)
129: {
130: this.datum = datum;
131: this.defaultPresentationModelFactory = defaultPresentationModelFactory;
132: contextSampler = new ContextSampler(datum);
133: }
134:
135: /*******************************************************************************************************************
136: *
137: * {@inheritDoc}
138: *
139: ******************************************************************************************************************/
140: @Override @Nonnull
141: public PresentationModel createPresentationModel (@Nonnull final Collection<Object> roles)
142: {
143: return internalCreatePresentationModel(datum, roles);
144: }
145:
146: /*******************************************************************************************************************
147: *
148: *
149: *
150: ******************************************************************************************************************/
151: @Nonnull
152: private PresentationModel internalCreatePresentationModel (@Nonnull final As datum,
153: @Nonnull final Collection<Object> roles)
154: {
155: final SCPFinder pmFinder = new SCPFinder(this, roles);
156:
157: return contextSampler.runWithContexts(new Task<PresentationModel, RuntimeException>()
158: {
159: @Override @Nonnull
160: public PresentationModel run()
161: {
162: final List<Object> r = resolveRoles(datum, roles);
163:
164: if (datum.maybeAs(_SimpleComposite_).isPresent())
165: {
166: r.add(SimpleComposite.of(pmFinder));
167: }
168:
169: log.trace(">>>> r for {}: {}", shortId(datum), shortIds(r));
170:
171: return defaultPresentationModelFactory.createPresentationModel(datum, r);
172: }
173: });
174: }
175:
176: /*******************************************************************************************************************
177: *
178: *
179: *
180: ******************************************************************************************************************/
181: @Nonnull
182: private List<Object> resolveRoles (@Nonnull final As datum, @Nonnull final Collection<Object> roles)
183: {
184: final List<Object> r = new ArrayList<>();
185:
186: for (final Object roleOrFactory : roles)
187: {
188: if (roleOrFactory instanceof RoleFactory)
189: {
190: r.add(((RoleFactory<As>)roleOrFactory).createRoleFor(datum));
191: }
192: else
193: {
194: r.add(roleOrFactory);
195: }
196: }
197:
198: return r;
199: }
200: }