Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X
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?
Solved! Go to Solution.
@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");
}
}
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);
}}}
@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);