MicroStrategy ONE

Creating a Web Subscription

This example demonstrates how to create a subscription in MicroStrategy Web. All subscriptions in MicroStrategy Web are displayed under the "Subscriptions" heading on the My Subscriptions page.

Code sample

/**

 * MicroStrategy SDK Sample

 *

 * Copyright © 2010 MicroStrategy Incorporated. All Rights Reserved.

 *

 * MICROSTRATEGY MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE

 * SUITABILITY OF THIS SAMPLE CODE, EITHER EXPRESS OR IMPLIED, INCLUDING

 * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS

 * FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. MICROSTRATEGY SHALL NOT

 * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,

 * MODIFYING OR DISTRIBUTING THIS SAMPLE CODE OR ITS DERIVATIVES.

 *

 *

 */

 

package com.microstrategy.standalone;

 

import com.microstrategy.web.objects.EnumWebSubscriptionObjectTypes;

import com.microstrategy.web.objects.SimpleList;

import com.microstrategy.web.objects.WebIServerSession;

import com.microstrategy.web.objects.WebObjectInfo;

import com.microstrategy.web.objects.WebObjectSource;

import com.microstrategy.web.objects.WebObjectsException;

import com.microstrategy.web.objects.WebObjectsFactory;

import com.microstrategy.web.objects.WebSubscription;

import com.microstrategy.web.objects.WebSubscriptionAddress;

import com.microstrategy.web.objects.WebSubscriptionDevice;

import com.microstrategy.web.objects.WebSubscriptionRecipientList;

import com.microstrategy.web.objects.WebSubscriptionTrigger;

import com.microstrategy.web.objects.WebSubscriptionsSource;

import com.microstrategy.webapi.EnumDSSXMLApplicationType;

import com.microstrategy.webapi.EnumDSSXMLSubscriptionDeliveryType;

 

   public class CreateSubscription {

      public static void main(String[] args){

         String SessionID = "";

         // Create factory object.

         WebObjectsFactory factory=WebObjectsFactory.getInstance();

         WebIServerSession serverSession=factory.getIServerSession();

         // Set properties on the session object

         serverSession.setServerName("ISERVERNAME");

         serverSession.setServerPort(0);

         serverSession.setProjectName("PROJECTNAME");

         serverSession.setLogin("USERID");

         serverSession.setPassword("PASSWORD");

         serverSession.setApplicationType(EnumDSSXMLApplicationType.DssXmlApplicationCustomApp);

 

         try {

            // Create the session

            SessionID = serverSession.getSessionID();

            System.out.println("Welcome " + serverSession.getLogin());

            System.out.println("SessionID: " + SessionID);

 

            // Get the WebSubscriptionsSource object and test by getting a count of subscriptions that were previously created

            WebSubscriptionsSource wss = factory.getSubscriptionsSource();

            SimpleList wsList = wss.getSubscriptions();

            int size = wsList.size();

            System.out.println("Current number of subscriptions: " + size);

 

            //Get the report object and the trigger object that will be used to define the new subscription

            WebObjectSource wos = factory.getObjectSource();

            WebObjectInfo rptInfo = wos.getObject("2DA151E14F2F906EBBDC2F8F4546D032", 3, true);

            System.out.println("Report name is: "+ rptInfo.getName());

 

            //Get the trigger object - 'On Database Load'

            WebSubscriptionTrigger trigger = (WebSubscriptionTrigger)wss.getObject("DE2B9DE549E75B363F3F6A95F2B0A3E6", EnumWebSubscriptionObjectTypes.WEB_SUBSCRIPTION_TRIGGER, true);

 

            // Create and save the new subscription object

            WebSubscription webSubscription = (WebSubscription) wss.getNewObject(EnumWebSubscriptionObjectTypes.WEB_SUBSCRIPTION);

            webSubscription.setName("My email subscription");

            webSubscription.setTrigger( trigger);

            webSubscription.setContent(rptInfo);

            webSubscription.setDeliveryMode(EnumDSSXMLSubscriptionDeliveryType.DssXmlDeliveryTypeEmail);

            SimpleList addresses = wss.getAddressesByDeliveryMode(EnumDSSXMLSubscriptionDeliveryType.DssXmlDeliveryTypeEmail);

            WebSubscriptionAddress defaultAddress = null;

            for (int i = 0; addresses != null && i < addresses.size(); i++) {

                WebSubscriptionAddress address = (WebSubscriptionAddress) addresses.item(i);

                if (address.getValue().equalsIgnoreCase("user@microstrategy.com")) {

                defaultAddress = address;

                }

            }

 

            // if the user don't have an email address assign one

            if(defaultAddress == null) {

               defaultAddress = (WebSubscriptionAddress)wss.getNewObject(EnumWebSubscriptionObjectTypes.WEB_SUBSCRIPTION_ADDRESS);

               defaultAddress.setName("myEmail");

               defaultAddress.setValue("user@microstrategy.com");

               SimpleList devices = wss.getDevicesByDeliveryMode(EnumDSSXMLSubscriptionDeliveryType.DssXmlDeliveryTypeEmail);

               if (devices != null && devices.size() > 0) {

                  defaultAddress.setDevice((WebSubscriptionDevice) devices.item(0));

               }

               defaultAddress.save();

            }

 

            WebSubscriptionRecipientList wrl = webSubscription.getRecipients();

            wrl.addAddress(defaultAddress);

            webSubscription.save();

            

            } catch (WebObjectsException ex) {

               System.out.println(ex.getMessage());

               ex.printStackTrace();

            }

      }

}