Understanding EJB

Hi,everybody today I am writing about EJB.Understanding each bean in very important.

Session bean
Session bean is about client-server interactive session.As we know EJB deployed on J2EE application server,
in here client may be a servlet or what ever.
For example suppose google.com is a servlet when we type on search box and hit search button,then
it invoke EJB session beans methods.That session bean is for client(servlet).
it is not shaired.Others can't view your search results.

Now you can understand what session bean do.It do your business tasks inside the server but as a separete unit of force.Servlet don't need to perform database related task or business logic.Servlet get data from front end and pass to EJB and backword.Actually it reduce complexity.

There are two types of session beans: stateful and stateless.




Stateful Session Beans
Servelt can talk with EJB.That is call conversation. Stateful session bean is keeping the state(values of variables) in the conversational session.It retained until client-bean session.

Stateless Session Beans
Stateless sesion bean keep state only on inside the method call. As we talk earlier getName() and getEmail() both method calls are not in same session in stateless bean.



Message Driven Beans
Message Driven beans are asynchronous.All other beans are synchronous. In above example we invoke
bean.getName();
bean.getEmail();

In synchronous once name is return then bean will return email.In session bean there is a queue or sequence.
But in asynchronous no wating,no queue,email can be recieved first or name can be received first there is no queue or sequence.

spring flex sample application

This sample application was develop by me for ceylon electricity board(CEB).application alow user to

· Insert/enter data

· Retrive/view data

· Edit/upate data and

· upload bulk data such as excel sheet

· Reporting & graphs

· Loging system(spring security)

Sample Application URL

http://dl.dropbox.com/u/7375335/sample%20ceb/TestApplication.swf

HIbernate - JSP/Servlet Simple Example

HIbernate - JSP/Servlet Simple Example
You can download full code example from here.
This example use Hibernate 3.5 and JPA 2.0 Annotatyion base classes.
You can see how to use JPA entities as HIbernate entities.
This example use Derby database.

hibernate-configuration file is as Following

This example is using annotated entity classes that genarated from eclipse.So no need to write xml mapping files for each entity.

Vedio resources For Hibernate reverse engineering
Here Another one
http://www.youtube.com/watch?v=WKWnbhc4_6g&NR=1





 


  
  jdbc:derby://localhost:1527/catalogueDB;create=true
  org.apache.derby.jdbc.ClientDriver
  app
  app
  

    
  
  
  
  thread
  
  
  org.hibernate.dialect.DerbyDialect
  
  create
  org.hibernate.cache.NoCacheProvider
  true
  org.hibernate.transaction.JDBCTransactionFactory
  
  
  
  
  
 



package com.snk.model;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;

// Generated May 30, 2009 6:49:31 AM by Hibernate Tools 3.2.4.GA

/**
*  This class contains the course details.
*  
*/
@Entity
public class Course implements java.io.Serializable {

 @Id
 @GeneratedValue
 private long courseId;
 @Column
 private String courseName;

 public Course() {
 }

 public Course(String courseName) {
  this.courseName = courseName;
 }

 public long getCourseId() {
  return this.courseId;
 }

 public void setCourseId(long courseId) {
  this.courseId = courseId;
 }

 public String getCourseName() {
  return this.courseName;
 }

 public void setCourseName(String courseName) {
  this.courseName = courseName;
 }

}


package com.snk.frontend;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.snk.controler.ServiceLocator;
import com.snk.model.Course;


/**
* Servlet implementation class View
*/
/**
* @author Administrator
*
*/
public class View extends HttpServlet {
 private static final long serialVersionUID = 1L;
     
