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

com.ivanceras.db.model.ModelMetaData Maven / Gradle / Ivy

There is a newer version: 0.1.2
Show newest version
package com.ivanceras.db.model;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.ivanceras.db.api.DAOFactory;
import com.ivanceras.db.api.ModelDef;
import com.ivanceras.db.server.util.DAOGenerator;
import com.ivanceras.db.shared.DAO;
import com.ivanceras.db.shared.exception.DatabaseException;


/**
 * This class is inherited by a Generated Class {@link DAOGenerator#generateModelMetaData}
 * @author lee
 *
 */

public class ModelMetaData{

	
	Map, ModelDef> modelDefList = null;
	Map namedModelDefList = null;
	Map, String> modelNameListNdx = null;
	Map> classListNdx = null;
	private DAOFactory instanceProvider = null;
	
	public ModelMetaData(){
		this.modelDefList = new LinkedHashMap, ModelDef>();
		this.namedModelDefList = new LinkedHashMap();
		this.modelNameListNdx = new LinkedHashMap, String>();
		this.classListNdx = new LinkedHashMap>();
	}
	
	public ModelMetaData(Map, ModelDef> modelDefList, Map namedModelDefList, Map, String> modelNameList, Map> classList){
		this.modelDefList = modelDefList;
		this.namedModelDefList = namedModelDefList;
		this.modelNameListNdx = modelNameList;
		this.classListNdx = classList;
	}
	
	public ModelMetaData addMeta(ModelMetaData metaData1) {
		modelDefList.putAll(metaData1.modelDefList);
		modelNameListNdx.putAll(metaData1.modelNameListNdx);
		classListNdx.putAll(metaData1.classListNdx);
		return this;
	}
	
	/**
	 * 
	 * This is for adding models that are dynamic, if overwrite, then replace the current model if it exist
	 * @param model
	 * @return
	 * @throws DatabaseException
	 */
	public boolean addModel(ModelDef model, boolean overwrite) throws DatabaseException{
		String modelName = model.getModelName();
		if(!overwrite && this.namedModelDefList.containsKey(modelName)){
			throw new DatabaseException("Model ["+modelName+"] can not be added since It already exists");
		}
		else{
			this.namedModelDefList.put(modelName, model);
			return true;
		}
	}
	
	public List getModelDefinitionList(){
		List list = new ArrayList();
		for(Entry, ModelDef> entry : modelDefList.entrySet()){
			list.add(entry.getValue());
		}
		return list;
	}
	

	
	public ModelDef getDefinition(Class daoClass){
		if(modelDefList == null){
			System.out.println("Model definition List is not set!");
		}
		if(modelDefList.containsKey(daoClass)){
			return modelDefList.get(daoClass);
		}
		else{
			String msg = "No model definition found for ["+daoClass+"]";
			try {
				throw new DatabaseException(msg);
			} catch (DatabaseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public Class getDaoClass(String modelName){
		if(classListNdx == null){
			System.out.println("Class List Index is not set");
		}
		if(classListNdx.containsKey(modelName)){
			return classListNdx.get(modelName);
		}else{
			System.out.println("No DAO class index found for "+modelName);
		}
		return null;
	}

	public ModelDef getDefinition(String modelName){
		if(namedModelDefList == null){
			System.out.println("Model definition List is not set!");
		}
		if(namedModelDefList.containsKey(modelName)){
			return namedModelDefList.get(modelName);
		}
		else{
			String msg = "No model definition found for ["+modelName+"]";
			try {
				throw new DatabaseException(msg);
			} catch (DatabaseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getModelName(Class daoClass) {
		return modelNameListNdx.get(daoClass);
	}

	public DAOFactory getInstanceProvider() {
		return this.instanceProvider;
	}
	public void setInstanceProvider(DAOFactory ip) {
		this.instanceProvider = ip;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy