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

Custom Scenario Event

For a custom action click here

Overview

This example creates a custom event and configures the Scenario Manager to record an Audit when the event is fired.

There are three java classes involved in this sample as well as the necessary configuration in the atg.dynamo.Messaging.dynamoMessagingSystem.xml file.

The complete module is included here. Extract this file in ATG\Dynamo5.1 to and startDynamo with -m Training.ClickTracker to see it at work. This module requires Dynamo Scenario Server to be installed.

The Tracker is a session based component which is trigger using an anchor tag like:

<A HREF="index.jhtml" BEAN="/ClickTracking/ClickTracker.key" 
         VALUE="A0001">Test Promo A0001</A>

This causes the setKey method of the tracker to be called when clicked, thus firing the event.

The scenario is set up as follows (note the custom event 'Clicks') and the use of the 'key' property on the event. The available properties of the event message are bean properties of the atg.clicktracker.Message class, the relationship between the event and the concrete message class is defined in the dynamoMessagingSystem.xml file in the message-registry section.

This could all be done with one scenario, instead of a different one for each promo, see the custom action article to see how.

atg.clicktraker.Tracker

This class actually detects the clicks from the user and tells the globally scoped Sender object to broadcast the message. This split is necessary because the MessageSource must be a globally scoped component to be initialized by the DMS.

package atg.clicktracker;

import atg.nucleus.GenericService;
import atg.dms.patchbay.*;
import atg.nucleus.ServiceException;
import atg.userprofiling.Profile;
import javax.jms.ObjectMessage;
import java.util.Date;
import atg.servlet.DynamoHttpServletRequest;
import atg.servlet.DynamoHttpServletResponse;

public class Tracker extends GenericService
{
  void log(Throwable t)
  {
    if(this.isLoggingError())
    {
       logError(t);
    }
  }
  void log(String s)
  {
    if(this.isLoggingDebug())
    {
       logDebug(s);
    }
  }

  public Profile getProfile(){return mProfile;}
  public void setProfile(Profile p){mProfile = p;}
  Profile mProfile;

  public Sender getSender(){return mSender;}
  public void setSender(Sender p){mSender = p;}
  Sender mSender;

  String getProfileId()
  {
    String s = "0";
    if(mProfile != null)
    {
      s = mProfile.getRepositoryId();
    }
    else
    {
      log("No Profile configured");
    }
    return s;
  }

  public void setKey(String pKey)
  {
    mKey = pKey;
    log("Key set to " + pKey);
  }

  String mKey;

  public boolean handleKey(DynamoHttpServletRequest req,
                           DynamoHttpServletResponse res)
  {
     boolean bContinue = true;

     if((mKey != null) && (mKey.length() > 0))
     {
        if(mSender != null)
        {
          boolean b = mSender.sendNotification(req.getRequestURI(), 
                                               mKey,getProfileId());
          log("sendNotification result = " + b);
        }
        else
        {
          log("No Sender configured");
        }
     }

     // Clear the record
     mKey = null;

     return bContinue;
  }

  public Tracker()
  {
  }
}

atg.clicktracker.Sender.java

This globally scoped component actually sends out the message, of type Message

package atg.clicktracker;

import atg.nucleus.GenericService;
import atg.dms.patchbay.*;
import atg.nucleus.ServiceException;
import atg.userprofiling.Profile;
import javax.jms.ObjectMessage;
import java.util.Date;

