Search in sources :

Example 11 with DataSupportException

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());
    }
}
Also used : DataSupportException(org.litepal.exceptions.DataSupportException) Teacher(com.litepaltest.model.Teacher)

Example 12 with DataSupportException

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());
    }
}
Also used : DataSupportException(org.litepal.exceptions.DataSupportException) Computer(com.litepaltest.model.Computer)

Example 13 with DataSupportException

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());
    }
}
Also used : DataSupportException(org.litepal.exceptions.DataSupportException) Teacher(com.litepaltest.model.Teacher)

Example 14 with DataSupportException

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();
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) DatabaseGenerateException(org.litepal.exceptions.DatabaseGenerateException) DataSupportException(org.litepal.exceptions.DataSupportException) InvocationTargetException(java.lang.reflect.InvocationTargetException) GenericModel(org.litepal.tablemanager.model.GenericModel) Field(java.lang.reflect.Field) SparseArray(android.util.SparseArray) DataSupportException(org.litepal.exceptions.DataSupportException) Collection(java.util.Collection) AssociationsInfo(org.litepal.crud.model.AssociationsInfo) HashSet(java.util.HashSet)

Example 15 with DataSupportException

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);
    }
}
Also used : DatabaseGenerateException(org.litepal.exceptions.DatabaseGenerateException) DataSupportException(org.litepal.exceptions.DataSupportException) DatabaseGenerateException(org.litepal.exceptions.DatabaseGenerateException) DataSupportException(org.litepal.exceptions.DataSupportException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

DataSupportException (org.litepal.exceptions.DataSupportException)20 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 Cursor (android.database.Cursor)4 Field (java.lang.reflect.Field)4 DatabaseGenerateException (org.litepal.exceptions.DatabaseGenerateException)4 AssociationsInfo (org.litepal.crud.model.AssociationsInfo)3 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)2 SQLiteException (android.database.sqlite.SQLiteException)2 SparseArray (android.util.SparseArray)2 Teacher (com.litepaltest.model.Teacher)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 GenericModel (org.litepal.tablemanager.model.GenericModel)2 ContentValues (android.content.ContentValues)1 Computer (com.litepaltest.model.Computer)1 Student (com.litepaltest.model.Student)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1