Creating new Java class

Use IntelliJ to create a new java class and a new directory, and configure other build files.

Prerequisites

Complete the steps in the following task: Configuring build files.

Procedure

  1. Create a new Java Class, right-click the com.automationanywhere.botcommand package, and select New > Java Class. Enter the name for the new class GetFileDetails.
    1. Copy @BotCommand from the Concatenate. java and paste it into the new GetFileDetails.java file.
      import static com.automationanywhere.commandsdk.model.DataType.STRING;
      //BotCommand makes a class eligible for being considered as an action.
      
      @BotCommand
      //CommandPks adds required information to be dispalable on GUI.
      @CommandPkg(
      		//Unique name inside a package and label to display.
      		name = "concatenate", label = "[[Concatenate.label]]", 
      		node_label = "[[Concatenate.node_label]]", description = "[[Concatenate.description]]", icon = "pkg.svg", 
      		
      		//Return type information. return_type ensures only the right kind of variable is provided on the UI. 
      		return_label = "[[Concatenate.return_label]]", return_type = STRING, return_required = true)
      public class Concatenate {
      	
      	//Messages read from full qualified property file name and provide i18n capability.
      	private static final Messages MESSAGES = MessagesFactory
      			.getMessages("com.automationanywhere.botcommand.samples.messages");
      
      	//Identify the entry point for the action. Returns a Value<String> because the return type is String. 
      	@Execute
      	public Value<String> action(
      			//Idx 1 would be displayed first, with a text box for entering the value.
      			@Idx(index = "1", type = TEXT) 
      			//UI labels.
      			@Pkg(label = "[[Concatenate.firstString.label]]") 
      			//Ensure that a validation error is thrown when the value is null.
      			@NotEmpty 
      			String firstString,
      			
      			@Idx(index = "2", type = TEXT) 
      			@Pkg(label = "[[Concatenate.secondString.label]]") 
      			@NotEmpty 
      			String secondString) {
      		
      		//Internal validation, to disallow empty strings. No null check needed as we have NotEmpty on firstString.
      		if ("".equals(firstString.trim()))
      			throw new BotCommandException(MESSAGES.getString("emptyInputString", "firstString"));
      
      		if ("".equals(secondString.trim()))
      			throw new BotCommandException(MESSAGES.getString("emptyInputString", "secondString"));
      
      		//Business logic
      		String result = firstString + secondString;
      		
      		//Return StringValue.
      		return new StringValue(result);
    2. Update the @CommandPkg parameters such as: name, label, node_label, description, and icon.
    3. Update the return_label and the return_type.
    4. Add the NumberValue action, internal validation, business logic, and the return value.
    5. Delete the Concatenated. java file and samples.commands. basic directory, and the sample.commands directory.
  2. Configure the en_US.json file: go to src > main > resources > locales > en_US.json and add the following fields after the label and description values, and delete other parameters from the file.
    {
    	"label": "File Details",
    	"description": "Returns basic file details",
    	"GetFileDetails.label": "File Size",
    	"GetFileDetails.description": "Return the size of the selected file in bytes",
    	"GetFileDetails.node_label": "File Size in Bytes",
    	"GetFileDetails.return_label": "File Size",
    	"GetFileDetails.return_label_description": "Return in bytes",
    	"GetFileDetails.filePath.label": "Select a File for analysis"
    }
  3. Delete packages: go to src > main > java > com.automationanyhwere.botcommand, and delete the samples.commands and delete the samples packages.
  4. Import new icons from Github and update the CommandPkg annotation.
    1. Download any icons you want to use in your bot flow and save them as .svg files.
    2. Copy your image files into the src > main > resources > icons folder.
  5. Create a new directory: go to src, right- click, and select New > Directory.
    1. In the Name field, enter test\java. Alternatively, select the test\java, and enter the name TestFileSize.
    2. Configure test annotations in the TestFileSize java class.
    3. Create a @test public class, create a GetFileDetails object, and invoke the action.
    4. Optional: Run the TestGetFileDetails in IntelliJ to test it.
  6. Configure the TestFileSize file, and open TestFileSize and copy and paste the following code:
    {
        @Test
        public void TestGetFileDetails() {
            String filePath = "src\\main\\resources\\icons\\sample.svg";
            //Create GetFileDetails Object
            GetFileDetails getFileDetails = new GetFileDetails();
            //invoke action
            NumberValue output = getFileDetails.action(filePath);
            Assert.assertEquals(output.getAsDouble(), 5027.0);
        }
  7. Save the project: File > Save All.
  8. Click Reload All Gradle Projects, and then click Execute Gradle Task, and verify that the A2019FileDetails project is selected.
  9. In the Run Anything window, enter gradle clean build shadowJar.
    After it runs, the following message is displayed: BUILD SUCCESSFUL in 8s <number of seconds>

Next steps

Upload custom package to your Control Room