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

How do I set up to search things by shape in a Java Extension?

SOLVED
Level 1

How do I set up to search things by shape in a Java Extension?

I have the following implemented in javascript and need to convert it to Java.

I'm not finding any samples that help me with this.

var powerSourceThingShapeName = "CP_RestPowerSourceThingShape";

var query = {"filters": {"type":"EQ","fieldName":"DeviceId","value": DeviceId }};

var params = {

  thingShape: powerSourceThingShapeName,

  maxItems: 2,

  nameMask: undefined /* STRING */,

  query: query,

  tags: undefined /* TAGS */

};

// result: INFOTABLE dataShape: RootEntityList

// Throws an exception if not found or if too many are found

var things = Resources["SearchFunctions"].SearchThingsByShape(params);

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How do I set up to search things by shape in a Java Extension?

An alternate method was found by Joe Daniel that works as expected.

[Note that CP_ThingTemplate includes CP_ThingShape].


ThingTemplate template = (ThingTemplate)EntityUtilities.findEntity("CP_ThingTemplate", ThingworxRelationshipTypes.ThingTemplate);

InfoTable newThings = template.QueryImplementingThingsWithData(null, null, null, query);

In our application, the InfoTable newThings always has 1 entry, which is the thing that has the matching DeviceId.

Note that the query parameter used here is the same JSONObject that was used with the SearchThingsByShape.


Case 12996594 asserts that the SearchThingsByShape library call does not handle the query parameter correctly.

4 REPLIES 4

Re: How do I set up to search things by shape in a Java Extension?

Take a look at the ThingShapeUtilities class in the SDK.  There are a couple implementations of getImplementingThings()  that take a ThingShape or EntityReference as an argument and will return an EntityReferenceCollection which you can call toInfoTable() if you need it in that form. It doesn't take advantage of the query logic, but can get you a list of Things that implement that ThingShape.

Re: How do I set up to search things by shape in a Java Extension?

Thearon,

Thanks and good suggestion. This made me think that the SearchThingsByShape call in the javascript snippet must be in the Java library somewhere, and I found it in com.thingworx.resources.queries in the Searcher class. If I can set up the params properly this should give me just what I need.

Andy

Re: How do I set up to search things by shape in a Java Extension?

I was able to get the SetThingsByShape() call to work in Java but the query parameter is not being applied. I have tried a number of ways of constructing the JSONObject query parameter. Here is an example of the Java code ...

JSONObject filter = new JSONObject();

filter.put("type", "EQ");

filter.put("fieldName", "DeviceId");

filter.put("value", DeviceId);

JSONObject query = new JSONObject();

query.put("filters", filter);

Searcher searcher = new Searcher();

InfoTable things = searcher.SearchThingsByShape("CP_ThingShape", (double)100, null, null, query);

In this application there will always be exactly one Thing that is derived from CP_ThingShape that has the specified DeviceId.

So the InfoTable should always have exactly one row, but instead I get as many rows as there are Things derived from CP_ThingShape.

Is the query parameter not formed correctly?

Re: How do I set up to search things by shape in a Java Extension?

An alternate method was found by Joe Daniel that works as expected.

[Note that CP_ThingTemplate includes CP_ThingShape].


ThingTemplate template = (ThingTemplate)EntityUtilities.findEntity("CP_ThingTemplate", ThingworxRelationshipTypes.ThingTemplate);

InfoTable newThings = template.QueryImplementingThingsWithData(null, null, null, query);

In our application, the InfoTable newThings always has 1 entry, which is the thing that has the matching DeviceId.

Note that the query parameter used here is the same JSONObject that was used with the SearchThingsByShape.


Case 12996594 asserts that the SearchThingsByShape library call does not handle the query parameter correctly.