use of org.litepal.exceptions.DataSupportException in project LitePal by LitePalFramework.
the class UpdateUsingUpdateMethodTest method testUpdateAllToDefaultValueWithInstanceUpdateButWrongField.
public void testUpdateAllToDefaultValueWithInstanceUpdateButWrongField() {
try {
Teacher t = new Teacher();
t.setToDefault("name");
t.updateAll("");
fail();
} catch (DataSupportException e) {
assertEquals("The name field in com.litepaltest.model.Teacher class is necessary which does not exist.", e.getMessage());
}
}
use of org.litepal.exceptions.DataSupportException in project LitePal by LitePalFramework.
the class UpdateUsingUpdateMethodTest method testUpdateWithInstanceUpdateWithConstructor.
public void testUpdateWithInstanceUpdateWithConstructor() {
try {
Computer computer = new Computer("ACER", 5444);
computer.save();
computer.update(computer.getId());
fail();
} catch (DataSupportException e) {
assertEquals("com.litepaltest.model.Computer needs a default constructor.", e.getMessage());
}
}
use of org.litepal.exceptions.DataSupportException in project LitePal by LitePalFramework.
the class UpdateUsingUpdateMethodTest method testUpdateToDefaultValueWithInstanceUpdateButWrongField.
public void testUpdateToDefaultValueWithInstanceUpdateButWrongField() {
try {
Teacher t = new Teacher();
t.setToDefault("name");
t.update(t.getId());
fail();
} catch (DataSupportException e) {
assertEquals("The name field in com.litepaltest.model.Teacher class is necessary which does not exist.", e.getMessage());
}
}
use of org.litepal.exceptions.DataSupportException in project LitePal by LitePalFramework.
the class DataHandler method setAssociatedModel.
/**
* Finds the associated models of baseObj, then set them into baseObj.
*
* @param baseObj
* The class of base object.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private void setAssociatedModel(DataSupport baseObj) {
if (fkInOtherModel == null) {
return;
}
for (AssociationsInfo info : fkInOtherModel) {
Cursor cursor = null;
String associatedClassName = info.getAssociatedClassName();
boolean isM2M = info.getAssociationType() == Const.Model.MANY_TO_MANY;
try {
List<Field> supportedFields = getSupportedFields(associatedClassName);
List<Field> supportedGenericFields = getSupportedGenericFields(associatedClassName);
if (isM2M) {
String tableName = baseObj.getTableName();
String associatedTableName = DBUtility.getTableNameByClassName(associatedClassName);
String intermediateTableName = DBUtility.getIntermediateTableName(tableName, associatedTableName);
StringBuilder sql = new StringBuilder();
sql.append("select * from ").append(associatedTableName).append(" a inner join ").append(intermediateTableName).append(" b on a.id = b.").append(associatedTableName + "_id").append(" where b.").append(tableName).append("_id = ?");
cursor = DataSupport.findBySQL(BaseUtility.changeCase(sql.toString()), String.valueOf(baseObj.getBaseObjId()));
} else {
String foreignKeyColumn = getForeignKeyColumnName(DBUtility.getTableNameByClassName(info.getSelfClassName()));
String associatedTableName = DBUtility.getTableNameByClassName(associatedClassName);
cursor = mDatabase.query(BaseUtility.changeCase(associatedTableName), null, foreignKeyColumn + "=?", new String[] { String.valueOf(baseObj.getBaseObjId()) }, null, null, null, null);
}
if (cursor != null && cursor.moveToFirst()) {
SparseArray<QueryInfoCache> queryInfoCacheSparseArray = new SparseArray<QueryInfoCache>();
Map<Field, GenericModel> genericModelMap = new HashMap<Field, GenericModel>();
do {
DataSupport modelInstance = (DataSupport) createInstanceFromClass(Class.forName(associatedClassName));
giveBaseObjIdValue(modelInstance, cursor.getLong(cursor.getColumnIndexOrThrow("id")));
setValueToModel(modelInstance, supportedFields, null, cursor, queryInfoCacheSparseArray);
setGenericValueToModel(modelInstance, supportedGenericFields, genericModelMap);
if (info.getAssociationType() == Const.Model.MANY_TO_ONE || isM2M) {
Field field = info.getAssociateOtherModelFromSelf();
Collection collection = (Collection) getFieldValue(baseObj, field);
if (collection == null) {
if (isList(field.getType())) {
collection = new ArrayList();
} else {
collection = new HashSet();
}
DynamicExecutor.setField(baseObj, field.getName(), collection, baseObj.getClass());
}
collection.add(modelInstance);
} else if (info.getAssociationType() == Const.Model.ONE_TO_ONE) {
setFieldValue(baseObj, info.getAssociateOtherModelFromSelf(), modelInstance);
}
} while (cursor.moveToNext());
queryInfoCacheSparseArray.clear();
genericModelMap.clear();
}
} catch (Exception e) {
throw new DataSupportException(e.getMessage(), e);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
use of org.litepal.exceptions.DataSupportException in project LitePal by LitePalFramework.
the class DataHandler method getEmptyModel.
/**
* Create an empty instance of baseObj if it hasn't created one yet. If
* there's already an empty model existed in {@link #tempEmptyModel}, no
* need to create a new one.
*
* @param baseObj
* Current model to update.
* @return An empty instance of baseObj.
*/
protected DataSupport getEmptyModel(DataSupport baseObj) {
if (tempEmptyModel != null) {
return tempEmptyModel;
}
String className = null;
try {
className = baseObj.getClassName();
Class<?> modelClass = Class.forName(className);
tempEmptyModel = (DataSupport) modelClass.newInstance();
return tempEmptyModel;
} catch (ClassNotFoundException e) {
throw new DatabaseGenerateException(DatabaseGenerateException.CLASS_NOT_FOUND + className);
} catch (InstantiationException e) {
throw new DataSupportException(className + DataSupportException.INSTANTIATION_EXCEPTION, e);
} catch (Exception e) {
throw new DataSupportException(e.getMessage(), e);
}
}
Aggregations