2 Replies Latest reply on Jun 3, 2016 7:12 PM by asinclair-2 RSS
    asinclair-2 Apprentice

    Find all ThingTemplates implementing a base ThingTemplate

    If an application has only a handful of ThingTemplates but hundreds of Things, what is the best way to find all of the ThingTemplates that are derived from a base ThingTemplate?

      • Re: Find all ThingTemplates implementing a base ThingTemplate
        pchung Collaborator

        There are services that give you the dependencies. To narrow down the search and not just start from GenericThing, I would either tag the base templates or hardcode the basetemplate if there is only one.

          • Re: Find all ThingTemplates implementing a base ThingTemplate
            asinclair-2 Apprentice

            Pai,

            I found that services like GetAllImplementingThings and the like do not filtering on EntityType (or maybe I just don't know how).

            In our case, to find out if another ThingTemplate has been derived from the base ThingTemple, we will have to weed through an ever growing number of Things to find that one ThingTemplate that has probably been there from the beginning.

            Here is what I came up with ... I turned the problem over and started by getting a list of ThingTemplate entities, which should be a smaller list and more stable over time than the "implementing things" list, and then loop through that list looking for ThingTemplates that are derived from the base. Also, in the same logic, if you know that Model Tags are used in a disciplined way, this knowledge can be used to quickly narrow the field to a handful of user created ThingTemplates.

            Is there a more direct way to get what I am after?

            -Andy

             

            // Returns all ThingTemplates marked with Tags that are derived from Base


            // Get all ThingTemplates that are marked with 'Tags'

            var Types = { items: ['ThingTemplate'] }

            var params = {

                            maxItems: undefined /* NUMBER */,

                            searchExpression: undefined /* STRING */,

                            types: Types /* JSON */,

                            aspects: undefined /* JSON */,

                            excludedAspects: undefined /* JSON */,

                            maxSearchItems: undefined /* NUMBER */,

                            tags: Tags /* TAGS */

            }

            // result: INFOTABLE dataShape: EntityDescriptor

            var allTemplates = Resources["SearchFunctions"].SearchModelEntities(params);

             

            var derivedTemplates = Resources["InfoTableFunctions"].Clone({ t1: allTemplates});

            derivedTemplates.RemoveAllRows();

             

            // Discard templates that are not derived from Base

            var baseAsParam = { thingTemplateName: Base /* THINGTEMPLATENAME */ }

            var tableLength = allTemplates.rows.length;

            for (var x = 0; x < tableLength; x++) {

                            var row = allTemplates.rows[x];

                            //Your code here

                if (ThingTemplates[row.name].IsDerivedFromTemplate(baseAsParam))

                    derivedTemplates.AddRow(row);

            }

             

            result = derivedTemplates;