Back to project page android-mvc-framework.
The source code is released under:
Apache License
If you think the Android project android-mvc-framework 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.android_mvc.framework.db.entity; /* w w w . ja v a 2 s . c o m*/ import com.android_mvc.framework.common.FWUtil; import com.android_mvc.framework.controller.routing.IntentPortable; import com.android_mvc.framework.db.DBHelper; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; /** * ????????????????????????????Java????????????????????????? * Intent???????????????? * @author id:language_and_engineering * */ public abstract class BaseLogicalEntity<T> implements IntentPortable { /** * Serializable?????????? */ private static final long serialVersionUID = 1L; /** * ????id???????? */ public abstract Long getId(); /** * ?????DB????????????????????????????????????????? */ private boolean is_new_record() { return (getId() == null); } // ----- LP?????Logical ? Physical? ----- /** * DB?????????????????????????????????????? * ????????????????????????????????????????????????????????? * @param c ???? */ public abstract T logicalFromPhysical(Cursor c); /** * ????DB????????????????????????????????????? * ?????????????????????????ContentValues??????????????????? */ protected abstract ContentValues toPhysicalEntity( ContentValues values ); // NOTE: ???????????????PhysicalEntity?????????????????????????????????? // ?????????ContentValues???final??????????????????PhysicalEntity?????????? // ?????????????????????????????????????? // final?????????????????????????????? /** * FW?????DB????????????????????????????????????????? */ public ContentValues toContentValues() { ContentValues values = new ContentValues(); return toPhysicalEntity( values ); } // ----- CRUD ----- /** * ???????? */ public abstract String tableName(); /** * ????????????? */ public abstract String[] columns(); // C?U: /** * ??????? * ??????????????????????????????????????????????????????? */ public boolean save(DBHelper helper) { boolean ret = false; FWUtil.d("DB?????????????????"); SQLiteDatabase db = helper.getWritableDatabase(); try{ // ????????????????? ContentValues values = this.toContentValues(); FWUtil.d("???????????????????????????????????????"); if( this.is_new_record() ) { db.insert(this.tableName(), null, values); FWUtil.d("DB????????????????????????"); } else { // ????????????????????? db.update( this.tableName(), values, "id = " + this.getId(), null ); FWUtil.d("DB?????????????????????"); } ret = true; } finally { db.close(); } return ret; } // R: // Finder????????????????????? // D: /** * ?????? */ public void delete(DBHelper helper) { FWUtil.d("DB?????????????????"); SQLiteDatabase db = helper.getWritableDatabase(); try{ db.delete( this.tableName(), "id = " + getId().toString(), null ); FWUtil.d("DB????????????????????????????"); } finally { db.close(); } } }