PathAwareEntityDecorator.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;

  28. import javax.annotation.Nonnull;
  29. import java.util.Collection;
  30. import java.util.Collections;
  31. import java.util.Optional;
  32. import java.nio.file.Path;
  33. import java.nio.file.Paths;
  34. import it.tidalwave.bluemarine2.model.MediaFolder;
  35. import it.tidalwave.bluemarine2.model.spi.Entity;
  36. import it.tidalwave.bluemarine2.model.spi.PathAwareEntity;
  37. import lombok.Getter;
  38. import static it.tidalwave.role.Identifiable._Identifiable_;
  39. import static it.tidalwave.role.SimpleComposite._SimpleComposite_;

  40. /***********************************************************************************************************************
  41.  *
  42.  * An adapter for {@link Entity} to a {@link MediaFolder}. It can be used to adapt entities that naturally do
  43.  * not belong to a hierarchy, such as an artist, to contexts where a hierarchy is needed (e.g. for browsing).
  44.  *
  45.  * @author  Fabrizio Giudici
  46.  *
  47.  **********************************************************************************************************************/
  48. public class PathAwareEntityDecorator extends EntityDecorator implements PathAwareEntity
  49.   {
  50.     @Nonnull
  51.     protected final PathAwareEntity parent;

  52.     @Getter @Nonnull
  53.     protected final Path pathSegment;

  54.     /*******************************************************************************************************************
  55.      *
  56.      ******************************************************************************************************************/
  57.     protected PathAwareEntityDecorator (@Nonnull final Entity delegate,
  58.                                         @Nonnull final PathAwareEntity parent,
  59.                                         @Nonnull final Path pathSegment,
  60.                                         @Nonnull final Collection<Object> roles)
  61.       {
  62.         super(delegate, roles);
  63.         this.pathSegment = pathSegment;
  64.         this.parent = parent;
  65.       }

  66.     protected PathAwareEntityDecorator (@Nonnull final Entity delegate,
  67.                                         @Nonnull final PathAwareEntity parent,
  68.                                         @Nonnull final Path pathSegment)
  69.       {
  70.         this(delegate, parent, pathSegment, Collections.emptyList());
  71.       }

  72.     /*******************************************************************************************************************
  73.      *
  74.      * {@inheritDoc}
  75.      *
  76.      ******************************************************************************************************************/
  77.     @Override @Nonnull
  78.     public Optional<PathAwareEntity> getParent()
  79.       {
  80.         return Optional.of(parent);
  81.       }

  82.     /*******************************************************************************************************************
  83.      *
  84.      * {@inheritDoc}
  85.      *
  86.      ******************************************************************************************************************/
  87.     @Override @Nonnull
  88.     public Path getPath()
  89.       {
  90.         return parent.getPath().resolve(pathSegment);
  91.       }

  92.     /*******************************************************************************************************************
  93.      *
  94.      * {@inheritDoc}
  95.      *
  96.      ******************************************************************************************************************/
  97.     @Override @Nonnull
  98.     public String toString()
  99.       {
  100.         return String.format("%s(path=%s, delegate=%s, parent=%s)", getClass().getSimpleName(), getPath(), delegate, parent);
  101.       }

  102.     /*******************************************************************************************************************
  103.      *
  104.      * {@inheritDoc}
  105.      *
  106.      ******************************************************************************************************************/
  107.     @Override @Nonnull
  108.     public String toDumpString()
  109.       {
  110.         return String.format("Entity(path=%s, delegate=%s, parent=Folder(path=%s))", getPath(), delegate, parent.getPath());
  111.       }

  112.     /*******************************************************************************************************************
  113.      *
  114.      * Creates a wrapped entity, with the given parent.
  115.      *
  116.      * @param       parent      the parent
  117.      * @param       entity      the source entity
  118.      * @return                  the wrapped entity
  119.      *
  120.      ******************************************************************************************************************/
  121.     @Nonnull
  122.     protected static PathAwareEntity wrappedEntity (@Nonnull final PathAwareEntity parent, @Nonnull final Entity entity)
  123.       {
  124.         if (entity instanceof PathAwareEntity) // FIXME: possibly avoid calling
  125.           {
  126.             return (PathAwareEntity)entity;
  127.           }

  128.         final Path pathSegment = idToPathSegment(entity);
  129.         return entity.maybeAs(_SimpleComposite_).isPresent()
  130.                 ? new PathAwareMediaFolderDecorator(entity, parent, pathSegment)
  131.                 : new PathAwareEntityDecorator(entity, parent, pathSegment);
  132.       }

  133.     /*******************************************************************************************************************
  134.      *
  135.      ******************************************************************************************************************/
  136.     @Nonnull
  137.     private static Path idToPathSegment (@Nonnull final Entity entity)
  138.       {
  139.         return Paths.get(entity.as(_Identifiable_).getId().stringValue().replace('/', '_'));
  140.       }
  141.   }