Maitriser le Rollback

Pour abandonner les modifications effectuées dans un ViewObject updatable, un Rollback est nécessaire. Il peut être effectué de 2 manières :

  • Dans la page au moyen d’un command button
<af:commandButton actionListener="#{bindings.Rollback.execute}" />
  • Dans un managed Bean
private static final String ROLLBACK = "Rollback";
...
ADFUtils.findOperation(ROLLBACK).execute();

Le rollback efface tous les changements dans le cache des objets mais effectue aussi un reset de tous les iterators. Or ce reset global des iterators est parfois gênant surtout dans les pages comportant de nombreux composants dépendants eux-même de nombreux ViewObjects.
Prenons l’exemple d’une page comportant :

  • Un tableau updatable
  • Un bouton « Appliquer un traitement » qui permet de mettre à jour toutes les lignes du tableau updatable suivant une règle fonctionnelle
  • Un bouton « Valider » qui effectue un commit
  • Un bouton « Abandon » qui permet de revenir à l’état initial

Le principe va consister à mettre en place un savepoint avant traitement lors du clic sur le bouton « Appliquer un traitement » puis un rollback to savepoint appelé depuis le bouton « Abandon ».

Bouton « Appliquer un traitement »

  • Dans la page
<af:commandButton text="Appliquer un traitement"
	  id="cb3"
	  partialSubmit="true"
	  actionListener="#{pageFlowScope.backing_test.applyChanges}">
</af:commandButton>
  • Dans le managedBean
public void applyChangesToTable(ActionEvent actionEvent) {
//
//	Mise en place SavePoint
//
  BindingContext bctx = BindingContext.getCurrent();
  DCDataControl dcDataControl = bctx.getDefaultDataControl();
  String sph = (String) dcDataControl.createSavepoint();
  AdfFacesContext.getCurrentInstance().getPageFlowScope().put("SavePoint",sph);
//
//	Traitement
//
  String message = "[applyChangesToTable]";
  DCIteratorBinding iterator = ADFUtils.findIterator(ITERATOR_NAME);
  RowSetIterator iter = iterator.getViewObject().createRowSetIterator(null);
  iter.reset();
  TableRowImpl row;
  while (iter.hasNext()) {
    row = (TableRowImpl) iter.next();
    row.set...(...);
  }
}

Bouton « Abandon »

  • Dans la page
<af:commandButton
	text="Abandon"
	id="cb4"
	partialSubmit="true"
	actionListener="#{pageFlowScope.backing_test.dropActionListener}"
</af:commandButton>
  • Dans le managedBean
public void dropActionListener(ActionEvent actionEvent) {
  String sph = (String) AdfFacesContext.getCurrentInstance().getPageFlowScope().get("SavePoint");
  BindingContext bctx = BindingContext.getCurrent();
  DCDataControl dcDataControl = bctx.getDefaultDataControl();
  dcDataControl.restoreSavepoint(sph);
}

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *