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();