All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.venky.swf.db.model.io.ModelIO Maven / Gradle / Ivy

The newest version!
package com.venky.swf.db.model.io;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.venky.core.string.StringUtil;
import com.venky.reflection.BeanIntrospector;
import com.venky.swf.db.Database;
import com.venky.swf.db.model.Model;
import com.venky.swf.db.model.reflection.ModelReflector;
import com.venky.swf.db.model.reflection.ModelReflector.FieldGetterMissingException;

public class ModelIO extends BeanIntrospector{ 

	protected ModelIO(Class beanClass) {
		super(beanClass);
		ref = ModelReflector.instance(getBeanClass());
		for (String field: ref.getFields() ){
			String attributeName = StringUtil.camelize(field);
			attributeFieldMap.put(attributeName, field);
			fieldAttributeMap.put(field, attributeName);
		}
	}
	
	private Map fieldAttributeMap = new HashMap();
	private Map attributeFieldMap = new HashMap();
	
	public Set getFields(){
		return fieldAttributeMap.keySet();
	}
	
	public Set getAttributes(){
		return attributeFieldMap.keySet();
	}
	
	protected String getAttributeName(String fieldName){
		return fieldAttributeMap.get(fieldName);
	}

	protected String getFieldName(String attributeName){
		return attributeFieldMap.get(attributeName);
	}


	private ModelReflector ref = null;
	protected ModelReflector getReflector(){
		return ref;
	}

	protected M createInstance() {
		return Database.getTable(getBeanClass()).newRecord();
	}

	protected static enum GetterType {
		FIELD_GETTER, REFERENCE_MODEL_GETTER, UNKNOWN_GETTER,
	}


	protected Method getGetter(String attributeName) {
		Method m = super.getGetter(attributeName);
		if (m == null) {
			String field = getFieldName(attributeName);
			try {
				if (field != null){
					m = ref.getFieldGetter(field);
				}
			} catch (FieldGetterMissingException ex) {
				m = null;
			}
		}
		return m;
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy