Essential ATG Dynamo Training - Got atg Certified Relationship Management Developer?
This is not an official ATG site: ATG, Dynamo, Scenario Server and Personalization Server are trademarks or registered trademarks of Art Technology Group
Articles Exercises Resources Links Search

How Rollback a Transaction on Demand

This example shows a form handler which renames a user in the Profile Repository. Having done this it checks whether or not to rollback the transaction (Based on which button the user clicks).

If the commit button is clicked then the name is changed and the change committed. If the rollback button is clicked the name is changed then the transaction is rolled back, so in the end no changes are saved.

There are three files need to set this up and you need to be running DPS to use the ProfileAdapterRepository.

RenamerFormHandler.java

package imagescript.atg.repository;

import atg.droplet.GenericFormHandler;
import atg.repository.*;
import javax.servlet.*; // ServletException
import atg.servlet.*; // DynamoServlet
import java.io.*; // IOException
import java.util.Enumeration;
import atg.dtm.*;

import javax.transaction.*;


/**
 *
 *
 * @copyright Copyright (c) 2001
 * @company Art Technology Group
 * @author Stuart Jones
 * @version 1.0 ATG Training Supplemental Files
 */

public class RenamerFormHandler extends GenericFormHandler
{
  Repository mRepository = null;

  public Repository getRepository()
  {
    return mRepository;
  }

  public void setRepository(Repository r)
  {
    mRepository = r;
  }

  String mstrItemDescriptorName = "user";

  public void setItemDescriptorName(String s)
  {
    mstrItemDescriptorName = s;
  }

  public String getItemDescriptorName()
  {
    return mstrItemDescriptorName;
  }


  boolean mRollback = false;
  final static String sRollback = "rollback";

  public void setSubmit(String str)
  {
     mRollback = sRollback.equalsIgnoreCase(str);
  }

  TransactionManager mTransactionManager;

  public void setTransactionManager(TransactionManager pTransactionManager)
    {mTransactionManager = pTransactionManager;}

  public TransactionManager getTransactionManager()
    {return mTransactionManager;}


  String mId;
  String mName;
  public String getId(){return mId; }
  public String getName(){return mName; }
  public void setId(String s){mId = s; }
  public void setName(String s){mName = s; }

  void doUpdate(String id, String name)
  {
    try
    {
      MutableRepository mr =(MutableRepository)getRepository();
      MutableRepositoryItem item = mr.getItemForUpdate(getId(), getItemDescriptorName());
      item.setPropertyValue("firstName", name);
      mr.updateItem(item);
    }
    catch( RepositoryException e )
    {
      rollback();
    }
  }


  void rollback()
  {
    if (isLoggingDebug())
      logDebug("rollback()");

    try
    {
      TransactionManager mgr = getTransactionManager();
      if (mgr != null)
        mgr.setRollbackOnly();
    }
    catch (SystemException se)
    {
      error(se);
    }
  }


  private final void error(Throwable pThrowable)
  {
    if (isLoggingError())
      logError(pThrowable);
  }

  boolean isValid(String s)
  {
    return ((s != null) && (s.length() > 0));
  }

  boolean checkValues()
  {
    return (isValid(getId()) && isValid(getName()));
  }


  public boolean handleSubmit(DynamoHttpServletRequest req,
                              DynamoHttpServletResponse res)
      throws ServletException, IOException
  {
    boolean bResult = true;

    if(checkValues())
    {
      TransactionDemarcation td = new TransactionDemarcation();
      TransactionManager mgr = getTransactionManager();

      try
      {
        td.begin(mgr);
        doUpdate(getId(),getName());

        // If the user clicked the rollback button
        // This is just a DEMO of how to rollback when needed

        if(mRollback)
        {
          rollback();
        }
      }
      catch (TransactionDemarcationException tde)
      {
        error(tde);
        rollback();
      }
      finally
      {
        try
        {
          td.end();
        }
        catch (TransactionDemarcationException tde)
        {
          error(tde);
        }
      }
    }

    return bResult;
  }

  public RenamerFormHandler()
  {
  }
}

rename.jhtml

<IMPORTBEAN BEAN="/RepositoryForm/RenamerFormHandler">
<HTML> <HEAD>
<TITLE>TransactionDemo</TITLE>
</HEAD>

<BODY BGCOLOR="#FFFFFF">
<H1>TransactionDemo</H1>

<FORM ACTION="rename.jhtml" METHOD=POST>

  <!-- Default form error handling support -->
  <DROPLET BEAN="/atg/dynamo/droplet/ErrorMessageForEach">
    <OPARAM NAME="output">
      <B><VALUEOF PARAM="message"></VALUEOF></B><BR>
    </OPARAM>
    <OPARAM NAME="outputStart">
      <LI>
    </OPARAM>
    <OPARAM NAME="outputEnd">
      </LI>
    </OPARAM>
  </DROPLET>
      <SELECT BEAN="/RepositoryForm/RenamerFormHandler.id">
  <DROPLET BEAN="/atg/targeting/TargetingForEach">
    <PARAM NAME="targeter" VALUE="bean:/atg/registry/RepositoryTargeters/UserProfiles/AllUsers">
    <PARAM NAME="sortProperties" VALUE="+firstName">
    <PARAM NAME="fireContentEvent" VALUE="false">
    <PARAM NAME="fireContentTypeEvent" VALUE="false">
    <OPARAM NAME="output">
      <option value="param:element.repositoryId">
        <VALUEOF PARAM="element.firstName"></VALUEOF>
        <VALUEOF PARAM="element.lastName"></VALUEOF></option>
    </OPARAM>
  </DROPLET>
      </SELECT>
<input type="text" BEAN="/RepositoryForm/RenamerFormHandler.name">


 <input type="submit" BEAN="/RepositoryForm/RenamerFormHandler.submit" value="commit">
 <input type="submit" BEAN="/RepositoryForm/RenamerFormHandler.submit" value="rollback">

</FORM>


</BODY> </HTML>

/RepositoryForm/RenamerFormHandler.properties

# /RepositoryForm/RenamerFormHandler
#Fri Jun 08 10:39:40 PDT 2001
$class=imagescript.atg.repository.RenamerFormHandler
$scope=global
loggingDebug=true
repository=/atg/userprofiling/ProfileAdapterRepository
transactionManager=/atg/dynamo/transaction/TransactionManager


Technical Training Advertise your Training Programs for Free! Los Angeles Web Design Shopping Cart Software  Form a Corporation