Crear nueva clase de Java

Use IntelliJ para crear una nueva clase de Java y un nuevo directorio, y configure otros archivos de compilación.

Antes de empezar

Complete los pasos en la siguiente tarea: Configuración de los archivos de compilación.

Procedimiento

  1. Crear una nueva clase de Java, hacer clic con el botón derecho del ratón en el paquete com.automationanywhere.botcommand y seleccionar Nueva > Clase de Java. Ingresar el nombre para la nueva clase GetFileDetails.
    1. Copiar @BotCommand de Concatenate. java y pegarlo en el nuevo archivo GetFileDetails.java.
      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. Actualizar los parámetros de @CommandPkg como: name, label, node_label, description y icon.
    3. Actualizar return_label y return_type.
    4. Agregar la acción NumberValue, la validación interna, la lógica de negocio y el valor de retorno.
    5. Eliminar el archivo Concatenado. java, el directorio samples.commands. basic y el directorio sample.commands.
  2. Configurar el archivo es_US.json: vaya a src > principal > recursos > locales > es_US.json y agregue los siguientes campos después de los valores de la etiqueta y la descripción, y elimine otros parámetros del archivo.
    {
    	"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. Eliminar los paquetes: vaya a src > principal > java > com.automationanyhwere.botcommand y elimine samples.commands y los paquetes samples.
  4. Importe nuevos íconos desde Github y actualice la anotación CommandPkg.
    1. Descargue los íconos que desee usar en su flujo de bots y guárdelos como archivos .svg.
    2. Copie ambos archivos de imagen en la carpeta src > principal > recursos > íconos.
  5. Cree un nuevo directorio: vaya a src, haga clic con el botón derecho y seleccione Nuevo > Directorio.
    1. En el campo Nombre, introduzca test\java. Como alternativa, seleccione test\java e ingrese el nombre TestFileSize.
    2. Configure las anotaciones de prueba en la clase de Java TestFileSize.
    3. Cree una clase pública @test cree un objeto GetFileDetails e invoque la acción.
    4. Opcional: Ejecute TestGetFileDetails en IntelliJ para probarlo.
  6. Configure el archivo TestFileSize, abra el archivo TestFileSize y copie y pegue el siguiente código:
    {
        @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. Guarde el proyecto: Archivo > Guardar todo.
  8. Haga clic en Vuelva a cargar todos los proyectos Gradle y luego haga clic en Ejecutar tarea Gradle, luego verifique que esté seleccionado el proyecto A2019FileDetails.
  9. En la ventana Ejecutar cualquier cosa, introduzca gradle clean build shadowJar.
    Una vez que se ejecuta, se muestra el siguiente mensaje: SE CREA CORRECTAMENTE en 8 s <number of seconds>

Qué hacer a continuación

Cargue un paquete personalizado a su Control Room