Search code examples
javascriptjsforacle-adfjdeveloper

How to programatically achieve UncommitedDataWarning in ADF?


ADF has this document property (UncommitedDataWarning) that warns the user when he wants to navigate to another page having some not Commited date in the actual page. This is just a warning. I want to perform a Rollback for the entered data when the user press OK in the dialog box. Does anyone know any way on how to achieve this?

If someone has any idea to achieve this through JSF & JavaScript please tell me that, maybe I will find someway to adapt that :).


Solution

  • this is the code of phase listener i use it to check if the user is logged in in ADF.make the changes you want to check about anything else

    import javax.el.ELContext;
    import javax.el.ExpressionFactory;
    import javax.el.ValueExpression;
    
    import javax.faces.application.Application;
    import javax.faces.application.FacesMessage;
    import javax.faces.context.FacesContext;
    
    import oracle.adf.controller.ControllerContext;
    import oracle.adf.controller.faces.lifecycle.JSFLifecycle;
    import oracle.adf.controller.v2.lifecycle.PagePhaseEvent;
    import oracle.adf.controller.v2.lifecycle.PagePhaseListener;
    
    import view.backing.UserData;
    
    
     public class SecurityPagePhaseListener implements PagePhaseListener {
          public static final String LOGIN_VIEW = "/Home";
    public static final String LOGOUT_MSG = "You are not logged in";
    public static final String PASTE_MSG = "Don't try  to copy and paste URL address :)";
    public static final String currentView=FacesContext.getCurrentInstance().getViewRoot().getId();
    
    
    public static Object resolveExpression(String expression) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        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 void beforePhase(PagePhaseEvent event) {
    
        if (event.getPhaseId() == JSFLifecycle.INIT_CONTEXT_ID) {
    
            ControllerContext cc = ControllerContext.getInstance();
            String viewId = cc.getCurrentViewPort().getViewId();
            Boolean protectedView = SecurityUtil.isProtectedPage(viewId);
    
            /** ------ [ If requested page is protected area ] ----- */
            if (protectedView) {
                UserData ud = (UserData)resolveExpression("#{UserData}");
                Boolean logedIn = ud.getLoggedIn();
                /** ------ [ If user is not logged in ] ----- */
                if (!logedIn) {
                    FacesMessage fm = new FacesMessage(LOGOUT_MSG);
                    fm.setSeverity(FacesMessage.SEVERITY_ERROR);
                    FacesContext context = FacesContext.getCurrentInstance();
                    context.addMessage(null, fm);
                   SecurityUtil.displayView(LOGIN_VIEW);
                }
    
    
                /** [ If user try to paste direct link to protected page ] */
                if (!SecurityUtil.isViewState()) {
                    FacesMessage fm = new FacesMessage(PASTE_MSG);
                    fm.setSeverity(FacesMessage.SEVERITY_ERROR);
                    FacesContext context = FacesContext.getCurrentInstance();
                    context.addMessage(null, fm);
                   SecurityUtil.displayView(LOGIN_VIEW );
                }
            }
        }
    
    }
    
    public void afterPhase(PagePhaseEvent event) {
    }
    
     }
    

    Do not forget to register it in adf-settings.xml as follows

     <lifecycle>
      <phase-listener>
        <listener-id>SecurityPagePhaseListener</listener-id>
          <class>security.SecurityPagePhaseListener</class>
      </phase-listener>
    </lifecycle>