   /**
    * @see HttpServlet#HttpServlet()
    */
   public View() {
       super();
       // TODO Auto-generated constructor stub
   }

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  PrintWriter out=response.getWriter();
  out.println("sanka do get");
  doPorocess(request, response);
 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  PrintWriter out=response.getWriter();
  out.println("sanka do post");
  doPorocess(request, response);
 }
 
 
 protected void doPorocess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  PrintWriter out=response.getWriter();
  ServiceLocator obj = new ServiceLocator();
  out.println("Before Insert three records\n");
  Long courseId1 = obj.saveCourse("Physics");
  Long courseId2 = obj.saveCourse("Chemistry");
  Long courseId3 = obj.saveCourse("Maths");
  
  showCourse(obj.listCourse(),out);
  out.println("End of Insert three records\n\n\n\n");
  obj.updateCourse(courseId3, "Mathematics");
  showCourse(obj.listCourse(),out);
  out.println("End of update three records\n\n\n\n");
  obj.deleteCourse(courseId2);
  out.println("End of delete chemistry records\n\n\n\n");
  showCourse(obj.listCourse(),out);
  
 }
 
 private void showCourse(List courses,PrintWriter out)
 {
  for (Iterator iterator = courses.iterator(); iterator.hasNext();)
  {
   Course course = (Course) iterator.next();
   out.println("Cource name : "+course.getCourseName());
  }
 }
 
}




package com.snk.controler;
import java.util.List;
import java.util.Iterator;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.snk.model.Course;
import com.snk.util.HibernateUtil;

public class ServiceLocator {

 
 public Long saveCourse(String courseName)
 {
  Session session = HibernateUtil.getSessionFactory().openSession();
  Transaction transaction = null;
  Long courseId = null;
  try {
   transaction = session.beginTransaction();
   Course course = new Course();
   course.setCourseName(courseName);
   courseId = (Long) session.save(course);
   transaction.commit();
  } catch (HibernateException e) {
   transaction.rollback();
   e.printStackTrace();
  } finally {
   session.close();
  }
  return courseId;
  
 /* //Following Are the code in JPA for insert query
    EntityManagerFactory emf=Persistence.createEntityManagerFactory("hibernate-servlet");
    EntityManager em=emf.createEntityManager();
  Course course = new Course();
  course.setCourseName(courseName);
  course.setCourseId(200);
  Long courseId = 200L;
  em.persist(course);
  System.out.println("Afetr Save");*/
  
  
 }
 
 public List listCourse()
 {
  Session session = HibernateUtil.getSessionFactory().openSession();
  Transaction transaction = null;
  try {
   transaction = session.beginTransaction();
   List courses = session.createQuery("from Course").list();
   for (Iterator iterator = courses.iterator(); iterator.hasNext();)
   {
    Course course = (Course) iterator.next();
    System.out.println(course.getCourseName());
   }
   transaction.commit();
   return courses;
  } catch (HibernateException e) {
   transaction.rollback();
   e.printStackTrace();
   return null;
  } finally {
   session.close();
  }
 }
 
 public void updateCourse(Long courseId, String courseName)
 {
  Session session = HibernateUtil.getSessionFactory().openSession();
  Transaction transaction = null;
  try {
   transaction = session.beginTransaction();
   Course course = (Course) session.get(Course.class, courseId);
   course.setCourseName(courseName);
   transaction.commit();
  } catch (HibernateException e) {
   transaction.rollback();
   e.printStackTrace();
  } finally {
   session.close();
  }
 }
 
 public void deleteCourse(Long courseId)
 {
  Session session = HibernateUtil.getSessionFactory().openSession();
  Transaction transaction = null;
  try {
   transaction = session.beginTransaction();
   Course course = (Course) session.get(Course.class, courseId);
   session.delete(course);
   transaction.commit();
  } catch (HibernateException e) {
   transaction.rollback();
   e.printStackTrace();
  } finally {
   session.close();
  }
 }

}


package com.snk.util;

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

public class HibernateUtil {
 private static final SessionFactory sessionFactory;
 static {
  try {
//   sessionFactory = new Configuration().configure().buildSessionFactory();//Default configuration
   sessionFactory =  new AnnotationConfiguration().configure().buildSessionFactory();//Annotation Configuration
   
  } catch (Throwable ex) {
   System.err.println("Initial SessionFactory creation failed." + ex);
   throw new ExceptionInInitializerError(ex);
  }
 }

 public static SessionFactory getSessionFactory() {
  return sessionFactory;
 }
}




 hibernate-servlet
 
   index.jsp
 

 
   
   View
   View
   com.snk.frontend.View
 
 
   View
   /View
 
  

Spring BlazeDS Integration

Spring BlazeDS Integration

1. Configuring and Using the BlazeDS MessageBroker with Spring

