package org.jerrymouse.database.hadoop.jpa;

import java.io.File;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HadoopBean {
	private static Log log = LogFactory.getLog(HadoopBean.class);

	List<Field> fields;

	Field id;
	Class type;

	public HadoopBean(Class type) {
		this.type = type;
		try {
			fields = new ArrayList<Field>();
			Field[] tempFields = type.getDeclaredFields();
			for (Field field : tempFields) {
				field.setAccessible(true);
				if (field.isAnnotationPresent(Id.class)) {
					id = field;
				} else if (field.isAnnotationPresent(Column.class)) {
					fields.add(field);
				}
			}
		} catch (SecurityException e) {
			e.printStackTrace();
		}
	}

	public List<Field> getFields() {
		return fields;
	}

	String getTableName() {
		return type.getName();
	}

	public static List<Class> scanEntity(String packageName) {
		List<Class> classes = new ArrayList<Class>();
		Set<Class<?>> classSet = ClassPathUtil.getClasses(packageName);
		for (Class c : classSet) {
			if (c.isAnnotationPresent(Entity.class))
				classes.add(c);
		}
		return classes;
	}

}
