Content of file TrackDIDLAdapter.java.html

<?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>TrackDIDLAdapter.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> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-bluemarine2-upnp-mediaserver</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.upnp.mediaserver.impl.didl</a> &gt; <span class="el_source">TrackDIDLAdapter.java</span></div><h1>TrackDIDLAdapter.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 &quot;License&quot;); 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 &quot;AS IS&quot; 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.upnp.mediaserver.impl.didl;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import java.time.Duration;
import java.io.IOException;
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.StorageMedium;
import org.fourthline.cling.support.model.dlna.DLNAProtocolInfo;
import org.fourthline.cling.support.model.item.MusicTrack;
import it.tidalwave.role.ui.Displayable;
import it.tidalwave.dci.annotation.DciRole;
import it.tidalwave.bluemarine2.model.MediaItem.Metadata;
import it.tidalwave.bluemarine2.model.audio.AudioFile;
import it.tidalwave.bluemarine2.model.audio.Track;
import it.tidalwave.bluemarine2.rest.spi.ResourceServer;
import lombok.extern.slf4j.Slf4j;
import static it.tidalwave.role.ui.Displayable._Displayable_;
import static it.tidalwave.bluemarine2.model.MediaItem.Metadata.*;
import static it.tidalwave.bluemarine2.model.role.AudioFileSupplier._AudioFileSupplier_;

