cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - If community subscription notifications are filling up your inbox you can set up a daily digest and get all your notifications in a single email. X

Use an infotable passed as parameter in a java extension

cbaurand
12-Amethyst

Use an infotable passed as parameter in a java extension

I'm facing some problems when trying to use an infoTable in my extension.

The service is a utility service that is static:

 

 

 

public static void myService(Context cx, Scriptable me, Object[] args, Function func) throws Exception {
InfoTable iTable= (InfoTable) args[0];

...

 

 

 

in the metaData:

 

 

<FunctionDefinition description=""
name="myService
<ParameterDefinitions>
<FieldDefinition baseType="INFOTABLE"
aspect.dataShape="keysShape"
aspect.isEntityDataShape="true"
description="the key names" name="keys" ordinal="0" />
</ParameterDefinitions>
<ResultType baseType="NOTHING" name="result"
ordinal="0" />
</FunctionDefinition>

 

 

The call from the javascript;

 

 

 

let keys= Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({
    infoTableName: "InfoTable",
    dataShapeName: "keysShape"
});
keys.AddRow({key:"a string"});

myService(keys);

 

 

 

The problem if at this line:
InfoTable iTable= (InfoTable) args[0];

I get this exception:

Error executing service PrivateInitializeFastQueryCache. Message :: class com.thingworx.dsl.engine.adapters.ThingworxInfoTableAdapter cannot be cast to class com.thingworx.types.InfoTable (com.thingworx.dsl.engine.adapters.ThingworxInfoTableAdapter and com.thingworx.types.InfoTable are in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @4e4efc1b) - See Script Error Log for more details.

Any Idea?

 

 

 

ACCEPTED SOLUTION

Accepted Solutions

Finally, I did that in the extension:

InfoTable users = ((ThingworxInfoTableAdapter) args[0]).getInfoTable();

 

And it works fine.

 

Any comment?

View solution in original post

9 REPLIES 9

Try to convert to JSON when calling to service.

 

let keys= Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({
    infoTableName: "InfoTable",
    dataShapeName: "keysShape"
});
keys.AddRow({key:"a string"});

myService(keys.ToJSON());

 

from

https://community.ptc.com/t5/ThingWorx-Developers/Why-ThingworxInfoTableAdapter-cannot-be-cast-to-com-thingworx/td-p/523509

 

Finally, I did that in the extension:

InfoTable users = ((ThingworxInfoTableAdapter) args[0]).getInfoTable();

 

And it works fine.

 

Any comment?

I am a little confused about your method signature. I know signatures like

@ThingworxServiceDefinition(name = "Greeting", description = "Hello world")
@ThingworxServiceResult(name = "Result", description = "Result", baseType = "STRING")
public String Greeting(@ThingworxServiceParameter(name = "userToGreet", description = "The user you're greeting", baseType = "USERNAME") String 
userToGreet)throws Exception {
      return "Hello " + userToGreet + ". How are you?";
}

And with this syntax you just define baseType as INFOTABLE.

That service is from an example in the ThingWorx_Extension_Development_Guide  (I only found the guide for 8.3. Not sure where 9.x is - should be in helpcenter but had no luck) .

 

This is java extension. My example was a simple javascript service.

your example is okay. I am confused about the java extension code from the original question.

cbaurand
12-Amethyst
(To:nmutter)

It seems there are 2 ways to define the twx services in the extension:

1- with anotation

2- in the metadata file

 

Can someone confirm ?

You can define services in a Java Extension through annotations (the preferred way) or at runtime via addServiceDefinition(though less flexible).

I am not sure what you meant when saying it's possible to add services in metadata file - any example on how to do that?

Look at the beginning of the thread. You have the example!

What you are doing is called "Script Function Libraries". I found it in the "ThingWorx Extension Development Guide" I referenced to before.

I do not have experience with it and the docs do not show much documentation for this. Guess I cannot help futher

Announcements


Top Tags