Wednesday, June 25, 2014

Top adf code snippets for common tasks

Hello there!  Today let’s see the top commonly used ADF snippets (at least that’s what I thought while starting to share itso here you go with the entries.


1.      Getting the page bindings


This is commonly used to get the page bindings in the managed bean to get either specific binding values or get a hold on the action for the binding.

1:    public static BindingContainer getBindingContainer() {  
2:      FacesContext facesContext = FacesContext.getCurrentInstance();  
3:      Application app = facesContext.getApplication();  
4:      ExpressionFactory elFactory = app.getExpressionFactory();  
5:      ELContext elContext = facesContext.getELContext();  
6:      ValueExpression valueExp = elFactory.createValueExpression(elContext, "#{bindings}", Object.class);  
7:      return (BindingContainer)valueExp.getValue(elContext);  
8:    }  

2.     Getting hold off the Application Module


Application modules are required to do any sort of tasks binding the view object and the view links and all wrapped around by services. So needless to say that whenever there is a need to call your service getting to your AM is your first step.


1:    public static ApplicationModule getApplicationModuleForDataControl(String name) {  
2:      BindingContext bindingContext = BindingContext.getCurrent();  
3:      ApplicationModule appModule = null;  
4:      if (null != bindingContext) {  
5:        DCDataControl dc = bindingContext.findDataControl(name);  
6:        if (dc != null) {  
7:          appModule = (ApplicationModule)dc.getDataProvider();  
8:        }  
9:      }  
10:      return appModule;  
11:    }  


3.     Resource Bundle Properties


     There are scenarios where we want to get the property values from the resource bundle in our beans and this happens more often than you think!

1:    public static String getResourceProperty(String label, Locale userLocale) {  
2:      String bundle_name = "com.example.mybundle";  
3:      String txt = null;  
4:      try {  
5:        ResourceBundle bundle = BundleFactory.getBundle(bundle_name, userLocale);  
6:        if (bundle != null) {  
7:          txt = bundle.getString(label);  
8:        }  
9:      } catch (Exception e) {  
10:        logger.severe("Problem in loading resource bundle: " + bundle_name, e.getMessage());  
11:      }  
12:      return txt;  
13:    }  

4.     Is the request a PPR?


     Ever wondered if a request is a normal full request or a partial page request, well the following snippet gives you that information. This is a very useful thing to know when you try to filter out requests in your listeners.

1:    public static boolean isPprRequest() {  
2:      FacesContext facesContext = FacesContext.getCurrentInstance();  
3:      return AdfFacesContext.getCurrentInstance().isPartialRequest(facesContext);  
4:    }  

5.     Retrieving the request headers from within managed bean


     We can get the request headers from the bean using the following snippet


1:  FacesContext facesContext = FacesContext.getCurrentInstance();  
2:  ExternalContext externalContext = facesContext.getExternalContext();  
3:  Map requestHeaderMap = externalContext.getRequestHeaderMap();  


6.     Accessing request and response objects from a bean




1:  FacesContext facesContext = FacesContext.getCurrentInstance();  
2:  ExternalContext externalContext = facesContext.getExternalContext();  
3:  HttpServletResponse response = (HttpServletResponse)externalContext.getResponse();  
4:  HttpServletRequest request = (HttpServletRequest)facesContext.getExternalContext().getRequest(); 


7.     Putting and retrieving from session scope


     Quick ways to manage objects in session scope.


1:  ADFContext.getCurrent().getSessionScope().put("abc", abcValue);  
2:  ADFContext.getCurrent().getSessionScope().remove("abc");  
3:  ADFContext.getCurrent().getSessionScope().get("abc");  

8.     Adding partial targets at runtime



     We could add partial targets to components at runtime so that the property changes made in a bean method to a previously non target component reflects in UI.

1:  AdfFacesContext.getCurrentInstance().addPartialTarget(myComponent); 


9.     Adding JavaScript to response



     More often than not we might be required to trigger some external Javascript on click of a button. To achieve it we use the following code.

1:    public static void addScriptOnPartialRequest(String script) {  
2:      logger.fine("Adding Java Script " + script);  
3:      FacesContext context = FacesContext.getCurrentInstance();  
4:      if (AdfFacesContext.getCurrentInstance().isPartialRequest(context)) {  
5:        ExtendedRenderKitService erks = Service.getRenderKitService(context, ExtendedRenderKitService.class);  
6:        erks.addScript(context, script);  
7:      }  
8:    }  


10. Get the current Locale


     Snippet to get the current locale


1:  public static Locale getLocale() {  
2:  ADFContext adfctx = ADFContext.getCurrent();  
3:  return adfctx.getLocale();  
4:  }  



No comments :

Post a Comment

your comments welcomed !