JSF UTIL class in Oracle ADF

Well i am refering here the jsfutil class for my reference.this code doesn’t written by me.But it is very helpful for ADF developer.

 import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import oracle.adf.model.binding.DCBindingContainer;
/**
* General useful static utilies for working with JSF.
* NOTE: Updated to use JSF 1.2 ExpressionFactory.
*
*/
public class JSFUtils {
private static final String NO_RESOURCE_FOUND = "Missing resource: ";
/**
* Method for taking a reference to a JSF binding expression and returning
* the matching object (or creating it).
* @param expression EL expression
* @return Managed object
*/
public static Object resolveExpression(String expression) {
FacesContext facesContext = getFacesContext();
Application app = facesContext.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = facesContext.getELContext();
ValueExpression valueExp =
elFactory.createValueExpression(elContext, expression,
Object.class);
return valueExp.getValue(elContext);
}
public static String resolveRemoteUser() {
FacesContext facesContext = getFacesContext();
ExternalContext ectx = facesContext.getExternalContext();
return ectx.getRemoteUser();
}
public static String resolveUserPrincipal() {
FacesContext facesContext = getFacesContext();
ExternalContext ectx = facesContext.getExternalContext();
HttpServletRequest request = (HttpServletRequest)ectx.getRequest();
return request.getUserPrincipal().getName();
}
public static Object resloveMethodExpression(String expression,
Class returnType,
Class[] argTypes,
Object[] argValues) {
FacesContext facesContext = getFacesContext();
Application app = facesContext.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = facesContext.getELContext();
MethodExpression methodExpression =
elFactory.createMethodExpression(elContext, expression, returnType,
argTypes);
return methodExpression.invoke(elContext, argValues);
}
/**
* Method for taking a reference to a JSF binding expression and returning
* the matching Boolean.
* @param expression EL expression
* @return Managed object
*/
public static Boolean resolveExpressionAsBoolean(String expression) {
return (Boolean)resolveExpression(expression);
}
/**
* Method for taking a reference to a JSF binding expression and returning
* the matching String (or creating it).
* @param expression EL expression
* @return Managed object
*/
public static String resolveExpressionAsString(String expression) {
return (String)resolveExpression(expression);
}
/**
* Convenience method for resolving a reference to a managed bean by name
* rather than by expression.
* @param beanName name of managed bean
* @return Managed object
*/
public static Object getManagedBeanValue(String beanName) {
StringBuffer buff = new StringBuffer("#{");
buff.append(beanName);
buff.append("}");
return resolveExpression(buff.toString());
}
/**
* Method for setting a new object into a JSF managed bean
* Note: will fail silently if the supplied object does
* not match the type of the managed bean.
* @param expression EL expression
* @param newValue new value to set
*/
public static void setExpressionValue(String expression, Object newValue) {
FacesContext facesContext = getFacesContext();
Application app = facesContext.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = facesContext.getELContext();
ValueExpression valueExp =
elFactory.createValueExpression(elContext, expression,
Object.class);
//Check that the input newValue can be cast to the property type
//expected by the managed bean.
//If the managed Bean expects a primitive we rely on Auto-Unboxing
Class bindClass = valueExp.getType(elContext);
if (bindClass.isPrimitive() || bindClass.isInstance(newValue)) {
valueExp.setValue(elContext, newValue);
}
}
/**
* Convenience method for setting the value of a managed bean by name
* rather than by expression.
* @param beanName name of managed bean
* @param newValue new value to set
*/
public static void setManagedBeanValue(String beanName, Object newValue) {
StringBuffer buff = new StringBuffer("#{");
buff.append(beanName);
buff.append("}");
setExpressionValue(buff.toString(), newValue);
}
/**
* Convenience method for setting Session variables.
* @param key object key
* @param object value to store
*/
public static void storeOnSession(String key, Object object) {
FacesContext ctx = getFacesContext();
Map sessionState = ctx.getExternalContext().getSessionMap();
sessionState.put(key, object);
}
/**
* Convenience method for getting Session variables.
* @param key object key
* @return session object for key
*/
public static Object getFromSession(String key) {
FacesContext ctx = getFacesContext();
Map sessionState = ctx.getExternalContext().getSessionMap();
return sessionState.get(key);
}
public static String getFromHeader(String key) {
FacesContext ctx = getFacesContext();
ExternalContext ectx = ctx.getExternalContext();
return ectx.getRequestHeaderMap().get(key);
}
/**
* Convenience method for getting Request variables.
* @param key object key
* @return session object for key
*/
public static Object getFromRequest(String key) {
FacesContext ctx = getFacesContext();
Map sessionState = ctx.getExternalContext().getRequestMap();
return sessionState.get(key);
}
/**
* Pulls a String resource from the property bundle that
* is defined under the application <message-bundle> element in
* the faces config. Respects Locale
* @param key string message key
* @return Resource value or placeholder error String
*/
public static String getStringFromBundle(String key) {
ResourceBundle bundle = getBundle();
return getStringSafely(bundle, key, null);
}
/**
* Convenience method to construct a FacesMesssage
* from a defined error key and severity
* This assumes that the error keys follow the convention of
* using _detail for the detailed part of the
* message, otherwise the main message is returned for the
* detail as well.
* @param key for the error message in the resource bundle
* @param severity severity of message
* @return Faces Message object
*/
public static FacesMessage getMessageFromBundle(String key,
FacesMessage.Severity severity) {
ResourceBundle bundle = getBundle();
String summary = getStringSafely(bundle, key, null);
String detail = getStringSafely(bundle, key + "_detail", summary);
FacesMessage message = new FacesMessage(summary, detail);
message.setSeverity(severity);
return message;
}
/**
* Add JSF info message.
* @param msg info message string
*/
public static void addFacesInformationMessage(String msg) {
FacesContext ctx = getFacesContext();
FacesMessage fm =
new FacesMessage(FacesMessage.SEVERITY_INFO, msg, "");
ctx.addMessage(getRootViewComponentId(), fm);
}
/**
* Add JSF error message.
* @param msg error message string
*/
public static void addFacesErrorMessage(String msg) {
FacesContext ctx = getFacesContext();
FacesMessage fm =
new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, "");
ctx.addMessage(getRootViewComponentId(), fm);
}
/**
* Add JSF error message for a specific attribute.
* @param attrName name of attribute
* @param msg error message string
*/
public static void addFacesErrorMessage(String attrName, String msg) {
FacesContext ctx = getFacesContext();
FacesMessage fm =
new FacesMessage(FacesMessage.SEVERITY_ERROR, attrName, msg);
ctx.addMessage(getRootViewComponentId(), fm);
}
// Informational getters
/**
* Get view id of the view root.
* @return view id of the view root
*/
public static String getRootViewId() {
return getFacesContext().getViewRoot().getViewId();
}
/**
* Get component id of the view root.
* @return component id of the view root
*/
public static String getRootViewComponentId() {
return getFacesContext().getViewRoot().getId();
}
/**
* Get FacesContext.
* @return FacesContext
*/
public static FacesContext getFacesContext() {
return FacesContext.getCurrentInstance();
}
/*
* Internal method to pull out the correct local
* message bundle
*/
private static ResourceBundle getBundle() {
FacesContext ctx = getFacesContext();
UIViewRoot uiRoot = ctx.getViewRoot();
Locale locale = uiRoot.getLocale();
ClassLoader ldr = Thread.currentThread().getContextClassLoader();
return ResourceBundle.getBundle(ctx.getApplication().getMessageBundle(),
locale, ldr);
}
/**
* Get an HTTP Request attribute.
* @param name attribute name
* @return attribute value
*/
public static Object getRequestAttribute(String name) {
return getFacesContext().getExternalContext().getRequestMap().get(name);
}
/**
* Set an HTTP Request attribute.
* @param name attribute name
* @param value attribute value
*/
public static void setRequestAttribute(String name, Object value) {
getFacesContext().getExternalContext().getRequestMap().put(name,
value);
}
/*
* Internal method to proxy for resource keys that don't exist
*/
private static String getStringSafely(ResourceBundle bundle, String key,
String defaultValue) {
String resource = null;
try {
resource = bundle.getString(key);
} catch (MissingResourceException mrex) {
if (defaultValue != null) {
resource = defaultValue;
} else {
resource = NO_RESOURCE_FOUND + key;
}
}
return resource;
}
/**
* Locate an UIComponent in view root with its component id. Use a recursive way to achieve this.
* @param id UIComponent id
* @return UIComponent object
*/
public static UIComponent findComponentInRoot(String id) {
UIComponent component = null;
FacesContext facesContext = FacesContext.getCurrentInstance();
if (facesContext != null) {
UIComponent root = facesContext.getViewRoot();
component = findComponent(root, id);
}
return component;
}
/**
* Locate an UIComponent from its root component.
* Taken from http://www.jroller.com/page/mert?entry=how_to_find_a_uicomponent
* @param base root Component (parent)
* @param id UIComponent id
* @return UIComponent object
*/
public static UIComponent findComponent(UIComponent base, String id) {
if (id.equals(base.getId()))
return base;
UIComponent children = null;
UIComponent result = null;
Iterator childrens = base.getFacetsAndChildren();
while (childrens.hasNext() && (result == null)) {
children = (UIComponent)childrens.next();
if (id.equals(children.getId())) {
result = children;
break;
}
result = findComponent(children, id);
if (result != null) {
break;
}
}
return result;
}
/**
* Method to create a redirect URL. The assumption is that the JSF servlet mapping is
* "faces", which is the default
*
* @param view the JSP or JSPX page to redirect to
* @return a URL to redirect to
*/
public static String getPageURL(String view) {
FacesContext facesContext = getFacesContext();
ExternalContext externalContext = facesContext.getExternalContext();
String url =
((HttpServletRequest)externalContext.getRequest()).getRequestURL().toString();
StringBuffer newUrlBuffer = new StringBuffer();
newUrlBuffer.append(url.substring(0, url.lastIndexOf("faces/")));
newUrlBuffer.append("faces");
String targetPageUrl = view.startsWith("/") ? view : "/" + view;
newUrlBuffer.append(targetPageUrl);
return newUrlBuffer.toString();
}

