4 Replies Latest reply on Apr 6, 2016 10:58 AM by asinclair-2 RSS
    asinclair-2 Apprentice

    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);

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

        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?
          asinclair-2 Apprentice

          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?
              asinclair-2 Apprentice

              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.