Friday 7 June 2013

commiting the record from bean

In many situation we need to get binding container from our backingbean to execute some of the operation as ( Commit - Rollback - Next - Last - Execute With Param - ............. ) or get your Iterator to get some information as (get Current Row - get View Object - get number of row count - refresh iterator - .............. ) There are two way to get your binding container from backingbean :

1-BindingContainer bindngs=BindingContext.getCurrent().getCurrentBindingsEntry();
then you can use bindings object to execute some of binding operation as commit operation as:


OperationBinding operationBinding = bindings.getOperationBinding("Commit");
operationBinding.execute();


2- The second way to get binding container is to define a method that return binding container as :

private BindingContainer bindings;
public BindingContainer getBindings() {
if (this.bindings == null) {
FacesContext fc=FacesContext.getCurrentInstance();
this.bindings = (BindingContainer)fc.getApplication().evaluateExpressionGet(fc,"#{bindings}",
BindingContainer.class);
}
return this.bindings;
}

to get binding and execute a commit operation :
BindingContainer bindings = getBindings();
OperationBinding operationBinding = bindings.getOperationBinding("Commit");
Object result = operationBinding.execute();
if (!operationBinding.getErrors().isEmpty()) {
return null;
}

this is also use ful code when creating a button through managedbean


BindingContainer bindings = (BindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
OperationBinding operationBinding = (OperationBinding)bindings.getOperationBinding("CreateInsert");
Object result = operationBinding.execute();
if (!operationBinding.getErrors().isEmpty())
{
FacesMessage fm = new FacesMessage(operationBinding.getErrors().get(0).toString());
fm.setSeverity(FacesMessage.SEVERITY_ERROR);
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, fm);
}
   

No comments:

Post a Comment