Content of file FileSystemAudioFile.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>FileSystemAudioFile.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 :: Persistence</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-bluemarine2-model</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.model.impl</a> &gt; <span class="el_source">FileSystemAudioFile.java</span></div><h1>FileSystemAudioFile.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;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import java.util.List;
import java.util.Optional;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import it.tidalwave.util.Id;
import it.tidalwave.util.Key;
import it.tidalwave.util.spi.FinderSupport;
import it.tidalwave.util.spi.PriorityAsSupport;
import it.tidalwave.bluemarine2.model.audio.AudioFile;
import it.tidalwave.bluemarine2.model.audio.MusicArtist;
import it.tidalwave.bluemarine2.model.audio.Record;
import it.tidalwave.bluemarine2.model.finder.audio.MusicArtistFinder;
import it.tidalwave.bluemarine2.model.finder.audio.PerformanceFinder;
import it.tidalwave.bluemarine2.model.finder.audio.RecordFinder;
import it.tidalwave.bluemarine2.model.finder.audio.TrackFinder;
import it.tidalwave.bluemarine2.model.spi.NamedEntity;
import it.tidalwave.bluemarine2.model.spi.PathAwareEntity;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Delegate;
import static java.util.Collections.emptyList;
import static it.tidalwave.role.ui.Displayable._Displayable_;

/***********************************************************************************************************************
 *
 * The default implementation of {@link AudioFile}. It basically does nothing, it just acts as an aggregator of roles.
 *
 * @stereotype  Datum
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
@Immutable
public class FileSystemAudioFile implements AudioFile, PathAwareEntity
  {
    /*******************************************************************************************************************
     *
     * Minimal implementation of {@link MusicArtist} without search capabilities.
     *
     ******************************************************************************************************************/
    static class ArtistFallback extends NamedEntity implements MusicArtist
      {
        public ArtistFallback (@Nonnull final String displayName)
          {
<span class="nc" id="L80">            super(displayName);</span>
<span class="nc" id="L81">          }</span>

        @Override @Nonnull
        public TrackFinder findTracks()
          {
<span class="nc" id="L86">            throw new UnsupportedOperationException(&quot;Not supported yet.&quot;); // FIXME</span>
          }

        @Override @Nonnull
        public RecordFinder findRecords()
          {
<span class="nc" id="L92">            throw new UnsupportedOperationException(&quot;Not supported yet.&quot;); // FIXME</span>
          }

        @Override @Nonnull
        public PerformanceFinder findPerformances()
          {
<span class="nc" id="L98">            throw new UnsupportedOperationException(&quot;Not supported yet.&quot;); // FIXME</span>
          }

        @Override
        public int getType()
          {
<span class="nc" id="L104">            throw new UnsupportedOperationException(&quot;Not supported yet.&quot;); // FIXME</span>
          }

        @Override @Nonnull
        public Id getId()
          {
<span class="nc" id="L110">            throw new UnsupportedOperationException(&quot;Not supported yet.&quot;); // FIXME</span>
          }

        @Override @Nonnull
        public Optional&lt;Id&gt; getSource()
          {
<span class="nc" id="L116">            return Optional.of(Id.of(&quot;embedded&quot;)); // FIXME</span>
          }
      }

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
<span class="nc" id="L125">    @RequiredArgsConstructor</span>
    static class ArtistFallbackFinder extends FinderSupport&lt;MusicArtist, MusicArtistFinder&gt; implements MusicArtistFinder
      {
        private static final long serialVersionUID = 7969726066626602758L;

        @Nonnull
        private final Metadata metadata;

        @Nonnull
        private final Key&lt;String&gt; metadataKey;

        public ArtistFallbackFinder (@Nonnull final ArtistFallbackFinder other, @Nonnull final Object override)
          {
<span class="nc" id="L138">            super(other, override);</span>
<span class="nc" id="L139">            final ArtistFallbackFinder source = getSource(ArtistFallbackFinder.class, other, override);</span>
<span class="nc" id="L140">            this.metadata = source.metadata;</span>
<span class="nc" id="L141">            this.metadataKey = source.metadataKey;</span>
<span class="nc" id="L142">          }</span>

        @Override @Nonnull
        protected List&lt;? extends MusicArtist&gt; computeNeededResults()
          {
<span class="nc" id="L147">            return metadata.get(metadataKey).map(artistName -&gt; List.of(new ArtistFallback(artistName))).orElse(emptyList());</span>
          }

        @Override @Nonnull
        public MusicArtistFinder withId (@Nonnull final Id id)
          {
<span class="nc" id="L153">            throw new UnsupportedOperationException(&quot;Not supported yet.&quot;); // FIXME</span>
          }

        @Override @Nonnull
        public MusicArtistFinder makerOf (@Nonnull final Id entityId)
          {
<span class="nc" id="L159">            throw new UnsupportedOperationException(&quot;Not supported yet.&quot;); // FIXME</span>
          }

        @Override @Nonnull
        public MusicArtistFinder importedFrom (@Nonnull final Optional&lt;Id&gt; optionalSource)
          {
<span class="nc" id="L165">            return optionalSource.map(this::importedFrom).orElse(this);</span>
          }

        @Override @Nonnull
        public MusicArtistFinder importedFrom (@Nonnull final Id source)
          {
<span class="nc" id="L171">            throw new UnsupportedOperationException(&quot;Not supported yet.&quot;); // FIXME</span>
          }

        @Override @Nonnull
        public MusicArtistFinder withFallback (@Nonnull final Optional&lt;Id&gt; optionalFallback)
          {
<span class="nc" id="L177">            return optionalFallback.map(this::withFallback).orElse(this);</span>
          }

        @Override @Nonnull
        public MusicArtistFinder withFallback (@Nonnull final Id fallback)
          {
<span class="nc" id="L183">            throw new UnsupportedOperationException(&quot;Not supported yet.&quot;); // FIXME</span>
          }
      }

