Michael_Lowry

Get localized message from message number

Discussion created by Michael_Lowry on Mar 6, 2018

In another thread, I pointed out the existence of the TranslatedMesage Java API class. Thanks to a pointer from Automic, and with a bit of trial-and-error, I was able to figure out how to use this class to return localized versions of arbitrary Automic messages, including status codes and error messages.

package com.uc4.testing;

import com.uc4.api.TranslatedMessage;
import com.uc4.communication.Connection;
import com.uc4.communication.ConnectionAttributes;
import com.uc4.communication.requests.CreateSession;
import com.uc4.api.MessageBox;
import com.uc4.translate.Message;

import java.io.IOException;

public class Main {

    private static final String AE_IP_ADDRESS = "192.168.2.1";
    private static final int AE_IP_PORT = 2217;
    private static final int AE_CLIENT = 1;
    private static final String AE_USER = "AE_USER";
    private static final String AE_DEPT = "MYCORP";
    private static final String AE_PASSWORD = "XXXXXXXX";
    private static final char AE_LANG = 'E';

    public static Connection ConnectToAE() {
        boolean connected = false;
        Connection myConnection = null;
        CreateSession mySession = null;
        try {
            myConnection = Connection.open(AE_IP_ADDRESS, AE_IP_PORT);
            mySession = myConnection.login(AE_CLIENT, AE_USER, AE_DEPT, AE_PASSWORD, AE_LANG);
        } catch (IOException e) {
            e.printStackTrace();
        }
        MessageBox msgBox = mySession.getMessageBox();
        if (msgBox != null) {
            System.out.println("-‐-‐ Error: " + msgBox);
        }else{
            System.out.println("Connected successfully to Automation Engine.");
            if (mySession.isLoginSuccessful()){
                System.out.println("User " + mySession.getUserName() + "(" + AE_USER + "/" + AE_DEPT +
                        ") successfully logged into " + mySession.getSystemName() + ". Session ID: " +
                        mySession.getSessionID() + ".");
                System.out.println(mySession.getWelcomeMessage());
            }
            connected = true;
        }
        return myConnection;
    }

    public static boolean getTranslatedMessage(Connection myConnection, int messageNumber, String messageInsert) {
        ConnectionAttributes connAtt = new ConnectionAttributes();
        connAtt = myConnection.getSessionInfo();
        boolean isValid = connAtt.isValid();
        if (isValid) {
            Message translator = connAtt.getTranslator();
            if (translator != null) {
                TranslatedMessage translatedMessage = new TranslatedMessage(messageNumber, messageInsert, translator);
                char messageType = translatedMessage.getType();
                String messageTypeText = "";
                switch (messageType) {
                    case 'E':
                        messageTypeText = "ERROR";
                        break;
                    case 'W':
                        messageTypeText = "WARNING";
                        break;
                    case 'I':
                        messageTypeText = "INFO";
                        break;
                    case 'A':
                        messageTypeText = "ABORT";
                        break;
                    case 'Q':
                        messageTypeText = "QUESTION";
                        break;
                    case 'C':
                        messageTypeText = "CAPTION";
                        break;
                    case 'D':
                        messageTypeText = "SERVER";
                        break;
                    default:
                        messageTypeText = "UNKNOWN";
                        break;
                }
                String formattedMessageNumber = String.format("%08d",translatedMessage.getNumber());
                String finalMessage = messageTypeText + ": U" + formattedMessageNumber + " " + translatedMessage.getText();
                System.out.println(finalMessage);
            }else{
                System.out.println("Translator is null!");
                exit();
            }
        } else {
            System.out.println("Connection is NOT valid.");
            exit();
        }
        return isValid;
    }

    private static void exit() {
        System.exit(200);
    }

    public static void main(String[] args) {
        if (args.length < 2) {
            System.out.println("Please provide a message number, and optionally, a message insert string."); exit();
        }
        int messageNumber = Integer.parseInt(args[0]);
        String messageInsert = args[1];
        System.out.println("Message number: " + String.valueOf(messageNumber));
        System.out.println("Message insert: " + messageInsert);
        Connection myConnection = ConnectToAE();
        getTranslatedMessage(myConnection, messageNumber, messageInsert);
    }
}


Run the program with one or two arguments:

  1. The message number
  2. The message insert string (optional)

The message number is an integer corresponding to the message number in the messages documentation.
The message insert string is a vertical bar-delimited string containing zero or more message inserts. Not all messages have inserts. These appear as &1, &2, &3, etc. in the messages documentation and uc.msl file.

 

 

Example 1. Return status text for status code 1700

Message number: 1700
Message insert:
Connected successfully to Automation Engine.
User Test(
AE_USER/MYCORP) successfully logged into UC4_EXP2. Session ID: 0000000002910205.
Log on of 'Test User' successful, your last session:  '2018-03-06' at '12:07:14'.

Localized message:
SERVER: U00001700 Waiting for predecessor

 

Example 2. Print message U00011191

Message number: 11191
Message insert: 12345678
Connected successfully to Automation Engine.
User Test User (AE_USER/MYCORP) successfully logged into UC4_EXP2. Session ID: 0000000002910202.
Log on of 'Test User' successful, your last session:  '2018-03-06' at '12:20:56'.

Localized message:
ERROR: U00011191 Task with RunID '12345678' is not active.

Example 3. Print message U00010002

Message number: 10002
Message insert: UC0.TEST1.JOBI|UC0.TEST1.SCRI|5
Connected successfully to Automation Engine.
User Test User (AE_USER/MYCORP) successfully logged into UC4_EXP2. Session ID: 0000000002910202.
Log on of 'Test User' successful, your last session:  '2018-03-06' at '12:23:35'.

Localized message:
ERROR: U00010002 Include 'UC0.TEST1.JOBI' not found in object 'UC0.TEST1.SCRI', line '5'.

To change the localization language to German or French, just change the value of the AE_LANG variable in the program to D or F, respectively. Enjoy!

Outcomes