Content of file RepositoryEntitySupport.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>RepositoryEntitySupport.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 :: Headless Service</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-bluemarine2-catalog</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.model.impl.catalog</a> &gt; <span class="el_source">RepositoryEntitySupport.java</span></div><h1>RepositoryEntitySupport.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.Nonnull;
import javax.annotation.Nullable;
import java.time.Duration;
import java.util.Optional;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.query.Binding;
import org.eclipse.rdf4j.query.BindingSet;
import org.eclipse.rdf4j.repository.Repository;
import it.tidalwave.util.Id;
import it.tidalwave.util.spi.PriorityAsSupport;
import it.tidalwave.role.Identifiable;
import it.tidalwave.bluemarine2.model.finder.audio.MusicArtistFinder;
import it.tidalwave.bluemarine2.model.finder.audio.MusicPerformerFinder;
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.impl.catalog.finder.RepositoryMusicArtistFinder;
import it.tidalwave.bluemarine2.model.impl.catalog.finder.RepositoryMusicPerformerFinder;
import it.tidalwave.bluemarine2.model.impl.catalog.finder.RepositoryPerformanceFinder;
import it.tidalwave.bluemarine2.model.impl.catalog.finder.RepositoryRecordFinder;
import it.tidalwave.bluemarine2.model.impl.catalog.finder.RepositoryTrackFinder;
import it.tidalwave.bluemarine2.model.spi.Entity;
import it.tidalwave.bluemarine2.model.spi.SourceAwareFinder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import lombok.experimental.Delegate;
import lombok.extern.slf4j.Slf4j;
import static it.tidalwave.bluemarine2.util.PathNormalization.*;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="pc" id="L67">@RequiredArgsConstructor @ToString(of = { &quot;rdfsLabel&quot;, &quot;id&quot;}) @Slf4j</span>
public class RepositoryEntitySupport implements Entity, Identifiable
  {
    @Nonnull
    protected final Repository repository;

<span class="fc" id="L73">    @Getter @Nonnull</span>
    protected final Id id;

<span class="fc" id="L76">    @Getter @Nonnull</span>
    protected final String rdfsLabel;

<span class="fc" id="L79">    @Getter @Nonnull</span>
    protected final Optional&lt;Id&gt; source;

<span class="nc" id="L82">    @Getter @Nonnull</span>
    protected final Optional&lt;Id&gt; fallback;

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

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    public RepositoryEntitySupport (@Nonnull final Repository repository,
                                    @Nonnull final BindingSet bindingSet,
                                    @Nonnull final String idName)
      {
<span class="fc" id="L97">        this(repository, bindingSet, idName, toString(bindingSet.getBinding(&quot;label&quot;)).orElse(&quot;&quot;));</span>
<span class="fc" id="L98">      }</span>

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    public RepositoryEntitySupport (@Nonnull final Repository repository,
                                    @Nonnull final BindingSet bindingSet,
                                    @Nonnull final String idName,
                                    @Nonnull final String rdfsLabel)
<span class="fc" id="L109">      {</span>
<span class="fc" id="L110">        this.repository = repository;</span>
<span class="fc" id="L111">        this.id = Id.of(toString(bindingSet.getBinding(idName)).get());</span>
<span class="fc" id="L112">        this.rdfsLabel = rdfsLabel;</span>
<span class="fc" id="L113">        this.fallback = toId(bindingSet.getBinding(&quot;fallback&quot;));</span>
<span class="fc" id="L114">        this.source = toId(Optional.ofNullable(bindingSet.getBinding(&quot;source&quot;))</span>
<span class="fc" id="L115">                                       .orElse(bindingSet.getBinding(&quot;fallback&quot;)));</span>
<span class="fc" id="L116">      }</span>

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    @Nonnull
    protected MusicArtistFinder _findArtists()
      {
<span class="nc" id="L124">        return configured(new RepositoryMusicArtistFinder(repository));</span>
      }

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    @Nonnull
    protected MusicPerformerFinder _findPerformers()
      {
<span class="fc" id="L133">        return configured(new RepositoryMusicPerformerFinder(repository));</span>
      }

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    @Nonnull
    protected RecordFinder _findRecords()
      {
<span class="fc" id="L142">        return configured(new RepositoryRecordFinder(repository));</span>
      }

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    @Nonnull
    protected TrackFinder _findTracks()
      {
<span class="fc" id="L151">        return configured(new RepositoryTrackFinder(repository));</span>
      }

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    @Nonnull
    protected PerformanceFinder _findPerformances()
      {
<span class="fc" id="L160">        return configured(new RepositoryPerformanceFinder(repository));</span>
      }

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    @Nonnull
    protected static Optional&lt;String&gt; toString (@Nullable final Binding binding)
      {
<span class="fc" id="L169">        return Optional.ofNullable(binding).map(Binding::getValue).map(Value::stringValue);</span>
      }

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    @Nonnull
    protected static Optional&lt;Id&gt; toId (@Nullable final Binding binding)
      {
<span class="fc" id="L178">        return Optional.ofNullable(binding).map(Binding::getValue).map(Value::stringValue).map(Id::new);</span>
      }

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    @Nonnull
    protected static Optional&lt;Integer&gt; toInteger (@Nullable final Binding binding)
      {
<span class="fc" id="L187">        return Optional.ofNullable(binding).map(Binding::getValue).map(v -&gt; Integer.parseInt(v.stringValue()));</span>
      }

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    @Nonnull
    protected static Optional&lt;Long&gt; toLong (@Nullable final Binding binding)
      {
<span class="fc" id="L196">        return Optional.ofNullable(binding).map(Binding::getValue).map(v -&gt; Long.parseLong(v.stringValue()));</span>
      }

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    @Nonnull
    protected static Optional&lt;Duration&gt; toDuration (@Nullable final Binding binding)
      {
<span class="fc" id="L205">        return Optional.ofNullable(binding).map(Binding::getValue).map(v -&gt; Duration.ofMillis((int)Float.parseFloat(v.stringValue())));</span>
      }

    /*******************************************************************************************************************
     *
     * Tries to fix a path for character normalization issues (see BMT-46). The idea is to first normalize the encoding
     * to the native form. If it doesn't work, a broken path is replaced to avoid further errors (of course, the
     * resource won't be available when requested).
     * It doesn't try to call normalizedPath() because it's expensive.
     *
     * @param   binding     the binding
     * @return              the path
     *
     ******************************************************************************************************************/
    @Nonnull
    protected static Path toPath (@Nonnull final Binding binding)
      {
        try // FIXME: see BMT-46 - try all posibile normalizations
see BMT-46 - try all posibile normalizations
{ <span class="fc" id="L224"> return Paths.get(normalizedToNativeForm(toString(binding).get()));</span> } <span class="nc" id="L226"> catch (InvalidPathException e)</span> { // FIXME: perhaps we could try a similar trick to normalizedPath() - the problem being the fact that it // currently accepts a Path, but we can't convert to a Path. It should be rewritten to work with a String // in input. <span class="nc" id="L231"> log.error(&quot;Invalid path {}&quot;, e.toString());</span> <span class="nc" id="L232"> return Paths.get(&quot;broken SEE BMT-46&quot;);</span> } } /******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull protected &lt;ENTITY, FINDER extends SourceAwareFinder&lt;ENTITY, FINDER&gt;&gt; FINDER configured (@Nonnull final FINDER finder) { <span class="fc" id="L242"> return finder.importedFrom(source).withFallback(fallback);</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>