<span class="nc" id="L187">    @Getter @Nonnull</span>
    private final Path path;

<span class="nc" id="L190">    @Getter @Nonnull</span>
    private final Path relativePath;

    @Nonnull
    private final PathAwareEntity parent;

    @Nullable
    private Metadata metadata;

<span class="nc" id="L199">    @Delegate</span>
    private final PriorityAsSupport asSupport = new PriorityAsSupport(this);

    public FileSystemAudioFile (@Nonnull final Path path,
                                @Nonnull final PathAwareEntity parent,
                                @Nonnull final Path basePath)
<span class="nc" id="L205">      {</span>
<span class="nc" id="L206">        this.path = path;</span>
<span class="nc" id="L207">        this.parent = parent;</span>
<span class="nc" id="L208">        this.relativePath = basePath.relativize(path);</span>
<span class="nc" id="L209">      }</span>

    @Override @Nonnull
    public Id getId()
      {
<span class="nc" id="L214">        return Id.of(path.toString());</span>
      }

    @Override @Nonnull
    public Optional&lt;PathAwareEntity&gt; getParent()
      {
<span class="nc" id="L220">        return Optional.of(parent);</span>
      }


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

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

    @Override @Nonnull
    public synchronized Metadata getMetadata()
      {
<span class="nc bnc" id="L241" title="All 2 branches missed.">        if (metadata == null)</span>
          {
<span class="nc" id="L243">            metadata = AudioMetadataFactory.loadFrom(path);</span>
          }

<span class="nc" id="L246">        return metadata;</span>
      }

    @Override @Nonnull
    public MusicArtistFinder findComposers()
      {
        // FIXME: when present, should use a Repository finder
when present, should use a Repository finder
<span class="nc" id="L253"> return new ArtistFallbackFinder(getMetadata(), Metadata.COMPOSER);</span> } @Override @Nonnull public MusicArtistFinder findMakers() { // FIXME: when present, should use a Repository finder <span class="nc" id="L260"> return new ArtistFallbackFinder(getMetadata(), Metadata.ARTIST);</span> } @Override @Nonnull public AudioFile getAudioFile() { <span class="nc" id="L266"> return this;</span> } @Override @Nonnull public Optional&lt;Record&gt; getRecord() { // FIXME: check - parent should be always present - correct? <span class="nc" id="L273"> return getParent().map(parent -&gt; new NamedRecord(parent.as(_Displayable_).getDisplayName()));</span> } @Override @Nonnull public String toString() { <span class="nc" id="L279"> return String.format(&quot;FileSystemAudioFile(%s)&quot;, relativePath);</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>