All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.mobile.media.Media Maven / Gradle / Ivy

There is a newer version: 1.0.9
Show newest version
/***********************************************************************************************************************
 *
 * blueBill Mobile - open source birdwatching
 * ==========================================
 *
 * Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
 * http://bluebill.tidalwave.it/mobile/
 *
 ***********************************************************************************************************************
 *
 * 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.
 *
 ***********************************************************************************************************************
 *
 * $Id: Media.java,v 523e997730e9 2010/06/26 16:21:59 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.mobile.media;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import org.openide.util.Lookup;
import it.tidalwave.util.As;
import it.tidalwave.util.AsException;
import it.tidalwave.util.Id;
import it.tidalwave.util.Key;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.util.TypeSafeMap;
import it.tidalwave.util.TypeSafeHashMap;
import it.tidalwave.mobile.util.AsSupport;
import it.tidalwave.mobile.util.Downloadable;
import it.tidalwave.mobile.util.DownloadableImpl;
import it.tidalwave.util.Removable;
import it.tidalwave.util.logging.Logger;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
@Immutable
public class Media extends AsSupport implements As, Lookup.Provider
  {
    private static final String CLASS = Media.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    public static final Class Media = Media.class;

    public static final String DC = "http://purl.org/dc/elements/1.1/";
    public static final String XX = "urn:bluebill-mobile:";
    public static final String O = "http://www.tidalwave.it/rdf/observation/2009/02/24#"; // FIXME

    // FIXME: use Dunlin Core ids
    public static final Key AUTHOR = new Key(DC + "creator");
    public static final Key SOURCE = new Key(DC + "publisher");
    public static final Key LOCATION = new Key(O + "madeAt");
    public static final Key CALL_TYPE = new Key(DC + "title");
    public static final Key DATE = new Key(DC + "date");
    public static final Key ID = new Key(DC + "identifier");
    public static final Key RES_URL = new Key(XX + "url");
    public static final Key RIGHTS = new Key(DC + "rights");
    public static final Key MIME_TYPE = new Key(XX + "mime-type");
    public static final Key DURATION = new Key(XX + "duration");
    public static final Key FORMAT = new Key(DC + "format");
    public static final Key TYPE = new Key(DC + "type");

    @Nonnull
    private final TypeSafeMap map;

    @CheckForNull
    private Downloadable downloadable;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public Media()
      {
        this(Collections., Object>emptyMap());
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private Media (final @Nonnull Map, Object> map)
      {
        this.map = new TypeSafeHashMap(map);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public  T get (final @Nonnull Key key)
      throws NotFoundException
      {
        return map.get(key);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public boolean contains (final @Nonnull Key item)
      {
        return map.containsKey(item);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public Set> getKeys()
      {
        return map.getKeys();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnegative
    public int getSize()
      {
        return map.getSize();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public Iterator iterator()
      {
        return map.iterator();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public  Media with (final @Nonnull Key key, final @Nonnull T value)
      {
        if (key.equals(FORMAT))
          {
            final String[] parts = ((String)value).split(";");
            final String mimeType = parts[0];
            Media media = with(MIME_TYPE, mimeType);
            
            final String durationAsString = parts[1].replaceAll("s$", "").trim();
            
            if (!durationAsString.equals(""))
              {
                media = media.with(DURATION, Integer.valueOf(durationAsString));
              }

            return media;
          }

        final Map, Object> asMap = map.asMap();
        asMap.put(key, value);

        asMap.remove(FORMAT);

        if (asMap.containsKey(MIME_TYPE))
          {
            asMap.remove(TYPE);

            if (((String)asMap.get(MIME_TYPE)).startsWith("image/"))
              {
                asMap.put(TYPE, "http://purl.org/dc/dcmitype/StillImage");
              }
            else if(((String) asMap.get(MIME_TYPE)).startsWith("audio/"))
              {
                asMap.put(TYPE, "http://purl.org/dc/dcmitype/Sound");
              }
          }

        if (asMap.containsKey(MIME_TYPE) || asMap.containsKey(DURATION))
          {
            final StringBuilder buffer = new StringBuilder();
            final Key[] keys = new Key[] { MIME_TYPE, DURATION };

            String separator = "";

            for (final Key k : keys)
              {
                buffer.append(separator);

                if (k.equals(MIME_TYPE))
                  {
                    buffer.append(asMap.get(k));
                    separator = "; ";
                  }
                else if (asMap.containsKey(k))
                  {
                    buffer.append(asMap.get(k)).append("s");
                    separator = "; ";
                  }
              }

            if (buffer.length() > 0)
              {
                asMap.put(FORMAT, buffer.toString());
              }
          }

        return new Media(asMap);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    @Override
    public  T as (final @Nonnull Class type, final @Nonnull NotFoundBehaviour nfb)
      {
        if (type.equals(Downloadable.class) || type.equals(Removable.class))
          {
            return super.as(type, new NotFoundBehaviour()
              {
                @Nonnull
                public synchronized T run (final Throwable t)
                  {
                    if (downloadable == null)
                      {
                        try
                          {
                            downloadable = new DownloadableImpl(new URL(Media.this.get(ID).stringValue()));
                          }
                        catch (MalformedURLException e)
                          {
                            return nfb.run(new AsException(type, e));
                          }
                        catch (NotFoundException e)
                          {
                            return nfb.run(new AsException(type, e));
                          }
                      }

                    return type.cast(downloadable);
                  }
              });
          }

        return super.as(type, nfb);
      }
  }