Skip to content

Package: PathFinderSupport

PathFinderSupport

nameinstructionbranchcomplexitylinemethod
PathFinderSupport(PathFinderSupport, Object)
M: 99 C: 0
0%
M: 22 C: 0
0%
M: 12 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
PathFinderSupport(Resource)
M: 110 C: 0
0%
M: 22 C: 0
0%
M: 12 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
computeResults()
M: 24 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getInterface(Resource)
M: 39 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
lambda$computeResults$0(Site, ResourceFile)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
readResolve()
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%
static {...}
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
toString()
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%

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.core.impl.model;
28:
29: import javax.annotation.Nonnull;
30: import javax.inject.Inject;
31: import javax.inject.Provider;
32: import java.util.List;
33: import org.springframework.beans.factory.annotation.Configurable;
34: import it.tidalwave.util.NotFoundException;
35: import it.tidalwave.util.spi.HierarchicFinderSupport;
36: import it.tidalwave.northernwind.core.model.Content;
37: import it.tidalwave.northernwind.core.model.Media;
38: import it.tidalwave.northernwind.core.model.Resource;
39: import it.tidalwave.northernwind.core.model.ResourceFile;
40: import it.tidalwave.northernwind.core.model.ResourcePath;
41: import it.tidalwave.northernwind.core.model.SiteNode;
42: import it.tidalwave.northernwind.core.model.SiteProvider;
43: import lombok.ToString;
44: import lombok.extern.slf4j.Slf4j;
45: import static java.util.stream.Collectors.*;
46:
47: /***********************************************************************************************************************
48: *
49: *
50: *
51: * @author Fabrizio Giudici
52: *
53: **********************************************************************************************************************/
54: @Configurable(preConstruction = true) @Slf4j @ToString
55: public class PathFinderSupport<T extends Resource> extends HierarchicFinderSupport<T, PathFinderSupport<T>>
56: {
57: private static final long serialVersionUID = 2345536092354546452L;
58:
59: private static final List<Class<?>> ALLOWED_TYPES = List.of(Content.class, SiteNode.class, Media.class);
60:
61: @Inject
62: private transient Provider<SiteProvider> siteProvider;
63:
64: @Nonnull
65: private final Class<T> typeClass;
66:
67: @Nonnull
68: private final transient ResourceFile parentFile;
69:
70: @Nonnull
71: private final ResourcePath resourceRootPath;
72:
73: /*******************************************************************************************************************
74: *
75: *
76: ******************************************************************************************************************/
77: @SuppressWarnings("squid:S00112")
78: public PathFinderSupport (@Nonnull final T parentResource)
79:• {
80: try
81: {
82: final var site = siteProvider.get().getSite();
83: this.typeClass = getInterface(parentResource);
84: this.resourceRootPath = site.find(typeClass).withRelativePath("/").result().getFile().getPath();
85: this.parentFile = parentResource.getFile();
86: }
87: catch (NotFoundException e)
88: {
89: throw new RuntimeException(e); // never occurs
90: }
91:• }
92:
93: /*******************************************************************************************************************
94: *
95: * Clone constructor.
96: *
97: ******************************************************************************************************************/
98: public PathFinderSupport (@Nonnull final PathFinderSupport<T> other, @Nonnull final Object override)
99: {
100:• super(other, override);
101: final PathFinderSupport<T> source = getSource(PathFinderSupport.class, other, override);
102: this.typeClass = source.typeClass;
103: this.parentFile = source.parentFile;
104: this.resourceRootPath = source.resourceRootPath;
105:• }
106:
107: /*******************************************************************************************************************
108: *
109: * {@inheritDoc}
110: *
111: ******************************************************************************************************************/
112: @Override
113: @Nonnull
114: protected List<T> computeResults()
115: {
116: final var site = siteProvider.get().getSite();
117: return parentFile.findChildren().withRecursion(true).results().stream()
118: .filter(ResourceFile::isFolder)
119: .flatMap(c -> site.find(typeClass).withRelativePath(c.getPath().relativeTo(resourceRootPath).urlDecoded())
120: .results().stream())
121: .collect(toList());
122: }
123:
124: /*******************************************************************************************************************
125: *
126: *
127: ******************************************************************************************************************/
128: @Nonnull
129: private Class<T> getInterface (@Nonnull final T resource)
130: {
131:• for (Class<?> type = resource.getClass(); type != null; type = type.getSuperclass())
132: {
133:• for (final var interface_ : type.getInterfaces())
134: {
135:• if (ALLOWED_TYPES.contains(interface_))
136: {
137: return (Class<T>)interface_;
138: }
139: }
140: }
141:
142: throw new IllegalArgumentException("Illegal type: " + resource + "; allowed must implement " + ALLOWED_TYPES);
143: }
144: }