Noah Martin
Noah Martin

Reputation: 1809

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 :).

Upvotes: 0

Views: 1523

Answers (3)

hanan Ahmed
hanan Ahmed

Reputation: 442

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>

Upvotes: 1

hanan Ahmed
hanan Ahmed

Reputation: 442

ok you want to do a rollback yourself when you press ok although your changes will not be committed by itself if you do not commit it

you can check your Application module.If the data is dirty (so there are changes had been made) you can call a popup window (RichPopup) which you made in your page and has output text tells you about the changes and a button which rollback your changes

get your Application module by

private ApplicationModule getApplicationModule(String dataProvider) {
    FacesContext fc = FacesContext.getCurrentInstance();
    Application app = fc.getApplication();
    ExpressionFactory elFactory = app.getExpressionFactory();
    ELContext elContext = fc.getELContext();
    ValueExpression valueExp = elFactory.createValueExpression(elContext, dataProvider, Object.class);

    return (ApplicationModule)valueExp.getValue(elContext);
}

check for any changes by the following method

 private boolean pendingChangesExist() {
    return this.getApplicationModule("#{data.AppModuleDataControl.dataProvider}").getTransaction().isDirty();
}

and in the button you use for navigation call the following method

 public String gotosecondpage() {
    if (!this.pendingChangesExist()) {
        make navigation-
    } else {
         call your pop up window }
    return null;
}

Upvotes: 1

Optional
Optional

Reputation: 4507

Did you check https://blogs.oracle.com/shay/entry/warning_of_uncommitted_unsaved_changes.

I assume you just need to switch it on. <AF:document uncommittedDataWarning="on">

Upvotes: 1

Related Questions