public class Sender extends GenericService
  implements MessageSource
{
  String mPortName = "DEFAULT";
  public String getPortName()  {    return mPortName;  }
  public void setPortName(String s)  {    mPortName = s;  }
  String mJMSType = "atg.clicktracker.tracker";
  public String getJMSType()  {    return mJMSType;  }
  public void setJMSType(String s)  {    mJMSType = s;  }

  void log(Throwable t)
  {
    if(this.isLoggingError())
    {
       logError(t);
    }
  }
  void log(String s)
  {
    if(this.isLoggingDebug())
    {
       logDebug(s);
    }
  }

  //////////////////////////////////////////////////////////////////////////
  //
  //  This piece of code is boilerplate implementation for MessageSource
  //
  MessageSourceContext mContext = null;
  public void setMessageSourceContext(MessageSourceContext p0)
  {
    log("Setting MessageSourceContext");
    mContext = p0;
  }
  boolean mbStarted = false;
  public void startMessageSource()
  {

    log("startMessageSource");
    mbStarted = true;
  }
  public void stopMessageSource()
  {
    log("stopMessageSource");
    mbStarted = false;
  }
  //
  //////////////////////////////////////////////////////////////////////////

  ObjectMessage createMessage(String sURL, String sKey, String profileId)
  {
    ObjectMessage msg = null;
    try
    {
      msg = mContext.createObjectMessage(mPortName);
      msg.setJMSType(mJMSType);
      Message m = new Message();
      m.setDate(new Date(msg.getJMSTimestamp()));
      m.setKey(sKey);
      m.setURL(sURL);
      if(profileId != null)
      {
         m.setProfileId(profileId);
      }
      else
      {
         m.setProfileId("0");
         log("Null profileId");
      }
      msg.setObject(m);
    }
    catch(javax.jms.JMSException e)
    {
       log(e);
    }
    return msg;
  }

  public boolean sendNotification(String sURL, String pKey, String profileId)
  {
    boolean bResult = false;
    if((pKey != null) && (pKey.length() > 0))
    {
      mLastKey = "error (" + pKey + ":" + profileId + ") as " + mJMSType + " on " 
                 + mPortName;

      if(mbStarted)
      {
        if(mContext != null)
        {
          try
          {
            ObjectMessage msg = this.createMessage(sURL, pKey, profileId);

            log("Sending Notification");
            mContext.sendMessage(mPortName,msg);
            mLastKey = "success (" + pKey + ":" + profileId + ") as " + mJMSType 
                       + " on " + mPortName;
            log(mLastKey);
            bResult = true;
          }
          catch( javax.jms.JMSException e)
          {
            mLastKey = e.getMessage();
            log(e);
          }
        }
        else
        {
          log("No Message Context");
        }
      }
      else
      {
        log("Not started");
      }

      if(!bResult)
      {
        log("Notification Failed: " + mLastKey);
      }
    }
    return bResult;
  }

  String getLastKey(){ return mLastKey; }

  String mLastKey;

  public Sender()
  {
  }
}

atg.clicktracker.Message.java

This is the message class that we are sending out from our Sender class. (to the Scenario Manager)

package atg.clicktracker;
import java.io.Serializable;
import java.util.Date;

public class Message implements Serializable
{
  public Message()
  {
  }
  Date mDate;
  public void setDate(Date d){ mDate = d;}
  public Date getDate() { return mDate; }

  String mId;
  public void setProfileId(String id) { mId = id; }
  public String getProfileId() { return mId; }

  String mKey;
  public void setKey(String s) { mKey = s; }
  public String getKey() { return mKey; }

  String mURL;
  public void setURL(String s) { mURL = s; }
  public String getURL() { return mURL; }

}

atg.dynamo.Messaging.dynamoMessagingSystem.xml

Here we configure our click tracker sender to send it's event to the /ClickTracking/Topic topic. We also configure /atg/scenario/ScenarioManager as a sink on this topic

<?xml version=" ? 1.0">
<dynamo-message-system>
   <local-jms>
     <topic-name>/ClickTracking/Topic</topic-name>
   </local-jms>
   <patchbay>
      <message-source>
        <nucleus-name>/ClickTracking/Sender</nucleus-name>
          <output-port>
           <port-name>DEFAULT</port-name>
           <output-destination>
             <provider-name>local</provider-name>
             <destination-name>localdms:/local/ClickTracking/Topic
             </destination-name>
             <destination-type>Topic</destination-type>
           </output-destination>
         </output-port>
      </message-source>
      <message-sink>
        <nucleus-name>/atg/scenario/ScenarioManager</nucleus-name>
        <input-port>
          <port-name>IndividualEvents</port-name>
            <input-destination>
            <provider-name>local</provider-name>
            <destination-name>localdms:/local/ClickTracking/Topic
            </destination-name>
            <destination-type>Topic</destination-type>
          </input-destination>
        </input-port>
      </message-sink>
   </patchbay>

  <message-registry>
    <message-family>
      <message-family-name>
        atgClickTracking
      </message-family-name>
      <message-type>
        <jms-type>
          atg.clicktracker.tracker
        </jms-type>
        <message-class>
          atg.clicktracker.Message
        </message-class>

	<message-context>
	  session	
	</message-context>

	<display-name>
	  Clicks
	</display-name>
	<description>
	  message sent in response to the user making a tagged click, in 
           response to an advertisement, for example.
         </description>
      </message-type>
    </message-family>
  </message-registry>
</dynamo-message-system>


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