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

Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X

AddPropertyDefinition in Extension SDK

garambasic
1-Newbie

AddPropertyDefinition in Extension SDK

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?

1 ACCEPTED SOLUTION

Accepted Solutions
ankigupta
5-Regular Member
(To:garambasic)

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

    }

}

View solution in original post

4 REPLIES 4

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

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

 

}}}

ankigupta
5-Regular Member
(To:garambasic)

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

    }

}

hey Ankit,

thank you so much! It works now.

For future reference:

you have to provide some of the parameters though. not enough to set everything you dont need to null. this worked for me:

ThingShapeObject.AddPropertyDefinition("name", "", "STRING", "", null, false, true, true, "VALUE", null, false, null, null, "", null, null);



Top Tags