JSF 2.0

Hi.

This is my third attempt to develop something in JavaServerFaces 2.0 along with Hibernate 3.0 for OR/M.

The application’s concept is quite simple, web site for Movie Reviews where anyone can register and post his/her reviews or even add new movies.

System Requirements:

JDK 1.6+ (I have used JDK 1.7)

eclipse helios (or any other version)

TomCat 6.0+

JSF implementation (I have used Mojjaro)

features provided:

  1. Add new movie
  2. list existing movies
  3. add reviews to movies
  4. list reviews to movies
  5. login for users

packages:

  • com.mahendra.models

This package contains ALL model elements (i.e. Entity classes) which are mapped to “Tables” in my database.

  • com.mahendra.dao

These are my Data Access Objects. these classes are responsible to perform DML & DQL on tables.

  • com.mahendra.actions

My Business Logic and “JSF Backing beans” are here.

  • com.mahendra.utils

My utitlity classes (better to discuss them later on)

Database:

Right now i am using Java DB (Derby) database, but can be used with ANY other database!

database script to create all required tables:

create table app.movies
(
    movie_id int primary key,
    title varchar(100) not null,
    director varchar(50),
    category varchar(30),
    duration varchar(10),
    realsed_in int
);

create table app.users
(
    userid int primary key ,
    username varchar(20) unique not null,
    password varchar(20) not null,
    created timestamp default CURRENT_TIMESTAMP
);
create table app.reviews
(
    review_id int primary key,
    userid int,
    movie_id int,
    message varchar(1000) not null,
    ratings int check (ratings between 1 and 5) default 1
);

alter table app.reviews
add constraint fk1 foreign key (userid) references app.users(userid);

alter table app.reviews
add constraint fk2 foreign key (movie_id) references app.movies(movie_id);

alter table app.reviews
add record_time timestamp default current_timestamp

delete from app.reviews;

select * from app.movies;
select * from app.users;

UML- Class diagram

download

List of libraries (dependencies) (JSF libraries have been included in Tomcat\lib folder, file: jsf-api.jar and jsg-impl.jar)

Now, create new xml file “hibernate.cfg.xml” to configure hibernate

<hibernate-configuration>

<session-factory name=””>

<property name=”hibernate.connection.driver_class”>org.apache.derby.jdbc.ClientDriver</property>

<property name=”hibernate.connection.password”>mahendra</property>

<property name=”hibernate.connection.url”>jdbc:derby://localhost:1527/mahendraDB</property>

<property name=”hibernate.connection.username”>mahendra</property>

<property

it’s now time to start creating “hibernate mapping files”

(All these files are stored in com.mahendra.models package)

Movies.hbm.xml

<hibernate-mapping>

    <class name=“com.mahendra.models.Movies” table=“MOVIES” >

        <id name=“movieId” type=“int”>

            <column name=“MOVIE_ID” />

            <generator class=“increment” />

        </id>

        <property name=“title” type=“string”>

            <column name=“TITLE” length=“100” not-null=“true” />

        </property>

        <property name=“director” type=“string”>

            <column name=“DIRECTOR” length=“50” />

        </property>

        <property name=“category” type=“string”>

            <column name=“CATEGORY” length=“30” />

        </property>

        <property name=“duration” type=“string”>

            <column name=“DURATION” length=“10” />

        </property>

        <property name=“realsedIn” type=“java.lang.Integer”>

            <column name=“REALSED_IN” />

        </property>

    </class>

</hibernate-mapping>

Reviews.hbm.xml

<hibernate-mapping>

    <class name=“com.mahendra.models.Reviews” table=“REVIEWS” >

        <id name=“reviewId” type=“int”>

            <column name=“REVIEW_ID” />

            <generator class=“increment” />

        </id>

        <many-to-one name=“user” class=“com.mahendra.models.Users” lazy=“false”>

            <column name=“USERID” />

        </many-to-one>

        <many-to-one name=“movie” class=“com.mahendra.models.Movies” lazy=“false”>

            <column name=“MOVIE_ID” />

        </many-to-one>

        <property name=“message” type=“string”>

            <column name=“MESSAGE” length=“1000” not-null=“true” />

        </property>

        <property name=“ratings” type=“java.lang.Integer”>

            <column name=“RATINGS” />

        </property>

        <property name=“recordtime” type=“timestamp” update=“false” generated=“insert” insert=“false”>

            <column name=“record_time”></column>

        </property>

    </class>

</hibernate-mapping>

Users.hbm.xml

<hibernate-mapping>

    <class name=“com.mahendra.models.Users” table=“USERS”>

        <id name=“userid” type=“int”>

            <column name=“USERID” />

            <generator class=“increment” />

        </id>

        <property name=“username” type=“string”>

            <column name=“USERNAME” length=“20” not-null=“true” unique=“true” />

        </property>

        <property name=“password” type=“string”>

            <column name=“PASSWORD” length=“20” not-null=“true” />

        </property>

        <property name=“created” type=“timestamp” insert=“false” update=“false” generated=“insert”>

            <column name=“CREATED” length=“29” />

        </property>

    </class>

</hibernate-mapping>

Once mapping is done, we will concentrate on coding….

HibernateUtils.java in package: com.mahendra.utils

This class implements “Singleton” pattern. Only one instance is available for this class.

package com.mahendra.utils;

 

import org.hibernate.HibernateException;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

 

publicclass HibernateUtils {

      privatestatic SessionFactory sessionFactory = build();

     

      privatestatic SessionFactory build(){

            try{

            returnnew Configuration().configure().buildSessionFactory();

            }catch(HibernateException he){

                  he.printStackTrace();

                  thrownew ExceptionInInitializerError(“Error creating hibernate session factory”);             

            }

      }

     

      publicstatic SessionFactory getSessionFactory(){

            returnsessionFactory;

      }

      }

 

