over 4 years ago

在 Java 企業版,在建立商業模型有一套標準化工作,但在此不多說專有名詞,依據這樣的標準可以讓不同的業務流程更清楚乾淨達到模組化,主要的目的提供良好的程式可維護。

  1. DatabaseHelper
    • 初始化, 更新, 刪除資料庫綱要等工作
  2. Model
    • 資料表與物件對應,簡單物件
  3. Repository
    • 定義常用功能,並透過泛型提供不同 Model 使用
    • insert(T entity)
    • T update(String id, T entity)
    • boolean delete(String id)
    • Collection findAll()
    • Collection findById(String id)
    • Collection findByAgeLessThen(int number)
    • Collection findByWeightGreaterThen(int number)
    • Collection findByAgeOrderbyHight(int age):依照年齡作為查詢參數,依照身高排序
DatabaseHelper
package edu.tcu.gaduo.mode.helper;

import edu.tcu.gaduo.model.LocationModel;
import edu.tcu.gaduo.model.MapModel;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
    
    private final static String DATABASE_NAME = "LOCATION_RECORD";
    private final static int VERSION = 1;
    public DatabaseHelper(Context context, CursorFactory factory) {
        super(context, DATABASE_NAME, factory, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(LocationModel.CreateSQL); 
        db.execSQL(MapModel.CreateSQL); 
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(LocationModel.DropSQL);
        db.execSQL(MapModel.DropSQL);
    }
}
MapModel
package edu.tcu.gaduo.model;

import android.content.ContentValues;
import android.database.Cursor;

public class MapModel {

    public static String CreateSQL = 
            "CREATE TABLE IF NOT EXISTS Map "
            + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, "
            + "NAME TEXT, "
            + "CREATIONTIME TEXT)";
    public static String DropSQL = "DROP TABLE Map;";
    public static String TableName = "Map";
    
    private int id;
    private String name;
    private String creationTime;
    
    private ContentValues values;
    
    public MapModel(String name, String creationTime) {
        this.name = name;
        this.creationTime = creationTime;
        values = new ContentValues();
    }
    
    public MapModel(int id, String name, String creationTime) {
        this.id = id;
        this.name = name;
        this.creationTime = creationTime;
    }
    
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getCreationTime() {
        return creationTime;
    }
    
    public ContentValues getContentValues(){
        values.clear();
        values.put("NAME", name);
        values.put("CREATIONTIME", creationTime);
        return values;
    }
    
}
Repository
package edu.tcu.gaduo.model.repository;

public interface Repository<T> {
    public void insert(T entity);
    public void update(String Id, T entity);
    public void delete(String Id);
}
SQLiteMapRepository
package edu.tcu.gaduo.model.repository;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import edu.tcu.gaduo.mode.helper.DatabaseHelper;
import edu.tcu.gaduo.model.MapModel;

public class SQLiteMapRepository implements Repository<MapModel>{
    private Context context;
    public SQLiteMapRepository(Context context){
        this.context = context;
    }
  
    @Override
    public void insert(MapModel entity) {
        DatabaseHelper helper = new DatabaseHelper(context, null);
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = entity.getContentValues();
        db.insert(MapModel.TableName, null, values );
        db.close();
        helper.close();
    }

    @Override
    public void update(String Id, MapModel entity) {
        DatabaseHelper helper = new DatabaseHelper(context, null);
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = entity.getContentValues();
        db.update(MapModel.TableName, values, "ID=?", new String[]{Id});
        db.close();
        helper.close();
    }

    @Override
    public void delete(String Id) {
        DatabaseHelper helper = new DatabaseHelper(context, null);
        SQLiteDatabase db = helper.getWritableDatabase();
        db.delete(MapModel.TableName, "ID=?", new String[]{Id});
        db.close();
        helper.close();
    }
    
    public List<MapModel> findAll(){
        List<MapModel> list = new ArrayList<MapModel>();
        DatabaseHelper helper = new DatabaseHelper(context, null);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query(MapModel.TableName, new String[]{}, null, null, null, null, "ID DESC");
        while(cursor.moveToNext()){
            MapModel model = new MapModel(cursor.getString(0), cursor.getString(1));
            list.add(model);
        }
        
        list.add(new MapModel("map01", "2014-06-15"));
        list.add(new MapModel("map02", "2014-06-15"));
        return list;
    }
    
    public List<String> findAllMapNames(){
        List<String> list = new ArrayList<String>();
        DatabaseHelper helper = new DatabaseHelper(context, null);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query(MapModel.TableName, new String[]{}, null, null, null, null, "ID DESC");
        while(cursor.moveToNext()){
            if(cursor.getColumnCount() > 0){
                list.add(cursor.getString(0));
            }
        }
        list.add("map01");
        list.add("map02");
        return list;
    }
    
    public MapModel findMapById(int id){
        DatabaseHelper helper = new DatabaseHelper(context, null);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query(MapModel.TableName, new String[]{}, "ID=?", new String[]{id + ""}, null, null, "ID DESC");
        MapModel model = new MapModel(cursor.getString(0), cursor.getString(1));
        return model;
    }
}
← Android - 取得 GPS 資訊 - LocationListener IHE - 資訊技術基礎建設技術架構 XDS,Metadata Generator →
 
comments powered by Disqus