Back to project page anluwage.
The source code is released under:
GNU General Public License
If you think the Android project anluwage listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.sudocode.content.provider.api.dao; //from w w w . j av a 2 s. c o m import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.provider.BaseColumns; import com.sudocode.content.provider.api.annotation.Column; import com.sudocode.content.provider.api.annotation.ForeignField; import com.sudocode.content.provider.api.annotation.Id; import com.sudocode.content.provider.api.annotation.Table; import com.sudocode.content.provider.api.exception.AnnotationBasedProviderRuntimeException; import java.lang.reflect.Field; /** * Created by RM on 1/31/14. */ public abstract class AnnotationBasedDaoSupport { protected Table getTableAnnotation(Class<?> tableModelClass) { return tableModelClass.getAnnotation(Table.class); } protected Column getColumnAnnotation(Field field) { return field.getAnnotation(Column.class); } protected ForeignField getForeignFieldAnnotation(Field field) { return field.getAnnotation(ForeignField.class); } protected Id getIdAnnotation(Field field) { return field.getAnnotation(Id.class); } protected <T> long getId(T model) { Class<?> modelClass = model.getClass(); Field[] fields = modelClass.getDeclaredFields(); for (Field field : fields) { Id id = getIdAnnotation(field); if (id != null) { try { field.setAccessible(true); return long.class.cast(field.get(model)); } catch (IllegalAccessException e) { throw new AnnotationBasedProviderRuntimeException(e); } finally { field.setAccessible(false); } } } throw new AnnotationBasedProviderRuntimeException("Model for ContentProvider must include a column _id"); } public <T> T mapToObject(Uri uri, Class<T> dataType, boolean lazyLoad) { Cursor cursor = null; try { cursor = getContentResolver().query(uri, null, null, null, null); return mapToObject(cursor, dataType, lazyLoad); } catch (Exception e) { throw new AnnotationBasedProviderRuntimeException(e); } finally { if (cursor != null) { cursor.close(); } } } public <T> T mapToObject(Cursor cursor, Class<T> dataType, boolean lazyLoad) { try { if (!cursor.moveToNext()) { return null; } T object = dataType.newInstance(); Field[] fields = dataType.getDeclaredFields(); if (fields != null) { for (Field field : fields) { try { field.setAccessible(true); Column column = getColumnAnnotation(field); if (column != null) { if (String.class.equals(field.getType())) { field.set(object, cursor.getString(cursor.getColumnIndex(column.value()))); } else if (int.class.equals(field.getType())) { field.set(object, cursor.getInt(cursor.getColumnIndex(column.value()))); } else if (long.class.equals(field.getType())) { field.set(object, cursor.getLong(cursor.getColumnIndex(column.value()))); } else if (double.class.equals(field.getType())) { field.set(object, cursor.getDouble(cursor.getColumnIndex(column.value()))); } else if (float.class.equals(field.getType())) { field.set(object, cursor.getFloat(cursor.getColumnIndex(column.value()))); } else { throw new AnnotationBasedProviderRuntimeException("Data type not supported: " + field.getType()); } } Id id = getIdAnnotation(field); if (id != null) { field.set(object, cursor.getLong(cursor.getColumnIndex(BaseColumns._ID))); } } catch (IllegalAccessException e) { throw new AnnotationBasedProviderRuntimeException("Unable to access class member: " + field.getName(), e); } finally { field.setAccessible(false); } } if (!lazyLoad) { for (Field field : fields) { try { field.setAccessible(true); ForeignField foreignField = getForeignFieldAnnotation(field); if (foreignField != null) { long foreignId = getId(dataType.getDeclaredField(foreignField.fieldName()), object); Uri foreignContentUri = ContentUris.withAppendedId( getContentUri(foreignField.tableModelClass()), foreignId); Cursor foreignCursor = getContentResolver().query(foreignContentUri, null, null, null, null); // if (foreignCursor.moveToNext()) { Object foreignObject = mapToObject(foreignCursor, foreignField.tableModelClass(), false); field.set(object, foreignObject); // } foreignCursor.close(); } } catch (IllegalAccessException e) { throw new AnnotationBasedProviderRuntimeException("Unable to access class member: " + field.getName(), e); } catch (NoSuchFieldException e) { throw new AnnotationBasedProviderRuntimeException("Unable to find field", e); } finally { field.setAccessible(false); } } } } return object; } catch (InstantiationException e) { throw new AnnotationBasedProviderRuntimeException("Unable to instantiate data type: " + dataType, e); } catch (IllegalAccessException e) { throw new AnnotationBasedProviderRuntimeException("Unable to access member of data type: " + dataType, e); } } private long getId(Field field, Object object) throws IllegalAccessException { try { field.setAccessible(true); return ((Long) field.get(object)).longValue(); } catch (IllegalAccessException e) { throw e; } finally { field.setAccessible(false); } } protected Uri getContentUri(Class<?> tableModelClass) { try { Class<?>[] classes = tableModelClass.getDeclaredClasses(); if (classes != null) { for (Class<?> declaredClass : classes) { if (declaredClass.getSimpleName().equals("Contract")) { Field field = declaredClass.getDeclaredField("CONTENT_URI"); Object dummyObject = declaredClass.newInstance(); return Uri.class.cast(field.get(dummyObject)); } } } throw new AnnotationBasedProviderRuntimeException("Unable to access CONTENT_URI"); } catch (NoSuchFieldException e) { throw new AnnotationBasedProviderRuntimeException("Unable to access CONTENT_URI", e); } catch (InstantiationException e) { throw new AnnotationBasedProviderRuntimeException("Unable to access CONTENT_URI", e); } catch (IllegalAccessException e) { throw new AnnotationBasedProviderRuntimeException("Unable to access CONTENT_URI", e); } } protected ContentValues generateContentValues(Object object) { ContentValues contentValues = new ContentValues(); Class<?> dataType = object.getClass(); Field[] fields = dataType.getDeclaredFields(); if (fields != null) { for (Field field : fields) { try { Column column = field.getAnnotation(Column.class); if (column != null) { field.setAccessible(true); if (String.class.equals(field.getType())) { contentValues.put(column.value(), field.get(object).toString()); } else if (int.class.equals(field.getType())) { contentValues.put(column.value(), field.getInt(object)); } else if (long.class.equals(field.getType())) { contentValues.put(column.value(), field.getLong(object)); } else if (double.class.equals(field.getType())) { contentValues.put(column.value(), field.getDouble(object)); } else if (float.class.equals(field.getType())) { contentValues.put(column.value(), field.getFloat(object)); } else { throw new AnnotationBasedProviderRuntimeException("Data type not supported: " + field.getType()); } } } catch (IllegalAccessException e) { throw new AnnotationBasedProviderRuntimeException("Unable to access class member of object: " + object.getClass()); } finally { field.setAccessible(false); } } } return contentValues; } protected abstract ContentResolver getContentResolver(); }