package info.breezes.orm.utils;

import android.database.Cursor;
import android.util.Log;
import info.breezes.orm.OrmConfig;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Qiao on 2014/5/27.
 */
public class CursorUtils {
    public static <T> T readCurrentEntity(Class<T> type, Cursor cursor,Map<String,Integer> columnIndex) {
        long st=System.currentTimeMillis();
        try {
            T entity = type.newInstance();
            Field fields[] = type.getFields();
            for (Field field : fields) {
                String columnName = TableUtils.getColumnName(field);
                Class<?> fieldType = field.getType();
                Object value = OrmConfig.getTranslator(fieldType).readColumnValue(cursor, columnIndex.get(columnName), field);
                if (value != null) {
                    field.set(entity, value);
                }
            }
            Log.d("CursorUtils","readCurrentEntity cost:"+(System.currentTimeMillis()-st));
            return entity;
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> List<T> readEntities(Class<T> type, Cursor cursor,Map<String,Integer> columnIndex) {
        ArrayList<T> arrayList = new ArrayList<T>();
        while (cursor.moveToNext()) {
            arrayList.add(readCurrentEntity(type, cursor,columnIndex));
        }
        return arrayList;
    }

    public static <T> T readCurrentEntity(Class<T> type, Cursor cursor) {
        long st=System.currentTimeMillis();
        try {
            T entity = type.newInstance();
            Field fields[] = type.getFields();
            for (Field field : fields) {
                String columnName = TableUtils.getColumnName(field);
                Class<?> fieldType = field.getType();
                Object value = OrmConfig.getTranslator(fieldType).readColumnValue(cursor, cursor.getColumnIndex(columnName), field);
                if (value != null) {
                    field.set(entity, value);
                }
            }
            Log.d("CursorUtils","readCurrentEntity cost:"+(System.currentTimeMillis()-st));
            return entity;
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}

@@ -58,16 +58,47 @@
 Cursor;%0A
+import android.graphics.Color;%0A
 import a
@@ -148,16 +148,59 @@
 mConfig;
+%0Aimport info.breezes.orm.annotation.Column;
 %0A%0Aimport
@@ -430,32 +430,33 @@
 , Cursor cursor,
+ 
 Map%3CString,Integ
@@ -442,32 +442,33 @@
 sor, Map%3CString,
+ 
 Integer%3E columnI
@@ -482,33 +482,35 @@
 %0A        long st
-=
+ = 
 System.currentTi
@@ -658,32 +658,141 @@
 eld : fields) %7B%0A
+                Column column = field.getAnnotation(Column.class);%0A                if (column != null) %7B%0A    
                 
@@ -833,35 +833,47 @@
 ColumnName(field
-);%0A
+, column);%0A    
                 
@@ -918,32 +918,36 @@
                 
+  
+  
 Object value = O
@@ -1049,32 +1049,36 @@
                 
+    
 if (value != nul
@@ -1074,32 +1074,36 @@
 alue != null) %7B%0A
+    
                 
@@ -1124,32 +1124,54 @@
 entity, value);%0A
+                    %7D%0A
                 
@@ -1210,32 +1210,33 @@
 d(%22CursorUtils%22,
+ 
 %22readCurrentEnti
@@ -1230,39 +1230,61 @@
 adCurrentEntity 
+With ColumnIndexMap 
 cost:%22
-+
+ + 
 (System.currentT
@@ -1286,33 +1286,35 @@
 rentTimeMillis()
--
+ - 
 st));%0A          
@@ -1595,16 +1595,17 @@
  cursor,
+ 
 Map%3CStri
@@ -1607,16 +1607,17 @@
 %3CString,
+ 
 Integer%3E
@@ -1780,16 +1780,17 @@
  cursor,
+ 
 columnIn
@@ -1928,17 +1928,19 @@
  long st
-=
+ = 
 System.c
@@ -2096,32 +2096,141 @@
 eld : fields) %7B%0A
+                Column column = field.getAnnotation(Column.class);%0A                if (column != null) %7B%0A    
                 
@@ -2290,32 +2290,36 @@
                 
+    
 Class%3C?%3E fieldTy
@@ -2348,32 +2348,36 @@
                 
+ 
+   
 Object value = O
@@ -2485,32 +2485,36 @@
                 
+    
 if (value != nul
@@ -2510,32 +2510,36 @@
 alue != null) %7B%0A
+    
                 
@@ -2576,34 +2576,42 @@
                 
-%7D%0A
+    %7D%0A    
             %7D%0A  
@@ -2600,32 +2600,46 @@
               %7D%0A
+            %7D%0A
             Log.
@@ -2654,16 +2654,17 @@
 rUtils%22,
+ 
 %22readCur
@@ -2680,17 +2680,19 @@
 y cost:%22
-+
+ + 
 (System.
@@ -2710,17 +2710,19 @@
 Millis()
--
+ - 
 st));%0A  

fix bugs