package org.litepal.crud;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import org.litepal.util.BaseUtility;
import org.litepal.util.LogUtil;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

class QueryHandler extends DataHandler {

	QueryHandler(SQLiteDatabase db) {
		mDatabase = db;
	}

	<T> T onFind(Class<T> modelClass, long id) {
		try {
			Constructor<?> constructor = findBestSuitConstructor(modelClass);
			T con = (T)constructor.newInstance(getConstructorParams(constructor));
			List<Field> supportedFields = getSupportedFields(modelClass.getName());
			String tableName = getTableName(modelClass);
			Cursor cursor = mDatabase.query(tableName, null, "id = " + id, null, null, null, null);
			if (cursor.moveToFirst()) {
				for (Field field : supportedFields) {
					String methodName = genGetColumnMethod(field);
					LogUtil.d(TAG, "method name is " + methodName);
					int columnIndex = cursor.getColumnIndex(BaseUtility.changeCase(field.getName()));
					LogUtil.d(TAG, "field name is " + field.getName() + " column index is "
							+ columnIndex);
					if (columnIndex != -1) {
						Class<?> cursorClass = cursor.getClass();
						Method method = cursorClass.getMethod(methodName, int.class);
						Object object = method.invoke(cursor, columnIndex);
						LogUtil.d(TAG, "object is " + object);
					}
				}
			}
			cursor.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private String genGetColumnMethod(Field field) {
		String fieldType;
		if (field.getType().isPrimitive()) {
			fieldType = BaseUtility.capitalize(field.getType().getName());
		} else {
			fieldType = field.getType().getSimpleName();
		}
		String methodName = "get" + fieldType;
		return methodName;
	}

}

@@ -94,60 +94,8 @@
 ld;%0A
-import java.lang.reflect.InvocationTargetException;%0A
 impo
@@ -527,19 +527,50 @@
 %0A%09%09%09
-T con = (T)
+DataSupport modelInstance = (DataSupport) 
 cons
@@ -576,16 +576,22 @@
 structor
+%0A%09%09%09%09%09
 .newInst
@@ -1048,24 +1048,32 @@
 dex = cursor
+%0A%09%09%09%09%09%09%09
 .getColumnIn
@@ -1377,22 +1377,21 @@
 %09Object 
-object
+value
  = metho
@@ -1447,28 +1447,525 @@
 G, %22
-object is %22 + object
+value is %22 + value);%0A%09%09%09%09%09%09LogUtil.d(TAG, %22type is %22 + field.getType());%0A%09%09%09%09%09%09if (field.getType() == boolean.class %7C%7C field.getType() == Boolean.class) %7B%0A%09%09%09%09%09%09%09if (%220%22.equals(String.valueOf(value))) %7B%0A%09%09%09%09%09%09%09%09value = false;%0A%09%09%09%09%09%09%09%7D else if (%221%22.equals(String.valueOf(value))) %7B%0A%09%09%09%09%09%09%09%09value = true;%0A%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%7D else if (field.getType() == char.class%0A%09%09%09%09%09%09%09%09%7C%7C field.getType() == Character.class) %7B%0A%09%09%09%09%09%09%09value = ((String) value).charAt(0);%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09putSetMethodValueByField(modelInstance, field, value
 );%0A%09
@@ -2000,16 +2000,45 @@
 lose();%0A
+%09%09%09return (T) modelInstance;%0A
 %09%09%7D catc
@@ -2161,25 +2161,64 @@
 %09String 
-fieldType
+typeName;%0A%09%09Class%3C?%3E fieldType = field.getType()
 ;%0A%09%09if (
@@ -2222,26 +2222,20 @@
 f (field
-.get
 Type
-()
 .isPrimi
@@ -2243,32 +2243,31 @@
 ive()) %7B%0A%09%09%09
-fieldTyp
+typeNam
 e = BaseUtil
@@ -2282,34 +2282,28 @@
 talize(field
-.get
 Type
-()
 .getName());
@@ -2317,24 +2317,23 @@
 se %7B%0A%09%09%09
-fieldTyp
+typeNam
 e = fiel
@@ -2333,26 +2333,20 @@
  = field
-.get
 Type
-()
 .getSimp
@@ -2393,18 +2393,162 @@
 %22 + 
-fieldType;
+typeName;%0A%09%09if (%22getBoolean%22.equals(methodName)) %7B%0A%09%09%09methodName = %22getInt%22;%0A%09%09%7D else if (%22getChar%22.equals(methodName)) %7B%0A%09%09%09methodName = %22getString%22;%0A%09%09%7D
 %0A%09%09r

Add support query object containing char type.