// Path: onam/src/main/java/com/basilgregory/onam/android/DbUtil.java
// static String getColumnName(Field field){
//     return getColumnName(getMethod("get", field));
// }
// 
// Path: onam/src/main/java/com/basilgregory/onam/android/DbUtil.java
// static String getMappingForeignColumnNameClass(Class aClass){
//     return aClass.getSimpleName().toLowerCase()+"_id";
// }


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.basilgregory.onam.annotations.AfterCreate;
import com.basilgregory.onam.annotations.AfterUpdate;
import com.basilgregory.onam.annotations.BeforeCreate;
import com.basilgregory.onam.annotations.BeforeUpdate;
import com.basilgregory.onam.annotations.ManyToMany;
import com.basilgregory.onam.annotations.OneToMany;
import com.basilgregory.onam.annotations.Table;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.basilgregory.onam.android.DbUtil.getColumnName;
import static com.basilgregory.onam.android.DbUtil.getMappingForeignColumnNameClass;
        DbUtil.invokeSetter(entity,DbUtil.getSetterMethod(method),entities);
        return entities;

    }

    /**
     * Mapping table name has to be provided by the user.
     * Foreignkey column names will be auto generated using #{getMappingForeignColumnNameClass}.
     * @param entity
     * @param holderClass
     * @param method
     * @return
     */
    private List<Entity> findManyToManyRelatedEntities(ManyToMany manyToMany,Entity entity, Object holderClass
            , Method method ) {
        Cursor cursor =  findByProperty(manyToMany.tableName(),
                getMappingForeignColumnNameClass(entity.getClass()),entity.getId(),null,null);
        List<Entity> entities  = EntityBuilder.getEntityFromMappingTable(cursor,holderClass.getClass().getSuperclass());
        DbUtil.invokeSetter(entity,DbUtil.getSetterMethod(method),entities);
        return entities;
    }

    private void fetchAndSetFirstDegreeRelatedObject(Entity entity) {
        if (entity == null) return;
        Field[] fields = entity.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (field.getType().getAnnotation(Table.class) == null) continue;
            try {
                if (DbUtil.getFetchType(field) != FetchType.EAGER)
                    continue; //Fetching related entity only if fetch type is eager.
// Path: onam/src/main/java/com/basilgregory/onam/android/DbUtil.java
// static String getColumnName(Field field){
//     return getColumnName(getMethod("get", field));
// }
// 
// Path: onam/src/main/java/com/basilgregory/onam/android/DbUtil.java
// static String getMappingForeignColumnNameClass(Class aClass){
//     return aClass.getSimpleName().toLowerCase()+"_id";
// }


// Path: onam/src/main/java/com/basilgregory/onam/android/DBExecutor.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.basilgregory.onam.annotations.AfterCreate;
import com.basilgregory.onam.annotations.AfterUpdate;
import com.basilgregory.onam.annotations.BeforeCreate;
import com.basilgregory.onam.annotations.BeforeUpdate;
import com.basilgregory.onam.annotations.ManyToMany;
import com.basilgregory.onam.annotations.OneToMany;
import com.basilgregory.onam.annotations.Table;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.basilgregory.onam.android.DbUtil.getColumnName;
import static com.basilgregory.onam.android.DbUtil.getMappingForeignColumnNameClass;

        DbUtil.invokeSetter(entity,DbUtil.getSetterMethod(method),entities);
        return entities;

    }

    /**
     * Mapping table name has to be provided by the user.
     * Foreignkey column names will be auto generated using #{getMappingForeignColumnNameClass}.
     * @param entity
     * @param holderClass
     * @param method
     * @return
     */
    private List<Entity> findManyToManyRelatedEntities(ManyToMany manyToMany,Entity entity, Object holderClass
            , Method method ) {
        Cursor cursor =  findByProperty(manyToMany.tableName(),
                getMappingForeignColumnNameClass(entity.getClass()),entity.getId(),null,null);
        List<Entity> entities  = EntityBuilder.getEntityFromMappingTable(cursor,holderClass.getClass().getSuperclass());
        DbUtil.invokeSetter(entity,DbUtil.getSetterMethod(method),entities);
        return entities;
    }

    private void fetchAndSetFirstDegreeRelatedObject(Entity entity) {
        if (entity == null) return;
        Field[] fields = entity.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (field.getType().getAnnotation(Table.class) == null) continue;
            try {
                if (DbUtil.getFetchType(field) != FetchType.EAGER)
                    continue; //Fetching related entity only if fetch type is eager.
                String foreignKeyColumn = getColumnName(field);