Database: Distributed EJB Application
Database: Distributed EJB Application
Database: Distributed EJB Application
Introduction
The application is to Register Employees to the projects which they have assigned to and search the directory of the Project files of every individual projects. The application includes one to one relation in the Employee entity and many to many in Project entity. The application contains a Stateful session beans in Register interface bean and stateless session bean Search interface bean. Graphical user interface is used in both of the client main methods. Beans business interface is generated by the container by using the EJB annotations. Lifecycle Remove is used in the Register interface to remove the bean instance after calling all the methods. The application is built on Netbeans, the server used is Jboss and the query language used is EJB QL. The application consists of two graphical user interface main methods (Register and Search) in the client. The application consists three entity beans (Employee, Project and Files), two Interfaces (RegisterInt and Search), a Stateful session bean (RegisterIntBean) and a stateless session bean (SearchIntBean) in the server.
Database
The database consists of four tables. Employee_bio table consists of all the details of the employees and it also has a one to one relationship with the project_details table. Project_details table consists of the list of employees who are assigned to the specific project and it has many to many relationships with the files table. Files table consists of all the files which are related to the each and every project. Project_files table consists of the ProjectID and FilesID by using many to many relationships with the Project_details table and Files table.
Register Jframe
Register Jframe is a graphical user interface main method in the client side used to call the methods in the Register interface in the server. Getcontext is used to call the methods from the RegisterInt interface in the remote server from the client which. The Register method of the RegisterInt interface is executed by getting the value from the client during the run time from the textboxes in graphical user interface form. The server URL is described as localhost.
Context ctx = getContext(); RegisterInt reg = (RegisterInt) ctx.lookup("RegisterIntBean/remote"); private static String serverURL = "localhost"; private Context getContext() throws NamingException { Properties p = new Properties(); p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); p.put(Context.PROVIDER_URL, serverURL); Context ctx = new InitialContext(p); return ctx; }
To call the methods from the RegisterInt interface in the remote server context is used in the client. The methods of the RegisterInt interface are executed by getting the variables from the client during the run time from the textboxes in graphical user interface form. A try catch exception error is added to get the value from the textboxes from the graphical user interface form in order to avoid error values from the client during the run time.
try { ag = Integer.parseInt(this.jTextField3.getText()); } catch (Exception e) { JOptionPane.showMessageDialog(this, "Invalid Quantity", "ERROR", JOptionPane.ERROR_MESSAGE); return; } fi = (File) reg.createFile(proj, "java"); pro = (Project) reg.createProject(sna, proj, f); emp = (Employee) reg.createEmployee(fna, sna, ag, se, ema, pro);
The Register Form consists of three buttons. The register button registers the employees to the projects and updates the tables in database by collecting the data from the textboxes and a pop-up message box will conform the registration of the employee. The search button will take us to the search window. The exit button will close the application.
Search Form
Search Jframe is a graphical user interface main method in the client side used to call the methods in the Search interface in the server. To call the methods from the SearchInt interface in the remote server context is used in the client which is similar to the one we used in the Register form. The search method of the SearchInt interface is executed by getting the value from the client during the run time from the search textbox in graphical user interface form. For every row found in the database table with the project name it keeps on looping in the for loop and adds the name of the file to the label in the JFrame.
List<File> files = dao.findFiles(ser); for (File file : files) { this.jLabel1.setText(" " + this.jLabel1.getText() + "," + file.getFilename()); }
The Search Form consists of three buttons. The search button searched the database and displays the files of the project searched. The register button will take us to the register window. The exit button will close the application.
Adepu Siddartha(21159745) MSc Software Engineering 4
Entity Beans
Entity Bean represents persistent data maintained in a database. An entity bean can manage its own persistence and the primary key property is explained below.
@Id @GeneratedValue(strategy = GenerationType.AUTO)
@Id annotation is used to assign is as the primary key property and @GeneratedValue is used to automatically generate the value of the parameter by the container. @Table is used to assign the name of the table and similarly fot column @Colunm is used.
Employee Entity
Employee entity consists of the Biographies of all the employees at the organisation. Every Employee will be registered in a single project at a given time (for the sake of the application at least). The relationship between the Employee entity and the project entity (one to one relation) is explained below.
@OneToOne @JoinColumn(name = "Project_id") public Project getProject() { return project; }
There is a unidirectional one to one relationship defined between Employee entity and Project entity. The project_id column of the Project entity is merged with the Employee entitys table in the database. The name of the table in the database is assigned as employee_bio.
Project Entity
When the employees are registered to the Projects, The project entity creates a table in the database with the name project_details. It contains all the details of the employees who are working on the project. Every project consists of number of files. The relationship between the Project entity and File entity (many to many) is explained below.
@ManyToMany @JoinTable(name = "Project_Files", joinColumns = @JoinColumn(name = "Project_ID", referencedColumnName = "ID"), inverseJoinColumns = @JoinColumn(name = "File_ID", referencedColumnName = "ID")) public Collection<File> getFile() { return file; }
There is a many to many relationship defined between Project entity and File entity. The table in the database will be created with the name project_files where project_id column of the project_files table from the Project entity and the file_id column of the file from the File entity will be joined.
File Entity
File entity creates a table in the database which consists of all files that are related to each and every project.
Session Beans
A session bean is the enterprise bean that directly interacts with the client and contains the business logic of the enterprise application. A session bean represents a single client accessing the enterprise application deployed on the server by invoking its method. There are two types of session beans: stateful and stateless.
Register Interface
Register interface is used to register the employees to the projects, which is called from the Register Jframe main method. Register interface uses the remove from the lifecycle to clear the data.
employee = new Employee(); ArrayList<Employee> e = new ArrayList<Employee>(); employee.setFname(fname); employee.setSname(sname); employee.setProject(project); em.persist(getEmployee()); e.add(employee); return getEmployee();
It assigns the values to the database and creates a list of the employee and adds it to the array with the name e and returns details of the employees. The remove from the lifecycle is used remove the bean instance after calling all the methods.
@Remove public void finish() { em.persist(project); em.persist(employee); em.persist(file); }
Search Interface
Search interface is used to search the files of the projects, which is called from the Search Jframe main method. EJB query language is used to read the data from the database.
public List<File> findFiles(String project) { String qry = "SELECT o FROM File o WHERE o.projectname LIKE '%" + project + "%' "; System.out.println("file found" + manager.createQuery(qry).getResultList()); List<File> files = manager.createQuery(qry).getResultList(); return files; }
The rows which are selected with the project name are assigned to a list with the name files and methods returns the list of the files with the project name from the search.
Figure 4: Database
The database consists of four tables. Employee_bio table consists of all the details of the employees. Project_details table consists of the list of employees who are assigned to the specific project. Files table consists of all the files which are related to the each and every project. Project_files table consists of the ProjectID and FilesID.
The above figure displays that the application is built successfully without any errors from any of the classes and interfaces. Once it is displayed it is good to go.
10
The above figure displays that the application is deployed successfully without any errors from any of the classes and interfaces.
11
EJB Annotations
@Stateful - Stateful business interface is generated by the container. @Stateless - Stateless business interface is generated by the container. @Romote is used to indicate the remote interfaces by the container. @Local is used to indicate the local interfaces by the container. @Entity to define the class to a entity by the container. @Id - to assign is as the primary key property to the entity. @GeneratedValue - to automatically generate the value of the parameter by the container. @Column - to assign the name of the column. @Table - to assign the name of the table. @One To One to define one to one the relationship between the columns of the tables of two entities. @Many To Many - to define many to many the relationship between the columns of the tables of two entities. @PersistenceContext to define the persistence context of the application. @Remove - to remove the bean instance after calling all the methods.
12
Application Coding
Register Jframe
package dad.client; import dad.server.Employee; import dad.server.File; import dad.server.Project; import dad.server.RegisterInt; import java.util.ArrayList; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.swing.JOptionPane; public class Register extends javax.swing.JFrame { /** Creates new form Register */ public Register() { initComponents(); } @SuppressWarnings("unchecked") private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { try { reg(); } catch (NamingException ex) { Logger.getLogger(Register.class.getName()).log(Level.SEVERE, null, ex); } } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { Search sss = new Search(); sss.setVisible(true); Register hhh =new Register(); hhh.setVisible(false); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() {
13
14
Search Jframe
package dad.client; import dad.server.File; import dad.server.SearchInt; import java.util.List; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.swing.JOptionPane; public class Search extends javax.swing.JFrame { /** Creates new form Search */ public Search() { initComponents(); } @SuppressWarnings("unchecked") private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { try { srch(); } catch (NamingException ex) { Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex); } }
15
16
17
Project Entity
package dad.server; import java.io.Serializable; import java.util.Collection; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; @Entity @Table(name = "Project_Details") public class Project implements Serializable {
18
19
20
RegisterIntBean
package dad.server; import java.util.ArrayList; import java.util.Collection; import javax.ejb.Local; import javax.ejb.Remote; import javax.ejb.Remove; import javax.ejb.Stateful; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @Stateful @Remote(RegisterInt.class) @Local public class RegisterIntBean implements RegisterInt { @PersistenceContext(unitName = "tempdb") private EntityManager em; private Employee employee; private Project project; private File file; public Project createProject(String sname, String projectname, Collection<File> file) { project = new Project(); project.setEmployee(sname); project.setProjectname(projectname); project.setFile(file); em.persist(getProject()); return getProject(); } public File createFile(String Projectname, String filename) {
21
SearchInt Interface
package dad.server; import java.util.List; public interface SearchInt { public File findFile(int fileID); public List<File> findFiles(String project); }
22
23