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

Marcus Loza
  • 185
  • 8
  • 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? – Knut Herrmann Jul 28 '17 at 12:26
  • This is an array of a Java class defined by the developer. – Marcus Loza Jul 28 '17 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... – xpages-noob Aug 01 '17 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. – Marcus Loza Aug 01 '17 at 17:52

2 Answers2

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()));
xpages-noob
  • 1,569
  • 1
  • 10
  • 37
  • 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. – Marcus Loza Aug 01 '17 at 13:13
  • 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. – xpages-noob Aug 01 '17 at 13:36
  • I put more details on the problem statement. – Marcus Loza Aug 01 '17 at 16:49
  • @MarcusLoza: Did you try the updated solution? Did it work? – xpages-noob Aug 04 '17 at 07:12
  • 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! – Marcus Loza Aug 04 '17 at 13:13
  • I've tried your suggestion, but the error below is displayed. The strange thing is that by putting br.com.mercantil.dmdws.ESTRUTURACHECKLIST. It lists the 'getJavaArray' method. I cleaned the project, rebuilt it, rebooted the Domino server, but nothing solved. Unknown member 'getJavaArray' in Java class 'br.com.mercantil.dmdws.ESTRUTURACHECKLIST' – Marcus Loza Aug 16 '17 at 19:18
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.

Paul Stephen Withers
  • 15,699
  • 1
  • 15
  • 33
  • 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? – Marcus Loza Jul 28 '17 at 13:30
  • 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 https://stackoverflow.com/questions/7849421/is-the-use-of-javas-default-package-a-bad-practice. 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. – Paul Stephen Withers Jul 28 '17 at 15:33
  • 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");_ – Marcus Loza Jul 28 '17 at 16:05
  • 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. – Paul Stephen Withers Jul 31 '17 at 07:14
  • 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. – Marcus Loza Jul 31 '17 at 12:01