Skip to main content
1-Visitor
November 18, 2016
Solved

AddPropertyDefinition in Extension SDK

  • November 18, 2016
  • 1 reply
  • 3497 views

hello,

can someone help out with this method?

Id like to add a simple Property to my ThingShape from my extension-created service

I'm using AddPropertyDefinition on my thingShape object like this:

thingShapeObject.AddPropertyDefinition(name,[...]);

but i keep getting the message

Unable to Invoke testService on testThing: Cannot cast java.lang.String to com.thingworx.thingshape.ThingShape

no matter what i do

Can anyone provide an working example?

Only need to set the Name parameter and type parameter to String. Should be totally enough, or do i need to set all parameters?

Best answer by ankigupta

@garambasic,

Try something like:

@ThingworxBaseTemplateDefinition(name = "GenericThing")

public class testThing extends Thing {

    private static Logger _logger = LogUtilities.getInstance().getApplicationLogger(testThing.class);

    public testThing() {

        // TODO Auto-generated constructor stub

    }

    @ThingworxServiceDefinition(name = "setProperties", description = "", category = "", isAllowOverride = false, aspects = {

            "isAsync:false" })

    @ThingworxServiceResult(name = "Result", description = "", baseType = "NOTHING", aspects = {})

    public void setProperties(

            @ThingworxServiceParameter(name = "thingshape", description = "thingshape to set", baseType = "THINGSHAPENAME") String thingshape) {

        _logger.trace("Entering Service: setProperties");

        ThingShape ThingShapeObject = (ThingShape) EntityUtilities.findEntity(thingshape, ThingworxRelationshipTypes.ThingShape);

        ThingShapeObject.AddPropertyDefinition(name, description, type, category, dataShape, readOnly, persistent, logged, dataChangeType, dataChangeThreshold, remote, remotePropertyName, timeout, pushType, pushThreshold, defaultValue);

        _logger.trace("Exiting Service: setProperties");

    }

}

1 reply

5-Regular Member
November 18, 2016

Can you provide the full line of code? What type of parameters are you passing into the method?

1-Visitor
November 19, 2016

yea, the code is below.

basically i'm creating an infotable and trying to set properties on the passed thingshape-parameter from the infotable-values.

my guess is that i got the parameters wrong on this one:

thingshape.AddPropertyDefinition(propName, null, "STRING", null, null, null, true, null, null, null, null, null, null, null, null, null);

complete Code:

public class testThing extends Thing {

  @ThingworxServiceDefinition(name = "setProperties", description = "set properties of thingshape")

  public void setProperties(

@ThingworxServiceParameter(name = "thingshape", description = "thingshape to set", baseType = "THINGSHAPENAME") ThingShape thingshape)

  throws Exception

  {

  DataShapeDefinition AV = new DataShapeDefinition();

  AV.addFieldDefinition(new FieldDefinition("Attribute", BaseTypes.STRING));

  AV.addFieldDefinition(new FieldDefinition("Value", BaseTypes.STRING));

  InfoTable infoTab = new InfoTable();

  infoTab.setDataShape(AV);

  ValueCollection valColl = new ValueCollection();

  valColl.put("Attribute", BaseTypes.ConvertToPrimitive("test", BaseTypes.STRING));

  valColl.put("Value", BaseTypes.ConvertToPrimitive("testValue", BaseTypes.STRING));

  infoTab.addRow(valColl);

  ValueCollectionList valCollList = infoTab.getRows();

 

  for (ValueCollection infoVal : valCollList) {

  String propName = infoVal.getStringValue("Attribute");

  thingshape.AddPropertyDefinition(propName, null, "STRING", null, null, null, true, null, null, null, null, null, null, null, null, null);

 

}}}

ankigupta5-Regular MemberAnswer
5-Regular Member
November 21, 2016

@garambasic,

Try something like:

@ThingworxBaseTemplateDefinition(name = "GenericThing")

public class testThing extends Thing {

    private static Logger _logger = LogUtilities.getInstance().getApplicationLogger(testThing.class);

    public testThing() {

        // TODO Auto-generated constructor stub

    }

    @ThingworxServiceDefinition(name = "setProperties", description = "", category = "", isAllowOverride = false, aspects = {

            "isAsync:false" })

    @ThingworxServiceResult(name = "Result", description = "", baseType = "NOTHING", aspects = {})

    public void setProperties(

            @ThingworxServiceParameter(name = "thingshape", description = "thingshape to set", baseType = "THINGSHAPENAME") String thingshape) {

        _logger.trace("Entering Service: setProperties");

        ThingShape ThingShapeObject = (ThingShape) EntityUtilities.findEntity(thingshape, ThingworxRelationshipTypes.ThingShape);

        ThingShapeObject.AddPropertyDefinition(name, description, type, category, dataShape, readOnly, persistent, logged, dataChangeType, dataChangeThreshold, remote, remotePropertyName, timeout, pushType, pushThreshold, defaultValue);

        _logger.trace("Exiting Service: setProperties");

    }

}