Creating new Java class
- Updated: 2022/05/19
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
-
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.
-
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);
-
Update the @CommandPkg parameters such as:
name
,label
,node_label
,description
, andicon
. -
Update the
return_label
and thereturn_type
. -
Add the
NumberValue
action, internal validation, business logic, and the return value. - Delete the Concatenated. java file and samples.commands. basic directory, and the sample.commands directory.
-
Copy
-
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" }
- Delete packages: go to src > main > java > com.automationanyhwere.botcommand, and delete the samples.commands and delete the samples packages.
-
Import new icons from Github and update the CommandPkg
annotation.
- Download any icons you want to use in your bot flow and save them as .svg files.
- Copy your image files into the src > main > resources > icons folder.
-
Create a new directory: go to src, right- click, and
select New > Directory.
-
In the Name field, enter
test\java. Alternatively, select the
test\java
, and enter the name TestFileSize. - Configure test annotations in the TestFileSize java class.
-
Create a
@test
public class, create aGetFileDetails
object, and invoke the action. - Optional: Run the TestGetFileDetails in IntelliJ to test it.
-
In the Name field, enter
test\java. Alternatively, select the
-
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); }
- Save the project: File > Save All.
- Click Reload All Gradle Projects, and then click Execute Gradle Task, and verify that the A2019FileDetails project is selected.
-
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>