package test.util;
import com.sun.faces.mgbean.ManagedBeanCreationException;
import java.util.HashMap;
import java.util.Map;
import javax.el.ELContext;
import javax.el.Expression;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import javax.el.ValueExpression;
import javax.faces.application.FacesMessage;
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseId;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import oracle.adf.model.BindingContext;
import oracle.adf.model.DataControlFrame;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCDataControl;
import oracle.adf.view.rich.component.rich.RichPopup;
import oracle.adf.view.rich.component.rich.fragment.RichRegion;
import oracle.adf.view.rich.component.rich.layout.RichPanelSplitter;
import oracle.adf.view.rich.context.AdfFacesContext;
import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
import org.apache.myfaces.trinidad.util.Service;
/**
* Provides various utility methods that are handy to
* have around when working with ADF.
*/
public class ADFUtil {
/**
* When a bounded task flow manages a transaction (marked as
* requires-transaction, requires-new-transaction, or requires-
* existing-transaction), then the task flow must issue any
* commits or rollbacks that are needed.
* This is essentially to keep the state of the transaction that
* the task flow understands in synch with the state of the
* transaction in the ADFbc layer.
* Use this method to issue a commit in the middle of a task flow
* while staying in the task flow.
*/
public static void saveAndContinue() {
Map sessionMap =
FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
BindingContext context =
(BindingContext)sessionMap.get(BindingContext.CONTEXT_ID);
String currentFrameName = context.getCurrentDataControlFrame();
DataControlFrame dcFrame =
context.findDataControlFrame(currentFrameName);
dcFrame.commit();
dcFrame.beginTransaction(null);
}
/**
* Programmatic evaluation of EL.
*
* @param el EL to evaluate
* @return Result of the evaluation
*/
public static Object evaluateEL(String el) {
FacesContext facesContext = FacesContext.getCurrentInstance();
ELContext elContext = facesContext.getELContext();
ExpressionFactory expressionFactory =
facesContext.getApplication().getExpressionFactory();
ValueExpression exp =
expressionFactory.createValueExpression(elContext, el,
Object.class);
if(exp == null || elContext == null){
return null;
}
Object obj = null;
try{
obj = exp.getValue(elContext);
}catch(ManagedBeanCreationException ex){
}
return obj;
}
public static String getLocaleValue(String el,String code){
Map map = (Map)evaluateEL(el);
if(map != null){
if(map.get(code) != null)
return (String)map.get(code);
else
return code;
}
return "";
}
/**
* Programmatic invocation of a method that an EL evaluates to.
* The method must not take any parameters.
* @param el EL of the method to invoke
* @return Object that the method returns
*/
public static Object invokeEL(String el) {
return invokeEL(el, new Class[0], new Object[0]);
}
/**
* Programmatic invocation of a method that an EL evaluates to.
*
* @param el EL of the method to invoke
* @param paramType Class defining the type of the
* parameter
* @param param Object defining the value of the
* parameter
* @return Object that the method returns
*/
public static Object invokeEL(String el, Class paramType, Object param) {
return invokeEL(el, new Class[] { paramType }, new Object[] { param });
}
/**
* Programmatic invocation of a method that an EL evaluates to.
*
* @param el EL of the method to invoke
* @param paramTypes Array of Class defining the types of the
* parameters
* @param params Array of Object defining the values of the
* parametrs
* @return Object that the method returns
*/
public static Object invokeEL(String el, Class[] paramTypes,
Object[] params) {
FacesContext facesContext = FacesContext.getCurrentInstance();
ELContext elContext = facesContext.getELContext();
ExpressionFactory expressionFactory =
facesContext.getApplication().getExpressionFactory();
MethodExpression exp =
expressionFactory.createMethodExpression(elContext, el,
Object.class, paramTypes);
return exp.invoke(elContext, params);
}
/**
* Sets a value into an EL object. Provides similar
* functionality to
* the <af:setActionListener> tag, except the
* <code>from</code> is
* not an EL. You can get similar behavior by using the
* following...<br>
* <code>setEL(<b>to</b>, evaluateEL(<b>from</b>))</code>
*
* @param el EL object to assign a value
* @param val Value to assign
*/
public static void setEL(String el, Object val) {
FacesContext facesContext = FacesContext.getCurrentInstance();
ELContext elContext = facesContext.getELContext();
ExpressionFactory expressionFactory =
facesContext.getApplication().getExpressionFactory();
ValueExpression exp =
expressionFactory.createValueExpression(elContext, el,
Object.class);
exp.setValue(elContext, val);
}
public static String errorDialog(String errorMessage)
{
FacesContext context = FacesContext.getCurrentInstance();
((HttpSession)context.getExternalContext().getSession(false)).setAttribute("errorMessage", errorMessage);
ViewHandler viewHandler = context.getApplication().getViewHandler();
UIViewRoot dialog = viewHandler.createView(context, "/Error.jspx");
HashMap properties = new HashMap();
properties.put("width", new Integer(250));
properties.put("height", new Integer(150));
AdfFacesContext afContext = AdfFacesContext.getCurrentInstance();
afContext.launchDialog(dialog,
null, // not launched from any component
null, // no particular parameters
true,properties); // show it in a dialog
context.renderResponse();
return "";
}
/**
*Get the ADF datacontrol reference given the control name
* @param controlName data control name as given in data bindings file
* @return
*/
public static DCDataControl findDataControl(String controlName){
DCBindingContainer context = (DCBindingContainer)ADFUtil.evaluateEL("#{bindings}");
DCDataControl dcf = context.findDataControl(controlName);
return dcf;
}
/**
* Initiates control flow in a task flow region.
* @param region - RichRegion that represents taskflow.
* @param outComeEl - action outcome, based on whic control flows
*/
public static void performControlFlow(RichRegion region, String outComeEl){
FacesContext ctx = FacesContext.getCurrentInstance();
ExpressionFactory f = ctx.getApplication().getExpressionFactory();
ELContext elctx = ctx.getELContext();
MethodExpression m = f.createMethodExpression(elctx,outComeEl, String.class, new Class[]{});
region.queueActionEventInRegion(m,null,null,false,-1,-1,PhaseId.ANY_PHASE);
}
public static HttpServletRequest getHttpServletRequest(){
return ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest());
}
public static HttpSession getHttpSession(){
return ((HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false));
}
/**
* Returns the Binding Container
* @return
*/
public static DCBindingContainer getBindingContainer(){
return (DCBindingContainer)evaluateEL("#{bindings}");
}
/**
* Renders Faces message showing some validation error message.
* @param id
* @param message
*/
public static void addFacesMessage(String id, FacesMessage message){
FacesContext.getCurrentInstance().addMessage(id, message);
FacesContext.getCurrentInstance().renderResponse();
}
public static void addFacesMessage(String clientId,javax.faces.application.FacesMessage.Severity severity,String localizedText){
FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(severity, "",localizedText));
FacesContext.getCurrentInstance().renderResponse();
}
public static String showError(String clientId,String errorMessage) {
FacesContext ctx = FacesContext.getCurrentInstance();
StringBuilder javaScriptPopup =
new StringBuilder("var popObj=AdfPage.PAGE.findComponent('" +
clientId + "');");
javaScriptPopup.append("if(popObj != null){var errorField = popObj.findComponent('errorMessage');if(errorField != null)");
javaScriptPopup.append("{errorField.setValue('");
javaScriptPopup.append(errorMessage);
javaScriptPopup.append("')}");
javaScriptPopup.append("popObj.show();}");
ExtendedRenderKitService erks =
Service.getRenderKitService(ctx, ExtendedRenderKitService.class);
erks.addScript(ctx, javaScriptPopup.toString());
ctx.renderResponse();
return "";
}
public static void showPopup(String clientId){
FacesContext ctx = FacesContext.getCurrentInstance();
StringBuilder javaScriptPopup =
new StringBuilder("var popObj=AdfPage.PAGE.findComponent('" +
clientId + "');");
javaScriptPopup.append("popObj.show();");
ExtendedRenderKitService erks =
Service.getRenderKitService(ctx, ExtendedRenderKitService.class);
erks.addScript(ctx, javaScriptPopup.toString());
ctx.renderResponse();
}
public static void expandSpilter(RichPanelSplitter splitter){
String clientId =splitter.getClientId(FacesContext.getCurrentInstance());
FacesContext facesContext = FacesContext.getCurrentInstance();
ExtendedRenderKitService service = org.apache.myfaces.trinidad.util.Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);
StringBuilder script = new StringBuilder();
script.append("var panelSpliter = AdfPage.PAGE.findComponent('").append(clientId).append("');\n");
script.append("panelSpliter.setCollapsed('');\n");
service.addScript(facesContext, script.toString());
}
public static void collapseSplitter(RichPanelSplitter splitter){
String clientId =splitter.getClientId(FacesContext.getCurrentInstance());
FacesContext facesContext = FacesContext.getCurrentInstance();
ExtendedRenderKitService service = org.apache.myfaces.trinidad.util.Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);
StringBuilder script = new StringBuilder();
script.append("var panelSpliter = AdfPage.PAGE.findComponent('").append(clientId).append("');\n");
script.append("panelSpliter.setCollapsed(true);\n");
service.addScript(facesContext, script.toString());
}
public static void closwBrowser(){
FacesContext facesContext = FacesContext.getCurrentInstance();
ExtendedRenderKitService erks = Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);
StringBuilder sb=new StringBuilder("window.close();");
erks.addScript(facesContext,sb.toString());
facesContext.renderResponse();
}
}
import com.sun.faces.mgbean.ManagedBeanCreationException;
import java.util.HashMap;
import java.util.Map;
import javax.el.ELContext;
import javax.el.Expression;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import javax.el.ValueExpression;
import javax.faces.application.FacesMessage;
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseId;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import oracle.adf.model.BindingContext;
import oracle.adf.model.DataControlFrame;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCDataControl;
import oracle.adf.view.rich.component.rich.RichPopup;
import oracle.adf.view.rich.component.rich.fragment.RichRegion;
import oracle.adf.view.rich.component.rich.layout.RichPanelSplitter;
import oracle.adf.view.rich.context.AdfFacesContext;
import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
import org.apache.myfaces.trinidad.util.Service;
/**
* Provides various utility methods that are handy to
* have around when working with ADF.
*/
public class ADFUtil {
/**
* When a bounded task flow manages a transaction (marked as
* requires-transaction, requires-new-transaction, or requires-
* existing-transaction), then the task flow must issue any
* commits or rollbacks that are needed.
* This is essentially to keep the state of the transaction that
* the task flow understands in synch with the state of the
* transaction in the ADFbc layer.
* Use this method to issue a commit in the middle of a task flow
* while staying in the task flow.
*/
public static void saveAndContinue() {
Map sessionMap =
FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
BindingContext context =
(BindingContext)sessionMap.get(BindingContext.CONTEXT_ID);
String currentFrameName = context.getCurrentDataControlFrame();
DataControlFrame dcFrame =
context.findDataControlFrame(currentFrameName);
dcFrame.commit();
dcFrame.beginTransaction(null);
}
/**
* Programmatic evaluation of EL.
*
* @param el EL to evaluate
* @return Result of the evaluation
*/
public static Object evaluateEL(String el) {
FacesContext facesContext = FacesContext.getCurrentInstance();
ELContext elContext = facesContext.getELContext();
ExpressionFactory expressionFactory =
facesContext.getApplication().getExpressionFactory();
ValueExpression exp =
expressionFactory.createValueExpression(elContext, el,
Object.class);
if(exp == null || elContext == null){
return null;
}
Object obj = null;
try{
obj = exp.getValue(elContext);
}catch(ManagedBeanCreationException ex){
}
return obj;
}
public static String getLocaleValue(String el,String code){
Map map = (Map)evaluateEL(el);
if(map != null){
if(map.get(code) != null)
return (String)map.get(code);
else
return code;
}
return "";
}
/**
* Programmatic invocation of a method that an EL evaluates to.
* The method must not take any parameters.
* @param el EL of the method to invoke
* @return Object that the method returns
*/
public static Object invokeEL(String el) {
return invokeEL(el, new Class[0], new Object[0]);
}
/**
* Programmatic invocation of a method that an EL evaluates to.
*
* @param el EL of the method to invoke
* @param paramType Class defining the type of the
* parameter
* @param param Object defining the value of the
* parameter
* @return Object that the method returns
*/
public static Object invokeEL(String el, Class paramType, Object param) {
return invokeEL(el, new Class[] { paramType }, new Object[] { param });
}
/**
* Programmatic invocation of a method that an EL evaluates to.
*
* @param el EL of the method to invoke
* @param paramTypes Array of Class defining the types of the
* parameters
* @param params Array of Object defining the values of the
* parametrs
* @return Object that the method returns
*/
public static Object invokeEL(String el, Class[] paramTypes,
Object[] params) {
FacesContext facesContext = FacesContext.getCurrentInstance();
ELContext elContext = facesContext.getELContext();
ExpressionFactory expressionFactory =
facesContext.getApplication().getExpressionFactory();
MethodExpression exp =
expressionFactory.createMethodExpression(elContext, el,
Object.class, paramTypes);
return exp.invoke(elContext, params);
}
/**
* Sets a value into an EL object. Provides similar
* functionality to
* the <af:setActionListener> tag, except the
* <code>from</code> is
* not an EL. You can get similar behavior by using the
* following...<br>
* <code>setEL(<b>to</b>, evaluateEL(<b>from</b>))</code>
*
* @param el EL object to assign a value
* @param val Value to assign
*/
public static void setEL(String el, Object val) {
FacesContext facesContext = FacesContext.getCurrentInstance();
ELContext elContext = facesContext.getELContext();
ExpressionFactory expressionFactory =
facesContext.getApplication().getExpressionFactory();
ValueExpression exp =
expressionFactory.createValueExpression(elContext, el,
Object.class);
exp.setValue(elContext, val);
}
public static String errorDialog(String errorMessage)
{
FacesContext context = FacesContext.getCurrentInstance();
((HttpSession)context.getExternalContext().getSession(false)).setAttribute("errorMessage", errorMessage);
ViewHandler viewHandler = context.getApplication().getViewHandler();
UIViewRoot dialog = viewHandler.createView(context, "/Error.jspx");
HashMap properties = new HashMap();
properties.put("width", new Integer(250));
properties.put("height", new Integer(150));
AdfFacesContext afContext = AdfFacesContext.getCurrentInstance();
afContext.launchDialog(dialog,
null, // not launched from any component
null, // no particular parameters
true,properties); // show it in a dialog
context.renderResponse();
return "";
}
/**
*Get the ADF datacontrol reference given the control name
* @param controlName data control name as given in data bindings file
* @return
*/
public static DCDataControl findDataControl(String controlName){
DCBindingContainer context = (DCBindingContainer)ADFUtil.evaluateEL("#{bindings}");
DCDataControl dcf = context.findDataControl(controlName);
return dcf;
}
/**
* Initiates control flow in a task flow region.
* @param region - RichRegion that represents taskflow.
* @param outComeEl - action outcome, based on whic control flows
*/
public static void performControlFlow(RichRegion region, String outComeEl){
FacesContext ctx = FacesContext.getCurrentInstance();
ExpressionFactory f = ctx.getApplication().getExpressionFactory();
ELContext elctx = ctx.getELContext();
MethodExpression m = f.createMethodExpression(elctx,outComeEl, String.class, new Class[]{});
region.queueActionEventInRegion(m,null,null,false,-1,-1,PhaseId.ANY_PHASE);
}
public static HttpServletRequest getHttpServletRequest(){
return ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest());
}
public static HttpSession getHttpSession(){
return ((HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false));
}
/**
* Returns the Binding Container
* @return
*/
public static DCBindingContainer getBindingContainer(){
return (DCBindingContainer)evaluateEL("#{bindings}");
}
/**
* Renders Faces message showing some validation error message.
* @param id
* @param message
*/
public static void addFacesMessage(String id, FacesMessage message){
FacesContext.getCurrentInstance().addMessage(id, message);
FacesContext.getCurrentInstance().renderResponse();
}
public static void addFacesMessage(String clientId,javax.faces.application.FacesMessage.Severity severity,String localizedText){
FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(severity, "",localizedText));
FacesContext.getCurrentInstance().renderResponse();
}
public static String showError(String clientId,String errorMessage) {
FacesContext ctx = FacesContext.getCurrentInstance();
StringBuilder javaScriptPopup =
new StringBuilder("var popObj=AdfPage.PAGE.findComponent('" +
clientId + "');");
javaScriptPopup.append("if(popObj != null){var errorField = popObj.findComponent('errorMessage');if(errorField != null)");
javaScriptPopup.append("{errorField.setValue('");
javaScriptPopup.append(errorMessage);
javaScriptPopup.append("')}");
javaScriptPopup.append("popObj.show();}");
ExtendedRenderKitService erks =
Service.getRenderKitService(ctx, ExtendedRenderKitService.class);
erks.addScript(ctx, javaScriptPopup.toString());
ctx.renderResponse();
return "";
}
public static void showPopup(String clientId){
FacesContext ctx = FacesContext.getCurrentInstance();
StringBuilder javaScriptPopup =
new StringBuilder("var popObj=AdfPage.PAGE.findComponent('" +
clientId + "');");
javaScriptPopup.append("popObj.show();");
ExtendedRenderKitService erks =
Service.getRenderKitService(ctx, ExtendedRenderKitService.class);
erks.addScript(ctx, javaScriptPopup.toString());
ctx.renderResponse();
}
public static void expandSpilter(RichPanelSplitter splitter){
String clientId =splitter.getClientId(FacesContext.getCurrentInstance());
FacesContext facesContext = FacesContext.getCurrentInstance();
ExtendedRenderKitService service = org.apache.myfaces.trinidad.util.Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);
StringBuilder script = new StringBuilder();
script.append("var panelSpliter = AdfPage.PAGE.findComponent('").append(clientId).append("');\n");
script.append("panelSpliter.setCollapsed('');\n");
service.addScript(facesContext, script.toString());
}
public static void collapseSplitter(RichPanelSplitter splitter){
String clientId =splitter.getClientId(FacesContext.getCurrentInstance());
FacesContext facesContext = FacesContext.getCurrentInstance();
ExtendedRenderKitService service = org.apache.myfaces.trinidad.util.Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);
StringBuilder script = new StringBuilder();
script.append("var panelSpliter = AdfPage.PAGE.findComponent('").append(clientId).append("');\n");
script.append("panelSpliter.setCollapsed(true);\n");
service.addScript(facesContext, script.toString());
}
public static void closwBrowser(){
FacesContext facesContext = FacesContext.getCurrentInstance();
ExtendedRenderKitService erks = Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);
StringBuilder sb=new StringBuilder("window.close();");
erks.addScript(facesContext,sb.toString());
facesContext.renderResponse();
}
}
This comment has been removed by the author.
ReplyDelete