Skip to content

Method: getId()

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;
28:
29: import javax.annotation.Nonnegative;
30: import javax.annotation.Nonnull;
31: import javax.annotation.Nullable;
32: import javax.annotation.concurrent.Immutable;
33: import java.util.List;
34: import java.util.Optional;
35: import java.io.IOException;
36: import java.nio.file.Files;
37: import java.nio.file.Path;
38: import org.springframework.core.io.FileSystemResource;
39: import org.springframework.core.io.Resource;
40: import it.tidalwave.util.Id;
41: import it.tidalwave.util.Key;
42: import it.tidalwave.util.spi.FinderSupport;
43: import it.tidalwave.util.spi.PriorityAsSupport;
44: import it.tidalwave.bluemarine2.model.audio.AudioFile;
45: import it.tidalwave.bluemarine2.model.audio.MusicArtist;
46: import it.tidalwave.bluemarine2.model.audio.Record;
47: import it.tidalwave.bluemarine2.model.finder.audio.MusicArtistFinder;
48: import it.tidalwave.bluemarine2.model.finder.audio.PerformanceFinder;
49: import it.tidalwave.bluemarine2.model.finder.audio.RecordFinder;
50: import it.tidalwave.bluemarine2.model.finder.audio.TrackFinder;
51: import it.tidalwave.bluemarine2.model.spi.NamedEntity;
52: import it.tidalwave.bluemarine2.model.spi.PathAwareEntity;
53: import lombok.Getter;
54: import lombok.RequiredArgsConstructor;
55: import lombok.experimental.Delegate;
56: import static java.util.Collections.emptyList;
57: import static it.tidalwave.role.ui.Displayable._Displayable_;
58:
59: /***********************************************************************************************************************
60: *
61: * The default implementation of {@link AudioFile}. It basically does nothing, it just acts as an aggregator of roles.
62: *
63: * @stereotype Datum
64: *
65: * @author Fabrizio Giudici
66: *
67: **********************************************************************************************************************/
68: @Immutable
69: public class FileSystemAudioFile implements AudioFile, PathAwareEntity
70: {
71: /*******************************************************************************************************************
72: *
73: * Minimal implementation of {@link MusicArtist} without search capabilities.
74: *
75: ******************************************************************************************************************/
76: static class ArtistFallback extends NamedEntity implements MusicArtist
77: {
78: public ArtistFallback (@Nonnull final String displayName)
79: {
80: super(displayName);
81: }
82:
83: @Override @Nonnull
84: public TrackFinder findTracks()
85: {
86: throw new UnsupportedOperationException("Not supported yet."); // FIXME
87: }
88:
89: @Override @Nonnull
90: public RecordFinder findRecords()
91: {
92: throw new UnsupportedOperationException("Not supported yet."); // FIXME
93: }
94:
95: @Override @Nonnull
96: public PerformanceFinder findPerformances()
97: {
98: throw new UnsupportedOperationException("Not supported yet."); // FIXME
99: }
100:
101: @Override
102: public int getType()
103: {
104: throw new UnsupportedOperationException("Not supported yet."); // FIXME
105: }
106:
107: @Override @Nonnull
108: public Id getId()
109: {
110: throw new UnsupportedOperationException("Not supported yet."); // FIXME
111: }
112:
113: @Override @Nonnull
114: public Optional<Id> getSource()
115: {
116: return Optional.of(Id.of("embedded")); // FIXME
117: }
118: }
119:
120: /*******************************************************************************************************************
121: *
122: *
123: *
124: ******************************************************************************************************************/
125: @RequiredArgsConstructor
126: static class ArtistFallbackFinder extends FinderSupport<MusicArtist, MusicArtistFinder> implements MusicArtistFinder
127: {
128: private static final long serialVersionUID = 7969726066626602758L;
129:
130: @Nonnull
131: private final Metadata metadata;
132:
133: @Nonnull
134: private final Key<String> metadataKey;
135:
136: public ArtistFallbackFinder (@Nonnull final ArtistFallbackFinder other, @Nonnull final Object override)
137: {
138: super(other, override);
139: final ArtistFallbackFinder source = getSource(ArtistFallbackFinder.class, other, override);
140: this.metadata = source.metadata;
141: this.metadataKey = source.metadataKey;
142: }
143:
144: @Override @Nonnull
145: protected List<? extends MusicArtist> computeNeededResults()
146: {
147: return metadata.get(metadataKey).map(artistName -> List.of(new ArtistFallback(artistName))).orElse(emptyList());
148: }
149:
150: @Override @Nonnull
151: public MusicArtistFinder withId (@Nonnull final Id id)
152: {
153: throw new UnsupportedOperationException("Not supported yet."); // FIXME
154: }
155:
156: @Override @Nonnull
157: public MusicArtistFinder makerOf (@Nonnull final Id entityId)
158: {
159: throw new UnsupportedOperationException("Not supported yet."); // FIXME
160: }
161:
162: @Override @Nonnull
163: public MusicArtistFinder importedFrom (@Nonnull final Optional<Id> optionalSource)
164: {
165: return optionalSource.map(this::importedFrom).orElse(this);
166: }
167:
168: @Override @Nonnull
169: public MusicArtistFinder importedFrom (@Nonnull final Id source)
170: {
171: throw new UnsupportedOperationException("Not supported yet."); // FIXME
172: }
173:
174: @Override @Nonnull
175: public MusicArtistFinder withFallback (@Nonnull final Optional<Id> optionalFallback)
176: {
177: return optionalFallback.map(this::withFallback).orElse(this);
178: }
179:
180: @Override @Nonnull
181: public MusicArtistFinder withFallback (@Nonnull final Id fallback)
182: {
183: throw new UnsupportedOperationException("Not supported yet."); // FIXME
184: }
185: }
186:
187: @Getter @Nonnull
188: private final Path path;
189:
190: @Getter @Nonnull
191: private final Path relativePath;
192:
193: @Nonnull
194: private final PathAwareEntity parent;
195:
196: @Nullable
197: private Metadata metadata;
198:
199: @Delegate
200: private final PriorityAsSupport asSupport = new PriorityAsSupport(this);
201:
202: public FileSystemAudioFile (@Nonnull final Path path,
203: @Nonnull final PathAwareEntity parent,
204: @Nonnull final Path basePath)
205: {
206: this.path = path;
207: this.parent = parent;
208: this.relativePath = basePath.relativize(path);
209: }
210:
211: @Override @Nonnull
212: public Id getId()
213: {
214: return Id.of(path.toString());
215: }
216:
217: @Override @Nonnull
218: public Optional<PathAwareEntity> getParent()
219: {
220: return Optional.of(parent);
221: }
222:
223:
224: @Override @Nonnull
225: public Optional<Resource> getContent()
226: throws IOException
227: {
228: return Files.exists(path) ? Optional.of(new FileSystemResource(path.toFile())) : Optional.empty();
229: }
230:
231: @Override @Nonnegative
232: public long getSize()
233: throws IOException
234: {
235: return Files.size(path);
236: }
237:
238: @Override @Nonnull
239: public synchronized Metadata getMetadata()
240: {
241: if (metadata == null)
242: {
243: metadata = AudioMetadataFactory.loadFrom(path);
244: }
245:
246: return metadata;
247: }
248:
249: @Override @Nonnull
250: public MusicArtistFinder findComposers()
251: {
252: // FIXME: when present, should use a Repository finder
253: return new ArtistFallbackFinder(getMetadata(), Metadata.COMPOSER);
254: }
255:
256: @Override @Nonnull
257: public MusicArtistFinder findMakers()
258: {
259: // FIXME: when present, should use a Repository finder
260: return new ArtistFallbackFinder(getMetadata(), Metadata.ARTIST);
261: }
262:
263: @Override @Nonnull
264: public AudioFile getAudioFile()
265: {
266: return this;
267: }
268:
269: @Override @Nonnull
270: public Optional<Record> getRecord()
271: {
272: // FIXME: check - parent should be always present - correct?
273: return getParent().map(parent -> new NamedRecord(parent.as(_Displayable_).getDisplayName()));
274: }
275:
276: @Override @Nonnull
277: public String toString()
278: {
279: return String.format("FileSystemAudioFile(%s)", relativePath);
280: }
281: }