API Example :: SendEbiItmNotif.java

EBI Java API :: SendEbiItmNotif.java




/* EXTOL International, all rights reserved
 * Author: tfrance
 * Date: 10/2003
 *
 * Modifications:
 * Developer  :
 * Date       : [YYYY/MM/DD]
 * Description:
 *
 * Developer  :
 * Date       : [YYYY/MM/DD]
 * Description:
 *
 */
import com.extol.eventmanager.NotificationType;
import com.extol.eventmanager.NotificationService;
import com.extol.eventmanager.Notification;
import com.extol.eventmanager.NotificationException;
import com.extol.eventmanager.NotificationProperty;

/**
 * Sample code that demonstrates how a client can use the EBI Notification API to send and receive notifications.
 */
public class SendEbiItmNotif {

  NotificationService service;
  NotificationType type;
  Notification notification;


  public SendEbiItmNotif(String formattedTypeName) {

    System.out.println("...constructing the SendEbiItemNotif object...");

    if (service == null) service = new NotificationService();
    type = service.createNotificationType(formattedTypeName);
    notification = service.createNotification(type);

  }

  public static void sendItemNotification(String formattedTypeName, String itemKeyValue, String notificationMessage,
                                          Long sequenceNumber) {

    if (formattedTypeName != null) {
      System.out.println("formattedTypeLength (prior to trimming): " + formattedTypeName.length());
      formattedTypeName.trim();
      formattedTypeName = formattedTypeName.trim();
      System.out.println("formattedTypeLength (after to trimming): " + formattedTypeName.length());
    }

    SendEbiItmNotif sender = new SendEbiItmNotif(formattedTypeName);

    System.out.println("...decorating the notification with the item key value, notification message," +
                       " and sequence number...");

    if (notificationMessage != null && notificationMessage.length() > 0) {
      sender.notification.setMessage(notificationMessage);
    } else {
      System.out.println("...notification message was either null or the length was zero...");
    }

    sender.notification.setSequenceNumber(sequenceNumber.longValue());

    if (itemKeyValue != null && itemKeyValue.length() > 0) {

      // sender.notification.setUserData(new String[]{itemKeyValue});

      NotificationProperty itemNumberProperty = new NotificationProperty("item_number", "item number of the" +
                                                                                        " database row", 0);
      itemNumberProperty.setValue(itemKeyValue);
      sender.notification.addProperty(itemNumberProperty);

    } else {
      System.out.println("...item key value was either null or the lenght was zero...");
    }

    System.out.println("...sending the Notification object to EBI...");

    try {
      // Send the notification to EBI:
      sender.service.sendNotification(sender.notification);
      System.out.println("...sent successfully...");
    }
    catch (NotificationException ne) {
      ne.printStackTrace();
      System.out.println("Exception: " + ne.getMessage());
    }
  }

  public static void testSend() {
    SendEbiItmNotif.sendItemNotification("com.extol.test.itmphy01.insert","82131", "Test notification", new Long(5));
  }

  public static void main(String[] args) {

    // Assume the arguments are in the same order and type as the method sendItemNotification, except the last arg
    //  which is a String that needs to be convereted into a Long

    // Dump the arguments to System.out
    if (args != null) {
      System.out.println("Arguments:");
      for (int q=0; q < args.length; q++) {
        System.out.println(" " + q + ") " + args[q]);
      }
    }

    String localFormattedName = null;
    String localItmNumber = null;
    String localNotificationMessage = "Triggered Notification on ITMPHY01";
    String localSequenceNumber = null;
    Long localSequenceNumberL = null;

    try {
      localFormattedName = args[0];
      localItmNumber = args[1];
      localSequenceNumber = args[2];
      localSequenceNumberL = new Long(localSequenceNumber);
    }
    catch (Exception e) {
      e.printStackTrace();  //To change body of catch statement use Options | File Templates.
      System.err.println("Error while attempting to extract arguments: " + SendEbiItmNotif.class.getName());
      StringBuffer errorBuffer = new StringBuffer();
      errorBuffer.append("Expected:\n1)formattedTypeName (e.g. com.extol.test.itmphy01.insert\n2) itemKeyValue (e.g " +
                         "12345)\n3) sequenceNumber (e.g. 123)");
    }

    try {
      SendEbiItmNotif.sendItemNotification(localFormattedName, localItmNumber, localNotificationMessage, localSequenceNumberL);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

}



By: on