Tuesday, April 23, 2024

Iterate through extended child classes dynamically using X++

/// <summary>
/// Base class for search
/// </summary>
class searchBaseClass
{
    const private Integer priorityMax = 99;

    /// <summary>
    /// Method that searches for values
    /// </summary>
   
    protected void search(//your parmeters)
    {
    }

    /// <summary>
    /// Method that indicates the class priority and therefor the number in which it is executed
    /// </summary>
    /// <returns>
    /// An integer indicating the class priority and therefor the number in which it is executed
    /// </returns>
    protected Priority priority()
    {
        return searchBaseClass::PriorityMax;
    }

    /// <summary>
    /// Method that creates a map of search classes to be executed in a priorized way
    /// </summary>
    /// <returns>
    /// A map containing class objects to execute
    /// </returns>
    private Map createExecutionMap()
    {
        DictClass                       dictClass;
        searchBaseClass    		searchBase;
        ListEnumerator                  listEnumerator;
        List                            listClass = new DictClass(classnum(searchBaseClass)).extendedBy();
        Map                             mapExecutionClasses = new Map(Types::Integer, Types::Class);

        listEnumerator = listClass.getEnumerator();
        while (listEnumerator.moveNext())
        {
            dictClass = new DictClass(listEnumerator.current());
            if (dictClass)
            {
                searchBase = dictClass.makeObject();
                if (searchBase)
                {
                    // Add class object to execution list unless the priority is already added
                    if (!mapExecutionClasses.exists(searchBase.priority()))
                    {
                        mapExecutionClasses.insert(searchBase.priority(), searchBase);
                    }
                    else
                    {
                        warning(strFmt("SearchSkipped", dictClass.name(), searchBase.priority()));
                    }
                }
            }
        }

        return mapExecutionClasses;
    }

    /// <summary>
    /// Method that run through all classes that searches for data
    /// </summary>
    public void run()
    {
        searchBaseClass  		  	  searchBase;
        Map                           mapExecutionClasses;

        mapExecutionClasses = this.createExecutionMap();

        for (int i = 1; i <= searchBase::priorityMax; i++)
        {
            if (mapExecutionClasses.exists(i))
            {
                searchBase = mapExecutionClasses.lookup(i);
                if (searchBase)
                {
                    searchBase.search();
                }
            }
        }
    }

}


/// <summary>
/// child Class searching 
/// </summary>
class searchChildClass extends searchBaseClass
{
    /// <summary>
    /// Method that indicates the class priority and therefor the number in which it is executed
    /// </summary>
    /// <returns>
    /// An integer indicating the class priority and therefor the number in which it is executed
    /// </returns>
    protected Priority priority()
    {
        return 1;
    }

    /// <summary>
    /// Method searching
    /// </summary>
  
    protected void search(//your parmeters)
    {
        //your logic
    }

No comments:

Post a Comment

Table browser URL in D365FO

Critical Thinking icon icon by Icons8