Wednesday, August 6, 2014

How to compare two table buffers. Or how to understand which fields were updated.

Sometimes I need to understand which field values were updated. In most of the cases we can override the modifiedField() method on a table... But what if I don't want to check all (or almost all) table fields on this method? The best solution that I'm using is to compare the original table buffer with current. So, I'm going to share this approach...
I have create a new and simplest class named vpxCompareTableBuffers. The class will be initialized with 3 parameters origBuffer and currentBuffer type of Common and a container - skipFields with field IDs that should be skipped. The new method will be:

public void new(Common origBuffer, 
                Common currentBuffer, 
                Container skipFields)
{
    DictTable   orig;
    DictField   origDf;
    int         counter, fieldId;

    //Check if the table buffers are from the identical tables.
    if (origBuffer.TableId != currentBuffer.TableId)
    {
        throw error("Table buffers are for different tables");
    }

    orig = new DictTable(origBuffer.TableId);

    for (counter = 1; counter <= orig.fieldCnt(); counter++)
    {
        fieldId = orig.fieldCnt2Id(counter);
        origDf = new DictField(origBuffer.TableId, fieldId);
        
        //If it is not a system field and current field 
        //should not be skipped compare the value.
        if (!origDf.isSystem() && !conFind(skipFields, fieldId))
        {
            //If values differs then store the fieldId to a container.
            if (origBuffer.(fieldId) != currentBuffer.(fieldId))
            {
                modifiedFields += fieldId;
            }
        }
    }
}

No comments:

Post a Comment