MessageBroker is the heart of the Spring BlazeDS Integration. When HTTP messages come from Flex client will be routed through the Spring DispatcherServlet to the Spring-managed MessageBroker. There is no need to configure the BlazeDS MessageBrokerServlet when using the Spring-managed MessageBroker.

2. Configuring the Spring DispatcherServlet

web.xml is the heart of the j2ee web project.So we have to configure it because each an every request is map to web.xml. The DispatcherServlet must be configured in web.xml to bootstrap a Spring WebApplicationContext. For example:



Spring MVC Dispatcher Servlet
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
/WEB-INF/config/web-application-config.xml

1

3. Configuring the MessageBroker in Spring

A simplified Spring XML config namespace is provided for configuring the MessageBroker in your WebApplicationContext. To use the namespace support you must add the schema location in your Spring XML config files. A typical config will look something like the following:


...

Following XML config namespace tags makes the Spring BlazeDS Integration configuration tags available under the flex namespace in your configuration files. The above setup will be assumed for the rest of the configuration examples to follow. For the full detail of every attribute and tag available in the

config namespace, be sure to refer to the spring-flex-1.0.xsd as every element and attribute is fully documented there. Using an XSD-aware XML editor such as the one in Eclipse should bring up the documentation automatically as you type. At a minimum, the MessageBrokerFactoryBean must be configured as a bean in your Spring WebApplicationContext in order to bootstrap the MessageBroker, along with a MessageBrokerHandlerAdapter and an appropriate HandlerMapping (usually a SimpleUrlHandlerMapping) to route incoming requests to the Spring-managed MessageBroker. These beans will be registered automatically by using the provided message-broker tag in your bean definition file. For example, in its simplest form:


This will set up the MessageBroker and necessary supporting infrastructure using sensible defaults. The defaults can be overriden using the provided attributes of the message-broker tag and its associated child elements. For example, the default location of the BlazeDS XML configuration file (/WEB-INF/flex/services-config.xml) can be overridden using the services-config-path attribute. The MessageBrokerFactoryBean uses Spring's ResourceLoader abstraction, so that typical Spring resource paths may be used. For example, to load the configuration from the application's classpath:
The equivalent MessageBrokerFactoryBean definition using vanilla Spring configuration would be:




Note especially that with the message-broker tag, it is not necessary to assign a custom id to the MessageBroker, and it is in fact discouraged so that you won't have to continually reference it later. The only reason you would ever need to provide a custom id is if you were bootstrapping more than one MessageBroker in the same WebApplicationContext.

How to navigating through the Data using Flex form

How to navigating through the Data or ArrayCollection using Flex form


I know you guys have a problem of having navigating through tha data of ArrayCollection by using the Flex Form
Here is the solution for viewing data, or navigating data of Flec ArrayColletion that contain bean objects....














 
   selectedMember)
    {
     selectedMember = selectedMember + 1;
     tiDebAddress1.text=lststatus.getItemAt(selectedMember).status; 
     cmdPrevious.enabled=true;
     if(lststatus.length-1 == selectedMember)
     {
     cmdNext.enabled=false;
     cmdPrevious.enabled=true;
     }
     else
     cmdNext.enabled=true;
    }
   }
   
   private function prevousItem():void
   {
    if(lststatus.length > 0 && selectedMember > 0)
    {
     selectedMember = selectedMember - 1;
     tiDebAddress1.text=lststatus.getItemAt(selectedMember).status;
     if(selectedMember==0)
     {
     cmdPrevious.enabled=false;
     cmdNext.enabled=true;
     } 
     else
     cmdPrevious.enabled=true;
    }
   }
   
  ]]>
 
 



Flex : How to calculate Datagrid column total

How to calculate Datagrid column total
Manipulating flex datagrid colomns
Select a colomn name from comboBox and hit the button and enjoy...
Happy coding.....


you can download the code from here


Flex Dynamically popup searchable Datagrid

Flex Dynamically popup searchable Data-grid For Demo purpose I have create non DB version as following.Full code example is following... Check it and get idea...No need to describe this.

mainApp.mxml

PopUpGrid.as

This is non Database program MXML file

This is non Database program .as file

Wp Theme by Templatesnext . Sanka Thank for templete to Anshul