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.scrobble;
028
029import static de.umass.util.StringUtilities.decode;
030import static de.umass.util.StringUtilities.encode;
031
032/**
033 * Bean that contains track information.
034 *
035 * @author Janni Kovacs
036 * @see de.umass.lastfm.scrobble.ScrobbleData
037 * @see de.umass.lastfm.Track#scrobble(ScrobbleData, de.umass.lastfm.Session)
038 * @deprecated The 1.2.x scrobble protocol has now been deprecated in favour of the 2.0 protocol which is part of the Last.fm web services
039 *             API.
040 */
041@Deprecated
042public class SubmissionData {
043
044        private String artist;
045        private String track;
046        private String album;
047        private long startTime;
048        private Source source;
049        private Rating rating;
050        private String recommendationKey;
051        private int length;
052        private int tracknumber;
053
054        public SubmissionData(String artist, String track, String album, int length, int tracknumber, Source source,
055                                                  long startTime) {
056                this(artist, track, album, length, tracknumber, source, null, startTime);
057        }
058
059        public SubmissionData(String artist, String track, String album, int length, int tracknumber, Source source,
060                                                  Rating rating, long startTime) {
061                this.artist = artist;
062                this.track = track;
063                this.album = album;
064                this.length = length;
065                this.tracknumber = tracknumber;
066                this.source = source;
067                this.rating = rating;
068                this.startTime = startTime;
069        }
070
071        public SubmissionData(String artist, String track, String album, int length, int tracknumber, Source source,
072                                                  Rating rating, long startTime, String recommendationKey) {
073                this(artist, track, album, length, tracknumber, source, rating, startTime);
074                this.recommendationKey = recommendationKey;
075        }
076
077        /**
078         * Creates a new SubmissionData object based on a String returned by {@link #toString()}.
079         *
080         * @param s A String
081         */
082        public SubmissionData(String s) {
083                String[] parts = s.split("&", 9);
084                artist = decode(parts[0]);
085                track = decode(parts[1]);
086                startTime = parts[2].length() == 0 ? 0 : Long.valueOf(parts[2]);
087                source = Source.valueOf(parts[3]);
088                recommendationKey = parts[4].length() == 0 ? null : parts[4];
089                rating = parts[5].length() == 0 ? null : Rating.valueOf(parts[5]);
090                length = parts[6].length() == 0 ? -1 : Integer.valueOf(parts[6]);
091                album = parts[7].length() == 0 ? null : decode(parts[7]);
092                tracknumber = parts[8].length() == 0 ? -1 : Integer.valueOf(parts[8]);
093        }
094
095        /**
096         * Returns a String representation of this submission with the fields separated by &.
097         * Order of the fields is:<br/>
098         * <tt>artist&track&startTime&Source&RecommendationKey&Rating&length&album&tracknumber</tt><br/>
099         * Note that:
100         * - Values may possibly be <code>null</code> or empty
101         * - enum values such as Rating and Source are <code>null</code> or their constant name is used (i.e. "LOVE")
102         * - all string values (artist, track, album) are utf8-url-encoded
103         *
104         * @return a String
105         */
106        public String toString() {
107                String b = encode(album != null ? album : "");
108                String artist = encode(this.artist);
109                String track = encode(this.track);
110                String l = length == -1 ? "" : String.valueOf(length);
111                String n = tracknumber == -1 ? "" : String.valueOf(tracknumber);
112
113                String r = "";
114                if (rating != null)
115                        r = rating.name();
116                String rec = "";
117                if (recommendationKey != null && source == Source.LAST_FM && recommendationKey.length() == 5)
118                        rec = recommendationKey;
119
120                return String.format("%s&%s&%s&%s&%s&%s&%s&%s&%s", artist, track, startTime, source.name(), rec, r, l, b, n);
121        }
122
123        String toString(String sessionId, int index) {
124                String b = encode(album != null ? album : "");
125                String artist = encode(this.artist);
126                String track = encode(this.track);
127                String l = length == -1 ? "" : String.valueOf(length);
128                String n = tracknumber == -1 ? "" : String.valueOf(tracknumber);
129
130                String r = "";
131                if (rating != null)
132                        r = rating.getCode();
133                String rec = "";
134                if (recommendationKey != null && source == Source.LAST_FM && recommendationKey.length() == 5)
135                        rec = recommendationKey;
136
137                return String
138                                .format("s=%s&a[%10$d]=%s&t[%10$d]=%s&i[%10$d]=%s&o[%10$d]=%s&r[%10$d]=%s&l[%10$d]=%s&b[%10$d]=%s&n[%10$d]=%s&m[%10$d]=",
139                                                sessionId, artist, track, startTime, source.getCode() + rec, r, l, b, n, index);
140        }
141
142}