/**
* Copyright (c) 2012-2013, Michael Yang ��� (www.yangfuhai.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.tsz.afinal.db.table;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @title ����
* @description �����������ġ������������͡� ��������
* @company ̽�������繤����(www.tsz.net)
* @author michael Young (www.YangFuhai.com)
* @version 1.0
* @created 2012-10-10
*/
public class Property {
private String fieldName;
private String column;
private String defaultValue;
private Class<?> dataType;
private Method get;
private Method set;
public void setValue(Object receiver , Object value){
if(set!=null){
try {
if (dataType == String.class) {
set.invoke(receiver, value.toString());
} else if (dataType == int.class || dataType == Integer.class) {
set.invoke(receiver, value == null ? (Integer) null : Integer.parseInt(value.toString()));
} else if (dataType == float.class || dataType == Float.class) {
set.invoke(receiver, value == null ? (Float) null: Float.parseFloat(value.toString()));
} else if (dataType == long.class || dataType == Long.class) {
set.invoke(receiver, value == null ? (Long) null: Long.parseLong(value.toString()));
} else if (dataType == Date.class) {
set.invoke(receiver, value == null ? (Date) null: stringToDateTime(value.toString()));
} else {
set.invoke(receiver, value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* ��ȡij��ʵ��ִ��ij�������Ľ��
* @param obj
* @param method
* @return
*/
@SuppressWarnings("unchecked")
public <T> T getValue(Object obj){
if(obj != null && get != null) {
try {
return (T)get.invoke(obj);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return null;
}
private static Date stringToDateTime(String strDate) {
if (strDate != null) {
try {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
return null;
}
public String getFieldName() {
return fieldName;
}
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
public String getColumn() {
return column;
}
public void setColumn(String column) {
this.column = column;
}
public String getDefaultValue() {
return defaultValue;
}
public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}
public Class<?> getDataType() {
return dataType;
}
public void setDataType(Class<?> dataType) {
this.dataType = dataType;
}
public Method getGet() {
return get;
}
public void setGet(Method get) {
this.get = get;
}
public Method getSet() {
return set;
}
public void setSet(Method set) {
this.set = set;
}
}