// Get the error


String error = iterBind.getError().getMessage();













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

// find a jsf component

private UIComponent getUIComponent(String name) {

FacesContext facesCtx = FacesContext.getCurrentInstance();

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

}

// change the locale

Locale newLocale = new Locale(this.language);

FacesContext context = FacesContext.getCurrentInstance();

context.getViewRoot().setLocale(newLocale);
} 


Happy coding with Vinay in Techartifact.

@PostConstruct & @PreDestroy annotation in JSF

Quick JSF tip-

I found these two annotation really useful and very tricky too.It should be used at right time otherwise you will see error like me. 🙂

@PostConstruct as per Oracle Doc-
The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization. This method MUST be invoked before the class is put into service. This annotation MUST be supported on all classes that support dependency injection. The method annotated with PostConstruct MUST be invoked even if the class does not request any resources to be injected. Only one method can be annotated with this annotation. The method on which the PostConstruct annotation is applied MUST fulfill all of the following criteria – – The method MUST NOT have any parameters except in the case of EJB interceptors in which case it takes an InvocationC ontext object as defined by the EJB specification. – The return type of the method MUST be void. – The method MUST NOT throw a checked exception. – The method on which PostConstruct is applied MAY be public, protected, package private or private. – The method MUST NOT be static except for the application client. – The method MAY be final. – If the method throws an unchecked exception the class MUST NOT be put into service except in the case of EJBs where the EJB can handle exceptions and even recover from them.

