Powered by Blogger.

Archive for February 2014

CRUD

------------------------------------------------------------------------------------------------------------


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.Mk.bean;

import com.Mk.dao.customerDao;
import com.Mk.daoImpl.customerDaoImpl;
import com.Mk.entity.Customer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.RowEditEvent;
import org.primefaces.model.LazyDataModel;


@ManagedBean(name = "customer")
@SessionScoped
public class customerBean {

    private String customerName;
    private String customerEmail;
    private String customerPhoneNo;
    private String customerAddress;
    private String customerCity;
   private String customerPincode;
  private String customerState;
  private String customerCountry;
  private String customerWebsite;
 private String customerComments;


 private Customer selectedcust;

    public Customer getSelectedcust() {
        return selectedcust;
    }

    public void setSelectedcust(Customer selectedcust) {
     
        this.selectedcust = selectedcust;
    }


    public String getCustomerAddress() {
        return customerAddress;
    }

    public void setCustomerAddress(String customerAddress) {
        this.customerAddress = customerAddress;
    }

    public String getCustomerCity() {
        return customerCity;
    }

    public void setCustomerCity(String customerCity) {
        this.customerCity = customerCity;
    }

    public String getCustomerPincode() {
        return customerPincode;
    }

    public void setCustomerPincode(String customerPincode) {
        this.customerPincode = customerPincode;
    }

    public String getCustomerState() {
        return customerState;
    }

    public void setCustomerState(String customerState) {
        this.customerState = customerState;
    }

    public String getCustomerCountry() {
        return customerCountry;
    }

    public void setCustomerCountry(String customerCountry) {
        this.customerCountry = customerCountry;
    }

    public String getCustomerWebsite() {
        return customerWebsite;
    }

    public void setCustomerWebsite(String customerWebsite) {
        this.customerWebsite = customerWebsite;
    }

    public String getCustomerComments() {
        return customerComments;
    }

    public void setCustomerComments(String customerComments) {
        this.customerComments = customerComments;
    }
 
 
    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getCustomerEmail() {
        return customerEmail;
    }

    public void setCustomerEmail(String customerEmail) {
        this.customerEmail = customerEmail;
    }

    public String getCustomerPhoneNo() {
        return customerPhoneNo;
    }

    public void setCustomerPhoneNo(String customerPhoneNo) {
        this.customerPhoneNo = customerPhoneNo;
    }
 
    private void addMessage(FacesMessage message) {
        FacesContext.getCurrentInstance().addMessage(null, message);

    }
 
 
 
    public void addCustomer() {
        customerDao dao=new customerDaoImpl();
        Customer cust=new Customer();
        cust.setCustomerName(customerName);
        cust.setCustomerEmail(customerEmail);
        cust.setCustomerPhoneNo(customerPhoneNo);
        cust.setCustomerAddress(customerAddress);
        cust.setCustomerCity(customerCity);
        cust.setCustomerComments(customerComments);
        cust.setCustomerPincode(customerPincode);
        cust.setCustomerWebsite(customerWebsite);
        cust.setCustomerCountry(customerCountry);
        cust.setCustomerState(customerState);
        dao.insertCustomer(cust);
         addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, customerName
                        + " Details Was Saved Successfully!!!", null));
    }
 
    public void resetCustomer() {
        this.customerName=null;
        this.customerEmail=null;
        this.customerPhoneNo=null;
        this.customerAddress=null;
        this.customerCity=null;
        this.customerComments=null;
        this.customerPincode=null;
        this.customerWebsite=null;
        this.customerCountry=null;
        this.customerState=null;
     
    }
 
    private List<Customer> allCustomer;

 public List<Customer> findAll() {
        allCustomer=new ArrayList<Customer>();
         customerDao dao=new customerDaoImpl();
        allCustomer=dao.viewCustomer();
        return allCustomer;
     
    }
 public void editCustomer(RowEditEvent event) {
     
 customerDao dao=new customerDaoImpl();
       dao.editCustomer((Customer) event.getObject());
     
 }

     @SuppressWarnings("empty-statement")
     public void onEdit(RowEditEvent event) {
        FacesMessage msg = new FacesMessage("Customer Edited", ((Customer) event.getObject()).getCustomerName());
        customerDao dao=new customerDaoImpl();
       dao.editCustomer((Customer) event.getObject());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
   
    public void onCancel(RowEditEvent event) {
        FacesMessage msg = new FacesMessage("Customer Cancelled", ((Customer) event.getObject()).getCustomerName());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

 
   public List<Customer> remove(Customer c) {
 
        customerDao dao=new customerDaoImpl();
                dao.removeCustomer(c);            
               allCustomer=dao.viewCustomer();
        return allCustomer;
    }
 

}


------------------------------------------------------------------------------------------------------------


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.Mk.bean;


import java.io.IOException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import javax.servlet.http.HttpSession;

/**
 *
 * @author manoj
 */
@ManagedBean(name = "login")
@SessionScoped
public class loginbean {
    
 String username;
 String Password;
//<f:event listener="#{login.sessiontest(login.username)}" type="preRenderView" />

   

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return Password;
    }

    public void setPassword(String Password) {
        this.Password = Password;
    }
 public String validate()
 {
     FacesContext fc = FacesContext.getCurrentInstance();
 String user = (String) fc.getExternalContext().getSessionMap().put("user", username);
 if ("admin".equals(username) && "admin".equals(Password))
 {  

 return "/faces/registration?faces-redirect=true";
 }
 if (!"admin".equals(username) || !"admin".equals(Password))
 {  show();}
 return null;
 }
public void show()    
{
FacesContext context = FacesContext.getCurrentInstance();  
context.addMessage(null, new FacesMessage("Login failed","Invalid Details"));
}


public String sessiontest() throws IOException
{
FacesContext fc = FacesContext.getCurrentInstance();
 String user = (String) fc.getExternalContext().getSessionMap().get("user");
if (user==null)
     redirect("login.xhtml");

}
return null;

}
private void redirect(String url) throws IOException {
        FacesContext fc = FacesContext.getCurrentInstance();
        fc.getExternalContext().redirect(url);
    }


