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; //from ww w . j a v a2 s. co m import android.content.ContentProvider; import android.content.Context; import android.content.pm.PackageManager; import android.content.res.Resources; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; import android.util.Log; import com.sudocode.content.provider.api.annotation.Column; import com.sudocode.content.provider.api.annotation.InitScripts; import com.sudocode.content.provider.api.annotation.Table; import com.sudocode.content.provider.api.exception.AnnotationBasedProviderRuntimeException; import com.sudocode.mrsq.pricer.utils.ProviderMetaDataUtils; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; /** * Created by RM on 1/26/14. */ public class AnnotationBasedProviderDatabaseHelper extends SQLiteOpenHelper { private ProviderModel providerModel; private Context context; public AnnotationBasedProviderDatabaseHelper(Context context, ProviderModel provider) { super(context, provider.getDatabaseName(), provider.getCursorFactory(), provider.getVersion()); this.providerModel = provider; this.context = context; } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { Class<?>[] tableModelClasses = providerModel.getTableModelClasses(); if (tableModelClasses != null) { CreateTableScriptGenerator scriptGenerator = new CreateTableScriptGenerator(); for (Class<?> tableModelClass : tableModelClasses) { String script = scriptGenerator.generateScript(tableModelClass); sqLiteDatabase.execSQL(script); try { scriptGenerator.executeInitScripts(context, (AnnotationBasedProvider) providerModel, sqLiteDatabase, tableModelClass); } catch (PackageManager.NameNotFoundException e) { throw new AnnotationBasedProviderRuntimeException("Unable to find component", e); } } } } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { } private static class CreateTableScriptGenerator { String generateScript(Class<?> tableModelClass) { StringBuilder scriptBuilder = new StringBuilder(); Table table = getTableAnnotation(tableModelClass); scriptBuilder.append("create table "); scriptBuilder.append(table.value()); scriptBuilder.append("(_id integer primary key autoincrement"); ColumnInfo[] columns = getColumnInfo(tableModelClass); for (int i = 0; i < columns.length; i++) { scriptBuilder.append(", "); ColumnInfo columnInfo = columns[i]; scriptBuilder.append(columnInfo.columnName); scriptBuilder.append(" "); scriptBuilder.append(columnInfo.dataType); if (!columnInfo.nullable) { scriptBuilder.append(" not null"); } if (columnInfo.unique) { scriptBuilder.append(" unique"); } } scriptBuilder.append(");"); Log.d(getClass().getSimpleName(), "table-schema: " + scriptBuilder.toString()); return scriptBuilder.toString(); } private Table getTableAnnotation(Class<?> tableModelClass) { Table tableAnnotation = tableModelClass.getAnnotation(Table.class); if (tableAnnotation == null || !(tableAnnotation instanceof Table)) { throw new AnnotationBasedProviderRuntimeException("Not Table annotation found!"); } return tableAnnotation; } private ColumnInfo[] getColumnInfo(Class<?> tableModelClass) { List<ColumnInfo> columnInfoList = new ArrayList<ColumnInfo>(); Field[] fields = tableModelClass.getDeclaredFields(); if (fields != null) { for (Field field : fields) { Column column = field.getAnnotation(Column.class); if (column != null && column instanceof Column) { ColumnInfo columnInfo = new ColumnInfo(); columnInfo.columnName = column.value(); columnInfo.nullable = column.nullable(); columnInfo.unique = column.unique(); if (String.class.equals(field.getType())) { columnInfo.dataType = "TEXT"; } else if (int.class.equals(field.getType())) { columnInfo.dataType = "INTEGER"; } else if (long.class.equals(field.getType())) { columnInfo.dataType = "INTEGER"; } else if (double.class.equals(field.getType())) { columnInfo.dataType = "REAL"; } else if (float.class.equals(field.getType())) { columnInfo.dataType = "REAL"; } else { Log.w(getClass().getSimpleName(), "Unsupported field type: " + field.getType()); continue; } columnInfoList.add(columnInfo); } } } return columnInfoList.toArray(new ColumnInfo[columnInfoList.size()]); } void executeInitScripts(Context context, ContentProvider provider, SQLiteDatabase database, Class<?> tableModelClass) throws PackageManager.NameNotFoundException { InitScripts initScripts = tableModelClass.getAnnotation(InitScripts.class); if (initScripts != null && initScripts.value() != null) { Bundle metaData = ProviderMetaDataUtils.getProviderMetaData(provider); if (metaData != null) { Resources resources = context.getResources(); int resourceId = metaData.getInt(initScripts.value()); String[] scripts = resources.getStringArray(resourceId); if (scripts != null) { for (String script : scripts) { database.execSQL(script); } } } } } } private static class ColumnInfo { String columnName; String dataType; boolean nullable; boolean unique; } }