}
public boolean insertVideoIntoPlaylist(String playlistId, String videoId, boolean retry) {
log.info(String.format("Attempting to insert video id '%s' into playlist id '%s'...",
videoId, playlistId));
PlaylistEntry playlistEntry = new PlaylistEntry();
playlistEntry.setId(videoId);
if (getVideoInPlaylist(playlistId, videoId) != null) {
log.warning(String.format("Video id '%s' is already in playlist id '%s'.", videoId,
playlistId));
// Return true here, so that the video is flagged as being in the playlist.
return true;
}
// As of Aug 2011, it's now possible to set the playlist position at insertion time.
playlistEntry.setPosition(0);
try {
playlistEntry = service.insert(new URL(getPlaylistFeedUrl(playlistId)), playlistEntry);
log.info(String.format("Inserted video id '%s' into playlist id '%s' at position 1.",
videoId, playlistId));
return true;
} catch (MalformedURLException e) {
log.log(Level.WARNING, "", e);
} catch (IOException e) {
log.log(Level.WARNING, "", e);
} catch (ServiceForbiddenException e) {
log.log(Level.INFO, "Maximum size of playlist reached.", e);
if (retry) {
log.info("Removing oldest entry from playlist and retrying...");
PlaylistEntry lastVideo = getLastVideoInPlaylist(playlistId);
if (lastVideo != null) {
try {
lastVideo.delete();
log.info("Last entry removed.");
return insertVideoIntoPlaylist(playlistId, videoId, false);
} catch (IOException innerEx) {
log.log(Level.WARNING, "", innerEx);