Working with Entities in Google Web Toolkit 2

Exclusive offer: get 50% off this eBook here
Google Web Toolkit 2 Application Development Cookbook

Google Web Toolkit 2 Application Development Cookbook — Save 50%

Google Web Toolkit book and eBook - over 70 simple but incredibly effective practical recipes to develop web applications using GWT with JPA , MySQL and i Report

$23.99    $12.00
by Shamsuddin Ahammad | January 2011 | Cookbooks Open Source

GWT 2 radically improves the web experience for users by using the existing Java tools to build a no-compromise AJAX for any modern browser. It provides a solid platform so that the other great libraries can be built on top of the GWT. Creating web applications efficiently and making them impressive, however, is not as easy as it sounds. Writing web applications for multiple browsers can be quite tasking. In addition, building, reusing, and maintaining large JavaScript code bases and AJAX components can be difficult.

In the previous article, Communicating with Server using Google Web Toolkit RPC, we saw the communication between the server and the client in GWT RPC. In this article by Shamsuddin Ahammad, author of Google Web Toolkit 2 Application Development Cookbook, we will cover:

  • Finding an entity
  • Updating an entity
  • Deleting an entity
  • Managing a list for RPC
  • Authenticating a user through username and password

 

Google Web Toolkit 2 Application Development Cookbook

Google Web Toolkit 2 Application Development Cookbook

Over 70 simple but incredibly effective practical recipes to develop web applications using GWT with JPA , MySQL and i Report

  • Create impressive, complex browser-based web applications with GWT 2
  • Learn the most effective ways to create reports with parameters, variables, and subreports using iReport
  • Create Swing-like web-based GUIs using the Ext GWT class library
  • Develop applications using browser quirks, Javascript,HTML scriplets from scratch
  • Part of Packt's Cookbook series: Each recipe is a carefully organized sequence of instructions to complete the task as efficiently as possible
        Read more about this book      

(For more resources on GWT, see here.)

Finding an entity

In this recipe, we are going to write the code to find an entity. From the client side, the ID of the entity will be passed to the server; the server will find the entity in the database using the JPA controller class, and then return the entity to the client in order to display it.

