Thursday 4 August 2016

Which Context used at Which Time in Oracle ADF

Some handy code for your managed Beans ( ADF & JSF )

Back in 2009, I already a made a blogpost about some handy code which you can use in your ADF Web Application. You can say this blogspot is part 2 and here I will show you the code, I use most  in my own managed Beans.

I start with FacesContext class, with this class you can use to find a JSF Component, change the Locale, get the ELContext, Add a message to your view and get the ExternalContext
// FacesContext
FacesContext facesCtx = FacesContext.getCurrentInstance();
// find UIComponent
UIComponent input = facesCtx.getViewRoot().findComponent("f1");
// change the locale
facesCtx.getViewRoot().setLocale( Locale.ENGLISH);
// el expression
Application app = facesCtx.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = facesCtx.getELContext();
ValueExpression valueExp = elFactory.createValueExpression(elContext,
"#{xxxx}",
Object.class);
Object result = valueExp.getValue(elContext);
// add a message
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN,
"header",
"detail");
facesCtx.addMessage(input.getClientId(facesCtx), msg);
// ExternalContext
ExternalContext ectx = facesCtx.getExternalContext();
view rawFacesContext.java hosted with ❤ by GitHub
The ExternalContext class, with this you can retrieve all the java init & context (web.xml) parameters, the Request & Session parameters and your web application url.
// ExternalContext
ExternalContext ectx = facesCtx.getExternalContext();
// all the java init parameters
Map<String, Object> initParamsVar = ectx.getInitParameterMap();
Map<String, String> requestParamsVar = ectx.getRequestParameterMap();
Map<String, Object> sessionParamsVar = ectx.getSessionMap();
// web application context root
String contextPath = ectx.getRequestContextPath();
view rawExternalContext.java hosted with ❤ by GitHub
AdfFacesContext class, you can use this class for Partial Page Rendering ( PPR), get the PageFlowScope and ViewScope variables
// AdfFacesContext
AdfFacesContext adfFacesCtx = AdfFacesContext.getCurrentInstance();
// PPR
adfFacesCtx.addPartialTarget(input);
// get the PageFlowScope Params
Map<String, Object> scopePageFlowScopeVar= adfFacesCtx.getPageFlowScope();
// get the viewScope Params
Map<String, Object> scopeViewScopeVar= adfFacesCtx.getViewScope();
view rawAdfFacesContext.java hosted with ❤ by GitHub
ADFContext class, with this you can get all the memory scopes variables even the application scope variables, ELContext and the SecurityContext.
// ADFContext
ADFContext adfCtx = ADFContext.getCurrent();
// Get the scope variables
Map<String, Object> applicationVar2 = adfCtx.getApplicationScope();
Map<String, Object> pageParamsVar2 = adfCtx.getPageFlowScope();
Map<String, String> requestParamsVar2 = adfCtx.getRequestScope();
Map<String, Object> sessionParamsVar2 = adfCtx.getSessionScope();
// el expression
ELContext elContext2 = adfCtx.getELContext();
ExpressionFactory elFactory2 = adfCtx.getExpressionFactory();
ValueExpression valueExp2 = elFactory2.createValueExpression(elContext2,
"#{xxxx}",
Object.class);
Object result2 = valueExp2.getValue(elContext2);
// Security
SecurityContext secCntx = adfCtx.getSecurityContext();
view rawADFContext.java hosted with ❤ by GitHub
SecurityContext class, retrieve the current user and its roles.
// Security
SecurityContext secCntx = adfCtx.getSecurityContext();
String user = secCntx.getUserName();
String[] roles = secCntx.getUserRoles();
view rawSecurityContext.java hosted with ❤ by GitHub
BindingContext, BindingContainer and DCBindingContainer class. These classes are well known when you want to retrieve the ADF pagedef objects.
BindingContext bc = BindingContext.getCurrent();
BindingContainer bcon = bc.getCurrentBindingsEntry();
List<AttributeBinding> attr = bcon.getAttributeBindings();
List<OperationBinding> oper = bcon.getOperationBindings();
List<ControlBinding> ctrl = bcon.getControlBindings();
DCBindingContainer dcbcon = (DCBindingContainer) bc.getCurrentBindingsEntry();
List<AttributeBinding> attr2 = dcbcon.getAttributeBindings();
List<OperationBinding> oper2 = dcbcon.getOperationBindings();
List<ControlBinding> ctrl2 = dcbcon.getControlBindings();
List iters = dcbcon.getIterBindingList();
List exec = dcbcon.getExecutableBindings();
view rawBindingContext.java hosted with ❤ by GitHub
The last class is ControllerContext, which you can use to retrieve the exceptions
ControllerContext cc = ControllerContext.getInstance();
// get the exception
Exception exp = cc.getCurrentViewPort().getExceptionData();

No comments:

Post a Comment