DefaultContentDirectory.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.mediaserver.impl;

  28. import javax.annotation.Nonnull;
  29. import javax.annotation.PostConstruct;
  30. import java.util.Collection;
  31. import java.util.Collections;
  32. import java.util.List;
  33. import java.util.Optional;
  34. import java.nio.file.Path;
  35. import java.nio.file.Paths;
  36. import org.springframework.beans.factory.annotation.Autowired;
  37. import it.tidalwave.role.ui.Displayable;
  38. import it.tidalwave.bluemarine2.model.MediaFolder;
  39. import it.tidalwave.bluemarine2.model.VirtualMediaFolder;
  40. import it.tidalwave.bluemarine2.model.VirtualMediaFolder.EntityCollectionFactory;
  41. import it.tidalwave.bluemarine2.model.role.EntityBrowser;
  42. import it.tidalwave.bluemarine2.model.spi.PathAwareEntity;
  43. import it.tidalwave.bluemarine2.model.impl.PathAwareMediaFolderDecorator;
  44. import it.tidalwave.bluemarine2.mediaserver.ContentDirectory;
  45. import it.tidalwave.bluemarine2.mediaserver.spi.MediaServerService;
  46. import lombok.extern.slf4j.Slf4j;
  47. import static java.util.Comparator.comparing;
  48. import static java.util.stream.Collectors.*;
  49. import static it.tidalwave.role.Identifiable._Identifiable_;
  50. import static it.tidalwave.util.Parameters.r;

  51. /***********************************************************************************************************************
  52.  *
  53.  * @author  Fabrizio Giudici
  54.  *
  55.  **********************************************************************************************************************/
  56. @Slf4j
  57. public class DefaultContentDirectory implements ContentDirectory
  58.   {
  59.     private static final Path PATH_ROOT = Paths.get("/");

  60.     private static final Path PATH_VIDEOS = Paths.get("videos");

  61.     private static final Path PATH_PHOTOS = Paths.get("photos");

  62.     private static final Path PATH_MUSIC = Paths.get("music");

  63.     private static final Path PATH_SERVICES = Paths.get("services");

  64.     private static final EntityCollectionFactory EMPTY = x -> Collections.emptyList(); // FIXME: move to ECF

  65. //    @Inject FIXME
  66.     @Autowired(required = false)
  67.     private final List<EntityBrowser> entityBrowsers = Collections.emptyList();

  68. //    @Inject FIXME
  69.     @Autowired(required = false)
  70.     private final List<MediaServerService> services = Collections.emptyList();

  71.     private MediaFolder root;

  72.     @Override @Nonnull
  73.     public MediaFolder findRoot()
  74.       {
  75.         return root;
  76.       }

  77.     @PostConstruct
  78.     private void initialize()
  79.       {
  80.         // FIXME: why is this called multiple times?
  81.         log.info(">>>> discovered entity browsers: {}", entityBrowsers);
  82.         log.info(">>>> discovered services: {}", services);
  83.         root = new VirtualMediaFolder(Optional.empty(), PATH_ROOT, "", this::childrenFactory);
  84.       }

  85.     @Nonnull
  86.     private Collection<PathAwareEntity> childrenFactory (@Nonnull final MediaFolder parent)
  87.       {
  88.         return List.of(new VirtualMediaFolder(parent, PATH_MUSIC,    "Music",    this::musicFactory),
  89.                        new VirtualMediaFolder(parent, PATH_PHOTOS,   "Photos",   EMPTY),
  90.                        new VirtualMediaFolder(parent, PATH_VIDEOS,   "Videos",   EMPTY),
  91.                        new VirtualMediaFolder(parent, PATH_SERVICES, "Services", this::servicesFactory));
  92.       }

  93.     @Nonnull
  94.     private Collection<MediaFolder> musicFactory (@Nonnull final MediaFolder parent)
  95.       {
  96.         // TODO: filter by MIME type
  97.         return entityBrowsers.stream()
  98.                              .sorted(comparing(browser -> browser.as(Displayable.class).getDisplayName()))
  99.                              .map(browser -> createMediaFolder(parent, browser))
  100.                              .collect(toList());
  101.       }

  102.     @Nonnull
  103.     private Collection<MediaFolder> servicesFactory (@Nonnull final MediaFolder parent)
  104.       {
  105.         return services.stream().map(service -> service.createRootFolder(parent)).collect(toList());
  106.       }

  107.     @Nonnull
  108.     private static MediaFolder createMediaFolder (@Nonnull final MediaFolder parent,
  109.                                                   @Nonnull final EntityBrowser browser)
  110.       {
  111.         final String fallBack = browser.getClass().getSimpleName();
  112.         final String pathSegment = browser.maybeAs(_Identifiable_).map(i -> i.getId().stringValue()).orElse(fallBack);
  113.         final Displayable displayable = browser.maybeAs(Displayable.class).orElse(Displayable.of(fallBack));
  114.         log.trace("createMediaFolder({}, {}) - path: {} displayable: {}", parent, browser, pathSegment, displayable);
  115.         return new PathAwareMediaFolderDecorator(browser.getRoot(), parent, Paths.get(pathSegment), r(displayable));
  116.       }
  117.   }