package org.amc.dao;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.amc.DAOException;
import org.amc.model.Material;
import org.amc.servlet.action.search.WebFormSearch;
import org.apache.log4j.Logger;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

/**
 * 
 * @author Adrian Mclaughlin
 * @version 1
 */
public class MaterialDAO extends DAO<Material> implements Serializable
{
	private static final long serialVersionUID = -4397260307883862647L;
	private static Logger logger=Logger.getLogger(MaterialDAO.class);
	
	public MaterialDAO()
	{
		super(Material.class);
	}


	public Map<Integer, Material> findMaterials(String col, String value) throws DAOException
	{
		EntityManager em=getEntityManager();
		Map<Integer, Material> list;
		try
		{
			Query q=em.createQuery("Select x from Material x where x."+col+"='"+value+"'");
			list = new HashMap<Integer, Material>();
			List<Material> resultList=q.getResultList();
			for(Material m:resultList)
			{
				list.put(m.getId(),m);
			}
		}
		catch(PersistenceException pe)
		{
			em.getTransaction().rollback();
			logger.error("DAO<"+getEntityClass().getSimpleName()+">:Error has occurred when trying to find entities");
			throw new DAOException(pe);
		}
		return list;
	}

	public Map<Integer, Material> findMaterials() throws DAOException
	{
		EntityManager em=getEntityManager();
		Map<Integer, Material> list;
		try
		{
			Query q=em.createQuery("Select x from Material x ORDER BY x.id");
			list = new TreeMap<Integer, Material>();
			List<Material> resultList=q.getResultList();
			for(Material m:resultList)
			{
				list.put(m.getId(),m);
			}
		}
		catch(PersistenceException pe)
		{
			em.getTransaction().rollback();
			logger.error("DAO<"+getEntityClass().getSimpleName()+">:Error has occurred when trying to find entities");
			throw new DAOException(pe);
		}
		return list;
	}
	
	
	public Map<Integer,Material> findMaterials(WebFormSearch search) throws DAOException
	{
		Map<Integer, Material> list=new TreeMap<Integer,Material>();
		List<Material> materials=super.findEntities(search);
		for(Material m:materials)
		{
			list.put(m.getId(),m);
		}
		return list;
	}

}


	

@@ -48,33 +48,8 @@
 le;%0A
-import java.util.HashMap;
 %0Aimp
@@ -233,167 +233,41 @@
 ch;%0A
-import org.apache.log4j.Logger;%0A%0Aimport javax.persistence.EntityManager;%0Aimport javax.persistence.PersistenceException;%0Aimport javax.persistence.Query;%0A%0A/**%0A *
+/**%0A * Material Data Acess Object
  %0A *
@@ -457,76 +457,8 @@
 7L;%0A
-%09private static Logger logger=Logger.getLogger(MaterialDAO.class);%0A%09
 %0A%09pu
@@ -609,187 +609,46 @@
 %7B%0A%09%09
-EntityManager em=getEntityManager();%0A%09%09Map%3CInteger, Material%3E list;%0A%09%09try%0A%09%09%7B%0A%09%09%09Query q=em.createQuery(%22Select x from Material x where x.%22+col+%22='%22+value+%22'%22);%0A%09%09%09list = new Hash
+Map%3CInteger, Material%3E list = new Tree
 Map%3C
@@ -663,33 +663,32 @@
  Material%3E();%0A%09%09
-%09
 List%3CMaterial%3E r
@@ -697,36 +697,47 @@
 ultList=
-q.getResultList(
+super.findEntities(col,value
 );%0A
-%09
 %09%09for(Ma
@@ -753,33 +753,31 @@
 resultList)%0A
-%09
 %09%09%7B%0A%09%09%09
-%09
 list.put(m.g
@@ -793,563 +793,215 @@
 ;%0A%09%09
-%09%7D%0A%09%09%7D%0A%09%09catch(PersistenceException pe)%0A%09%09%7B%0A%09%09%09em.getTransaction().rollback();%0A%09%09%09logger.error(%22DAO%3C%22+getEntityClass().getSimpleName()+%22%3E:Error has occurred when trying to find entities%22);%0A%09%09%09throw new DAOException(pe);%0A%09%09%7D%0A%09%09return list;%0A%09%7D%0A%0A%09public Map%3CInteger, Material%3E findMaterials() throws DAOException%0A%09%7B%0A%09%09EntityManager em=getEntityManager();%0A%09%09Map%3CInteger, Material%3E list;%0A%09%09try%0A%09%09%7B%0A%09%09%09Query q=em.createQuery(%22Select x from Material x ORDER BY x.id%22);%0A%09%09%09list = new TreeMap%3CInteger, Material%3E();%0A%09%09%09List%3CMaterial%3E resultList=q.getResultList();%0A%09
+%7D%0A%09%09return list;%0A%09%7D%0A%0A%09public Map%3CInteger, Material%3E findMaterials() throws DAOException%0A%09%7B%0A%09%09List%3CMaterial%3E resultList=super.findEntities();%0A%09%09Map%3CInteger, Material%3E list = new TreeMap%3CInteger, Material%3E();%0A
 %09%09fo
@@ -1029,17 +1029,15 @@
 st)%0A
-%09
 %09%09%7B%0A%09%09%09
-%09
 list
@@ -1059,230 +1059,8 @@
 m);%0A
-%09%09%09%7D%0A%09%09%7D%0A%09%09catch(PersistenceException pe)%0A%09%09%7B%0A%09%09%09em.getTransaction().rollback();%0A%09%09%09logger.error(%22DAO%3C%22+getEntityClass().getSimpleName()+%22%3E:Error has occurred when trying to find entities%22);%0A%09%09%09throw new DAOException(pe);%0A
 %09%09%7D%0A

Add findMaterials(Search), Refactored other methods to use the super class methods for accessing the database