Hi Alexandr,
Thank you for your suggestion.
When you wrote: “[…] you should first retrieve the parent object with relations from server.”
Do you mean that I have to retrieve my object first from the server, by setting also the depth of the query in order to retrieve all the relations?
I've tried something different on my custom server code class, and I also added an "update" method to my models classes in order to update only what is needed.
Here is the Person class
package com.example.models;
// Imports
public class Person {
// Attributes, Constructors and Methods (the same as above)
public Person update(String jsonString) {
JSONObject person = getJSONObjectFromString(jsonString);
try {
if (person.has("name"))
this.name = person.getString("name");
if (person.has("surname"))
this.surname = person.getString("surname");
if (person.has("idCars") && person.getJSONArray("idCars").length() > 0) {
JSONArray jsonArray = person.getJSONArray("idCars");
StringBuilder whereClause = new StringBuilder();
whereClause.append("objectId='").append(jsonArray.getString(0)).append("'");
for (int i = 1; i < jsonArray.length(); i++)
whereClause.append(" OR ").append("objectId='").append(jsonArray.getString(i)).append("'");
BackendlessDataQuery dataQuery = new BackendlessDataQuery();
dataQuery.setWhereClause(whereClause.toString());
List<Car> carList = Backendless.Persistence.of(Car.class).find(dataQuery).getData();
if (carList != null) {
if (this.cars != null) {
this.cars.clear();
} else {
this.cars = new ArrayList<>();
}
this.cars.addAll(carList);
}
}
} catch (BackendlessException e) {
e.printStackTrace();
}
return this.save();
}
/**************************************************************************
* PRIVATE METHODS *
**************************************************************************/
private JSONObject getJSONObjectFromString(String jsonString) {
JSONObject jsonObject = null;
try {
JSONTokener jsonTokener = new JSONTokener(jsonString);
jsonObject = (JSONObject) jsonTokener.nextValue();
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
}
Here is the Car class
package com.example.models;
// Imports
public class Car {
// Attributes, Constructors and Methods (the same as above)
public Car update(String jsonString) {
JSONObject person = getJSONObjectFromString(jsonString);
try {
if (person.has("name"))
this.name = person.getString("name");
if (person.has("colour"))
this.colour = person.getString("colour");
} catch (BackendlessException e) {
e.printStackTrace();
}
return this.save();
}
/******************************************************************
* PRIVATE METHODS *
*******************************************************************/
private JSONObject getJSONObjectFromString(String jsonString) {
JSONObject jsonObject = null;
try {
JSONTokener jsonTokener = new JSONTokener(jsonString);
jsonObject = (JSONObject) jsonTokener.nextValue();
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
}
And finaly here it is my custom server code used to both insert and update the records on the DB
package com.example.service;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.json.JSONException;
import com.example.models.Car;
import com.example.models.Person;
import com.backendless.servercode.IBackendlessService;
import com.backendless.exceptions.BackendlessException;
public class Services implements IBackendlessService {
/************************************************************
* INSERT *
*************************************************************/
public String insertPerson(String jsonData) {
JSONObject result = new JSONObject();
Boolean resultValue = false;
try {
Person person = new Person(jsonData);
Person personToInsert = person.save();
if (personToInsert != null)
resultValue = true;
} catch (BackendlessException e) {
e.printStackTrace();
}
result.put("success", resultValue);
return result.toString();
}
public String insertCar(String jsonData) {
JSONObject result = new JSONObject();
Boolean resultValue = false;
try {
Car car = new Car(jsonData);
Car carToInsert = car.save();
if (car != null)
resultValue = true;
} catch (BackendlessException e) {
e.printStackTrace();
}
result.put("success", resultValue);
return result.toString();
}
/*************************************************************
* UPDATE *
**************************************************************/
public String updatePerson(String jsonData) {
JSONObject result = new JSONObject();
Boolean resultStatus = false;
try {
JSONObject jsonnedData = getJSONObjectFromString(jsonData);
if (jsonnedData != null && jsonnedData.has("objectId")) {
Person personToUpdate = Person.findById(jsonnedData.getString("objectId"));
if (personToUpdate != null) {
Person updatedPerson = personToUpdate.update(jsonData);
if (updatedPerson != null) {
resultStatus = true;
}
}
}
} catch (BackendlessException e) {
e.printStackTrace();
resultStatus = false;
JSONArray errorData = getBackendlessExceptionErrorJSONArray(e);
result.put("error", errorData);
}
result.put("success", resultStatus);
return result.toString();
}
public String updateCar(String jsonData) {
JSONObject result = new JSONObject();
Boolean resultStatus = false;
try {
JSONObject jsonnedData = getJSONObjectFromString(jsonData);
if (jsonnedData != null && jsonnedData.has("objectId")) {
Car carToUpdate = Car.findById(jsonnedData.getString("objectId"));
if (carToUpdate != null) {
Car updatedCar = carToUpdate.update(jsonData);
if (updatedCar != null) {
resultStatus = true;
}
}
}
} catch (BackendlessException e) {
e.printStackTrace();
resultStatus = false;
JSONArray errorData = getBackendlessExceptionErrorJSONArray(e);
result.put("error", errorData);
}
result.put("success", resultStatus);
return result.toString();
}
/***************************************************************************************
* PRIVATE METHODS *
***************************************************************************************/
private JSONObject getJSONObjectFromString(String jsonString) {
JSONObject jsonObject = null;
try {
JSONTokener jsonTokener = new JSONTokener(jsonString);
jsonObject = (JSONObject) jsonTokener.nextValue();
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
private JSONArray getBackendlessExceptionErrorJSONArray (BackendlessException e) {
JSONArray errorData = new JSONArray();
JSONObject data = new JSONObject();
data.put("code", e.getCode());
data.put("message", e.getMessage());
data.put("detail", e.getDetail());
return errorData;
}
}
I am really sorry about this long comment, but I think it is necessary in order to better understand what is going on.
Do you think that there is a better approach for retrieving the data from the DB before updating it?
I really don't like wasting and API call in the updating methods of my custom server code, do you know a better solution which avoids wasting API calls?
Thank you for you for your help and answer.
PS: the code area is really bad in the comments, it doen’t keep the indentetions, and I can’t either attach the Java classes. I’m sorry