/* * UsersbeanBean.java * * Created on 02 February 2007, 14:32 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package machinedetails2; import machinedetails2.*; import javax.ejb.EJBException; import javax.ejb.Stateful; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; /** * * @author eve.pokua */ @Stateful //@Stateful(name="usersb", mappedName="UsersbJNDI") public class UsersBean implements UsersRemote { //refer to the persistence unit with a persistenceContext @PersistenceContext(name="MACHINEDETAILS-ejbPU") EntityManager em; /** Creates a new instance of UsersbeanBean */ public UsersBean() { } public void createUser() { try{ Usersent userent = new Usersent(); userent.setUserid("dsfgwer"); userent.setFirstname("asdfasdgf"); userent.setSurname("sdfasdfasdf"); userent.setPassword("uqwer"); em.persist(userent); //} } catch (Exception ex) { System.err.println("Caught an exception: data not inserted into db"); throw new EJBException(ex); } } public void createUser(String userid, String firstname, String surname, String password) { try{ Usersent usersent= new Usersent(); //(userid, Firstname,Surname,Password ); usersent.setUserid(userid); usersent.setFirstname(firstname); usersent.setSurname(surname); usersent.setPassword(password); em.persist(usersent); }catch(Exception ex){ System.err.print("can not insert new user details into the DB"); ex.printStackTrace(); } } public Usersent getUserlogindet(String userid) { //Check the user id exists in the DB //if //find this user Usersent results=null; try{ //Usersent results =em.find(Usersent.class, new String(userid)); //Usersent usersent =em.find(Usersent.class, new String(Password)); //usersent =new Usersent( //usersent.getUserid(), //usersent.getPassword()); //results =new Usersent( //results.getUserid(), //results.getPassword()); }catch(Exception ex){ System.err.println("User is not allowed to use this program"); ex.printStackTrace(); } return results; //return usersent; } /**call the Usersent contructor, using the getUserlogindet defined in remote interface, retrive the user's details after login*/ public Usersent getUserlogindet(String userid, String Password) { //Check the user id exists in the DB //if //find this user // Usersent results= null; //new Usersent(); //try{ Usersent usersent =em.find(Usersent.class, new String(userid)); //Usersent usersent =em.find(Usersent.class, new String(Password)); // usersent =new Usersent( // usersent.getUserid(), //usersent.getPassword()); //}catch(Exception ex){ //System.err.println("User is not allowed to use this program"); //ex.printStackTrace(); //} return usersent; } public Usersent searchbyusername(String UserID){ //Usersent usersent=null; try{ Usersent usersent=em.find(Usersent.class,new String(UserID)); //usersent=(Usersent) em.createNamedQuery("Machinedetail2.Usersent.findMachinebyuser").setParameter("UserID" ,UserID).getSingleResult(); return usersent; }catch(Exception ex){ throw new EJBException(ex);} } }