RepositoryEntitySupport.java

  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. import javax.annotation.Nonnull;
  29. import javax.annotation.Nullable;
  30. import java.util.Optional;
  31. import java.time.Duration;
  32. import java.nio.file.InvalidPathException;
  33. import java.nio.file.Path;
  34. import java.nio.file.Paths;
  35. import org.eclipse.rdf4j.model.Value;
  36. import org.eclipse.rdf4j.repository.Repository;
  37. import org.eclipse.rdf4j.query.Binding;
  38. import org.eclipse.rdf4j.query.BindingSet;
  39. import it.tidalwave.util.Id;
  40. import it.tidalwave.util.spi.PriorityAsSupport;
  41. import it.tidalwave.role.Identifiable;
  42. import it.tidalwave.bluemarine2.model.finder.audio.MusicArtistFinder;
  43. import it.tidalwave.bluemarine2.model.finder.audio.MusicPerformerFinder;
  44. import it.tidalwave.bluemarine2.model.finder.audio.PerformanceFinder;
  45. import it.tidalwave.bluemarine2.model.finder.audio.RecordFinder;
  46. import it.tidalwave.bluemarine2.model.finder.audio.TrackFinder;
  47. import it.tidalwave.bluemarine2.model.spi.Entity;
  48. import it.tidalwave.bluemarine2.model.spi.SourceAwareFinder;
  49. import it.tidalwave.bluemarine2.model.impl.catalog.finder.RepositoryMusicArtistFinder;
  50. import it.tidalwave.bluemarine2.model.impl.catalog.finder.RepositoryMusicPerformerFinder;
  51. import it.tidalwave.bluemarine2.model.impl.catalog.finder.RepositoryPerformanceFinder;
  52. import it.tidalwave.bluemarine2.model.impl.catalog.finder.RepositoryRecordFinder;
  53. import it.tidalwave.bluemarine2.model.impl.catalog.finder.RepositoryTrackFinder;
  54. import lombok.experimental.Delegate;
  55. import lombok.Getter;
  56. import lombok.RequiredArgsConstructor;
  57. import lombok.ToString;
  58. import lombok.extern.slf4j.Slf4j;
  59. import static it.tidalwave.bluemarine2.util.Miscellaneous.normalizedToNativeForm;

  60. /***********************************************************************************************************************
  61.  *
  62.  * @author  Fabrizio Giudici
  63.  *
  64.  **********************************************************************************************************************/
  65. @RequiredArgsConstructor @ToString(of = { "rdfsLabel", "id"}) @Slf4j
  66. public class RepositoryEntitySupport implements Entity, Identifiable
  67.   {
  68.     @Nonnull
  69.     protected final Repository repository;

  70.     @Getter @Nonnull
  71.     protected final Id id;

  72.     @Getter @Nonnull
  73.     protected final String rdfsLabel;

  74.     @Getter @Nonnull
  75.     protected final Optional<Id> source;

  76.     @Getter @Nonnull
  77.     protected final Optional<Id> fallback;

  78.     @Delegate
  79.     private final PriorityAsSupport asSupport = new PriorityAsSupport(this);

  80.     /*******************************************************************************************************************
  81.      *
  82.      *
  83.      *
  84.      ******************************************************************************************************************/
  85.     public RepositoryEntitySupport (@Nonnull final Repository repository,
  86.                                     @Nonnull final BindingSet bindingSet,
  87.                                     @Nonnull final String idName)
  88.       {
  89.         this(repository, bindingSet, idName, toString(bindingSet.getBinding("label")).orElse(""));
  90.       }

  91.     /*******************************************************************************************************************
  92.      *
  93.      *
  94.      *
  95.      ******************************************************************************************************************/
  96.     public RepositoryEntitySupport (@Nonnull final Repository repository,
  97.                                     @Nonnull final BindingSet bindingSet,
  98.                                     @Nonnull final String idName,
  99.                                     @Nonnull final String rdfsLabel)
  100.       {
  101.         this.repository = repository;
  102.         this.id = Id.of(toString(bindingSet.getBinding(idName)).get());
  103.         this.rdfsLabel = rdfsLabel;
  104.         this.fallback = toId(bindingSet.getBinding("fallback"));
  105.         this.source = toId(Optional.ofNullable(bindingSet.getBinding("source"))
  106.                                        .orElse(bindingSet.getBinding("fallback")));
  107.       }

  108.     /*******************************************************************************************************************
  109.      *
  110.      ******************************************************************************************************************/
  111.     @Nonnull
  112.     protected MusicArtistFinder _findArtists()
  113.       {
  114.         return configured(new RepositoryMusicArtistFinder(repository));
  115.       }

  116.     /*******************************************************************************************************************
  117.      *
  118.      ******************************************************************************************************************/
  119.     @Nonnull
  120.     protected MusicPerformerFinder _findPerformers()
  121.       {
  122.         return configured(new RepositoryMusicPerformerFinder(repository));
  123.       }

  124.     /*******************************************************************************************************************
  125.      *
  126.      ******************************************************************************************************************/
  127.     @Nonnull
  128.     protected RecordFinder _findRecords()
  129.       {
  130.         return configured(new RepositoryRecordFinder(repository));
  131.       }

  132.     /*******************************************************************************************************************
  133.      *
  134.      ******************************************************************************************************************/
  135.     @Nonnull
  136.     protected TrackFinder _findTracks()
  137.       {
  138.         return configured(new RepositoryTrackFinder(repository));
  139.       }

  140.     /*******************************************************************************************************************
  141.      *
  142.      ******************************************************************************************************************/
  143.     @Nonnull
  144.     protected PerformanceFinder _findPerformances()
  145.       {
  146.         return configured(new RepositoryPerformanceFinder(repository));
  147.       }

  148.     /*******************************************************************************************************************
  149.      *
  150.      ******************************************************************************************************************/
  151.     @Nonnull
  152.     protected static Optional<String> toString (@Nullable final Binding binding)
  153.       {
  154.         return Optional.ofNullable(binding).map(Binding::getValue).map(Value::stringValue);
  155.       }

  156.     /*******************************************************************************************************************
  157.      *
  158.      ******************************************************************************************************************/
  159.     @Nonnull
  160.     protected static Optional<Id> toId (@Nullable final Binding binding)
  161.       {
  162.         return Optional.ofNullable(binding).map(Binding::getValue).map(Value::stringValue).map(Id::new);
  163.       }

  164.     /*******************************************************************************************************************
  165.      *
  166.      ******************************************************************************************************************/
  167.     @Nonnull
  168.     protected static Optional<Integer> toInteger (@Nullable final Binding binding)
  169.       {
  170.         return Optional.ofNullable(binding).map(Binding::getValue).map(v -> Integer.parseInt(v.stringValue()));
  171.       }

  172.     /*******************************************************************************************************************
  173.      *
  174.      ******************************************************************************************************************/
  175.     @Nonnull
  176.     protected static Optional<Long> toLong (@Nullable final Binding binding)
  177.       {
  178.         return Optional.ofNullable(binding).map(Binding::getValue).map(v -> Long.parseLong(v.stringValue()));
  179.       }

  180.     /*******************************************************************************************************************
  181.      *
  182.      ******************************************************************************************************************/
  183.     @Nonnull
  184.     protected static Optional<Duration> toDuration (@Nullable final Binding binding)
  185.       {
  186.         return Optional.ofNullable(binding).map(Binding::getValue).map(v -> Duration.ofMillis((int)Float.parseFloat(v.stringValue())));
  187.       }

  188.     /*******************************************************************************************************************
  189.      *
  190.      * Tries to fix a path for character normalization issues (see BMT-46). The idea is to first normalize the encoding
  191.      * to the native form. If it doesn't work, a broken path is replaced to avoid further errors (of course, the
  192.      * resource won't be available when requested).
  193.      * It doesn't try to call normalizedPath() because it's expensive.
  194.      *
  195.      * @param   binding     the binding
  196.      * @return              the path
  197.      *
  198.      ******************************************************************************************************************/
  199.     @Nonnull
  200.     protected static Path toPath (@Nonnull final Binding binding)
  201.       {
  202.         try // FIXME: see BMT-46 - try all posibile normalizations
  203.           {
  204.             return Paths.get(normalizedToNativeForm(toString(binding).get()));
  205.           }
  206.         catch (InvalidPathException e)
  207.           {
  208.             // FIXME: perhaps we could try a similar trick to normalizedPath() - the problem being the fact that it
  209.             // currently accepts a Path, but we can't convert to a Path. It should be rewritten to work with a String
  210.             // in input.
  211.             log.error("Invalid path {}", e.toString());
  212.             return Paths.get("broken SEE BMT-46");
  213.           }
  214.       }

  215.     /*******************************************************************************************************************
  216.      *
  217.      ******************************************************************************************************************/
  218.     @Nonnull
  219.     protected <ENTITY, FINDER extends SourceAwareFinder<ENTITY, FINDER>> FINDER configured (@Nonnull final FINDER finder)
  220.       {
  221.         return finder.importedFrom(source).withFallback(fallback);
  222.       }
  223.   }