Skip to main content
1-Visitor
August 31, 2020
Solved

Creating Custom Services on top of a DataTable created in composer using the Eclipse SDK

  • August 31, 2020
  • 10 replies
  • 3451 views

Dear community,

 

I would like to perform the activity I have written in the subject, and by looking at the documentation it looks doable. However, I have some issues:

  1. I have created a DataShape and a DataTable in the composer, and exported them via the "Export" option.
  2. I have then imported them in Eclipse (Right click on Extension Project -> Import -> ThingWorx -> Entities and the .xml files are imported correctly in the project.

After this, I am not sure on how to proceed, since the import option does not cause the creation of the corresponding Java classes, or the entries in the metadata.xml file. By following tutorials/documentation, custom services should be created in the .java class of the related entity. Should I perform those manually, and then map them to the entities I created? 

I hope my question is clear, but feel free to ask for more details.

Federico

    Best answer by VladimirRosu_116627

    Hi Federico,

     

    I don't believe you necessarily need this part, since you will assign it anyway below.

    = new DataTableThing()

     Regarding your questions:

    1. Is there a way to override the http status of the response?

    -I haven't tried to do this, and typically at the SDK level I expect you would not be able to do that.

    2. Is there a suggested best practice to develop multiple service for the single entity, e.g., build many Services in the main Java class or having a Service per Java class?

    -typically you would want multiple services per ThingShape class, but you should follow architectural separation rules if your application uses more than one tier, and in that case you might need to separate the services in different classes that you attach to different Things maybe. The ThingShape is theoretically supposed to offer reusability, but also, like in your case, to offer opposite language capabilities to the entity you attach it to. For example, in the GitBackup Extension I have a Java ThingTemplate in which I do only the operations I can not do in Javascript. For those operations I have a JavaScript ThingShape that I attach to my Things.

    3. Is there a suggested way to interact between different DataTables, e.g., joining two tables?

    Yes, that is to use the 34 snippets dedicated to infotable interaction from the Service editor in Composer 😄

     

     

    10 replies

    19-Tanzanite
    August 31, 2020

    What is the use-case you're trying to achieve here? I would suggest adding normal ThingWorx Composer services directly at the level of the DataTable, but maybe you're aiming for something that can't be done via this method?

     

    The import process you used adds entities to a ThingWorx Extension Project with the aim to package them in extension zip file and nothing else.

    The reason to do this is because extensions in some cases must contain entities that do not make sense to be created via code because of flexibility and speed reasons and instead it just embeds them in the Entities folder that will be automatically imported once you load your extension.

    To be clear, such entities will never generate Java classes.

    To achieve your usecase, declare a ThingShape, write your own services in the Java, then add it to an existing DataTable.

    One additional option, but which I never tested and I'm not sure it will work, is to generate a Template that inherits the DataTable thing, and then manually change the class it extends - which should be Thing - to DataTableThing.

     

     

    Raccone1-VisitorAuthor
    1-Visitor
    August 31, 2020

    Hello Vladmir,

     

    I'd like to use the Eclipse SDK because the business logic behind the Service could become rather complex, and I have seen in the docs that Services written in JS using the composed cannot be debugged.

     

    If I understand your first option, I can write the Services in a ThingShape class, then deploy the extension to my instance and subsequently derive the DataTable thing from the ThingShape I have deployed?

     

    Thanks,

    Federico

     

    Edit: I wrongly put DataShape in lieu of ThingShape

    19-Tanzanite
    August 31, 2020

    Yes, that's almost correct because you can't derive the DataTable from a ThingShape, but instead you add a ThingShape to a DataTable. The ThingShape is almost like a Java abstract class.

     

    Gotcha regarding the usage of Eclipse SDK, but I would advise that keep in mind that when you code in Java you need to recompile, deploy + a platform restart that is required to reload the classes. With all the lack of debugger and the resulting workarounds (write a logger.warn will become your friend), it is still faster to change the JS code. Another point is of maintainability: if you code in JS, then you have immediate access to that code in Composer, which will help with future maintainability - I mean there's no chance to forget to send or loose the source code with this approach. I can also mention the hundreds of snippets available for consumption directly in the service editor.

     

    So my best advice here is really to time how effective you are in both approaches and then choose one that makes sense.