ok .Quite good description and confusing too.

To Initialize a Managed Bean we use the @PostConstruct Annotation.As we see that @PostConstruct Annotation is mostly used to initialize the resources that are Context specific. The method which you marked with @PostConstruct Annotation will be called immediately by the Container as soon as an instance of bean is created. There are few certain guidelines to be followed, while creating the PostConstruct method such as-

– The method should not be marked as static.
– Return type should be void.
– It should not throw any checked Exceptions and so on….

I did a mistake of calling a bean and i got some null on the bean.Not sure what is problem. After spending my few hours on it 🙁 .
I found the problem.I am calling before the instantiation of the bean.after using this annotation, it resolved the issue , so sharing with everyone.

I have a method which is clearing all the value .i used annotation like this

@PostConstruct
public void clear() {
    this.userName = "";
    this.userBal= 0;
    this.maximum = maxNumber;
    this.number = randomInt.get(); 
}


@PreDestroy-
counter-part to @PostConstruct Annotation is the @PreDestroy Annotation. As the name specify, we know that the method that is marked with this Annotation will be called before object is going to be removed or destroyed by the Container. Like the @PostConstruct Annotation, this is also a method-Level Annotation and i used like this

@PreDestroy()
    public void releaseConnection()
    {
 
        // Close the Connection.
     
    }

method releaseConnection() will call by the container, before the object is going to be destroyed. In Jee 6 ,CDI calls this method before starting to destroy the bean.

Generate id using dbsequence in Oracle ADF Without Losing it .

Requirements: Populate Primary key or ID using dbsequence in Oracle ADF only during database commit

Solution: In my previous post of generating the sequence generating sequence in ADF ,previous post ,But the problem is you will lose sequence at time of error or cancel time.

Following changes to achieve this requirement

1. Go the entity and put a default value as some -999 or -9999 (This cant be your dbsequence value, preferably negative values)

2. Generate EntityImpl.class file and add below lines of code to the file at the end.

Note: Here we are overriding doDML() method of EntityImpl.class so that it will the get the value from db sequence just before commit to database.

    public void doDML(int operation, TransactionEvent e) {
        this.setEmployeeId((Integer)(new SequenceImpl("Vinaytableid_seq",getDBTransaction()).getSequenceNumber()).intValue());
        super.doDML(operation, e);
    }

Note2: it is not a mandatory rule to modify doDML() method only, you can use prepareForDML() method as well if you like. As doDML() is the last method that gets fired before beforeCommit() method, I preferred it.

3. In UI page, users don’t want to see any negative values initially, to show it as blank, just put an ELExpression to hide it whenever the value is negative.i.e
#{bindings.ContractId.inputValue == -1 ? ” : bindings.EmployeeId.inputValue}

Happy coding with techartifact