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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
/*
* 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<Chapters, Chapter> mPlaylist;
private Date mLastUpdated;
/**
* Construct an empty playlist.
*/
public Playlist() {
mPlaylist = new HashMap<>();
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(Chapters 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.
*/
public void addChapter(Chapters chapterId, Chapter chapter) {
chapter.setName(chapterId);
mPlaylist.put(chapterId, chapter);
}
/**
* Variation of addChapter() with a String key for Jackson.
*/
@JsonAnySetter
private void addChapter(String chapterName, Chapter chapter) {
addChapter(Chapters.fromString(chapterName), chapter);
}
/**
* @return a map of chapter id to chapter.
*/
@JsonAnyGetter
public Map<Chapters, Chapter> getChaptersMap() {
return mPlaylist;
}
/**
* @return The last chapter to be completed.
*/
@JsonIgnore
public Map<Chapters, Boolean> getChapterStatuses() {
Map<Chapters, Boolean> completed = new HashMap<>();
for (Map.Entry<Chapters, 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(Chapters 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.
*
* Additionally,
* * any "required" videos that are only present in this playlist are
* marked as "not required".
* * any new "required" videos in a completed chapter are marked as
* "not required".
*/
public void merge(Playlist source) {
if (source.getLastUpdated().before(mLastUpdated)) {
// Already up to date.
return;
}
for (Map.Entry<Chapters, Chapter> entry : source.getChaptersMap().entrySet()) {
Chapters 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);
}
// If my chapter is already complete, no new videos will be required.
boolean myChapterComplete = true;
for (Map.Entry<String, VideoRecord> videoEntry : myChapter.getVideos().entrySet()) {
VideoRecord myVideo = videoEntry.getValue();
myChapterComplete &= (myVideo.getComplete() || !myVideo.getRequired());
// Mark all existing, uncompleted videos as not required.
// We'll mark them as required again if they are required in the new playlist.
myVideo.setRequired(myVideo.getRequired() && myVideo.getComplete());
}
// 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);
if (myChapterComplete) {
myVideo.setRequired(false);
}
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e); // Unexpected...
}
} else {
// Video moved
findChapter(videoId).removeVideoRecord(videoId);
myChapter.setVideoRecord(videoId, myVideo);
}
} else {
// Copy the required property from the newer video.
// However, like new videos, newly required videos aren't applied to completed
// chapters.
if (!myChapterComplete) {
myVideo.setRequired(videoEntry.getValue().getRequired());
}
}
}
}
mLastUpdated = source.getLastUpdated();
}
}
|