import java.text.SimpleDateFormat;
import java.util.Map;
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.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.share.ADFContext;
import oracle.adf.share.logging.ADFLogger;
import oracle.adf.view.rich.component.rich.RichPopup;
import oracle.adf.view.rich.event.QueryEvent;
import oracle.binding.AttributeBinding;
import oracle.binding.BindingContainer;
import oracle.binding.ControlBinding;
import oracle.binding.OperationBinding;
import oracle.jbo.ApplicationModule;
import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
import org.apache.myfaces.trinidad.util.Service;
public class AdfUtil {
public static final ADFLogger LOGGER = ADFLogger.createADFLogger(AdfUtil.class);
public AdfUtil() {
super();
}
public static String getCurrentUser(){
return ADFContext.getCurrent().getSecurityContext().getUserName();
}
public static String getCurrentUserDisplayName(){
return ADFContext.getCurrent().getSecurityContext().getUserProfile().getDisplayName();
}
public static BindingContext getBindingContext(){
return BindingContext.getCurrent();
}
public static BindingContainer getBindingContainer(){
return getBindingContext().getCurrentBindingsEntry();
}
public static DCBindingContainer getDCBindingContainer(){
return (DCBindingContainer)getBindingContainer();
}
public static DCIteratorBinding getDCIteratorBindingByName(String iteratorName){
return getDCBindingContainer().findIteratorBinding(iteratorName) ;
}
public static OperationBinding getOperationBinding(String operationName){
OperationBinding operation = getDCBindingContainer().getOperationBinding(operationName);
return operation;
}
public static oracle.jbo.domain.Date getCurrentDateTime(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.sql.Date currentDate = new java.sql.Date(System.currentTimeMillis());
String currentDateStr = simpleDateFormat.format(currentDate);
return new oracle.jbo.domain.Date(currentDateStr);
}
public static FacesContext getFacesContext(){
return FacesContext.getCurrentInstance();
}
public static void setExpressionValue(String expression,Object value){
FacesContext facesContext = FacesContext.getCurrentInstance();
ELContext elContext = facesContext.getELContext();
Application application = facesContext.getApplication();
ExpressionFactory elFactory = application.getExpressionFactory();
ValueExpression valueExpression = elFactory.createValueExpression(elContext, expression, Object.class);
Class bindClass = valueExpression.getType(elContext);
if(bindClass.isPrimitive()||bindClass.isInstance(value)){
valueExpression.setValue(elContext, value);
}
}
public static Object getExpressionValue(String expressionStr){
FacesContext facesContext = FacesContext.getCurrentInstance();
ELContext elContext = facesContext.getELContext();
Application application = facesContext.getApplication();
ExpressionFactory expressionFactory = application.getExpressionFactory();
ValueExpression valueExpression = expressionFactory.createValueExpression(elContext, expressionStr, Object.class);
return valueExpression.getValue(elContext);
}
public static void showPopup(RichPopup richPopup){
FacesContext facesContext = FacesContext.getCurrentInstance();
ExtendedRenderKitService kitService = Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);
String scriptStr = "var popup; popup=AdfPage.PAGE.findComponent('"+richPopup.getClientId(facesContext)+"'); popup.show();";
kitService.addScript(facesContext, scriptStr);
return ;
}
public static void addFacesInfoMessage(String messageStr){
FacesContext facesContext = FacesContext.getCurrentInstance();
UIViewRoot viewRoot = facesContext.getViewRoot();
String viewId = viewRoot.getViewId();
FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO,messageStr,"");
facesContext.addMessage(viewId, facesMessage);
}
public static void addFacesWarmMessage(String messageStr){
FacesContext facesContext = FacesContext.getCurrentInstance();
UIViewRoot viewRoot = facesContext.getViewRoot();
String viewId = viewRoot.getViewId();
FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_WARN,messageStr,"");
facesContext.addMessage(viewId, facesMessage);
}
public static void addFacesErrorMessage(String messageStr){
FacesContext facesContext = FacesContext.getCurrentInstance();
UIViewRoot viewRoot = facesContext.getViewRoot();
String viewId = viewRoot.getViewId();
FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR,messageStr,"");
facesContext.addMessage(viewId,facesMessage);
}
public static Object getRequestParameterByName(String paraName){
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
Map paraMap = externalContext.getInitParameterMap();
return paraMap.get(paraName);
}
public static void closeCurrentPage(){
FacesContext facesContext = FacesContext.getCurrentInstance();
ExtendedRenderKitService extendService = Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);
String scriptStr = "window.opener=null;window.open('','_self');window.close();";
extendService.addScript(facesContext,scriptStr);
}
// The code below should be in a Utility clas
public Object invokeMethodExpression(String expr, Class returnType,
Class[] argTypes, Object[] args) {
FacesContext fc = FacesContext.getCurrentInstance();
ELContext elctx = fc.getELContext();
ExpressionFactory elFactory =
fc.getApplication().getExpressionFactory();
MethodExpression methodExpr =
elFactory.createMethodExpression(elctx, expr, returnType,
argTypes);
return methodExpr.invoke(elctx, args);
}
/**
* invokeMethodExpression("#{bindings.ProcessInstanceVO1Query.processQuery}",Object.class, QueryEvent.class, queryEvent);
* @param expr
* @param returnType
* @param argType
* @param argument
* @return
*/
public Object invokeMethodExpression(String expr, Class returnType,
Class argType, Object argument) {
return invokeMethodExpression(expr, returnType,
new Class[] { argType },
new Object[] { argument });
}
public static ApplicationModule getApplicationModuleForDataControl(String name) {
return (ApplicationModule)getExpressionValue("#{data." + name +
".dataProvider}");
}
public static AttributeBinding findControlBinding(BindingContainer bindingContainer,
String attributeName) {
if (attributeName != null) {
if (bindingContainer != null) {
ControlBinding ctrlBinding =
bindingContainer.getControlBinding(attributeName);
if (ctrlBinding instanceof AttributeBinding) {
return (AttributeBinding)ctrlBinding;
}
}
}
return null;
}
public static AttributeBinding findControlBinding(String attributeName) {
return findControlBinding(getBindingContainer(), attributeName);
}
public static void makeCurrent(SelectionEvent selectionEvent) {
RichTable _table = (RichTable)selectionEvent.getSource();
CollectionModel _tableModel = (CollectionModel)_table.getValue();
JUCtrlHierBinding _adfTableBinding =
(JUCtrlHierBinding)_tableModel.getWrappedData();
DCIteratorBinding _tableIteratorBinding =
_adfTableBinding.getDCIteratorBinding();
Object _selectedRowData = _table.getSelectedRowData();
JUCtrlHierNodeBinding _nodeBinding =
(JUCtrlHierNodeBinding)_selectedRowData;
Key _rwKey = _nodeBinding.getRowKey();
if (_rwKey != null) {
_tableIteratorBinding.setCurrentRowWithKey(_rwKey.toStringFormat(true));
}
}
}