public String logout()
{
((HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true)).invalidate();
   
return "/faces/login?faces-redirect=true";
}  

}

------------------------------------------------------------------------------------------------------------
DAO
------------------------------------------------------------------------------------------------------------
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.Mk.dao;

import com.Mk.entity.Customer;
import java.util.List;


public interface customerDao {
    
    public void insertCustomer(Customer cust);
    public List<Customer> viewCustomer();
    public void editCustomer(Customer cust);
    public List<Customer> removeCustomer(Customer cust);
   
}

------------------------------------------------------------------------------------------------------------
DAO impl
------------------------------------------------------------------------------------------------------------
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.Mk.daoImpl;

import com.Mk.dao.customerDao;
import com.Mk.entity.Customer;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;


public class customerDaoImpl implements customerDao {

    @Override
    public void insertCustomer(Customer cust) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("SampleCrudJSFPU");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(cust);
            em.getTransaction().commit();
             }
        catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
          
        } finally {
            em.close();
        }
    }

    @Override
    public List<Customer> viewCustomer() {
         EntityManagerFactory emf = Persistence.createEntityManagerFactory("SampleCrudJSFPU");
        EntityManager em = emf.createEntityManager();
        List<Customer> allCustomer=null;
        try {
            em.getTransaction().begin();
            allCustomer = em.createNamedQuery("Customer.findAll").getResultList();
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
           
        return allCustomer;
        
    }

    @Override
    public void editCustomer(Customer cust) {
         EntityManagerFactory emf = Persistence.createEntityManagerFactory("SampleCrudJSFPU");
        EntityManager em = emf.createEntityManager();
        
      
        try {
em.getTransaction().begin();
         
                     em.merge(cust);
                     em.getTransaction().commit();

} catch (Exception e) {
e.printStackTrace();
em.getTransaction().rollback();
} finally {
em.close();
}  
    }

    @Override
    public List<Customer> removeCustomer(Customer cust) {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("SampleCrudJSFPU");
    EntityManager em = emf.createEntityManager();
   List<Customer> allCustomer=null;
        try {
           
          em.getTransaction().begin();
          
            em.remove(em.find(Customer.class, cust.getCustomerid()));
            em.flush();
    
            em.getTransaction().commit();
        
             }
        catch (Exception e) {
            e.printStackTrace(); 
             em.clear();
em.close();
        }
    return allCustomer; 
}

}
------------------------------------------------------------------------------------------------------------
entity
------------------------------------------------------------------------------------------------------------
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.Mk.entity;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author manoj
 */
