001/*
002 * Copyright (c) 2012, the Last.fm Java Project and Committers
003 * All rights reserved.
004 *
005 * Redistribution and use of this software in source and binary forms, with or without modification, are
006 * permitted provided that the following conditions are met:
007 *
008 * - Redistributions of source code must retain the above
009 *   copyright notice, this list of conditions and the
010 *   following disclaimer.
011 *
012 * - Redistributions in binary form must reproduce the above
013 *   copyright notice, this list of conditions and the
014 *   following disclaimer in the documentation and/or other
015 *   materials provided with the distribution.
016 *
017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
018 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
019 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
020 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
021 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
023 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
024 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
025 */
026
027package de.umass.lastfm;
028
029import de.umass.xml.DomElement;
030
031/**
032 * A <code>BuyLink</code> contains information about places to buy an Album or Track. BuyLinks can point to physical
033 * and digital music stores. Some suppliers have icons, some do have price information, others don't (eBay for example).
034 * Common suppliers you will receive via the <code>getBuylinks()</code> methods are Amazon, Amazon MP3, iTunes and
035 * 7digital. All stores but eBay do supply icons at the time of writing.
036 *
037 * @author Janni Kovacs
038 * @see Album#getBuylinks(String, String, String, String)
039 * @see Track#getBuylinks(String, String, String, String)
040 */
041public class BuyLink {
042
043        public static enum StoreType {
044                PHYSICAl,
045                DIGITAL
046        }
047
048        private StoreType type;
049        private String name;
050        private String link;
051        private String icon;
052        private boolean search;
053
054        private String currency;
055        private double price;
056
057        private BuyLink(String name, StoreType type, String link) {
058                this.name = name;
059                this.type = type;
060                this.link = link;
061        }
062
063        public String getName() {
064                return name;
065        }
066
067        public String getLink() {
068                return link;
069        }
070
071        public StoreType getType() {
072                return type;
073        }
074
075        /**
076         * Returns a url to a 16x16 pixel icon for the store, or <code>null</code> if no icon url was supplied.
077         *
078         * @return Icon URL or <code>null</code>
079         */
080        public String getIcon() {
081                return icon;
082        }
083
084        /**
085         * Returns <code>true</code> if this link points to a search page instead of an actual product page. Note that
086         * for search links there is no price information available.
087         *
088         * @return if this is a search link
089         */
090        public boolean isSearch() {
091                return search;
092        }
093
094        /**
095         * Returns the currency of the price of the item. Check if this is <code>null</code> to double-check if there is
096         * price information available
097         *
098         * @return currency
099         */
100        public String getCurrency() {
101                return currency;
102        }
103
104        /**
105         * Returns the price for the item, or 0.0 if no price information is available. Use {@link #getCurrency()} and
106         * {@link #isSearch()} to check if price information is available.
107         *
108         * @return price, if available
109         */
110        public double getPrice() {
111                return price;
112        }
113
114        static BuyLink linkFromElement(StoreType type, DomElement element) {
115                BuyLink link = new BuyLink(element.getChildText("supplierName"), type, element.getChildText("buyLink"));
116                link.search = "1".equals(element.getChildText("isSearch"));
117                link.icon = element.getChildText("supplierIcon");
118                if (link.icon != null && link.icon.length() == 0)
119                        link.icon = null;
120                if (element.hasChild("price")) {
121                        DomElement child = element.getChild("price");
122                        link.currency = child.getChildText("currency");
123                        link.price = Double.parseDouble(child.getChildText("amount"));
124                }
125                return link;
126        }
127}