MicroStrategy ONE

Using Web Objects to Create a Java-based Task

The code sample provided below illustrates how to construct a Java-based task using Web Objects, instead of Web Beans.

The explanations below go through the code, section by section, and provide a detailed description of what each part of the code does. The explanations precede each section of the code.

  • Explanation: First, you specify the name of the package to which the task class belongs and import the classes that are needed to compile this class, including classes from the com.microstrategy.web.tasks hierarchy. Classes from this hierarchy are normally needed for any Java-based task that you create.

    Copy
    package com.microstrategy.sdk.tasks;
    import com.microstrategy.web.beans.MarkupOutput;
    import com.microstrategy.web.beans.RequestKeys;
    import com.microstrategy.web.objects.WebIServerSession;
    import com.microstrategy.web.objects.WebObjectsFactory;
    import com.microstrategy.web.objects.admin.WebObjectsAdminException;
    import com.microstrategy.web.objects.admin.monitors.CacheDetails;
    import com.microstrategy.web.objects.admin.monitors.CacheResults;
    import com.microstrategy.web.objects.admin.monitors.CacheSource;
    import com.microstrategy.web.objects.admin.monitors.Caches;
    import com.microstrategy.web.objects.admin.monitors.EnumWebMonitorType;
    import com.microstrategy.web.objects.admin.monitors.MonitorFilter;
    import com.microstrategy.web.tags.Tag;
    import com.microstrategy.web.tags.TagsFactory;
    import com.microstrategy.web.tasks.AbstractBaseTask;
    import com.microstrategy.web.tasks.TaskException;
    import com.microstrategy.web.tasks.TaskInternalException;
    import com.microstrategy.web.tasks.TaskParameterMetadata;
    import com.microstrategy.web.tasks.TaskRequestContext;
    import com.microstrategy.web.tasks.TaskRequestMalformedException;
    import com.microstrategy.webapi.EnumDSSXMLCacheInfo;
    import com.microstrategy.webapi.EnumDSSXMLLevelFlags;
    import com.microstrategy.webapi.EnumDSSXMLMonitorFilterOperator;
    public class GetCacheInfoByProjectsAndSize extends AbstractBaseTask  {
    public final String PARAM_CACHE_SIZE = "cacheSize";
    public final String PARAM_PROJECT_GUID = "projectGUID";
  • Explanation: Next, you declare the variables for the parameters that will be used to provide information to the task. In this example, you create variables for the cache size and the project ID.

    Copy
    private TaskParameterMetadata cacheSizeTaskParam;
    private TaskParameterMetadata projectGUIDTaskParam;
  • Explanation: Then, you add the constructor for your task class. You set the description for the task you are creating and the parameters needed for the task. In this example, the task description is "Retrieves monitor information for caches in projects that are above a certain size" and the task parameters are the cache size and project ID parameters you declared above. You also add the session state parameter to the task.

    Copy
       public GetCacheInfoByProjectsAndSize() {
        //Set the description for the Task
            super("Retrieves monitor information for caches in projects that are above a certain size");
            
            //Associate Parameter to the Task
            cacheSizeTaskParam = addParameterMetadata(PARAM_CACHE_SIZE, "Cache Size", true, "1");
            projectGUIDTaskParam = addParameterMetadata(PARAM_PROJECT_GUID, "Project GUID", false, "B19DEDCC11D4E0EFC000EB9495D0F44F");
            addSessionStateParam(true, "");
        }
  • Explanation: Finally, you override the processRequest method. This method will perform most of the processing and will format the response.

    Copy
        public void processRequest(TaskRequestContext context, MarkupOutput markup) throws TaskException {

    You override this method so that it does the following:

    • Explanation: You obtain the RequestKeys object and check to make sure that it contains all required parameters.

      Copy
              //Obtain request keys
              RequestKeys requestKeys = context.getRequestKeys();
          
              checkForRequiredParameters(requestKeys);
    • Explanation: You check to see whether the task envelope parameter (taskEnv) is set to "xml" and throw an exception if it is not. In this example, you display the message "taskEnv value xxxx is not supported by this task".

      Copy
              //throw exception if it is not an xml envelope
              if (!"xml".equals(requestKeys.getValue("taskEnv"))) {
                  throw new TaskRequestMalformedException("taskEvn value (" + requestKeys.getValue("taskEnv") + ") is not supported by this task");
              }
    • Explanation: You retrieve the values for your two task parameters—cache size and project ID—from the requestKeys object.

      Copy
              //Obtain Task Parameters from the request keys
              String projectGUID = projectGUIDTaskParam.getValue(requestKeys);
              String cacheSize = cacheSizeTaskParam.getValue(requestKeys);
    • Explanation: You retrieve the current session state from the Task Request Context..

      Copy
              //Obtain the session from the Task Request Context
              WebIServerSession session = context.getWebIServerSession(PARAM_NAME_SESSION_STATE, null);
    • Explanation: You create a WebObjectsFactory instance and use it to retrieve a cache source.

      Copy
              //Retrieve a cache source from the factory
              WebObjectsFactory factory = session.getFactory();
              CacheSource cacheSource= (CacheSource)factory.getMonitorSource(EnumWebMonitorType.WebMonitorTypeCache);
    • Explanation: You set the cache monitoring level and the filter. In this example, you set the cache monitoring to the detail level and you add two filters—(1) cache size greater than or equal to the value represented by cache size task parameter and (2) project ID equal to the value represented by project ID task parameter.

      Copy
              //Set monitoring level and filter, in this case we are setting the project ID and cache Size
              cacheSource.setLevel(EnumDSSXMLLevelFlags.DssXmlDetailLevel);
          
              MonitorFilter filter = cacheSource.getFilter();
          
              filter.add(EnumDSSXMLCacheInfo.DssXmlCacheInfoSize,
              EnumDSSXMLMonitorFilterOperator.DssXmlGreaterOrEqual,
              cacheSize);
       
              filter.add(EnumDSSXMLCacheInfo.DssXmlCacheInfoProjectGuid,
              EnumDSSXMLMonitorFilterOperator.DssXmlEqual,
              projectGUID);
    • Explanation: Finally, you create the XML output. The output in this example will look like the commented output in the code below.

      Copy
         /**
          * The output format will look like this:
          *
          * <caches>
          *    <cache>
          *       <cache-name>Simple Report</cache-name>
          *       <cache-size>70</cache-size>
          *       <hit-count>22</hit-count>
          *       <last-hit>11/4/03 8:14 PM</last-hit>
          *       <id>D60543234AECA1AE29C7EFB138C5F87F</id>
          *    </cache>
          * ...
          * </caches>
          *
          */
       
              //Create the root node of the XML
              Tag cachesTag = TagsFactory.getInstance().newTag("caches");
                  try {
                     CacheResults cacheResults = cacheSource.getCaches();
       
                     if(cacheResults.size()==0){
                         //Throw exception that cache results did
                         throw new TaskRequestMalformedException("No Project found for this ID");
                     }
       
                    //There will only be one set of caches, since we are setting the project
                   Caches caches = cacheResults.get(0);
        
                   //Loop through caches and create the correct XML
                   for(int i = 0; i < caches.getCount(); i++){
                       CacheDetails cache = (CacheDetails)caches.get(i);
                       Tag cacheTag =cachesTag.addChild("cache");
                       cacheTag.addChild("cache-name").addTextChild(cache.getCacheSourceName());
                       cacheTag.addChild("hit-count").addTextChild(Integer.toString(cache.getHitCount()));
                       cacheTag.addChild("id").addTextChild(cache.getID());
                   }
        
               }
               catch (WebObjectsAdminException e) {
                   throw new TaskInternalException(e);
               }
        
               //Render the caches tag
               cachesTag.render(markup);
           }