RepositoryRecordFinder.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.finder;

  28. import javax.annotation.Nonnull;
  29. import java.util.Optional;
  30. import org.eclipse.rdf4j.repository.Repository;
  31. import it.tidalwave.util.Id;
  32. import it.tidalwave.bluemarine2.model.audio.Record;
  33. import it.tidalwave.bluemarine2.model.finder.audio.RecordFinder;
  34. import lombok.ToString;

  35. /***********************************************************************************************************************
  36.  *
  37.  * @stereotype      Finder
  38.  *
  39.  * @author  Fabrizio Giudici
  40.  *
  41.  **********************************************************************************************************************/
  42. @ToString
  43. public class RepositoryRecordFinder extends RepositoryFinderSupport<Record, RecordFinder>
  44.                                     implements RecordFinder
  45.   {
  46.     private static final long serialVersionUID = -6899011281060253740L;

  47.     private static final String QUERY_RECORDS = readSparql(RepositoryRecordFinder.class, "Records.sparql");

  48.     @Nonnull
  49.     private final Optional<Id> makerId;

  50.     @Nonnull
  51.     private final Optional<Id> trackId;

  52.     /*******************************************************************************************************************
  53.      *
  54.      * Default constructor.
  55.      *
  56.      ******************************************************************************************************************/
  57.     public RepositoryRecordFinder (@Nonnull final Repository repository)
  58.       {
  59.         this(repository, Optional.empty(), Optional.empty());
  60.       }

  61.     /*******************************************************************************************************************
  62.      *
  63.      * Clone constructor.
  64.      *
  65.      ******************************************************************************************************************/
  66.     public RepositoryRecordFinder (@Nonnull final RepositoryRecordFinder other, @Nonnull final Object override)
  67.       {
  68.         super(other, override);
  69.         final RepositoryRecordFinder source = getSource(RepositoryRecordFinder.class, other, override);
  70.         this.makerId = source.makerId;
  71.         this.trackId = source.trackId;
  72.       }

  73.     /*******************************************************************************************************************
  74.      *
  75.      * Override constructor.
  76.      *
  77.      ******************************************************************************************************************/
  78.     private RepositoryRecordFinder (@Nonnull final Repository repository,
  79.                                     @Nonnull final Optional<Id> makerId,
  80.                                     @Nonnull final Optional<Id> trackId)
  81.       {
  82.         super(repository, "record");
  83.         this.makerId = makerId;
  84.         this.trackId = trackId;
  85.       }

  86.     /*******************************************************************************************************************
  87.      *
  88.      * {@inheritDoc}
  89.      *
  90.      ******************************************************************************************************************/
  91.     @Override @Nonnull
  92.     public RecordFinder madeBy (@Nonnull final Id artistId)
  93.       {
  94.         return clonedWith(new RepositoryRecordFinder(repository, Optional.of(artistId), trackId));
  95.       }

  96.     /*******************************************************************************************************************
  97.      *
  98.      * {@inheritDoc}
  99.      *
  100.      ******************************************************************************************************************/
  101.     @Override @Nonnull
  102.     public RecordFinder containingTrack (@Nonnull final Id trackId)
  103.       {
  104.         return clonedWith(new RepositoryRecordFinder(repository, makerId, Optional.of(trackId)));
  105.       }

  106.     /*******************************************************************************************************************
  107.      *
  108.      * {@inheritDoc}
  109.      *
  110.      ******************************************************************************************************************/
  111.     @Override @Nonnull
  112.     protected QueryAndParameters prepareQuery()
  113.       {
  114.         return QueryAndParameters.withSparql(QUERY_RECORDS)
  115.                                  .withParameter("artist",   makerId.map(this::iriFor))
  116.                                  .withParameter("track",    trackId.map(this::iriFor));
  117.       }
  118.   }