cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
188
Views
0
Helpful
0
Comments
cdnadmin
Level 11
Level 11

Subject: RE: Here's a custom element that provides random int values
Replied by: Tieying Xuan on 07-07-2010 01:41:40 AM
Thanks for your post! Someone will need it. And it's a very good sample for customizing a Action element!
This document was generated from CDN thread

Created by: Voiceops SSC on 06-07-2010 10:24:03 PM
I thought somebody could use this; I built it to be able to play random holding prompts. 
This is an element that generates a random integer between 0 and some upper bound number (that you provide).
Input an integer and it will return a random integer between 0 and one less than the input.
 
lets say you need 5 random values:
input 6
 
*********************Code starts here:****************************************
 
// These classes are used by custom configurable elements.
import com.audium.server.session.ActionElementData;
import com.audium.server.voiceElement.ActionElementBase;
import com.audium.server.voiceElement.ElementData;
import com.audium.server.voiceElement.ElementException;
import com.audium.server.voiceElement.ElementInterface;
import com.audium.server.voiceElement.Setting;
import com.audium.server.xml.ActionElementConfig;
import java.util.Random;

public class RandomNumber extends ActionElementBase implements ElementInterface
{
   
    public void doAction(String name, ActionElementData actionData) throws ElementException
    {
        try {
            // Get the configuration
            ActionElementConfig config = actionData.getActionElementConfig();
           
            //get the setting values from the setting array
            //each setting is a String. You can convert them.
            String input = config.getSettingValue("input",actionData);
            String resultType = config.getSettingValue("resultType",actionData);
            String result = config.getSettingValue("result",actionData);
                                   
            //instantiate the generator class.
            //get the input string and convert it to integer
            Random generator = new Random();
            int outputint = generator.nextInt( Integer.parseInt(input) );
                                               
            //Element or Session data as requested
            //and store it into your Studio variable
            //also convert the output from int to string
            if(resultType.equals("Element")){
                actionData.setElementData(result,Integer.toString(outputint));
            } else {
                actionData.setSessionData(result,Integer.toString(outputint));
            }
            actionData.setElementData("status","success");
        } catch (Exception e) {
            //return an empty string
            e.printStackTrace();
            actionData.setElementData("status","failure");
        }
       
    }

    public String getElementName()
    {
        return "Random Number";
    }

    public String getDisplayFolderName()
    {
        return "Custom Elements";
    }

    public String getDescription()
    {
        return "This class generates a random integer between 0 and some upper bound.\n" + "Provide an integer and it will return an integer between 0 and one less than the provided.\n" + "e.g. 6 (n-1) = between 0 and 5";
    }

    public Setting[] getSettings() throws ElementException
    {
         //number of settings for element
         Setting[] settingArray = new Setting[3];
         
          //Settings:
        settingArray[0] = new Setting("input", "Int upper bound",
                 "This is the upper bound from which to generate the random number.",
                 true,   // It is required
                 true,   // It appears only once
                 true,   // It allows substitution
                 Setting.STRING);
           
        settingArray[1] = new Setting("resultType", "Result Type",
                "Choose where to store result \n" +
                "into Element or Session data",
                true,   // It is required
                true,   // It appears only once
                false,  // It does NOT allow substitution
                new String[]{"Element","Session"});//pull-down menu
        settingArray[1].setDefaultValue("Session");
       
        settingArray[2] = new Setting("result", "RandomPick",
                "Name of variable to hold the result.",
                true,   // It is required
                true,   // It appears only once
                true,   // It allows substitution
                Setting.STRING);   
        settingArray[2].setDefaultValue("RandomPick");
       
             

return settingArray;
    }
   
    public ElementData[] getElementData() throws ElementException
    {
        return null;
    }
}
 
******************************************************************************************
Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community:

Quick Links