private void removeWorksheet(Worksheets dto) { equipmentDao.deleteListAttachedWithWorksheet(dto.getId()); labourDao.deleteListAttachedWithWorksheet(dto.getId()); materialDao.deleteListAttachedWithWorksheet(dto.getId()); travelTimeDao.deleteListAttachedWithWorksheet(dto.getId()); worksheetsDao.remove(dto.getId()); }
@Override protected boolean processServerResponse(String value, Worksheets dto) throws JSONException { boolean success = true; if (isEmptyJsonResponse(value)) { Log.w("Worksheet Push", "No details received from server: " + value); if (dto.isNew()) { removeWorksheet(dto); } return success; } // Check the response received from the server: JSONObject response = new JSONObject(value); int status = response.optInt("type"); String message = response.optString("message"); boolean parseDto = false; switch (status) { case HttpStatus.SC_OK: case HttpStatus.SC_ACCEPTED: // Request successfully processed by the server. // Remove our old local copy of the worksheet as we should have received the // latest one in the response. If not, we will get it in the next periodic sync removeWorksheet(dto); parseDto = true; break; case HttpStatus.SC_FORBIDDEN: // Unauthorized request case HttpStatus.SC_NOT_FOUND: // Worksheet / Contract not found Log.e("Worksheet Push", "Client-side error received: " + status + " - " + message); removeWorksheet(dto); // Alert the user about the failure alertUser( "Failed to push worksheet changes to server. Status: " + status + ", details: " + message); break; default: Log.e("Worksheet Push", "Server-side error received: " + status + " - " + message); // Request failed to be processed on the server. // Alert the user about the failure alertUser( "Failed to push worksheet changes to server. Will try again later. Status: " + status + ", details: " + message); // Mark DTO as dirty and try again later success = false; } if (parseDto) { // Let's replace our local copy with the latest version received from the server JSONObject jsonObject = response.getJSONObject("data"); Worksheets serverDto = dtoParser.parseWorksheet(jsonObject, "Worksheet"); if (serverDto != null) { // We need to replace our local copy of the worksheet (and any dependant tables) // with this new copy. worksheetsDao.insertOrReplace(serverDto); for (WorksheetLabour labour : serverDto.getWorksheetLabourList()) { labourDao.insertOrReplace(labour); } for (WorksheetEquipment equipment : serverDto.getWorksheetEquipmentList()) { equipmentDao.insertOrReplace(equipment); } for (WorksheetMaterial material : serverDto.getWorksheetMaterialList()) { materialDao.insertOrReplace(material); } for (WorksheetTravelTime traveltime : serverDto.getWorksheetTravelTimeList()) { travelTimeDao.insertOrReplace(traveltime); } } else { Log.w("Worksheet Push", "No DTO received from server: " + value); } } return success; }