RepositoryMusicArtistFinder.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.MusicArtist;
  33. import it.tidalwave.bluemarine2.model.finder.audio.MusicArtistFinder;
  34. import lombok.ToString;

  35. /***********************************************************************************************************************
  36.  *
  37.  * @author  Fabrizio Giudici
  38.  *
  39.  **********************************************************************************************************************/
  40. @ToString
  41. public class RepositoryMusicArtistFinder extends RepositoryFinderSupport<MusicArtist, MusicArtistFinder>
  42.                                          implements MusicArtistFinder
  43.   {
  44.     private static final long serialVersionUID = 2271161001432418095L;

  45.     private static final String QUERY_ARTISTS = readSparql(RepositoryMusicArtistFinder.class, "AllMusicArtists.sparql");

  46.     private static final String QUERY_ARTISTS_MAKER_OF = readSparql(RepositoryMusicArtistFinder.class, "MakerArtists.sparql");

  47.     @Nonnull
  48.     private final Optional<Id> madeEntityId;

  49.     /*******************************************************************************************************************
  50.      *
  51.      * Default constructor.
  52.      *
  53.      ******************************************************************************************************************/
  54.     public RepositoryMusicArtistFinder (@Nonnull final Repository repository)
  55.       {
  56.         this(repository, Optional.empty());
  57.       }

  58.     /*******************************************************************************************************************
  59.      *
  60.      * Clone constructor.
  61.      *
  62.      ******************************************************************************************************************/
  63.     public RepositoryMusicArtistFinder (@Nonnull final RepositoryMusicArtistFinder other,
  64.                                         @Nonnull final Object override)
  65.       {
  66.         super(other, override);
  67.         final RepositoryMusicArtistFinder source = getSource(RepositoryMusicArtistFinder.class, other, override);
  68.         this.madeEntityId = source.madeEntityId;
  69.       }

  70.     /*******************************************************************************************************************
  71.      *
  72.      * Override constructor.
  73.      *
  74.      ******************************************************************************************************************/
  75.     private RepositoryMusicArtistFinder (@Nonnull final Repository repository,
  76.                                          @Nonnull final Optional<Id> madeEntityId)
  77.       {
  78.         super(repository, "artist");
  79.         this.madeEntityId = madeEntityId;
  80.       }

  81.     /*******************************************************************************************************************
  82.      *
  83.      * {@inheritDoc}
  84.      *
  85.      ******************************************************************************************************************/
  86.     @Override @Nonnull
  87.     public MusicArtistFinder makerOf (@Nonnull final Id madeEntityId)
  88.       {
  89.         return clonedWith(new RepositoryMusicArtistFinder(repository, Optional.of(madeEntityId)));
  90.       }

  91.     /*******************************************************************************************************************
  92.      *
  93.      * {@inheritDoc}
  94.      *
  95.      ******************************************************************************************************************/
  96.     @Override @Nonnull
  97.     protected QueryAndParameters prepareQuery()
  98.       {
  99.         // Two different queries because for the 'makerOf' we want to include collborations, as it's important their label
  100.         // In other words, 'Ella Fitzgerald and Duke Ellington' matters, rather than a list of the two individuals
  101.         return QueryAndParameters.withSparql(madeEntityId.isPresent() ? QUERY_ARTISTS_MAKER_OF : QUERY_ARTISTS)
  102.                                  .withParameter("madeEntity", madeEntityId.map(this::iriFor));
  103.       }
  104.   }