0

I have to make a call to a method of a web service via SSJS. One of the input parameters of the method is a structure array. The web service consumer is implemented in java. I would like to know how to declare and instantiate the java strucuture array in SSJS.

The signature of the method is:

(short , short , java.lang.String , java.lang.String , java.lang.String , java.lang.String , java.lang.String , java.lang.String , java.lang.String , short , java.lang.String , java.lang.String , ESTRUTURACHECKLIST[] )

I am creating the array as per your suggestion:

lst=new ArrayList();

var chk:xx.xxx.xxxx.xxxx.ESTRUTURACHECKLIST=new 
xx.xxx.xxxx.xxxx.ESTRUTURACHECKLIST();
chk.setCONTEUDOCHECKLIST("XXXX");
chk.setDESCRICAOCHECKLIST("CÓDIGO USUÁRIO");
lst.add(chk);

var chk1:xx.xxx.xxxx.xxxx..ESTRUTURACHECKLIST=new 
xx.xxx.xxxx.xxxx..ESTRUTURACHECKLIST();
chk1.setCONTEUDOCHECKLIST("TESTE");
chk1.setDESCRICAOCHECKLIST("NOME USUARIO");
lst.add(chk1);
var chk2:xx.xxx.xxxx.xxxx..ESTRUTURACHECKLIST=new 
xx.xxx.xxxx.xxxx..ESTRUTURACHECKLIST();
chk2.setCONTEUDOCHECKLIST("TESTE NOTES");
chk2.setDESCRICAOCHECKLIST("NOME NOTES");
lst.add(chk2);
arr=lst.toArray(); 

When I created the structure array following its suggestion, the java method gives the error and does not recognize the last array. To be sure, I changed the signature of the class that instantiates the web service client by removing the array, there was no error. What I think is occurring is that the java class is not recognizing the array passed by the SSJS with an array of the specified structure.

The error calling the method is:

Error while executing Javascript action expression Script interpreter error, line=75, col=13: Java method 'xxxxx(number, number, string, string, string, string,string, string,string, string,string, [Ljava.lang.Object;)'on java class xx.xxxx.xxxx.xxx not found

4
  • What do you mean by "structure array"? Is it an array of a certain class? Is it a ListArray? How is the exact signature of your method you want to call? Commented Jul 28, 2017 at 12:26
  • This is an array of a Java class defined by the developer. Commented Jul 28, 2017 at 13:05
  • If I did not make a mistake while counting then the input for your method should contain 2x number + 7x string + 1x number + 2x string + 1x array but the error indicates that you are trying to execute with 2x number + 9x string + 1x array. It seems like 1 argument (number) is missing... Commented Aug 1, 2017 at 17:03
  • Actually I have an intermediate class that receives the signature of 2x number + 9x string + 1x array, and calls the signature method mentioned in the problem statement, also passing the 1x number. Commented Aug 1, 2017 at 17:52

2 Answers 2

1

If you need a Java array of the given objects, you can first put them in a List-like structure, like ArrayList or Vector, and afterwards retrieve the Java array by calling the toArray method.

Here is a SSJS code snippet that should help you:

importPackage(java.util);
importPackage(br.com.mercantil.dmdws);

var lst,chk,arr;

lst=new ArrayList();

chk=new ESTRUTURACHECKLIST();
// ... do whatever you need to do to the object
lst.add(chk);

// ... repeat previous step if needed

arr=lst.toArray(); // this is the Java array

Update

If the method you use cannot handle the Object array because it demands the array to be of a certain class you can provide an array with the needed runtime type as first argument to the toArray method. As I do not know how to create or cast such an array in SSJS I would add a "helper" method to the br.com.mercantil.dmdws.ESTRUTURACHECKLIST class which looks like this

public static ESTRUTURACHECKLIST[] getJavaArray(int n) {
    return new ESTRUTURACHECKLIST[n];
}

and create the array in the example above in the following way:

arr=lst.toArray(ESTRUTURACHECKLIST.getJavaArray(lst.size()));
Sign up to request clarification or add additional context in comments.

6 Comments

The problem is that when calling the java method of the web service, it does not recognize the array of objects as an array array and gives an error.
It's hard to find out what exactly the problem is without having information about the method you are calling and the exception it raises. You should provide that here.
I put more details on the problem statement.
@MarcusLoza: Did you try the updated solution? Did it work?
I have not had the time yet, but next week I will implement the solution you suggested and I will give a feedback here. Thank you very much!
|
0

Using the full name of the relevant class, including package name, will work. For example, if it was needing to pass a Java HashMap, you could use:

var myMap:java.util.HashMap = new java.util.HashMap();

This assumes the relevant class is accessible to the code, for example the HashMap class here is accessible because it's part of the Java core, which XPages has access to.

5 Comments

The class that represents the structure has the name of STRUTURACHECKLIST in java the statement would look like this: ESTRUTURACHECKLIST [] chk = new ESTRUTURACHECKLIST [0]. How do I declare in SSJS?
In Java there should be import statements to resolve that or it's a class somehow adjacent to what you're seeing. It may be in a default package of whatever you're looking at. I've not come across that in my Java experience and Java editors usually warn against that.This SO question quote a Sun book stating it's bad practice stackoverflow.com/questions/7849421/…. One of the reasons it gives - difficulty importing the class in, so using it - seems to be what you're hitting here. I don't know a solution, unless you can give a package name.
But the code quoted above for an example, the code is related to the class-related package. See in the code below, which just for the structure and methods, though it's not like declaring the array. 'var chk:br.com.mercantil.dmdws.ESTRUTURACHECKLIST=new br.com.mercantil.dmdws.ESTRUTURACHECKLIST; chk.setCONTEUDOCHECKLIST("XXXXXX"); chk.setDESCRICAOCHECKLIST("CÓDIGO USUÁRIO");
That's the kind of approach that's needed, to use the full name. But bear in mind new calls the constructor of a Java class, and the constructor is a method, so it would need to be var chk:br.com.mercantil.dmdws.ESTRUTURACHECKLIST=new br.com.mercantil.dmdws.ESTRUTURACHECKLIST(); From there you should be able to test and debug.
But that is the statement to the structure. This I was able to do. I could not declare the array of structures. What would be the statement for an array in this case. I tried various shapes and none of them worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.