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

Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X

Create InfoTable using Java Extension SDK

bwinslow24
12-Amethyst

Create InfoTable using Java Extension SDK

Hi,

 

I am having troubles creating Infotable objects using the Thingworx Extension SDK. I have tried multiple methods and no seem to work.

 

Using From JSON

I tried to use the fromJSON method but i didnt work. 

JSONObject jsonObj = new JSONObject("{\"dataShape\":{\"fieldDefinitions\":{\"id\":{\"name\":\"id\",\"description\":\"id\",\"baseType\":\"STRING\",\"ordinal\":0,\"alias\":\"_0\",\"aspects\":{}}}},\"rows\":[{\"_0\":\"178498585\",\"_1\":1531849323285,\"_2\":true},{\"_0\":\"178496396\",\"_1\":1531849187726,\"_2\":false},{\"_0\":\"178482871\",\"_1\":1531848400119,\"_2\":true}]}");
InfoTable it = InfoTable.fromJSON(jsonObj);
   

 

Building From Scratch

I have tried to build the InfoTable from scratch but no methods work.

 

1) Create Field Definition and using addField

InfoTable myIF = new InfoTable();
FieldDefinition Field1 = new FieldDefinition("Field1", BaseTypes.INTEGER);
myIF.addField(Field1);

2) Create DataShapeDefinition -> Add field to that definition -> use infotable constructor with the data shape definition as input. I have also tried to use BaseTypes.String.

DataShapeDefinition dsd = new DataShapeDefinition();
dsd.addFieldDefinition(new FieldDefinition("MockedField", "STRING"));

//also tried
dsd.addFieldDefinition(new FieldDefinition("MockedField", BaseTypes.STRING));

InfoTable it = new InfoTable(dsd);

What is the correct way to create the InfoTable Object?

 

Thanks!

2 REPLIES 2
supandey
19-Tanzanite
(To:bwinslow24)

Hi @bwinslow24 you'll need to invoke the InfoTableFunctions class to create an Infotable, I've created a sample ThingTemplate below which contains a Property called "InfotableAsProperty" and a service called "CreateInfoTable" which also creates an infotable, and finally a datashape which is used in both Infotables

 

@ThingworxBaseTemplateDefinition(name = "GenericThing")
@ThingworxPropertyDefinitions(properties = {
		@ThingworxPropertyDefinition(name = "InfotableAsProperty", description = "", category = "", baseType = "INFOTABLE", isLocalOnly = false, aspects = {
				"isPersistent:true", "dataChangeType:VALUE", "isEntityDataShape:true", "dataShape:CustomDataShape" }) })
public class CustomInfoTableExtension extends Thing {

	private static Logger _logger = LogUtilities.getInstance().getApplicationLogger(CustomInfoTableExtension.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 
	 */
	public CustomInfoTableExtension() {
		// TODO Auto-generated constructor stub
	}

	/*
	 * CreateInfoTable service creates an infotable.
	 */
	@ThingworxServiceDefinition(name = "CreateInfoTable", description = "Creates an infotable", category = "", isAllowOverride = false, aspects = {
			"isAsync:false" })
	@ThingworxServiceResult(name = "Result", description = "", baseType = "NOTHING", aspects = {})
	public void CreateInfoTable() throws Exception {
		_logger.trace("Entering Service: CreateInfoTable");
		InfoTable myIF = new InfoTable();

		FieldDefinition Field1 = new FieldDefinition("Field1", BaseTypes.INTEGER);
		FieldDefinition Field2 = new FieldDefinition("Field2", BaseTypes.STRING);

		myIF.addField(Field1);
		myIF.addField(Field2);
		TagCollection mytag = null;
		EntityServices es = new EntityServices();

		try {
			es.CreateDataShape("CustomDataShape", "Created from the extension sdk", mytag, myIF);
		} catch (Exception e) {
			_logger.error("Failed to create datashape" + es.getName());
			e.printStackTrace();
		}

		InfoTableFunctions myImf = new InfoTableFunctions();

		try {
			myImf.CreateInfoTableFromDataShape("Infotable1", "CustomDataShape"); // creates infotable from a datashape.
		} catch (Exception e) {
			_logger.error("Failed to create Infotable" + myImf.getName());
			e.printStackTrace();
		}

		_logger.trace("Exiting Service: CreateInfoTable");
	}

}

Hi @supandey,

 

Thanks for the response, but its not exactly what I'm looking for. I should have explained a little better in my question. I am trying to mock the output of QueryBooleanPropertyHistory so I can test parts of my code with JUNIT. The output is an InfoTable. I want to build an InfoTable object and use that in my tests. I can instantiate the InfoTable object but when I try to add any fields or rows it doesn't work.

 

Thanks,

Brandon

Announcements


Top Tags