BaseModel based on ActiveAndroid framework

Keywords: Database Hibernate

brief introduction

Based on the ActiveAndroid framework, the Self-encapsulated BaseModel class, and some other operation classes simply need to inherit from BaseModel to have a method of database operation, which is very convenient to use.
On how to use ActiveAndroid, I wrote in another of my blogs:
http://blog.csdn.net/hbdatouerzi/article/details/53504809

Source code

BaseModel is an abstract template Class that contains the abstract method getTag(), which the subclass must implement, and the subclass needs to pass in Class that inherits from Model.

//Database Operations Base Class
public abstract class BaseModel<T extends Model> {

    private List<T> mCache;

    private Class<T> cl;

    public BaseModel(final Class<T> cl){
        this.cl = cl;
        mCache = new Select().from(cl).execute();
        if(mCache == null){
            mCache = new ArrayList<>();
        }
    }

    //Add to
    public void add(final T instance){
        for(T t : mCache){
            if(t.equals(instance)){
                Log.i(getTag(),"is aready added");
                return;
            }
        }
        mCache.add(instance);
        ThreadFactory.getNormalPool().execute(new Runnable() {
            @Override
            public void run() {
                instance.save();
                Log.i(getTag(),"instance added success");
            }
        });
    }

    //Preservation
    public void save(final T instance){
        for(T t : mCache){
            if(t.equals(instance)){
                mCache.remove(t);
            }
        }
        mCache.add(instance);
        ThreadFactory.getNormalPool().execute(new Runnable() {
            @Override
            public void run() {
                instance.save();
            }
        });
    }

    //delete
    public void delete(T instance){
        T tobeDel = null;
        int size = mCache.size();
        for(int i=0;i<size;i++){
            if(mCache.get(i).equals(instance)){
                tobeDel = mCache.get(i);
                mCache.remove(i);
            }
        }
        if(tobeDel != null){
            final T finalTobeDel = tobeDel;
            ThreadFactory.getNormalPool().execute(new Runnable() {
                @Override
                public void run() {
                    finalTobeDel.delete();
                }
            });
        }
    }

    //lookup
    public List<T> find(Map<String,Object> condition){
        if(condition == null || condition.isEmpty()){
            return null;
        }
        //Combining query parameters
        StringBuilder builder = new StringBuilder();
        Set<String> keys = condition.keySet();
        Iterator<String> iterator = keys.iterator();
        while(iterator.hasNext()){
            String key = iterator.next();
            builder.append(key);
            builder.append(" = ");
            builder.append("\"");
            builder.append(condition.get(key));
            builder.append("\"");
            if(iterator.hasNext()){
                builder.append(" and ");
            }
        }
        List<T> result = new Select().from(cl).where(builder.toString()).execute();
        return result;
    }

    public List<T> getAll(){
        return mCache;
    }

    public abstract String getTag();
}

Subclass Model
As an example of FriendModel, FriendModel is an operation class that operates on FriendDB tables by simply inheriting the BaseModel and passing it in FriendDB.class Now you have the ability to add, delete, and check FriendDB tables.
If you want another special feature, such as getManFriend() getting a boy friend or getWomenFriend() getting a girl friend, add it directly into the Friend Model.

public class FriendModel extends BaseModel {

    public FriendModel() {
        super(FriendDB.class);
    }

    @Override
    public String getTag() {
        return "FriendModel";
    }
}

summary

I've seen other people encapsulate databases based on hibernate before, which is very convenient to use, so I want to try it out. BaseModel encapsulates some operations common to tables in the parent class, and the children just need to inherit from BaseModel to use them conveniently.

Posted by Misticx on Sun, 28 Jun 2020 09:56:14 -0700