Monday, February 27, 2012

EJBQL Delete Query Example

If you have worked with SEAM you may have used the method "entityManager.remove()" which is used to delete an entry from database. This method accepts one parameter, which is an entity.
(However the entity should be a managed one. Otherwise it throws an exception saying that you are trying to remove a detached object.)
You can use an EJBQL query to delete a record from database directly. In this case no managed entity is required. Following is an example query, which deletes one record from "Client" table.
It deletes the client whose clientId is 10.

DELETE FROM Client client where client.clientId = 10

Now I am going to use SEAM managed EntityManager to create a Query object as below and execute the query by calling it's "executeUpdate()" method.

Injecting EntityManager.
@In
EntityManager entityManager

Creating and executing query
Query q = entityManager.createQuery("DELETE FROM Client client where client.clientId                                       = 10");
q.executeUpdate();//delete row

Wednesday, February 15, 2012

How to create a popup in seam using <rich:ModalPanel/> component
(Seam, JSF, Richfaces)

In this popup a '' component is used as the popup.
In your page body, create a Modal Panel component as below. I am using the 'header' facet to display the title.

You can add more components such as tables, input fields, etc. between <rich:ModalPanel></rich:ModalPanel>
tags.
Look that I am using a command button to hide the popup.
<rich:modalPanel id="myPopup" width="600" height="400">
  <f:facet name="header"> 
    <h:panelGroup>
      <h:outputText value="Hello Popup!!"></h:outputText>
    </h:panelGroup>
  </f:facet>
  <a4j:commandButton value="Hide" id="btn_hide"
              onclick="Richfaces.hideModalPanel('myPopup');"/>
</rich:modalPanel>

That is it.
Now run your page and open it using a browser. It displays nothing. This is because components are hidden by default.
Now edit your page and add another button as below.

<a4j:commandButton value="Show" onclick="Richfaces.showModalPanel('myPopup');"/>

Note that in this button our javascript is "Richfaces.showModalPanel('myPopup');".
Now run your page and click on 'Show' button. You will see the popup. Now click the 'Hide' button and the popup will be closed.

Another Way To Show/ Hide Popup
Instead of using javascripts you can use a '<rich:componentControl/>' tags to show and hide the popup.
In that case your 'Show' button would be as below.
<a4j:commandButton value="Show" id="btn_show">
  <rich:componentControl for="myPopup" attachTo="btn_show"
        operation="show" event="onclick"/>
</a4j:commandButton>
'Hide' button can be changed as below.
<a4j:commandButton value="Hide" id="btn_hide">
  <rich:componentControl for="myPopup" attachTo="btn_hide" 
        operation="hide" event="onclick"/>
</a4j:commandButton>

Custom converter class
(Seam, JSF, Richfaces)

I'm going to show you how to write a custom converter class to let users to select and pass an Object using a JSF's <h:selectOneMenu/> component.
Since this component is finally rendered as a HTML 'select' element, it can't hold or submit Objects. So you need a converter class to convert the submitted value to matching Object type.
I am using following simple class as the seam component. Seam name of it is 'test'. (Instead of the seam component you can use a JSF managed bean also)
The combo box I'm going to use is filled with 'datesSelectList'.
The selected date is going to be set to 'selectedDate' variable.

import java.util.*;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.*;
import javax.faces.model.SelectItem;

@Name("test")
public class Test {
  List<SelectItem> datesSelectList = new ArrayList<SelectItem>();
  Date selectedDate;

  @Create
  public void init() {
    buildDatesSelectList();
  }

  public List<SelectItem> getDatesSelectList(){
    return datesSelectList;
  }

  public void buildDatesSelectList(){
    Date baseDate = new Date();
    Date date1 = new Date(baseDate.getTime());
    Date date2 = new Date(baseDate.getTime() + 5000);
    Date date3 = new Date(baseDate.getTime() + 10000);
    datesSelectList.add(new SelectItem(date1, "Date 1"));
    datesSelectList.add(new SelectItem(date2, "Date 2"));
    datesSelectList.add(new SelectItem(date3, "Date 3"));
  }

  public Date getSelectedDate() {
    return selectedDate;
  }

  public void setSelectedDate(Date selectedDate) {
    System.out.println("Date Selected : " + selectedDate);
    this.selectedDate = selectedDate;
  }
}

