use of io.realm.exceptions.RealmException in project realm-java by realm.
the class Realm method createOrUpdateAllFromJson.
/**
* Tries to update a list of existing objects identified by their primary key with new JSON data. If an existing
* object could not be found in the Realm, a new object will be created. This must happen within a transaction.
* If updating a {@link RealmObject} and a field is not found in the JSON object, that field will not be updated.
* If a new {@link RealmObject} is created and a field is not found in the JSON object, that field will be assigned
* the default value for the field type.
*
* @param clazz type of {@link io.realm.RealmObject} to create or update. It must have a primary key defined.
* @param json string with an array of JSON objects.
* @throws IllegalArgumentException if trying to update a class without a {@link io.realm.annotations.PrimaryKey}.
* @throws RealmException if unable to create a JSON array from the json string.
* @throws IllegalArgumentException if the JSON object doesn't have a primary key property but the corresponding
* {@link RealmObjectSchema} has a {@link io.realm.annotations.PrimaryKey} defined.
* @see #createAllFromJson(Class, String)
*/
public <E extends RealmModel> void createOrUpdateAllFromJson(Class<E> clazz, String json) {
if (clazz == null || json == null || json.length() == 0) {
return;
}
checkIfValid();
checkHasPrimaryKey(clazz);
JSONArray arr;
try {
arr = new JSONArray(json);
} catch (JSONException e) {
throw new RealmException("Could not create JSON array from string", e);
}
createOrUpdateAllFromJson(clazz, arr);
}
use of io.realm.exceptions.RealmException in project realm-java by realm.
the class Realm method createAllFromJson.
/**
* Creates a Realm object for each object in a JSON array. This must be done within a transaction.
* JSON properties with unknown properties will be ignored. If a {@link RealmObject} field is not present in the
* JSON object the {@link RealmObject} field will be set to the default value for that type.
*
* @param clazz type of Realm objects to create.
* @param json the JSON array as a String where each object can map to the specified class.
* @throws RealmException if mapping from JSON fails.
* @throws IllegalArgumentException if the JSON object doesn't have a primary key property but the corresponding
* {@link RealmObjectSchema} has a {@link io.realm.annotations.PrimaryKey} defined.
*/
public <E extends RealmModel> void createAllFromJson(Class<E> clazz, String json) {
if (clazz == null || json == null || json.length() == 0) {
return;
}
JSONArray arr;
try {
arr = new JSONArray(json);
} catch (JSONException e) {
throw new RealmException("Could not create JSON array from string", e);
}
createAllFromJson(clazz, arr);
}
use of io.realm.exceptions.RealmException in project realm-java by realm.
the class Realm method createOrUpdateObjectFromJson.
/**
* Tries to update an existing object defined by its primary key with new JSON data. If no existing object could be
* found a new object will be saved in the Realm. This must happen within a transaction. If updating a
* {@link RealmObject} and a field is not found in the JSON object, that field will not be updated. If a new
* {@link RealmObject} is created and a field is not found in the JSON object, that field will be assigned the
* default value for the field type.
* <p>
* This API is only available in API level 11 or later.
*
* @param clazz type of {@link io.realm.RealmObject} to create or update. It must have a primary key defined.
* @param in the {@link InputStream} with object data in JSON format.
* @return created or updated {@link io.realm.RealmObject}.
* @throws IllegalArgumentException if trying to update a class without a {@link io.realm.annotations.PrimaryKey}.
* @throws IllegalArgumentException if the JSON object doesn't have a primary key property but the corresponding
* {@link RealmObjectSchema} has a {@link io.realm.annotations.PrimaryKey} defined.
* @throws RealmException if failure to read JSON.
* @see #createObjectFromJson(Class, java.io.InputStream)
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public <E extends RealmModel> E createOrUpdateObjectFromJson(Class<E> clazz, InputStream in) throws IOException {
if (clazz == null || in == null) {
return null;
}
checkIfValid();
checkHasPrimaryKey(clazz);
// As we need the primary key value we have to first parse the entire input stream as in the general
// case that value might be the last property. :(
Scanner scanner = null;
try {
scanner = getFullStringScanner(in);
JSONObject json = new JSONObject(scanner.next());
return createOrUpdateObjectFromJson(clazz, json);
} catch (JSONException e) {
throw new RealmException("Failed to read JSON", e);
} finally {
if (scanner != null) {
scanner.close();
}
}
}
use of io.realm.exceptions.RealmException in project realm-java by realm.
the class PrimaryKeyTests method removingPrimaryKeyRemovesConstraint_typeSetters.
// Tests that primary key constraints are actually removed.
@Test
public void removingPrimaryKeyRemovesConstraint_typeSetters() {
RealmConfiguration config = configFactory.createConfigurationBuilder().name("removeConstraints").build();
SharedRealm sharedRealm = SharedRealm.getInstance(config);
sharedRealm.beginTransaction();
Table tbl = sharedRealm.getTable("EmployeeTable");
tbl.addColumn(RealmFieldType.STRING, "name");
tbl.setPrimaryKey("name");
// Creates first entry with name "Foo".
tbl.setString(0, tbl.addEmptyRow(), "Foo", false);
long rowIndex = tbl.addEmptyRow();
try {
// Tries to create 2nd entry with name Foo.
tbl.setString(0, rowIndex, "Foo", false);
} catch (RealmPrimaryKeyConstraintException e1) {
// Primary key check worked, now removes it and tries again.
tbl.setPrimaryKey("");
try {
tbl.setString(0, rowIndex, "Foo", false);
return;
} catch (RealmException e2) {
fail("Primary key not removed");
}
}
fail("Primary key not enforced.");
sharedRealm.close();
}
use of io.realm.exceptions.RealmException in project realm-java by realm.
the class RealmTests method runMethodOnWrongThread.
// Calling methods on a wrong thread will fail.
private boolean runMethodOnWrongThread(final Method method) throws InterruptedException, ExecutionException {
if (method != Method.METHOD_BEGIN) {
realm.beginTransaction();
realm.createObject(Dog.class);
}
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<Boolean> future = executorService.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
try {
switch(method) {
case METHOD_BEGIN:
realm.beginTransaction();
break;
case METHOD_COMMIT:
realm.commitTransaction();
break;
case METHOD_CANCEL:
realm.cancelTransaction();
break;
case METHOD_DELETE_TYPE:
realm.delete(AllTypes.class);
break;
case METHOD_DELETE_ALL:
realm.deleteAll();
break;
case METHOD_CREATE_OBJECT:
realm.createObject(AllTypes.class);
break;
case METHOD_CREATE_OBJECT_WITH_PRIMARY_KEY:
realm.createObject(AllJavaTypes.class, 1L);
break;
case METHOD_COPY_TO_REALM:
realm.copyToRealm(new AllTypes());
break;
case METHOD_COPY_TO_REALM_OR_UPDATE:
realm.copyToRealm(new AllTypesPrimaryKey());
break;
case METHOD_CREATE_ALL_FROM_JSON:
realm.createAllFromJson(AllTypes.class, "[{}]");
break;
case METHOD_CREATE_OR_UPDATE_ALL_FROM_JSON:
realm.createOrUpdateAllFromJson(AllTypesPrimaryKey.class, "[{\"columnLong\":1," + " \"columnBoolean\": true}]");
break;
case METHOD_CREATE_FROM_JSON:
realm.createObjectFromJson(AllTypes.class, "{}");
break;
case METHOD_CREATE_OR_UPDATE_FROM_JSON:
realm.createOrUpdateObjectFromJson(AllTypesPrimaryKey.class, "{\"columnLong\":1," + " \"columnBoolean\": true}");
break;
case METHOD_INSERT_COLLECTION:
realm.insert(Arrays.asList(new AllTypes(), new AllTypes()));
break;
case METHOD_INSERT_OBJECT:
realm.insert(new AllTypes());
break;
case METHOD_INSERT_OR_UPDATE_COLLECTION:
realm.insert(Arrays.asList(new AllTypesPrimaryKey(), new AllTypesPrimaryKey()));
break;
case METHOD_INSERT_OR_UPDATE_OBJECT:
realm.insertOrUpdate(new AllTypesPrimaryKey());
break;
}
return false;
} catch (IllegalStateException ignored) {
return true;
} catch (RealmException jsonFailure) {
// TODO: Eew. Reconsider how our JSON methods reports failure. See https://github.com/realm/realm-java/issues/1594
return (jsonFailure.getMessage().equals("Could not map Json"));
}
}
});
boolean result = future.get();
if (method != Method.METHOD_BEGIN) {
realm.cancelTransaction();
}
return result;
}
Aggregations