@Override
public CommandResult executeCommand(List<String> parameters) {
Date currentTime = new Date();
if (parameters.size() == 0) {
return new CommandResult(false, "Missing parameters");
}
String objectId = parameters.get(0);
if (!objectId.startsWith(SpotifyAlbum.class.getSimpleName() + ":")) {
return new CommandResult(false, "Invalid object, needs album identity");
}
try {
objectId = objectId.substring(SpotifyAlbum.class.getSimpleName().length() + 1);
JSONObject object = Client.create().resource("http://ws.spotify.com/lookup/1/.json?uri=" + objectId + "&extras=trackdetail").accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
JSONObject jsonAlbum = object.getJSONObject("album");
ReleaseEntity release = new ReleaseEntity();
release.setName(jsonAlbum.getString("name"));
if (jsonAlbum.has("released")) {
release.setDate(new SimpleDateFormat("yyyy").parse(jsonAlbum.getString("released")));
}
release.setLastUpdated(currentTime);
release.setLastUpdatedBy(SPOTIFY_SOURCE);
releaseRepository.create(release);
GlobalIdentityEntity globalIdentity = new GlobalIdentityEntity();
globalIdentity.setSource(SPOTIFY_SOURCE);
globalIdentity.setUri(jsonAlbum.getString("href"));
globalIdentity.setEntityId(release.getId());
globalIdentity.setLastUpdated(currentTime);
globalIdentity.setLastUpdatedBy(SPOTIFY_SOURCE);
globalIdentityRepository.create(globalIdentity);
JSONArray array = jsonAlbum.getJSONArray("tracks");
for (int i = 0; i < array.length(); i++) {
JSONObject jsonTrack = array.getJSONObject(i);
WorkEntity work = new WorkEntity();
work.setName(jsonTrack.getString("name"));
work.setLastUpdated(currentTime);
work.setLastUpdatedBy(SPOTIFY_SOURCE);
workRepository.create(work);
RecordingEntity recording = new RecordingEntity();
recording.setLastUpdated(currentTime);
recording.setLastUpdatedBy(SPOTIFY_SOURCE);
recordingRepository.create(recording);
recording.getWorks().add(work);
TrackEntity track = new TrackEntity();
if (jsonTrack.has("track-number")) {
track.setNumber(jsonTrack.getInt("track-number"));
}
track.setRecording(recording);
release.addTrack(track);
track.setLastUpdated(currentTime);
track.setLastUpdatedBy(SPOTIFY_SOURCE);
trackRepository.create(track);
PlayableElementEntity playableElement = new PlayableElementEntity();
playableElement.setSmdID(jsonTrack.getString("href"));
playableElement.setUri(jsonTrack.getString("href"));
playableElement.setFormat(SPOTIFY_SOURCE);
playableElement.setLastUpdated(currentTime);
playableElement.setLastUpdatedBy(SPOTIFY_SOURCE);
playableElementRepository.create(playableElement);
track.getPlayableElements().add(playableElement);
JSONArray jsonArtists = jsonTrack.optJSONArray("artists");
if (jsonArtists != null && jsonArtists.length() > 0) {
for (int j = 0; j < jsonArtists.length(); j++) {
JSONObject jsonArtist = jsonArtists.getJSONObject(j);
String name = jsonArtist.getString("name");
Collection<ArtistEntity> artists = artistRepository.findByName(name);
if (artists.size() == 0) {
artists = new ArrayList<ArtistEntity>();
ArtistEntity artist = new ArtistEntity();
artist.setName(name);
artist.setLastUpdated(currentTime);
artist.setLastUpdatedBy(SPOTIFY_SOURCE);
artistRepository.create(artist);
artists.add(artist);
globalIdentity = new GlobalIdentityEntity();
globalIdentity.setSource(SPOTIFY_SOURCE);
globalIdentity.setUri(jsonArtist.getString("href"));
globalIdentity.setEntityId(artist.getId());
globalIdentity.setLastUpdated(currentTime);
globalIdentity.setLastUpdatedBy(SPOTIFY_SOURCE);
globalIdentityRepository.create(globalIdentity);
}
for (ArtistEntity artist : artists) {
ContributorEntity contributor = new ContributorEntity(artist, Contributor.PERFORMER);
recording.addContributor(contributor);
contributor.setLastUpdated(currentTime);
contributor.setLastUpdatedBy(SPOTIFY_SOURCE);
contributorRepository.create(contributor);
}
}
}
recordingRepository.refresh(recording);
}
return new CommandResult(true, "Added " + release.getName());
} catch (ParseException e) {
throw new RuntimeException(e);
} catch (JSONException e) {
throw new RuntimeException(e);
}