if (!isAborted()) {
if (i == 0) {
entityManager.getTransaction().begin();
}
TrackData track = new TrackData();
track.setFile(trackData.getUri());
track.setFormat("spotify");
track.setSmdID(trackData.getUri());
track.setUrl(trackData.getUri());
track.setTags(new ArrayList<TagData>());
track.getTags().add(new TagData(TagData.ALBUM, trackData.getAlbum()));
track.getTags().add(new TagData(TagData.TITLE, trackData.getName()));
track.getTags().add(new TagData(TagData.SPOTIFY_TRACK_ID, trackData.getUri()));
track.getTags().add(new TagData("SPOTIFYIMAGE", trackData.getCover()));
track.getTags().add(new TagData("DURATION", "" + trackData.getDuration()));
if (trackData.getIndex() != null) {
if (trackData.getIndex() != null && trackData.getIndex() != 0) {
track.getTags().add(new TagData(TagData.TRACKNUM, "" + trackData.getIndex()));
}
if (trackData.getDisc() != null && trackData.getDisc() != 0) {
track.getTags().add(new TagData(TagData.DISC, "" + trackData.getDisc()));
}
if (trackData.getAlbumuri() != null) {
track.getTags().add(new TagData(TagData.SPOTIFY_ALBUM_ID, trackData.getAlbumuri()));
}
if (trackData.getArtists() != null) {
for (SpotifyArtistData artistData : trackData.getArtists()) {
track.getTags().add(new TagData(TagData.ARTIST, artistData.getName()));
track.getTags().add(new TagData(TagData.SPOTIFY_ARTIST_ID, artistData.getUri()));
}
}
} else {
//TODO: Remove this as soon as 2.2.7 of spotifyd has been officially released
response = c.resource(SERVICE_URL + "/" + trackData.getUri() + "/browse.json").accept("application/json").get(JSONObject.class);
try {
if (response.has("index") && !response.getString("index").equals("0")) {
track.getTags().add(new TagData(TagData.TRACKNUM, response.getString("index")));
}
if (response.has("disc") && !response.getString("disc").equals("0")) {
track.getTags().add(new TagData(TagData.DISC, response.getString("disc")));
}
if (response.has("albumuri") && response.getString("albumuri").length() > 0) {
track.getTags().add(new TagData(TagData.SPOTIFY_ALBUM_ID, response.getString("albumuri")));
}
if (response.has("artists")) {
JSONArray artistsArray = response.getJSONArray("artists");
for (int j = 0; j < artistsArray.length(); j++) {
JSONObject artistObject = artistsArray.getJSONObject(j);
track.getTags().add(new TagData(TagData.ARTIST, artistObject.getString("name")));
track.getTags().add(new TagData(TagData.SPOTIFY_ARTIST_ID, artistObject.getString("uri")));
}
}
} catch (JSONException e) {
e.printStackTrace();
//TODO: Implement error handling
}
}
progressHandler.progress(getId(), trackData.getAlbum() + "/" + trackData.getName(), offset + 1, (long) noOfTracks);
if (track != null) {
try {
importNewPlayableElement(track);
} catch (ConstraintViolationException e) {
//TODO: Change this so it uses the logging framework
System.err.println("ERROR when importing: " + track.getFile() + ": ");
for (ConstraintViolation<?> violation : e.getConstraintViolations()) {
System.err.println("- " + violation.getLeafBean().getClass().getSimpleName() + "." + violation.getPropertyPath().toString() + ": " + violation.getMessage());
}
}
} else {