How to do it...

  1. Declare the following method in the GWTService interface:

    public BranchDTO findBranch(int branchId);

  2. Declare the asynchronous version of the above method in GWTServiceAsync interface

    public void findBranch(int branchId, AsyncCallback<BranchDTO>
    asyncCallback);

  3. Implement this method in GWTServiceImpl class

    @Override
    public BranchDTO findBranch(int branchId)
    {
    Branch branch=branchJpaController.findBranch(branchId);
    BranchDTO branchDTO=null;
    if(branch!=null)
    {
    branchDTO=new BranchDTO();
    branchDTO.setBranchId(branch.getBranchId());
    branchDTO.setName(branch.getName());
    branchDTO.setLocation(branch.getLocation());
    }
    return branchDTO;
    }

  4. Create a callback instance in client side (BranchForm in this case) to call this method as shown in the following code:

    final AsyncCallback<BranchDTO> callbackFind =
    new AsyncCallback<BranchDTO>()
    {
    @Override
    public void onFailure(Throwable caught)
    {
    MessageBox messageBox = new MessageBox();
    messageBox.setMessage("An error occured!
    Cannot complete the operation");
    messageBox.show();
    clear();
    }
    @Override
    public void onSuccess(BranchDTO result)
    {
    branchDTO=result;
    if(result!=null)
    {
    branchIdField.setValue(""+branchDTO.getBranchId());
    nameField.setValue(branchDTO.getName());
    locationField.setValue(branchDTO.getLocation());
    }
    else
    {
    MessageBox messageBox = new MessageBox();
    messageBox.setMessage("No such Branch found");
    messageBox.show();
    clear();
    }
    }
    };

  5. Write the event-handling code for the find button as follows:

    findButton.addSelectionListener(new
    SelectionListener<ButtonEvent>()
    {
    @Override
    public void componentSelected(ButtonEvent ce)
    {
    MessageBox inputBox = MessageBox.prompt("Input",
    "Enter the Branch ID");
    inputBox.addCallback(new Listener<MessageBoxEvent>()
    {
    public void handleEvent(MessageBoxEvent be)
    {
    int branchId = Integer.parseInt(be.getValue());
    ((GWTServiceAsync)GWT.create(GWTService.class)).
    findBranch(branchId,callbackFind);
    }
    });
    }
    });

How it works...

Here, the steps for calling the RPC method are the same as we had done for the add/save operation. The only difference is the type of the result we have received from the server. We have passed the int branch ID and have received the complete BrachDTO object, from which the values are shown in the branch form.

Updating an entity

In this recipe, we are going to write the code to update an entity. The client will transfer the DTO of updated object, and the server will update the entity in the database using the JPA controller class.

How to do it...

  1. Declare the following method in the GWTService interface:

    public boolean updateBranch(BranchDTO branchDTO);

  2. Declare the asynchronous version of this method in the GWTServiceAsync interface:

    public void updateBranch(BranchDTO branchDTO,
    AsyncCallback<java.lang.Boolean> asyncCallback);

  3. Implement the method in the GWTServiceImpl class:

    @Override
    public boolean updateBranch(BranchDTO branchDTO)
    {
    boolean updated=false;
    try
    {
    branchJpaController.edit(new Branch(branchDTO));
    updated=true;
    }
    catch (IllegalOrphanException ex)
    {
    Logger.getLogger(GWTServiceImpl.class.getName()).
    log(Level.SEVERE, null, ex);
    }
    catch (NonexistentEntityException ex)
    {
    Logger.getLogger(GWTServiceImpl.class.getName()).
    log(Level.SEVERE, null, ex);
    }
    catch (Exception ex)
    {
    Logger.getLogger(GWTServiceImpl.class.getName()).
    log(Level.SEVERE, null, ex);
    }
    return updated;
    }

  4. Create a callback instance for this method in the client side (BranchForm in this case, if it is not created yet):

    final AsyncCallback<Boolean> callback =
    new AsyncCallback<Boolean>()
    {
    MessageBox messageBox = new MessageBox();
    @Override
    public void onFailure(Throwable caught)
    {
    messageBox.setMessage("An error occured!
    Cannot complete the operation");
    messageBox.show();
    }
    @Override
    public void onSuccess(Boolean result)
    {
    if (result)
    {
    messageBox.setMessage("Operation completed successfully");
    } else
    {
    messageBox.setMessage("An error occured!
    Cannot complete the operation");
    }
    messageBox.show();
    }
    };

  5. Write the event handle code for the update button:

    updateButton.addSelectionListener(new
    SelectionListener<ButtonEvent>()
    {
    @Override
    public void componentSelected(ButtonEvent ce)
    {
    branchDTO.setName(nameField.getValue());
    branchDTO.setLocation(locationField.getValue());
    ((GWTServiceAsync)GWT.create(GWTService.class)).
    updateBranch(branchDTO,callback);
    clear();
    }
    });

How it works...

This operation is also almost the same as the add operation shown previously. The difference here is the method of controller class. The method edit of the controller class is used to update an entity.

Deleting an entity

In this recipe, we are going to write the code to delete an entity. The client will transfer the ID of the object, and the server will delete the entity from the database using the JPA controller class.

How to do it...

  1. Declare the following method in the GWTService interface

    public boolean deleteBranch(int branchId);

  2. Declare the asynchronous version of this method in GWTServiceAsync interface

    public void deleteBranch(int branchId,
    AsyncCallback<java.lang.Boolean> asyncCallback);

  3. Implement the method in GWTServiceImpl class

    @Override
    public boolean deleteBranch(int branchId)
    {
    boolean deleted=false;
    try
    {
    branchJpaController.destroy(branchId);
    deleted=true;
    }
    catch (IllegalOrphanException ex)
    {
    Logger.getLogger(GWTServiceImpl.class.getName()).
    log(Level.SEVERE, null, ex);
    }
    catch (NonexistentEntityException ex)
    {
    Logger.getLogger(GWTServiceImpl.class.getName()).
    log(Level.SEVERE, null, ex);
    }
    return deleted;
    }

  4. Create a callback instance for this method in the client side (BranchForm in this case, if it is not created yet):

    final AsyncCallback<Boolean> callback = new
    AsyncCallback<Boolean>()
    {
    MessageBox messageBox = new MessageBox();

    @Override
    public void onFailure(Throwable caught)
    {
    messageBox.setMessage("An error occured!
    Cannot complete the operation");
    messageBox.show();
    }
    @Override
    public void onSuccess(Boolean result)
    {
    if (result)
    {
    messageBox.setMessage("Operation completed successfully");
    } else
    {
    messageBox.setMessage("An error occured!
    Cannot complete the operation");
    }
    messageBox.show();
    }
    };

  5. Write the event handling code for the delete button:

    deleteButton.addSelectionListener(new
    SelectionListener<ButtonEvent>()
    {
    @Override
    public void componentSelected(ButtonEvent ce)
    {
    ((GWTServiceAsync)GWT.create(GWTService.class)).
    deleteBranch(branchDTO.getBranchId(),callback);
    clear();
    }
    });

Managing a list for RPC

Sometimes, we need to transfer a list of objects as java.util.List (or a collection) back and forth between the server and the client. We already know from the preceding recipes that the JPA entity class objects are not transferable directly using RPC. Because of the same reason, any list of the JPA entity class is not transferable directly. To transfer java.util.List using RPC, the list must contain objects from DTO classes only. In this recipe, we will see how we can manage a list for RPC.

In our scenario, we can consider two classes—Customer and Sales. The association between these two classes is that one customer makes zero or more sales and one sale is made by one customer. Because of such an association, the customer class contains a list of sales, and the sales class contains a single instance of customer class. For example, we want to transfer the full customer object with the list of sales made by this customer. Let's see how we can make that possible.

How to do it...

  1. Create DTO classes for Customer and Sales (CustomerDTO and SalesDTO, respectively). In the following table, the required changes in data types are shown for the entity and DTO class attributes. The list in the DTO class contains objects of only the DTO class; on the other hand, the list of the entity class contains objects of entity class.

    Google Web Toolkit 2

  2. Define the following constructor in the Customer entity class:

    public Customer(CustomerDTO customerDTO)
    {
    setCustomerNo(customerDTO.getCustomerNo());
    setName(customerDTO.getName());
    setAddress(customerDTO.getAddress());
    setContactNo(customerDTO.getContactNo());

    List<SalesDTO> salesDTOList=customerDTO.getSalesList();
    salesList = new ArrayList<Sales>();
    for(int i=0;i<salesDTOList.size();i++)
    {
    SalesDTO salesDTO=salesDTOList.get(i);
    Sales sales=new Sales(salesDTO);
    salesList.add(sales);
    }
    }

  3. Define the following constructor in the Sales entity class:

    public Sales(SalesDTO salesDTO)
    {
    setSalesNo(salesDTO.getSalesNo());
    setSalesDate(salesDTO.getSalesDate());
    setCustomer(new Customer(salesDTO.getCustomer()));
    // there's more but not relevant for this recipe
    }

How it works...

Now in the server side, the entity classes, Customer and Sales, will be used, and in the client side, CustomerDTO and SalesDTO, will be used. Constructors with DTO class type argument are defined for the mapping between entity class and DTO class.

But here, the addition is the loop used for creating the list. From the CustomerDTO class, we get a list of SalesDTO. The loop gets one SalesDTO from the list, converts it to Sales, and adds it in the Sales list—that's all.

Authenticating a user through username and password

In this recipe, we are going to create the necessary methods to authenticate a user through a login process.

Getting ready

Create the DTO class for the entity class Users.

How to do it...

  1. Declare the following method in the GWTService interface:

    public UsersDTO login(String username,String password);

  2. Declare the following method in the GWTServiceAsync interface:

    public void login(String username, String password,
    AsyncCallback<UsersDTO> asyncCallback);

  3. Implement the method in the GWTServiceImpl class:

    @Override
    public UsersDTO login(String username, String password)
    {
    UsersDTO userDTO = null;
    UsersJpaController usersJpaController =
    new UsersJpaController();
    Users user = (Users) usersJpaController.findUsers(username);
    if (user != null)
    {
    if (user.getPassword().equals(password))
    {
    userDTO=new UsersDTO();
    userDTO.setUserName(user.getUserName());
    userDTO.setPassword(user.getPassword());
    EmployeeDTO employeeDTO=
    new EmployeeDTO(user.getEmployee().getEmployeeId());
    employeeDTO.setName(user.getEmployee().getName());
    userDTO.setEmployeeDTO(employeeDTO);
    }
    }
    return userDTO;
    }

How it works...

A username and password are passed to the method. An object of the UsersJpaController class is created to find the Users object based on the given username. If the find method returns null, it means that no such user exists. Otherwise, the password of the Users object is compared with the given password. If both the passwords match, a UsersDTO object is constructed and returned.

The client will call this method during the login process. If the client gets null, the client should handle it accordingly, as the username/password is not correct. If it is not null, the user is authenticated.

Summary

In this article we how we can manage entities in GWT RPC. Specifically, we covered the following:

  • Finding an entity
  • Updating an entity
  • Deleting an entity
  • Managing a list for RPC
  • Authenticating a user through username and password

Further resources on this subject:


Google Web Toolkit 2 Application Development Cookbook Google Web Toolkit book and eBook - over 70 simple but incredibly effective practical recipes to develop web applications using GWT with JPA , MySQL and i Report
Published: November 2010
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:

About the Author :


Shamsuddin Ahammad

Shamsuddin Ahammad is a Senior Lecturer and the Course Coordinator at Daffodil Institute of IT, Bangladesh. He has been teaching Java, Programming Methods, and Database Systems since 2002. He has experience in supervising hundreds of academic projects. Shamsuddin has a Masters degree in Management Information Systems (MIS) from Daffodil International University, Dhaka. He obtained the BSc(Hons) degree in Computing & Information Systems (CIS) of NCC Education Ltd, UK and London Metropolitan University joint programme from Daffodil Institute of IT. Before that, he completed the IDCS & IADCS of NCC Education Ltd. He is an Additional Reviewer of Conference on Quality Engineering in Software Technology (CONQUEST) organized by International Software Quality Institute (iSQI) in Germany. He is the author of book titled iReport 3.7 published from PACKT Publishing in March 2010.

Books From Packt


Inkscape 0.48 Essentials for Web Designers
Inkscape 0.48 Essentials for Web Designers

TYPO3 Templates
TYPO3 Templates

CMS Made Simple 1.6: Beginner's Guide
CMS Made Simple 1.6: Beginner's Guide

Blender 2.5 Lighting and Rendering
Blender 2.5 Lighting and Rendering

PostgreSQL 9.0 High Performance
PostgreSQL 9.0 High Performance

PrestaShop 1.3 Theming – Beginner’s Guide
PrestaShop 1.3 Theming – Beginner’s Guide

jQuery 1.4 Reference Guide
jQuery 1.4 Reference Guide

Learning jQuery 1.3
Learning jQuery 1.3


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
g
A
x
s
8
L
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software