Content of file RepositoryAudioFile.java.html

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>RepositoryAudioFile.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">blueMarine II :: Catalog</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.model.impl.catalog</a> &gt; <span class="el_source">RepositoryAudioFile.java</span></div><h1>RepositoryAudioFile.java</h1><pre class="source lang-java linenums">/*
 * *********************************************************************************************************************
 *
 * blueMarine II: Semantic Media Centre
 * http://tidalwave.it/projects/bluemarine2
 *
 * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
 *
 * *********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * *********************************************************************************************************************
 *
 * git clone https://bitbucket.org/tidalwave/bluemarine2-src
 * git clone https://github.com/tidalwave-it/bluemarine2-src
 *
 * *********************************************************************************************************************
 */
package it.tidalwave.bluemarine2.model.impl.catalog;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import javax.inject.Inject;
import java.time.Duration;
import java.util.Optional;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.eclipse.rdf4j.query.BindingSet;
import org.eclipse.rdf4j.repository.Repository;
import it.tidalwave.util.Id;
import it.tidalwave.util.Memoize;
import it.tidalwave.bluemarine2.util.Formatters;
import it.tidalwave.bluemarine2.model.MediaFileSystem;
import it.tidalwave.bluemarine2.model.audio.AudioFile;
import it.tidalwave.bluemarine2.model.audio.Record;
import it.tidalwave.bluemarine2.model.finder.audio.MusicArtistFinder;
import it.tidalwave.bluemarine2.model.impl.AudioMetadataFactory;
import it.tidalwave.bluemarine2.model.impl.catalog.finder.RepositoryMusicArtistFinder;
import it.tidalwave.bluemarine2.model.impl.catalog.finder.RepositoryRecordFinder;
import it.tidalwave.bluemarine2.model.spi.MetadataSupport;
import it.tidalwave.bluemarine2.model.spi.PathAwareEntity;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import static it.tidalwave.bluemarine2.util.PathNormalization.*;
import static it.tidalwave.bluemarine2.model.MediaItem.Metadata.*;

/***********************************************************************************************************************
 *
 * An implementation of {@link AudioFile} that is mapped to a {@link Repository}.
 *
 * @stereotype  Datum
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="pc bnc" id="L70" title="All 22 branches missed.">@Immutable @Configurable @EqualsAndHashCode(of = { &quot;path&quot;, &quot;trackId&quot; }, callSuper = false) @Slf4j</span>
public class RepositoryAudioFile extends RepositoryEntitySupport implements AudioFile
  {
<span class="nc" id="L73">    @Getter @Nonnull</span>
    private final Path path; // FIXME: rename to relativePath?

<span class="nc" id="L76">    @Getter @Nonnull</span>
    private final Metadata metadata;

    @Nonnull
    private final Optional&lt;Id&gt; trackId;

    @Nonnull
    private final Optional&lt;Duration&gt; duration;

    @Nonnull
    private final Optional&lt;Long&gt; fileSize;

<span class="fc" id="L88">    private final Memoize&lt;Metadata&gt; fallbackMetadata = new Memoize&lt;&gt;();</span>

    @Inject
    private MediaFileSystem fileSystem;

    public RepositoryAudioFile (@Nonnull final Repository repository, @Nonnull final BindingSet bindingSet)
      {
<span class="pc bpc" id="L95" title="9 of 18 branches missed.">        super(repository, bindingSet, &quot;audioFile&quot;, rdfsLabelOf(bindingSet.getBinding(&quot;path&quot;).getValue().stringValue()));</span>
<span class="fc" id="L96">        this.path      = toPath(bindingSet.getBinding(&quot;path&quot;));</span>
<span class="fc" id="L97">        this.duration  = toDuration(bindingSet.getBinding(&quot;duration&quot;));</span>
<span class="fc" id="L98">        this.fileSize  = toLong(bindingSet.getBinding(&quot;fileSize&quot;));</span>
<span class="fc" id="L99">        this.trackId   = toId(bindingSet.getBinding(&quot;track&quot;));</span>

<span class="fc" id="L101">        this.metadata = new MetadataSupport(path).with(TITLE, rdfsLabel)</span>
<span class="fc" id="L102">                                                 .with(DURATION, duration)</span>
<span class="fc" id="L103">                                                 .with(FILE_SIZE, fileSize)</span>
<span class="pc" id="L104">                                                 .withFallback(key -&gt; fallbackMetadata.get(this::loadFallbackMetadata));</span>
<span class="pc bpc" id="L105" title="2 of 4 branches missed.">      }</span>

    @Override @Nonnull
    public AudioFile getAudioFile()
      {
<span class="nc" id="L110">        return this;</span>
      }

    @Override @Nonnull
    public Optional&lt;Resource&gt; getContent()
      throws IOException
      {
<span class="nc" id="L117">        final Path absolutePath = fixedPath(getAbsolutePath());</span>
<span class="nc bnc" id="L118" title="All 2 branches missed.">        return Files.exists(absolutePath) ? Optional.of(new FileSystemResource(absolutePath.toFile())) : Optional.empty();</span>
      }

    @Override @Nonnegative
    public long getSize()
      throws IOException
      {
<span class="nc" id="L125">        return Files.size(fixedPath(getAbsolutePath()));</span>
      }

    @Override @Nonnull
    public MusicArtistFinder findMakers()
      {
<span class="nc" id="L131">        return new RepositoryMusicArtistFinder(repository).makerOf(trackId.get());</span>
      }

    @Override @Nonnull
    public MusicArtistFinder findComposers()
      {
<span class="nc" id="L137">        return new RepositoryMusicArtistFinder(repository).makerOf(trackId.get());</span>
//        return new RepositoryAudioFileArtistFinder(this); FIXME
} @Override @Nonnull public Optional&lt;Record&gt; getRecord() { <span class="nc" id="L144"> return trackId.flatMap(tid -&gt; new RepositoryRecordFinder(repository).containingTrack(tid).optionalFirstResult());</span> } @Override @Nonnull public Optional&lt;PathAwareEntity&gt; getParent() // FIXME: drop it { <span class="nc" id="L150"> throw new UnsupportedOperationException();</span> } @Override @Nonnull public String toString() { <span class="nc" id="L156"> return String.format(&quot;RepositoryAudioFileEntity(%s, %s)&quot;, path, id);</span> } @Override @Nonnull public String toDumpString() { <span class="fc" id="L162"> return String.format(&quot;%s %8s %s %s - %s&quot;, duration.map(Formatters::format).orElse(&quot;??:??&quot;),</span> <span class="fc" id="L163"> fileSize.map(Object::toString).orElse(&quot;&quot;),</span> id, path, rdfsLabel); } @Nonnull private Metadata loadFallbackMetadata() { <span class="nc" id="L170"> final Path absolutePath = getAbsolutePath();</span> <span class="nc" id="L171"> log.debug(&quot;&gt;&gt;&gt;&gt; loading fallback metadata from: {}&quot;, absolutePath);</span> // Don't check for file existence, it would fail for some files - see BMT-46. AudioMetadataFactory does all. <span class="nc" id="L173"> return AudioMetadataFactory.loadFrom(absolutePath);</span> } @Nonnull private Path getAbsolutePath() { <span class="nc" id="L179"> return fileSystem.getRootPath().resolve(path);</span> } @Nonnull private static String rdfsLabelOf (@Nonnull final String path) { <span class="fc" id="L185"> return path.replaceAll(&quot;^.*/&quot;, &quot;&quot;);</span> } } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>