@Entity
@Table(name = "customer")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Customer.findAll", query = "SELECT c FROM Customer c"),
    @NamedQuery(name = "Customer.findByCustomerid", query = "SELECT c FROM Customer c WHERE c.customerid = :customerid"),
    @NamedQuery(name = "Customer.findByCustomerEmail", query = "SELECT c FROM Customer c WHERE c.customerEmail = :customerEmail"),
    @NamedQuery(name = "Customer.findByCustomerName", query = "SELECT c FROM Customer c WHERE c.customerName = :customerName"),
    @NamedQuery(name = "Customer.findByCustomerPhoneNo", query = "SELECT c FROM Customer c WHERE c.customerPhoneNo = :customerPhoneNo"),
    @NamedQuery(name = "Customer.findByCustomerAddress", query = "SELECT c FROM Customer c WHERE c.customerAddress = :customerAddress"),
    @NamedQuery(name = "Customer.findByCustomerCity", query = "SELECT c FROM Customer c WHERE c.customerCity = :customerCity"),
    @NamedQuery(name = "Customer.findByCustomerPincode", query = "SELECT c FROM Customer c WHERE c.customerPincode = :customerPincode"),
    @NamedQuery(name = "Customer.findByCustomerState", query = "SELECT c FROM Customer c WHERE c.customerState = :customerState"),
    @NamedQuery(name = "Customer.findByCustomerCountry", query = "SELECT c FROM Customer c WHERE c.customerCountry = :customerCountry"),
    @NamedQuery(name = "Customer.findByCustomerWebsite", query = "SELECT c FROM Customer c WHERE c.customerWebsite = :customerWebsite"),
    @NamedQuery(name = "Customer.findByCustomerComments", query = "SELECT c FROM Customer c WHERE c.customerComments = :customerComments")})
public class Customer implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "Customer_id")
    private Integer customerid;
    @Column(name = "customerEmail")
    private String customerEmail;
    @Column(name = "customerName")
    private String customerName;
    @Column(name = "customerPhoneNo")
    private String customerPhoneNo;
    @Column(name = "customerAddress")
    private String customerAddress;
    @Column(name = "customerCity")
    private String customerCity;
    @Column(name = "customerPincode")
    private String customerPincode;
    @Column(name = "customerState")
    private String customerState;
    @Column(name = "customerCountry")
    private String customerCountry;
    @Column(name = "customerWebsite")
    private String customerWebsite;
    @Column(name = "customerComments")
    private String customerComments;

    public Customer() {
    }

    public Customer(Integer customerid) {
        this.customerid = customerid;
    }

    public Integer getCustomerid() {
        return customerid;
    }

    public void setCustomerid(Integer customerid) {
        this.customerid = customerid;
    }

    public String getCustomerEmail() {
        return customerEmail;
    }

    public void setCustomerEmail(String customerEmail) {
        this.customerEmail = customerEmail;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getCustomerPhoneNo() {
        return customerPhoneNo;
    }

    public void setCustomerPhoneNo(String customerPhoneNo) {
        this.customerPhoneNo = customerPhoneNo;
    }

    public String getCustomerAddress() {
        return customerAddress;
    }

    public void setCustomerAddress(String customerAddress) {
        this.customerAddress = customerAddress;
    }

    public String getCustomerCity() {
        return customerCity;
    }

    public void setCustomerCity(String customerCity) {
        this.customerCity = customerCity;
    }

    public String getCustomerPincode() {
        return customerPincode;
    }

    public void setCustomerPincode(String customerPincode) {
        this.customerPincode = customerPincode;
    }

    public String getCustomerState() {
        return customerState;
    }

    public void setCustomerState(String customerState) {
        this.customerState = customerState;
    }

    public String getCustomerCountry() {
        return customerCountry;
    }

    public void setCustomerCountry(String customerCountry) {
        this.customerCountry = customerCountry;
    }

    public String getCustomerWebsite() {
        return customerWebsite;
    }

    public void setCustomerWebsite(String customerWebsite) {
        this.customerWebsite = customerWebsite;
    }

    public String getCustomerComments() {
        return customerComments;
    }

    public void setCustomerComments(String customerComments) {
        this.customerComments = customerComments;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (customerid != null ? customerid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Customer)) {
            return false;
        }
        Customer other = (Customer) object;
        if ((this.customerid == null && other.customerid != null) || (this.customerid != null && !this.customerid.equals(other.customerid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.Mk.entity.Customer[ customerid=" + customerid + " ]";
    }
    
}

- Copyright © Get Codes - Powered by Blogger - Designed by Mukunthan GJ -