/***********************************************************************************************************************
 *
 * The {@link DIDLAdapter} for {@link Track}.
 *
 * @stereotype Role
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="fc" id="L60">@Slf4j</span>
@Immutable @DciRole(datumType = Track.class)
public class TrackDIDLAdapter extends DIDLAdapterSupport&lt;Track&gt;
  {
    public TrackDIDLAdapter (@Nonnull final Track track , @Nonnull final ResourceServer server)
      {
<span class="fc" id="L66">        super(track, server);</span>
<span class="fc" id="L67">      }</span>

    @Override @Nonnull
    public DIDLObject toObject()
      throws IOException
      {
<span class="fc" id="L73">        log.debug(&quot;toObject() - {}&quot;, datum);</span>
        // parentID not set here
<span class="fc" id="L75">        final MusicTrack item = setCommonFields(new MusicTrack());</span>
<span class="fc" id="L76">        final AudioFile audioFile = datum.as(_AudioFileSupplier_).getAudioFile();</span>
<span class="fc" id="L77">        final Metadata trackMetadata = datum.getMetadata();</span>
<span class="fc" id="L78">        item.addResource(audioResourceOf(audioFile));</span>
<span class="fc" id="L79">        trackMetadata.get(TRACK_NUMBER).ifPresent(item::setOriginalTrackNumber);</span>

<span class="fc" id="L81">        datum.getRecord().flatMap(record -&gt; record.maybeAs(_Displayable_))</span>
<span class="fc" id="L82">                         .map(Displayable::getDisplayName)</span>
<span class="fc" id="L83">                         .ifPresent(item::setAlbum);</span>

//        trackMetadata.get(DISK_COUNT);
//        trackMetadata.get(DISK_NUMBER);
//        trackMetadata.get(COMPOSER);
//        trackMetadata.get(ARTIST);
//        item.setContributors(contributors);
//        item.setCreator(creator);
//        item.setDate(date);
//        item.setDescMetadata(descMetadata);
//        item.setDescription(description);
//        item.setLanguage(language);
//        item.setLongDescription(description);
<span class="fc" id="L96">        item.setStorageMedium(StorageMedium.HDD);</span>

//        item.setArtists(new PersonWithRole[] { new PersonWithRole(&quot;xyz&quot;, &quot;AlbumArtist&quot;) });
//        item.setGenres(new String[] { &quot;Classical&quot; });
//        final Person publisher = new Person(&quot;Unknown&quot;);
//        item.setPublishers(new Person[] { publisher });

//        datum.getDiskNumber();

<span class="fc" id="L105">        return item;</span>
      }

    @Nonnull
    private Res audioResourceOf (@Nonnull final AudioFile audioFile)
      {
<span class="fc" id="L111">        final ProtocolInfo protocolInfo = new DLNAProtocolInfo(Protocol.HTTP_GET, &quot;*&quot;, &quot;audio/mpeg&quot;, &quot;*&quot;); // FIXME: MIME</span>
<span class="fc" id="L112">        final Metadata audioFileMetadata = audioFile.getMetadata();</span>
<span class="fc" id="L113">        final Res resource = new Res(protocolInfo,</span>
<span class="fc" id="L114">                                     audioFileMetadata.get(FILE_SIZE).orElse(null),</span>
<span class="fc" id="L115">                                     server.absoluteUrl(String.format(&quot;rest/audiofile/%s/content&quot;, audioFile.getId().stringValue())));</span>
<span class="fc" id="L116">        audioFileMetadata.get(DURATION).ifPresent(duration -&gt; resource.setDuration(durationToString(duration)));</span>
<span class="fc" id="L117">        audioFileMetadata.get(BIT_RATE).ifPresent(bitRate -&gt; resource.setBitrate((long)(int)bitRate));</span>
<span class="fc" id="L118">        audioFileMetadata.get(BITS_PER_SAMPLE).ifPresent(bitPerSample -&gt; resource.setBitsPerSample((long)(int)bitPerSample));</span>
<span class="pc" id="L119">        audioFileMetadata.get(CHANNELS).ifPresent(channels -&gt; resource.setNrAudioChannels((long)(int)channels));</span>
<span class="fc" id="L120">        audioFileMetadata.get(SAMPLE_RATE).ifPresent(sampleRate -&gt; resource.setSampleFrequency((long)(int)sampleRate));</span>

<span class="fc" id="L122">        return resource;</span>
      }

    @Nonnull
    private static String durationToString (@Nonnull final Duration duration)
      {
<span class="fc" id="L128">        final int d = (int)duration.getSeconds();</span>
<span class="fc" id="L129">        final int seconds = d % 60;</span>
<span class="fc" id="L130">        final int minutes = (d / 60) % 60;</span>
<span class="fc" id="L131">        final int hours = d / 3600;</span>
<span class="fc" id="L132">        return String.format(&quot;%d:%02d:%02d.000&quot;, hours, minutes, seconds);</span>
      }

    /*
        // TODO     &lt;desc id=&quot;cdudn&quot; nameSpace=&quot;urn:schemas-rinconnetworks-com:metadata-1-0/&quot;&gt;SA_RINCON5127_42????35&lt;/desc&gt;
<desc id="cdudn" nameSpace="urn:schemas-rinconnetworks-com:metadata-1-0/">SA_RINCON5127_42????35</desc>
&lt;item id=&quot;T:\public_html\Jukebox\mp3\_Presets\10.dpl/0&quot; parentID=&quot;T:\public_html\Jukebox\mp3\_Presets\10.dpl&quot; restricted=&quot;False&quot;&gt; &lt;upnp:albumArtURI&quot;&gt;http://eng.linn.co.uk/~joshh/Jukebox/mp3/Amy%20Macdonald/This%20Is%20The%20Life/Folder.jpg&lt;/upnp:albumArtURI&gt; &lt;upnp:artworkURI&quot;&gt;http://eng.linn.co.uk/~joshh/Jukebox/mp3/Amy%20Macdonald/This%20Is%20The%20Life/Folder.jpg&lt;/upnp:artworkURI&gt; &lt;upnp:genre&quot;&gt;Pop&lt;/upnp:genre&gt; &lt;upnp:artist role=&quot;Performer&quot;&quot;&gt;Amy Macdonald&lt;/upnp:artist&gt; &lt;upnp:artist role=&quot;Composer&quot;&quot;&gt;Unknown&lt;/upnp:artist&gt; &lt;upnp:artist role=&quot;AlbumArtist&quot;&quot;&gt;Unknown&lt;/upnp:artist&gt; &lt;upnp:artist role=&quot;Conductor&quot;&quot;&gt;Unknown&lt;/upnp:artist&gt; &lt;dc:date &gt;Unknown&lt;/dc:date&gt; &lt;upnp:originalDiscNumber&quot;&gt;0&lt;/upnp:originalDiscNumber&gt; &lt;upnp:originalDiscCount&quot;&gt;0&lt;/upnp:originalDiscCount&gt; &lt;/item&gt; */ } </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>