CompositeDIDLAdapterSupport.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.upnp.mediaserver.impl.didl;

  28. import javax.annotation.Nonnegative;
  29. import javax.annotation.Nonnull;
  30. import javax.annotation.concurrent.Immutable;
  31. import org.fourthline.cling.support.model.BrowseFlag;
  32. import org.fourthline.cling.support.model.DIDLContent;
  33. import it.tidalwave.util.As;
  34. import it.tidalwave.util.Finder;
  35. import it.tidalwave.bluemarine2.model.spi.Entity;
  36. import it.tidalwave.bluemarine2.rest.spi.ResourceServer;
  37. import lombok.extern.slf4j.Slf4j;
  38. import static it.tidalwave.role.SimpleComposite._SimpleComposite_;
  39. import static it.tidalwave.util.FunctionalCheckedExceptionWrappers.*;

  40. /***********************************************************************************************************************
  41.  *
  42.  * @stereotype Role
  43.  *
  44.  * @author  Fabrizio Giudici
  45.  *
  46.  **********************************************************************************************************************/
  47. @Immutable @Slf4j
  48. public abstract class CompositeDIDLAdapterSupport<T extends As> extends DIDLAdapterSupport<T>
  49.   {
  50.     public CompositeDIDLAdapterSupport (@Nonnull final T datum, @Nonnull final ResourceServer server)
  51.       {
  52.         super(datum, server);
  53.       }

  54.     /*******************************************************************************************************************
  55.      *
  56.      * {@inheritDoc}
  57.      *
  58.      ******************************************************************************************************************/
  59.     @Override @Nonnull
  60.     public ContentHolder toContent (@Nonnull final BrowseFlag browseFlag,
  61.                                     @Nonnegative final int from,
  62.                                     @Nonnegative final int maxResults)
  63.       throws Exception
  64.       {
  65.         final DIDLContent content = new DIDLContent();
  66.         int numberReturned = 0;
  67.         int totalMatches = 0;

  68.         switch (browseFlag)
  69.           {
  70.             case METADATA:
  71.                 totalMatches = numberReturned = 1;
  72.                 content.addObject(toObject());
  73.                 break;

  74.             case DIRECT_CHILDREN:
  75.                 final Finder<Entity> finder = datum.as(_SimpleComposite_).findChildren();
  76.                 totalMatches = finder.count();
  77.                 finder.from(from)
  78.                       .max(maxResults)
  79.                       .results()
  80.                       .forEach(_c(child -> content.addObject(child.as(_DIDLAdapter_).toObject())));
  81.                 numberReturned = (int)content.getCount();
  82.                 break;

  83.             default:
  84.                 throw new IllegalArgumentException(browseFlag.toString());
  85.           }

  86.         return new ContentHolder(content, numberReturned, totalMatches);
  87.       }
  88.   }