Showing posts with label JSF. Show all posts
Showing posts with label JSF. Show all posts

Wednesday, 19 February 2014

Getting data from ADF bindings programmatically

These are some useful code snippets to work with ADF bindings programmatically:

// get the binding container  
BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry();  
  
// get an ADF attributevalue from the ADF page definitions  
AttributeBinding attr = (AttributeBinding)bindings.getControlBinding("test");  
attr.setInputValue("test");  
  
// get an Action or MethodAction  
OperationBinding method = bindings.getOperationBinding("methodAction");  
method.execute();  
List errors = method.getErrors();  
  
method = bindings.getOperationBinding("methodAction");  
Map paramsMap = method.getParamsMap();  
paramsMap.put("param","value")  ;        
method.execute();  


// Get the data from an ADF tree or table  
DCBindingContainer dcBindings = 
        (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();  
  
FacesCtrlHierBinding treeData = (FacesCtrlHierBinding)bc.getControlBinding("tree");  
Row[] rows = treeData.getAllRowsInRange();  
  
// Get a attribute value of the current row of iterator  
DCIteratorBinding iterBind= (DCIteratorBinding)dcBindings.get("testIterator");  
String attribute = (String)iterBind.getCurrentRow().getAttribute("field1");  
  
// Get the error  
String error = iterBind.getError().getMessage();  

// refresh the iterator  
bindings.refreshControl();  
iterBind.executeQuery();  
iterBind.refresh(DCIteratorBinding.RANGESIZE_UNLIMITED);  
  
// Get all the rows of a iterator  
Row[] rows = iterBind.getAllRowsInRange();  
TestData dataRow = null;  
for (Row row : rows) {  
  dataRow = (TestData)((DCDataRow)row).getDataProvider();  
}  

// Get the current row of a iterator , a different way  
FacesContext ctx = FacesContext.getCurrentInstance();  
ExpressionFactory ef = ctx.getApplication().getExpressionFactory();  
ValueExpression ve = ef.createValueExpression(ctx.getELContext(), 
                "#{bindings.testIter.currentRow.dataProvider}", TestHead.class);  
TestHead test = (TestHead)ve.getValue(ctx.getELContext());  

 
// get the selected rows from a table component  
RowKeySet selection = resultTable.getSelectedRowKeys();  
Object[] keys = selection.toArray();  
List receivers = new ArrayList(keys.length);  
for ( Object key : keys ) {  
  User user = modelFriends.get((Integer)key);  
}  
  
// get  selected Rows of a table 2  
for (Object facesRowKey : table.getSelectedRowKeys()) {  
 table.setRowKey(facesRowKey);  
 Object o = table.getRowData();  
 JUCtrlHierNodeBinding rowData = (JUCtrlHierNodeBinding)o;  
 Row row = rowData.getRow();  
 Test testRow = (Test)((DCDataRow)row).getDataProvider() ;  
}  

Useful code snippets for ADF Security

Here are some useful code snippets related to ADF Security (thanks to Edwin Biemond blog):

// print the roles of the current user  
for ( String role : ADFContext.getCurrent().getSecurityContext().getUserRoles() ) {  
   System.out.println("role "+role);  
}  
  
// get the ADF security context and test if the user has the role users         
SecurityContext sec = ADFContext.getCurrent().getSecurityContext();  
if ( sec.isUserInRole("users") ) {  
}  

// is the user valid  
public boolean isAuthenticated() {  
 return ADFContext.getCurrent().getSecurityContext().isAuthenticated();  
}
  
// return the user  
public String getCurrentUser() {  
 return ADFContext.getCurrent().getSecurityContext().getUserName();  
}  

Saturday, 5 November 2011

How to call JavaScript from a ADF Managed Bean

To run a JS function from our Managed Bean, first put the JS code in a <af:resource> component in our JSF page:
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" 
           xmlns:f="http://java.sun.com/jsf/core" 
           xmlns:pe="http://xmlns.oracle.com/adf/pageeditor" 
           xmlns:cust="http://xmlns.oracle.com/adf/faces/customizable" 
           xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
<af:resource type="javascript">
    function myFunc() { [ any JS code ] };
</af:resource>

In the managed bean, put the following code in the appropriate method (e.g. in a action listener if we want to execute the JS code after a commandButton or commandLink is selected):
import javax.faces.context.FacesContext;
import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
import org.apache.myfaces.trinidad.util.Service;
[…]
FacesContext facesContext = FacesContext.getCurrentInstance(); 
ExtendedRenderKitService service = (ExtendedRenderKitService) org.apache.myfaces.trinidad.util.Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);
service.addScript(facesContext, "myFunc();");

Tuesday, 2 March 2010

CRUD generico con Java Reflection API, EclipseLink ed ICEFaces

Problema: abbiamo un nutrito insieme di tabelle anagrafiche con struttura simile (solitamente ID, DESCRIZIONE e pochi altri campi), e vorremmo offirire nella nostra applicazione Web le funzionalità CRUD su di esse. Quello che vogliamo assolutamente evitare è la replicazione del codice che offre queste funzionalità per ogni tabella da gestire: in sostanza, si desidera un approccio quanto più generico possibile al problema.


Voglio condividere con voi una soluzione a questo problema a mio avviso molto interessante, che fa uso estensivo delle Reflection API di Java, EJB 3.0, JPA (con EclipseLink 1.20 come persistence engine) per il backend, e IceFaces 1.8 per la parte grafica.