<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>PhotoItemDIDLAdapter.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">blueMarine II :: Application :: JavaFX</a> > <a href="../index.html" class="el_bundle">it-tidalwave-bluemarine2-services-stoppingdown</a> > <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.service.stoppingdown.impl</a> > <span class="el_source">PhotoItemDIDLAdapter.java</span></div><h1>PhotoItemDIDLAdapter.java</h1><pre class="source lang-java linenums">/*
* *********************************************************************************************************************
*
* blueMarine II: Semantic Media Centre
* http://tidalwave.it/projects/bluemarine2
*
* Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
*
* *********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* *********************************************************************************************************************
*
* git clone https://bitbucket.org/tidalwave/bluemarine2-src
* git clone https://github.com/tidalwave-it/bluemarine2-src
*
* *********************************************************************************************************************
*/
package it.tidalwave.bluemarine2.service.stoppingdown.impl;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import java.util.List;
import java.nio.file.Path;
import org.fourthline.cling.support.model.DIDLObject;
import org.fourthline.cling.support.model.Protocol;
import org.fourthline.cling.support.model.ProtocolInfo;
import org.fourthline.cling.support.model.Res;
import org.fourthline.cling.support.model.dlna.DLNAProtocolInfo;
import org.fourthline.cling.support.model.item.Photo;
import it.tidalwave.dci.annotation.DciRole;
import it.tidalwave.bluemarine2.model.spi.PathAwareEntity;
import it.tidalwave.bluemarine2.upnp.mediaserver.impl.didl.DIDLAdapter;
import lombok.RequiredArgsConstructor;
import static java.util.Collections.reverseOrder;
/***********************************************************************************************************************
*
* A role that converts a {@link PhotoItem} into DIDL content.
*
* @stereotype Role
*
* @author Fabrizio Giudici
*
**********************************************************************************************************************/
// FIXME: this introduces a dependency on UPnP. It's needed because it contains stuff related to StoppingDown (URLs).
this introduces a dependency on UPnP. It's needed because it contains stuff related to StoppingDown (URLs).
// FIXME: move PhotoItem to Model, this class to UPnP and try to make the URLs contained in metadata of PhotoItem.
<span class="fc" id="L56">@RequiredArgsConstructor</span>
@Immutable @DciRole(datumType = PhotoItem.class)
public class PhotoItemDIDLAdapter implements DIDLAdapter
{
<span class="fc" id="L60"> private static final String MEDIA_URL_TEMPLATE =</span>
PhotoCollectionProviderSupport.URL_STOPPINGDOWN + "/media/stillimages/%s/%d/image.jpg";
<span class="fc" id="L63"> private static final List<Integer> SIZES = List.of(200, 400, 800, 1280, 1920, 2560);</span>
@Nonnull
private final PhotoItem datum;
<span class="fc" id="L68"> private final String creator = "Fabrizio Giudici";</span>
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override @Nonnull
public DIDLObject toObject()
{
<span class="fc" id="L78"> final ProtocolInfo protocolInfo = new DLNAProtocolInfo(Protocol.HTTP_GET, "*", "image/jpeg", "*");</span>
<span class="fc" id="L79"> final Res[] resources = SIZES.stream()</span>
<span class="fc" id="L80"> .sorted(reverseOrder())</span>
<span class="fc" id="L81"> .map(size -> createResource(protocolInfo, size))</span>
<span class="fc" id="L82"> .toArray(Res[]::new);</span>
<span class="fc" id="L83"> final Path parentPath = datum.getParent().map(PathAwareEntity::getPath).orElseThrow(RuntimeException::new);</span>
<span class="fc" id="L84"> final String parentId = parentPath.toString();</span>
<span class="fc" id="L85"> final String photoId = parentPath.resolve(datum.getId()).toString();</span>
<span class="fc" id="L86"> final String title = datum.getId();</span>
<span class="fc" id="L87"> final Photo item = new Photo(photoId, parentId, title, creator, parentId, resources);</span>
<span class="fc" id="L88"> item.setDescription(datum.getTitle());</span>
<span class="fc" id="L89"> item.setDate(dateFor(datum.getId()));</span>
<span class="fc" id="L90"> return item;</span>
}
/*******************************************************************************************************************
*
******************************************************************************************************************/
@Nonnull
private Res createResource (@Nonnull final ProtocolInfo protocolInfo, final int size)
{
<span class="fc" id="L99"> final Res resource = new Res(protocolInfo, null, computeUrl(size));</span>
<span class="fc" id="L100"> resource.setResolution(size, size);</span>
<span class="fc" id="L101"> return resource;</span>
}
/*******************************************************************************************************************
*
******************************************************************************************************************/
@Nonnull
private String computeUrl (final int size)
{
<span class="fc" id="L110"> return String.format(MEDIA_URL_TEMPLATE, datum.getId(), size);</span>
}
/*******************************************************************************************************************
*
******************************************************************************************************************/
@Nonnull
private static String dateFor (final String id)
{
<span class="fc" id="L119"> return String.format("%s-%s-%s", id.substring(0, 4), id.substring(4, 6), id.substring(6, 8));</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>