summaryrefslogtreecommitdiff
path: root/src/com/p4square/grow/model/Playlist.java
blob: 3e77ada2b8c774ca307cf0ac5e2f8f191dc4426c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright 2013 Jesse Morgan
 */

package com.p4square.grow.model;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * Representation of a user's playlist.
 *
 * @author Jesse Morgan <jesse@jesterpm.net>
 */
public class Playlist {
    /**
     * Map of Chapter ID to map of Video ID to VideoRecord.
     */
    private Map<String, Chapter> mPlaylist;

    private Date mLastUpdated;

    /**
     * Construct an empty playlist.
     */
    public Playlist() {
        mPlaylist = new HashMap<String, Chapter>();
        mLastUpdated = new Date(0); // Default to a prehistoric date if we don't have one.
    }

    /**
     * Find the VideoRecord for a video id.
     */
    public VideoRecord find(String videoId) {
        for (Chapter chapter : mPlaylist.values()) {
            VideoRecord r = chapter.getVideoRecord(videoId);

            if (r != null) {
                return r;
            }
        }

        return null;
    }

    /**
     * @param videoId The video to search for.
     * @return the Chapter containing videoId.
     */
    private Chapter findChapter(String videoId) {
        for (Chapter chapter : mPlaylist.values()) {
            VideoRecord r = chapter.getVideoRecord(videoId);

            if (r != null) {
                return chapter;
            }
        }

        return null;
    }

    /**
     * @return The last modified date of the source playlist.
     */
    public Date getLastUpdated() {
        return mLastUpdated;
    }

    /**
     * Set the last updated date.
     * @param date the new last updated date.
     */
    public void setLastUpdated(Date date) {
        mLastUpdated = date;
    }

    /**
     * Add a video to the playlist.
     */
    public VideoRecord add(String chapterId, String videoId) {
        Chapter chapter = mPlaylist.get(chapterId);

        if (chapter == null) {
            chapter = new Chapter(chapterId);
            mPlaylist.put(chapterId, chapter);
        }

        VideoRecord r = new VideoRecord();
        chapter.setVideoRecord(videoId, r);
        return r;
    }

    /**
     * Add a Chapter to the Playlist.
     * @param chapterId The name of the chapter.
     * @param chapter The Chapter object to add.
     */
    @JsonAnySetter
    public void addChapter(String chapterId, Chapter chapter) {
        chapter.setName(chapterId);
        mPlaylist.put(chapterId, chapter);
    }

    /**
     * @return a map of chapter id to chapter.
     */
    @JsonAnyGetter
    public Map<String, Chapter> getChaptersMap() {
        return mPlaylist;
    }

    /**
     * @return The last chapter to be completed.
     */
    @JsonIgnore
    public Map<String, Boolean> getChapterStatuses() {
        Map<String, Boolean> completed = new HashMap<String, Boolean>();

        for (Map.Entry<String, Chapter> entry : mPlaylist.entrySet()) {
            completed.put(entry.getKey(), entry.getValue().isComplete());
        }

        return completed;
    }

    /**
     * @return true if all required videos in the chapter have been watched.
     */
    public boolean isChapterComplete(String chapterId) {
        Chapter chapter = mPlaylist.get(chapterId);
        if (chapter != null) {
            return chapter.isComplete();
        }

        return false;
    }

    /**
     * Merge a playlist into this playlist.
     *
     * Merge is accomplished by adding all missing Chapters and VideoRecords to
     * this playlist.
     */
    public void merge(Playlist source) {
        if (source.getLastUpdated().before(mLastUpdated)) {
            // Already up to date.
            return;
        }

        for (Map.Entry<String, Chapter> entry : source.getChaptersMap().entrySet()) {
            String chapterName = entry.getKey();
            Chapter theirChapter = entry.getValue();
            Chapter myChapter = mPlaylist.get(entry.getKey());

            if (myChapter == null) {
                // Add new chapter
                myChapter = new Chapter(chapterName);
                addChapter(chapterName, myChapter);
            }

            // Check chapter for missing videos
            for (Map.Entry<String, VideoRecord> videoEntry : theirChapter.getVideos().entrySet()) {
                String videoId = videoEntry.getKey();
                VideoRecord myVideo = myChapter.getVideoRecord(videoId);

                if (myVideo == null) {
                    myVideo = find(videoId);
                    if (myVideo == null) {
                        // New Video
                        try {
                            myVideo = videoEntry.getValue().clone();
                            myChapter.setVideoRecord(videoId, myVideo);
                        } catch (CloneNotSupportedException e) {
                            throw new RuntimeException(e); // Unexpected...
                        }
                    } else {
                        // Video moved
                        findChapter(videoId).removeVideoRecord(videoId);
                        myChapter.setVideoRecord(videoId, myVideo);
                    }
                }
            }
        }

        mLastUpdated = source.getLastUpdated();
    }
}