Skip to main content

Useful ADF Code

// 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();

}

// 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 a session bean

FacesContext ctx = FacesContext.getCurrentInstance();

ExpressionFactory ef = ctx.getApplication().getExpressionFactory();

ValueExpression ve = ef.createValueExpression(ctx.getELContext(), "#{testSessionBean}", TestSession.class);

TestSession test = (TestSession)ve.getValue(ctx.getELContext());

// main jsf page

DCBindingContainer dc = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();

// taskflow binding

DCTaskFlowBinding tf = (DCTaskFlowBinding)dc.findExecutableBinding("dynamicRegion1");

// pagedef of a page fragment

JUFormBinding form = (JUFormBinding) tf.findExecutableBinding("regions_employee_regionPageDef");

// handle to binding container of the region.

DCBindingContainer dcRegion = form;

// return a methodexpression like a control flow case action or ADF pagedef action

private MethodExpression getMethodExpression(String name) {

Class [] argtypes = new Class[1];

argtypes[0] = ActionEvent.class;

FacesContext facesCtx = FacesContext.getCurrentInstance();

Application app = facesCtx.getApplication();

ExpressionFactory elFactory = app.getExpressionFactory();

ELContext elContext = facesCtx.getELContext();

return elFactory.createMethodExpression(elContext,name,null,argtypes);

}



//

RichCommandMenuItem menuPage1 = new RichCommandMenuItem();

menuPage1.setId("page1");

menuPage1.setText("Page 1");

menuPage1.setActionExpression(getMethodExpression("page1"));



RichCommandButton button = new RichCommandButton();

button.setValueExpression("disabled",getValueExpression("#{!bindings."+item+".enabled}"));

button.setId(item);

button.setText(item);

MethodExpression me = getMethodExpression("#{bindings."+item+".execute}");

button.addActionListener(new MethodExpressionActionListener(me));

footer.getChildren().add(button);

// get a value

private ValueExpression getValueExpression(String name) {

FacesContext facesCtx = FacesContext.getCurrentInstance();

Application app = facesCtx.getApplication();

ExpressionFactory elFactory = app.getExpressionFactory();

ELContext elContext = facesCtx.getELContext();

return elFactory.createValueExpression(elContext, name, Object.class);

}

// an example how to use this

RichInputText input = new RichInputText();

input.setValueExpression("value",getValueExpression("#{bindings."+item+".inputValue}"));

input.setValueExpression("label",getValueExpression("#{bindings."+item+".hints.label}"));

input.setId(item);

panelForm.getChildren().add(input);

// catch an exception and show it in the jsf page

catch(Exception e) {

FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), "");

FacesContext.getCurrentInstance().addMessage(null, msg);

}



FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN, msgHead , msgDetail);

facesContext.addMessage(uiComponent.getClientId(facesContext), msg);

// reset all the child uicomponents

private void resetValueInputItems(AdfFacesContext adfFacesContext,

UIComponent component){

List<UIComponent> items = component.getChildren();

for ( UIComponent item : items )

{ resetValueInputItems(adfFacesContext,item);

if ( item instanceof RichInputText )

{ RichInputText input = (RichInputText)item;

if ( !input.isDisabled() )

{ input.resetValue() ;

adfFacesContext.addPartialTarget(input);

}

}

else if ( item instanceof RichInputDate )

{ RichInputDate input = (RichInputDate)item;

if ( !input.isDisabled() ) {

input.resetValue() ;

adfFacesContext.addPartialTarget(input);

};

}

}

}



// redirect to a other url

ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();

HttpServletResponse response = (HttpServletResponse)ectx.getResponse();

String url = ectx.getRequestContextPath()+"/adfAuthentication?logout=true&end_url=/faces/start.jspx";



try {

response.sendRedirect(url);

} catch (Exception ex) {

ex.printStackTrace();

}

// PPR refresh a jsf component

AdfFacesContext.getCurrentInstance().addPartialTarget(UIComponent);



// find a jsf component

private UIComponent getUIComponent(String name) {

FacesContext facesCtx = FacesContext.getCurrentInstance();

return facesCtx.getViewRoot().findComponent(name) ;

}



// get the adf bc application module

private OEServiceImpl getAm(){

FacesContext fc = FacesContext.getCurrentInstance();

Application app = fc.getApplication();

ExpressionFactory elFactory = app.getExpressionFactory();

ELContext elContext = fc.getELContext();

ValueExpression valueExp =

elFactory.createValueExpression(elContext, "#{data.OEServiceDataControl.dataProvider}",

Object.class);

return (OEServiceImpl)valueExp.getValue(elContext);

}



// change the locale

Locale newLocale = new Locale(this.language);

FacesContext context = FacesContext.getCurrentInstance();

context.getViewRoot().setLocale(newLocale);



// get the stacktrace of a not handled exception

private ControllerContext cc = ControllerContext.getInstance();

public String getStacktrace() {

if ( cc.getCurrentViewPort().getExceptionData()!=null ) {

StringWriter sw = new StringWriter();

PrintWriter pw = new PrintWriter(sw);

cc.getCurrentViewPort().getExceptionData().printStackTrace(pw);

return sw.toString();

}

return null;

}

// get the selected rows from a table component

RowKeySet selection = resultTable.getSelectedRowKeys();

Object[] keys = selection.toArray();

List<Long> receivers = new ArrayList<Long>(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() ;

}

Comments

Popular posts from this blog

Passivation and Activation in ADF (Application Module )

1. For performance reasons, ADF keeps a pool of application modules in memory. It tries to give each session the same application module as the session used during the last request; however, this might not be possible during peak load of your application. 2. In this case, ADF saves the application modules state in a database table so the application module can be used by another session. This is called passivation . 3. When the first session needs the application module again, its state is retrieved from the database process known as activation . 4. If you have made an error in your code and depend on some variable that is not persisted correctly when your application module state is stored, you will experience mysterious errors under high load.   Enable/Disable Application Module Pooling : Right-click on your application module, choose Configurations.By default, each application module has two configurations. Ensure that the one ending in …Local is selected and then c...

Get modified rows from Entitiy Cache

To get the modified rows from entity cache we have getEntityState() method at EntityImpl class. Refer to my previous blog  Accessing EO impl methods from VO impl  where i am overriding the getEntityState() in EOimpl and calling it in VOImpl. We can use methods written or overridden in VOImpl class to AMImpl class. There are different states associated with an entity object. STATUS_UNMODIFIED STATUS_MODIFIED STATUS_NEW STATUS_DELETED STATUS_DEAD We have to check the state or row in our AmImpl class by using the VOImpl method and through this we can distinguish the rows present at vo. Add below code in AMImpl class along with my previous post. public void geCachedRowsCount(){         JobsVOImpl jobsVo = (JobsVOImpl)this.getJobsVO();         RowSetIterator iter = jobsVo.createRowSetIterator(null);             while(iter.hasNext()){             Row row = iter....

The file store "WLS_DIAGNOSTICS" could not be opened

WLS_DIAGNOSTIC ERROR weblogic.store.PersistentStoreException: [Store:280073]The file store "WLS_DIAGNOSTICS" could not be opened because it contained a file with the invalid version 1. A file of version 2 was expected. When you get this error while running your application on internal weblogic server delete the following file WLS_DIAGNOSTICS000000.DAT search the file in following path C:\jdev_work\system11.1.1.5.37.60.13\DefaultDomain this file is in DefaultDomain folder of your jdev. and delete the WLS_DIAGNOSTICS000000.DAT file . and run your applicatuon