/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entityJB;

import java.util.*;
import javax.ejb.*;
import javax.ejb.Stateful;
import javax.persistence.*;

/**
 *
 * @author eve
 */
@Stateful
public class BaleBean2 implements BaleBeanRemote2 {

   @PersistenceContext(unitName="TestingUN")
   EntityManager em;
   public void createBale(String contid,String balname, String description, String colour, double price, String balsize){
                             
                try{

                        Baleent2 Baleent2=new Baleent2();

                        Baleent2.setBalname(balname);
                        Baleent2.setDescription(description);
                        Baleent2.setColour(colour);
                        Baleent2.setPrice(price);

                        Baleent2.setBalsize(balsize);

                   //a call to the persit manager will result in inserting the argument
			
			em.persist(Baleent2);

		}catch(Exception ex){

			System.err.println("Can not insert new bale details");

			ex.printStackTrace();
		}
   }


    //retrieving by it's bale ID
	public Baleent2 findBalesbyid(Long balid){
            Baleent2 Baleent2=new Baleent2();
		try{
			 Baleent2=em.find(Baleent2.class, balid);
		}catch(Exception ex){
			System.err.println("Can not access DB");

		}//end of catch
                 return Baleent2;
	}//end of  findBalebyid()

        //retrieving bale by name
	public List<Baleent2> findBalebyname(String balname){

             List<Baleent2> Baleent2=null;

		try{
			Baleent2=(List<Baleent2>) em.createNamedQuery("getBaleswithname").setParameter("balname", balname).getResultList();

		}catch(Exception ex){
			System.err.println("Can not get bale details by name. Name entered not recogniced");

		}//end of catch

             return Baleent2;
	}//end of findbalebyname


            //To retrieve all bales
        public List<Baleent2> findAllBales(){
            List<Baleent2> Baleent2=null;

		try{
                   Baleent2=(List<Baleent2>)em.createNamedQuery("getAllBales").getResultList();

                 return Baleent2;
                }catch (Exception ex) {

                    System.err.println("Can not get all bales");
            throw new EJBException(ex);
        }
        }//end of All bales

        

        //retrieving bales by description
    public List<Baleent2> findBalebyDescript(String description){
             List<Baleent2> Baleent2=null;

		try{

			Baleent2=(List<Baleent2>) em.createNamedQuery("getBalesbydescption").setParameter("description", description).getResultList();

		}catch(Exception ex){
			System.err.println("Can not get bales details by Description entered. Name entered not recogniced");

		}//end of catch

             return Baleent2;
	}//end of DescriptItem


     //retrieving bales by description/ bale code or bales name
     public List<Baleent2> findbyDespcodename(String param){
             List<Baleent2> Baleent2=null;

                 if (param!=null){
		try{
			
			Baleent2=(List<Baleent2>) em.createNamedQuery("getBalesbydescodename").setParameter("param", param.toUpperCase()).getResultList();
                       
		}catch(Exception ex){
			System.err.println("Can not get bales details by Description/code or name entered. Name entered not recognised");}}


                
             return Baleent2;
	}//end of Despcodename

}
