Sunday, December 13, 2015

Rollback specific page when navigating from it

UseCase
When you create new row in a page and start to set a form values then navigate to another page then commit the validation of the row at first page will be fired , you want to rollback any transaction on the first page when navigate to anther page.
We want to rollback when navigating out of a target specific page.
Solution
1- At first page (target),when click CreateInsert button, set a value for a session variable with name viewIdd
2- Create class that implements PagePhaseListener.
3-Override afterPhase method and make rollback if the session variable was set and the target page has changed.
public class MyPageListener implements PagePhaseListener {
    @Override
    public void afterPhase(PagePhaseEvent pagePhaseEvent) {
        int phaseId = pagePhaseEvent.getPhaseId();
        // if the variable was set and the current page isn't the target page
        if (JSFUtils.getFromSession("viewIdd") != null &&
       !FacesContext.getCurrentInstance().getViewRoot().getViewId().contains("TargetPage")) {
            // Lifecyce.getPhaseName(phaseId) == 
              if (Lifecycle.getPhaseName(phaseId) == Lifecycle.PREPARE_RENDER_ID) {
                ADFUtils.getAppImpl().getDBTransaction().rollback();
                ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
                HttpSession session = (HttpSession) context.getSession(false);
                if (session != null) {
                    session.removeAttribute("viewIdd");
                    //  JSFUtils.setManagedBeanValue("#{sessionScope.viewIdd}", null);
                }
            }
        }
    }

3- Register the class as a listener in /METAINF/adf-settings file located at Application Resources
<?xml version="1.0" encoding="US-ASCII" ?> 
<adf-settings xmlns="http://xmlns.oracle.com/adf/settings"> 
  <adfc-controller-config xmlns="http://xmlns.oracle.com/adf/controller/config"> 
    <lifecycle> 
      <phase-listener> 
        <listener-id>MyPageListener1</listener-id> 
        <class>view.MyPageListener</class> 
        <before-id-set> 
          <listener-id> MyPageListener1</listener-id> 
        </before-id-set> 
        <after-id-set> 
          <listener-id>MyPageListener1</listener-id> 
        </after-id-set> 
     </phase-listener> 
    </lifecycle> 
  </adfc-controller-config> 
</adf-settings>

No comments:

Post a Comment

java - fill distinct objects in ArrayList

If you have a list that contains some objects that it is considered as duplication when two or three fields of these objects are equal. How ...