final long CHUNK_SIZE = 20;
final String SERVICE_URL = "http://" + squeezeboxServerHost + ":" + squeezeboxServerPort + "/jsonrpc.js";
long offset = 0;
try {
JSONObject request = createRequest(offset, CHUNK_SIZE);
Client c = Client.create();
if (squeezeboxServerUsername != null && squeezeboxServerUsername.length() > 0) {
if (squeezeboxServerPassword != null && squeezeboxServerPassword.length() > 0) {
c.addFilter(new HTTPBasicAuthFilter(squeezeboxServerUsername, squeezeboxServerPassword));
} else if (squeezeboxServerPasswordHash != null && squeezeboxServerPasswordHash.length() > 0) {
c.addFilter(new HTTPBasicAuthFilter(squeezeboxServerUsername, squeezeboxServerPasswordHash));
}
}
JSONObject response = null;
if (squeezeboxServerPasswordHash != null && squeezeboxServerPasswordHash.length() > 0) {
response = c.resource(SERVICE_URL).accept("application/json").header("X-Scanner", 1).post(JSONObject.class, request);
} else {
response = c.resource(SERVICE_URL).accept("application/json").post(JSONObject.class, request);
}
ObjectMapper mapper = new ObjectMapper();
trackList = mapper.readValue(response.getString("result"), TrackListData.class);
if (trackList != null) {
while (trackList != null && !isAborted()) {
long i = 0;
entityManager.getTransaction().begin();
for (TrackData track : trackList.getTracks()) {
progressHandler.progress(getId(), track.getFile(), trackList.getOffset() + i + 1, trackList.getCount());
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());
}
}
i++;
}
entityManager.flush();
entityManager.clear();
entityManager.getTransaction().commit();
if (offset + trackList.getTracks().size() < trackList.getCount()) {
offset = offset + trackList.getTracks().size();
request = createRequest(offset, CHUNK_SIZE);
if (squeezeboxServerPasswordHash != null && squeezeboxServerPasswordHash.length() > 0) {
response = c.resource(SERVICE_URL).accept("application/json").header("X-Scanner", 1).post(JSONObject.class, request);
} else {
response = c.resource(SERVICE_URL).accept("application/json").post(JSONObject.class, request);
}
trackList = mapper.readValue(response.getString("result"), TrackListData.class);
} else {
trackList = null;
}
}