Your page would be as below.
<h:form>
  <h:selectOneMenu value="#{test.selectedDate}" id="sList">
    <s:selectItems value="#{test.datesSelectList}" var="_date"
       itemValue="#{_date.value}" label="#{_date.label}" />
    <f:converter converterId="myDateConverter"></f:converter>
  </h:selectOneMenu>
  <rich:message for="sList"/><br/>
  <a4j:commandButton value="submit"/>
</h:form>

Now it is time to write our converter class. To do that I will have to implement the interface 'javax.faces.convert.Converter'.
In this example the user selects a Date from combo box, and that date value should be set to the 'selectedDate' variable through 'setSelectedDate()' method.
So our converter should capture the label of the selected date('Date 1', 'Date 2' or 'Date 3') and find the relevant Date object.

package inova.erp.jbpm.temp;
import java.util.*;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.model.SelectItem;
import org.jboss.seam.Component;


public class DateConverter implements Converter {
  
  public Object getAsObject(FacesContext context, UIComponent uiComp, String dateString) {
    System.out.println("dateString = " + dateString);
    Object obj = Component.getInstance("test");
    Test test = (Test)obj;
    List<SelectItem> selectItems = test.getDatesSelectList();
    for(SelectItem sItem : selectItems) {
      if(sItem.getLabel().equals(dateString)) {
        return (Date)sItem.getValue();//Finding matching Date
         object for selected label (dateString) 
      }
    }
    return null;
  }
  public String getAsString(FacesContext context, UIComponent uiComp, Object dateObj) {
    return null;
  }
}

Now our work is almost ok. The final part is we have to introduce our new "DateConverter" class as a converter. We can do it in 'faces-config.xml' as below.
<converter>
  <converter-id>myDateConverter</converter-id>
  <converter-class>  
    inova.erp.jbpm.temp.DateConverter
  </converter-class>
</converter>

Now build and run the page. Then select a value from combo box and press 'Submit' button.
You will see the output similar to 'Date Selected : Tue Feb 14 19:51:24 IST 2012' in your console.
It indicates that you have successfully selected a Date Object using your combo box.
Now remove the line
  '<f:converter converterId="myDateConverter"></f:converter>'
 and try again. You will get an error at submission.

Note Followings:

  • You can add the "converter" attribute to the <h:selectOneMenu/> and remove the <f:converter/> component as below.
    <h:form>
      <h:selectOneMenu value="#{test.selectedDate}" id="sList" converter="myDateConverter">
        <s:selectItems value="#{test.datesSelectList}" var="_date"
           itemValue="#{_date.value}" label="#{_date.label}" />
      </h:selectOneMenu>
      <rich:message for="sList"/><br/>
      <a4j:commandButton value="submit"/>
    </h:form>
  • If you are using seam, you can use the seam annotation '@Converter' together withe '@Name' annotation to define the converter instead editing 'faces-config.xml'.

    import org.jboss.seam.annotations.Name;
    import org.jboss.seam.annotations.faces.Converter;
    import org.jboss.seam.annotations.intercept.BypassInterceptors;
    @Name("myDateConverter")
    @Converter
    @BypassInterceptors
    public class DateConverter implements javax.faces.convert.Converter {

Tuesday, February 14, 2012

Reflect live back end data on webpage using <a4j:poll/> component
(Seam, JSF, Richfaces)

You can use <a4j:poll/> component to do following things.
1. Run a java method periodically. (Ex:- Run auto save method)
2. Rerender another component periodically to display recent data from database.
3. Periodically run a javascript.
4. To accomplish three of above at the same time.

<h:form>
  <a4j:poll interval="1000" reRender="txtAlertCount" action="#{myBean.queryFromDb()}"/>
</h:form>
<h:outputText value="#{myBean.rowCount()}" id="txtAlertCount" />

Main attributes:
interval - time duration in milliseconds
action - method to be invoked in given intervals
reRender - Id of the component to be re-rendered
oncomplete - Javascript function to run after action is completed.
onsubmit - Javascript function to run before form submission

Friday, February 10, 2012

Format string with leading zeros to have a fixed length

For an example lets think that you need to format a string so that it always has the length of 8 characters. Following method always returns a String which has the length of 8.

private String formatString(String myString) {
   String baseString = "00000000";//8 zeros
   String tempString = baseString + myString;
   return tempString.substring(myString.length());
}