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

com.ivanceras.db.shared.DAOArray Maven / Gradle / Ivy

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

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

import com.ivanceras.db.shared.exception.DAOArrayException;

public class DAOArray {
	
	String modelName;
	String[] attributes;
	List data = new ArrayList();
	
	public void add(DAO dao) throws DAOArrayException{
		if(dao == null){
			return;
		}
		if(modelName == null){
			Map prop = dao.getProperties();
			modelName = dao.getModelName();
			attributes = new String[prop.size()];
			int index = 0;
			for(Entry entry : prop.entrySet()){
				attributes[index] = entry.getKey();
				index++;
			}
		}
		if(dao.getModelName() != modelName){
			throw new DAOArrayException("Can not contain dao from different models");
		}
		Object[] rows = new Object[attributes.length];
		for(int i = 0; i < attributes.length; i++){
			rows[i] = dao.get_Value(attributes[i]);
		}
		data.add(rows);
		
	}
	
	/**
	 * convert a list of dao into this format
	 * @param daoList
	 * @return
	 * @throws DAOArrayException
	 */
	public DAOArray fromDao(DAO[] daoList) throws DAOArrayException{
		for(DAO dao : daoList){
			add(dao);
		}
		return this;
	}
	
	
	/**
	 * From this format convert back to DAO
	 * @return
	 */
	public DAO[] convert(){
		int size = data.size();
		DAO[] daoList = new DAO[size];
		
		for(int i = 0; i < size; i++){
			Object[] dat = data.get(i);
			DAO dao = new DAO(modelName);
			for(int j = 0; j < attributes.length; j++){
				dao.set_Value(attributes[j], dat[j]);
			}
			daoList[i] = dao;
		}
		return daoList;
	}
	
	public int size(){
		return data.size();
	}
	

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy