/*
 * 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 BaleBean implements BaleBeanRemote {

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

                        Baleent baleent=new Baleent();

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

                        baleent.setBalsize(balsize);

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

		}catch(Exception ex){

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

			ex.printStackTrace();
		}
   }


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

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

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

             List<Baleent> baleent=null;

		try{
			baleent=(List<Baleent>) 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 baleent;
	}//end of findbalebyname


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

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

                 return baleent;
                }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<Baleent> findBalebyDescript(String description){
             List<Baleent> baleent=null;

		try{

			baleent=(List<Baleent>) 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 baleent;
	}//end of DescriptItem


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

                 if (param!=null){
		try{
			
			baleent=(List<Baleent>) 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 baleent;
	}//end of Despcodename

}