Another utility class:  MovieReviewCount

package com.mahendra.utils;

 

publicclass MovieReviewCount {

      private String title;

      privateintcount;

      privateintaverage;

     

      public String getTitle() {

            returntitle;

      }

      publicvoid setTitle(String title) {

            this.title = title;

      }

      publicint getCount() {

            returncount;

      }

      publicvoid setCount(int count) {

            this.count = count;

      }

      public MovieReviewCount(String title, int count) {

            super();

            this.title = title;

            this.count = count;

      }

      public String toString(){

            returntitle + ” got “+count+” reviews”;

      }

      public MovieReviewCount() {

            super();

      }

      publicvoid setAverage(int average) {

            this.average = average;

      }

      publicint getAverage() {

            returnaverage;

      }

     

}

MessageBox : utility class to generate JSF messages:

package com.mahendra.utils;

 

import javax.faces.application.FacesMessage;

import javax.faces.context.FacesContext;

publicfinalclass MessageBox {

      publicstaticvoid show(String message){

            FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(message));

      }

      publicstaticvoid show(String title,String message){

            FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(title,message));

      }

}

Now the model components:

Movies.java – model components mapped with app.movies table

package com.mahendra.models;

 

publicclass Movies implements java.io.Serializable {

 

      privateintmovieId;

      private String title;

      private String director;

      private String category;

      private String duration;

      private Integer realsedIn;

 

      public Movies() {

      }

 

      public Movies(int movieId, String title) {

            this.movieId = movieId;

            this.title = title;

      }

 

      public Movies(int movieId, String title, String director, String category,

                  String duration, Integer realsedIn) {

            this.movieId = movieId;

            this.title = title;

            this.director = director;

            this.category = category;

            this.duration = duration;

            this.realsedIn = realsedIn;

      }

 

      publicint getMovieId() {

            returnthis.movieId;

      }

 

      publicvoid setMovieId(int movieId) {

            this.movieId = movieId;

      }

 

      public String getTitle() {

            returnthis.title;

      }

 

      publicvoid setTitle(String title) {

            this.title = title;

      }

 

      public String getDirector() {

            returnthis.director;

      }

 

      publicvoid setDirector(String director) {

            this.director = director;

      }

 

      public String getCategory() {

            returnthis.category;

      }

 

      publicvoid setCategory(String category) {

            this.category = category;

      }

 

      public String getDuration() {

            returnthis.duration;

      }

 

      publicvoid setDuration(String duration) {

            this.duration = duration;

      }

 

      public Integer getRealsedIn() {

            returnthis.realsedIn;

      }

 

      publicvoid setRealsedIn(Integer realsedIn) {

            this.realsedIn = realsedIn;

      }

 

}

Reviews.java            — model component mapped with app.reviews table

package com.mahendra.models;

 

import java.util.Date;

 

publicclass Reviews implements java.io.Serializable {

 

      privateintreviewId;

      private Users user;

      private Movies movie;

      private String message;

      private Integer ratings;

      private Date recordtime;

 

      public Reviews() {

      }

 

      public Reviews(int reviewId, String message) {

            this.reviewId = reviewId;

            this.message = message;

      }

 

      publicint getReviewId() {

            returnthis.reviewId;

      }

 

      publicvoid setReviewId(int reviewId) {

            this.reviewId = reviewId;

      }

 

      public Users getUser() {

            returnthis.user;

      }

 

      public Movies getMovie() {

            returnmovie;

      }

 

      publicvoid setMovie(Movies movie) {

            this.movie = movie;

      }

 

      publicvoid setUser(Users user) {

            this.user = user;

      }

 

      public String getMessage() {

            returnthis.message;

      }

 

      publicvoid setMessage(String message) {

            this.message = message;

      }

 

      public Integer getRatings() {

            returnthis.ratings;

      }

 

      publicvoid setRatings(Integer ratings) {

            this.ratings = ratings;

      }

 

      publicvoid setRecordtime(Date recordtime) {

            this.recordtime = recordtime;

      }

 

      public Date getRecordtime() {

            returnrecordtime;

      }

 

}

Users.java – model component mapped with app.users table

package com.mahendra.models;

 

import java.util.Date;

 

publicclass Users implements java.io.Serializable {

 

      privateintuserid;

      private String username;

      private String password;

      private Date created;

 

      public Users() {

      }

 

      public Users(int userid, String username, String password) {

            this.userid = userid;

            this.username = username;

            this.password = password;

      }

 

      public Users(int userid, String username, String password, Date created) {

            this.userid = userid;

            this.username = username;

            this.password = password;

            this.created = created;

      }

 

      publicint getUserid() {

            returnthis.userid;

      }

 

      publicvoid setUserid(int userid) {

            this.userid = userid;

      }

 

      public String getUsername() {

            returnthis.username;

      }

 

      publicvoid setUsername(String username) {

            this.username = username;

      }

 

      public String getPassword() {

            returnthis.password;

      }

 

      publicvoid setPassword(String password) {

            this.password = password;

      }

 

      public Date getCreated() {

            returnthis.created;

      }

 

      publicvoid setCreated(Date created) {

            this.created = created;

      }

 

}

 continued on next post…..

Advertisements

One comment on “JSF 2.0

  1. Pingback: JSF 2.0 with hibernate 3.0 tutorial, Online Movie Reviews web-app part 2 « mahendra's Unlimited Blogs

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