1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.ui.framework.ioc;
35
36 import info.magnolia.ui.framework.datasource.DatasourceHolder;
37 import info.magnolia.ui.framework.datasource.DatasourceSupport;
38 import info.magnolia.objectfactory.ParameterResolver;
39 import info.magnolia.util.OptionalConsumer;
40
41 import java.util.Objects;
42 import java.util.Optional;
43
44
45
46
47
48
49
50
51 public class ViewComponentProvider extends UiContextBoundComponentProvider {
52
53 private final UiContextReference viewContextReference;
54
55 public ViewComponentProvider(UiContextReference viewContextReference) {
56 super(viewContextReference);
57 this.viewContextReference = viewContextReference;
58 }
59
60 @Override
61 public <T> T newInstanceWithParameterResolvers(Class<T> type, ParameterResolver... parameters) {
62 final ParameterResolver[] combinedParameters = new ParameterResolver[parameters.length + 2];
63 combinedParameters[parameters.length] = new ViewContextParameterResolver(viewContextReference);
64
65 OptionalConsumer.of(lookupDatasourceDefinition())
66 .ifPresent(datasourceHolder -> combinedParameters[parameters.length + 1] = new DatasourceComponentParameterResolver(getComponent(DatasourceSupport.class), datasourceHolder.getDefinition()))
67 .ifNotPresent(() -> combinedParameters[parameters.length + 1] = parameter -> ParameterResolver.UNRESOLVED);
68
69 System.arraycopy(parameters, 0, combinedParameters, 0, parameters.length);
70 return super.newInstanceWithParameterResolvers(type, combinedParameters);
71 }
72
73 private Optional<DatasourceHolder> lookupDatasourceDefinition() {
74 final BeanStore beanStore = SessionStore.access().getBeanStore(viewContextReference);
75 if (beanStore.hasInstanceOf(DatasourceHolder.class)) {
76 return Optional.of(beanStore.get(DatasourceHolder.class));
77 }
78
79 return viewContextReference.getParentReferences().stream()
80 .map(parentRef -> SessionStore.access().getBeanStore(parentRef))
81 .map(parentStore -> parentStore.getClosestInstance(DatasourceHolder.class))
82 .filter(Objects::nonNull)
83 .map(DatasourceHolder.class::cast)
84 .findFirst();
85 }
86 }