Liferay 6.0.5 + JSF 2.0 + IceFaces 2.0 + Hibernate 3.5.6 – page 3

This is a service File which will help to add and get all the Employees.


package com.navin.iceFacesHibernateLiferay.example.hibernate;

import java.util.Iterator;
import java.util.List;

import org.hibernate.*;

import com.navin.iceFacesHibernateLiferay.example.hibernate.Employee;
import com.navin.iceFacesHibernateLiferay.example.util.HibernateUtil;
/**
 *
 * @author Navin Agarwal
 */
public class HibernateService {	
    public String addUsers(Employee employee){
			String res="failure";
			try{
				SessionFactory sf=HibernateUtil.getSessionFactory();
				Session session=sf.openSession();
				Transaction trx=session.beginTransaction();
                                session.save(employee);
				trx.commit();
				session.close();
				System.out.println("In Service - Employee Added Successfully.");
				res="success";
			}
			catch(Exception e){
				System.out.println("In Service - "+e);
				System.out.println("In Service - Operation Failed.User May Exist.");
			}
			return res;
    }

    public Employee getEmployee(String employeeEmail){
		
		Employee employee = null;
		try{			
			SessionFactory sf=HibernateUtil.getSessionFactory();
			Session session=sf.openSession();
			Transaction trx=session.beginTransaction();
			Query q=session.createQuery("from MDOCS where EEmailID='"+employeeEmail+"'");
			q.setCacheable(true);
			List l=q.list();
			Iterator i=l.iterator();
			while(i.hasNext()){
				employee=(Employee)i.next();
			}			
			trx.commit();
			session.close();
		}
		catch(Exception e){
			System.out.println(e);
		}
		return employee;
	}
}

Util Classes ..

package com.navin.iceFacesHibernateLiferay.example.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {
	private static final SessionFactory sessionFactory;
	static {
		try {
			sessionFactory = new AnnotationConfiguration().configure()
					.buildSessionFactory();
		} catch (Throwable ex) {
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}
	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
}
package com.navin.iceFacesHibernateLiferay.example.bean;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import com.navin.iceFacesHibernateLiferay.example.Pwd;
import com.navin.iceFacesHibernateLiferay.example.hibernate.Employee;
import com.navin.iceFacesHibernateLiferay.example.hibernate.HibernateService;

import com.navin.iceFacesHibernateLiferay.example.util.FacesMessageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ManagedBean(name = "applicantBackingBean")
@ViewScoped
public class ApplicantBackingBean implements Serializable {

	// serialVersionUID
	private static final long serialVersionUID = 2947548873495692163L;

	// Logger
	private static final transient Logger logger = LoggerFactory.getLogger(ApplicantBackingBean.class);

	// Injections
	@ManagedProperty(value = "#{applicantModelBean}")
	private transient ApplicantModelBean applicantModelBean;
	

	// JavaBeans Properties for UI
	
	public void setApplicantModelBean(ApplicantModelBean applicantModelBean) {
		// Injected via @ManagedProperty annotation
		this.applicantModelBean = applicantModelBean;
	}
	public void submit() {
			
		if (logger.isDebugEnabled()) {
			logger.debug("firstName=" + applicantModelBean.getFirstName());
			logger.debug("lastName=" + applicantModelBean.getLastName());
			logger.debug("emailAddress=" + applicantModelBean.getEmailAddress());
			logger.debug("phoneNumber=" + applicantModelBean.getPhoneNumber());
			logger.debug("dateOfBirth=" + applicantModelBean.getDateOfBirth());
			logger.debug("city=" + applicantModelBean.getCity());
		}
		
		Pwd autoPwd =new Pwd();
		String passwd=null;
		
		String autoPassword = autoPwd.Autopassword(passwd);
		
		HibernateService hibernateService = new HibernateService();
		
		Employee employee =new Employee();
		
		employee.setEmployeeFirstName(applicantModelBean.getFirstName());
		employee.setEmployeeLsatName(applicantModelBean.getLastName());
		employee.setPassword(autoPassword);
		employee.setEmployeeEmail(applicantModelBean.getEmailAddress());
		employee.setPhoneNumber(applicantModelBean.getPhoneNumber());
		employee.setDateOfBirth(applicantModelBean.getDateOfBirth());
		employee.setCity(applicantModelBean.getCity());
		
		String str=hibernateService.addUsers(employee);
		
		if(str=="failure") {
		   System.out.println("User failed to add ");
		}
		 else {
		
			System.out.println("Successfully add Employee ");
			String key = "thank-you-for-register";
			FacesMessageUtil.addGlobalInfoMessage(FacesContext.getCurrentInstance(), key,
				applicantModelBean.getFirstName());
			String key1 = "and-your-password-is";
			FacesMessageUtil.addGlobalInfoMessage(FacesContext.getCurrentInstance(), key1, autoPassword);
			applicantModelBean.clearProperties();
			
		 }
		
	}
}

package com.navin.iceFacesHibernateLiferay.example.bean;

import java.io.Serializable;
import java.util.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;



@ManagedBean(name = "applicantModelBean")
@ViewScoped
public class ApplicantModelBean implements Serializable {

	// serialVersionUID
	private static final long serialVersionUID = 7459628254337818761L;

	// Private Data Members
	
	private String city;
	private Date dateOfBirth;
	private String emailAddress;
	private String firstName;
	private String lastName;
	private String phoneNumber;

	public ApplicantModelBean() {
		clearProperties();
	}

	public void clearProperties() {
		
		city = null;
		
		dateOfBirth = null;
		emailAddress = null;
		firstName = null;
		lastName = null;
		phoneNumber = null;
	}

	public String getCity() {
		return city;
	}
	public Date getDateOfBirth() {
		return dateOfBirth;
	}

	public String getEmailAddress() {
		return emailAddress;
	}

	public String getFirstName() {
		return firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
}

Advertisements

One thought on “Liferay 6.0.5 + JSF 2.0 + IceFaces 2.0 + Hibernate 3.5.6 – page 3

  1. edburns (@edburns) October 31, 2011 at 9:10 pm Reply

    Thanks for sharing this. It would be nice if there was a collection of links so we could form a pattern catalog of such things.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: