Skip to content

Package: RepositoryEntitySupport

RepositoryEntitySupport

nameinstructionbranchcomplexitylinemethod
RepositoryEntitySupport(Repository, BindingSet, String)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
RepositoryEntitySupport(Repository, BindingSet, String, String)
M: 0 C: 41
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
RepositoryEntitySupport(Repository, Id, String, Optional, Optional)
M: 44 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
_findArtists()
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
_findPerformances()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
_findPerformers()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
_findRecords()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
_findTracks()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
as(Class)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
as(Class, As.NotFoundBehaviour)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
asMany(Class)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
asOptional(Class)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
configured(SourceAwareFinder)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getFallback()
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%
getId()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getRdfsLabel()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getSource()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$toDuration$2(Value)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$toInteger$0(Value)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$toLong$1(Value)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
maybeAs(Class)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toDuration(Binding)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toId(Binding)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toInteger(Binding)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toLong(Binding)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toPath(Binding)
M: 11 C: 9
45%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 3 C: 1
25%
M: 0 C: 1
100%
toString()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
toString(Binding)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 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/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.model.impl.catalog;
28:
29: import javax.annotation.Nonnull;
30: import javax.annotation.Nullable;
31: import java.time.Duration;
32: import java.util.Optional;
33: import java.nio.file.InvalidPathException;
34: import java.nio.file.Path;
35: import java.nio.file.Paths;
36: import org.eclipse.rdf4j.model.Value;
37: import org.eclipse.rdf4j.query.Binding;
38: import org.eclipse.rdf4j.query.BindingSet;
39: import org.eclipse.rdf4j.repository.Repository;
40: import it.tidalwave.util.Id;
41: import it.tidalwave.util.spi.PriorityAsSupport;
42: import it.tidalwave.role.Identifiable;
43: import it.tidalwave.bluemarine2.model.finder.audio.MusicArtistFinder;
44: import it.tidalwave.bluemarine2.model.finder.audio.MusicPerformerFinder;
45: import it.tidalwave.bluemarine2.model.finder.audio.PerformanceFinder;
46: import it.tidalwave.bluemarine2.model.finder.audio.RecordFinder;
47: import it.tidalwave.bluemarine2.model.finder.audio.TrackFinder;
48: import it.tidalwave.bluemarine2.model.impl.catalog.finder.RepositoryMusicArtistFinder;
49: import it.tidalwave.bluemarine2.model.impl.catalog.finder.RepositoryMusicPerformerFinder;
50: import it.tidalwave.bluemarine2.model.impl.catalog.finder.RepositoryPerformanceFinder;
51: import it.tidalwave.bluemarine2.model.impl.catalog.finder.RepositoryRecordFinder;
52: import it.tidalwave.bluemarine2.model.impl.catalog.finder.RepositoryTrackFinder;
53: import it.tidalwave.bluemarine2.model.spi.Entity;
54: import it.tidalwave.bluemarine2.model.spi.SourceAwareFinder;
55: import lombok.Getter;
56: import lombok.RequiredArgsConstructor;
57: import lombok.ToString;
58: import lombok.experimental.Delegate;
59: import lombok.extern.slf4j.Slf4j;
60: import static it.tidalwave.bluemarine2.util.PathNormalization.*;
61:
62: /***********************************************************************************************************************
63: *
64: * @author Fabrizio Giudici
65: *
66: **********************************************************************************************************************/
67: @RequiredArgsConstructor @ToString(of = { "rdfsLabel", "id"}) @Slf4j
68: public class RepositoryEntitySupport implements Entity, Identifiable
69: {
70: @Nonnull
71: protected final Repository repository;
72:
73: @Getter @Nonnull
74: protected final Id id;
75:
76: @Getter @Nonnull
77: protected final String rdfsLabel;
78:
79: @Getter @Nonnull
80: protected final Optional<Id> source;
81:
82: @Getter @Nonnull
83: protected final Optional<Id> fallback;
84:
85: @Delegate
86: private final PriorityAsSupport asSupport = new PriorityAsSupport(this);
87:
88: /*******************************************************************************************************************
89: *
90: *
91: *
92: ******************************************************************************************************************/
93: public RepositoryEntitySupport (@Nonnull final Repository repository,
94: @Nonnull final BindingSet bindingSet,
95: @Nonnull final String idName)
96: {
97: this(repository, bindingSet, idName, toString(bindingSet.getBinding("label")).orElse(""));
98: }
99:
100: /*******************************************************************************************************************
101: *
102: *
103: *
104: ******************************************************************************************************************/
105: public RepositoryEntitySupport (@Nonnull final Repository repository,
106: @Nonnull final BindingSet bindingSet,
107: @Nonnull final String idName,
108: @Nonnull final String rdfsLabel)
109: {
110: this.repository = repository;
111: this.id = Id.of(toString(bindingSet.getBinding(idName)).get());
112: this.rdfsLabel = rdfsLabel;
113: this.fallback = toId(bindingSet.getBinding("fallback"));
114: this.source = toId(Optional.ofNullable(bindingSet.getBinding("source"))
115: .orElse(bindingSet.getBinding("fallback")));
116: }
117:
118: /*******************************************************************************************************************
119: *
120: ******************************************************************************************************************/
121: @Nonnull
122: protected MusicArtistFinder _findArtists()
123: {
124: return configured(new RepositoryMusicArtistFinder(repository));
125: }
126:
127: /*******************************************************************************************************************
128: *
129: ******************************************************************************************************************/
130: @Nonnull
131: protected MusicPerformerFinder _findPerformers()
132: {
133: return configured(new RepositoryMusicPerformerFinder(repository));
134: }
135:
136: /*******************************************************************************************************************
137: *
138: ******************************************************************************************************************/
139: @Nonnull
140: protected RecordFinder _findRecords()
141: {
142: return configured(new RepositoryRecordFinder(repository));
143: }
144:
145: /*******************************************************************************************************************
146: *
147: ******************************************************************************************************************/
148: @Nonnull
149: protected TrackFinder _findTracks()
150: {
151: return configured(new RepositoryTrackFinder(repository));
152: }
153:
154: /*******************************************************************************************************************
155: *
156: ******************************************************************************************************************/
157: @Nonnull
158: protected PerformanceFinder _findPerformances()
159: {
160: return configured(new RepositoryPerformanceFinder(repository));
161: }
162:
163: /*******************************************************************************************************************
164: *
165: ******************************************************************************************************************/
166: @Nonnull
167: protected static Optional<String> toString (@Nullable final Binding binding)
168: {
169: return Optional.ofNullable(binding).map(Binding::getValue).map(Value::stringValue);
170: }
171:
172: /*******************************************************************************************************************
173: *
174: ******************************************************************************************************************/
175: @Nonnull
176: protected static Optional<Id> toId (@Nullable final Binding binding)
177: {
178: return Optional.ofNullable(binding).map(Binding::getValue).map(Value::stringValue).map(Id::new);
179: }
180:
181: /*******************************************************************************************************************
182: *
183: ******************************************************************************************************************/
184: @Nonnull
185: protected static Optional<Integer> toInteger (@Nullable final Binding binding)
186: {
187: return Optional.ofNullable(binding).map(Binding::getValue).map(v -> Integer.parseInt(v.stringValue()));
188: }
189:
190: /*******************************************************************************************************************
191: *
192: ******************************************************************************************************************/
193: @Nonnull
194: protected static Optional<Long> toLong (@Nullable final Binding binding)
195: {
196: return Optional.ofNullable(binding).map(Binding::getValue).map(v -> Long.parseLong(v.stringValue()));
197: }
198:
199: /*******************************************************************************************************************
200: *
201: ******************************************************************************************************************/
202: @Nonnull
203: protected static Optional<Duration> toDuration (@Nullable final Binding binding)
204: {
205: return Optional.ofNullable(binding).map(Binding::getValue).map(v -> Duration.ofMillis((int)Float.parseFloat(v.stringValue())));
206: }
207:
208: /*******************************************************************************************************************
209: *
210: * Tries to fix a path for character normalization issues (see BMT-46). The idea is to first normalize the encoding
211: * to the native form. If it doesn't work, a broken path is replaced to avoid further errors (of course, the
212: * resource won't be available when requested).
213: * It doesn't try to call normalizedPath() because it's expensive.
214: *
215: * @param binding the binding
216: * @return the path
217: *
218: ******************************************************************************************************************/
219: @Nonnull
220: protected static Path toPath (@Nonnull final Binding binding)
221: {
222: try // FIXME: see BMT-46 - try all posibile normalizations
223: {
224: return Paths.get(normalizedToNativeForm(toString(binding).get()));
225: }
226: catch (InvalidPathException e)
227: {
228: // FIXME: perhaps we could try a similar trick to normalizedPath() - the problem being the fact that it
229: // currently accepts a Path, but we can't convert to a Path. It should be rewritten to work with a String
230: // in input.
231: log.error("Invalid path {}", e.toString());
232: return Paths.get("broken SEE BMT-46");
233: }
234: }
235:
236: /*******************************************************************************************************************
237: *
238: ******************************************************************************************************************/
239: @Nonnull
240: protected <ENTITY, FINDER extends SourceAwareFinder<ENTITY, FINDER>> FINDER configured (@Nonnull final FINDER finder)
241: {
242: return finder.importedFrom(source).withFallback(fallback);
243: }
244: }