Tuesday, January 22, 2008

How To Slow Cook A Piglet

This is how the pieces are glued together:
  1. Consider the original XML data:
    <Order>
    <Number>01234</Number>
    <Status>OPEN</Status>
    </Order>
  2. The XML data is translated into Perl data structure, utilizing XML::SAX(why?):
    $VAR1 = {
    'Order' => {
    'Status' => 'OPEN',
    'Number' => '01234'
    }
    };
    The unordered hash index will not mangle the report anymore since:

  3. Along the way (of parsing XML data using XML::SAX), a report template is created with references at the right places.

    StatusNotes
    <Order>
    <Number>01234</Number>${$REPORT[0]}{'RESULT'}${$REPORT[0]}{'NOTES'}
    <Status>OPEN</Status>${$REPORT[1]}{'RESULT'}${$REPORT[1]}{'NOTES'}
    </Order>

  4. To the bridge the result of comparison and the report template, another data structure is created (will be called: bridge) and it has an exact hash structure as the XML data, but it is holding references to the report template.
    $BRIDGE1 = {
    'Order' => {
    'Status' => '$REPORT[1]',
    'Number' => '$REPORT[0]'
    }
    };
  5. Consider the check data for the XML data above:
    <Order>
    <Number><SQL>'SELECT Status FROM Order WHERE ORDER_ID = <value>'</SQL></Number>
    <Status><SQL>'SELECT OrdNum FROM Order WHERE ORDER_ID = <value>'</SQL></Status>
    </Order>
    Readability is the main reason the check/validation is written in XML format.

  6. Converted to Perl data structure using XML::Simple
    $CHECK1 = {
    'Order' => {
    'Status' => { SQL => 'SELECT Status FROM Order WHERE ORDER_ID = <value>'},
    'Number' => { SQL => 'SELECT OrdNum FROM Order WHERE ORDER_ID = <value>'},
    }
    };
  7. Iterating through the XML hash structure, while maintaining same path of traversal with the bridge and check hash structure:
    $XML{'Order'} 
    -> $BRIDGE1{'Order'}
    -> $CHECK1{'Order'}

    $XML{'Order'}{'Status'}
    -> $BRIDGE1{'Order'}{'Status'}
    -> $CHECK1{'Order'}{'Status'}
  8. Find the end of XML hash structure branch to extract the value.
    if (ref($XML{'Order'}{'Status'}) eq "") {
    $XMLValue = $XML{'Order'}{'Status'};
    }
  9. Get the SQL syntax from check hash and get the content from database.
    if (exists $CHECK1{'Order'}{'Status'}{'SQL'}) {
    $SQL = $CHECK1{'Order'}{'Status'}{'SQL'};
    }
    else {
    # bitch later: no SQL defined.
    }
    $DBVALUE = getValue($SQL);
  10. Compare the content from XML hash structure and database and write the result out to $REPORT[$index], which provided by bridge hash.
    if ($XMLValue eq $DBVALUE) {
    eval "\${".$BRIDGE1{'Order'}{'Status'}."}{'RESULT'} = 'PASSED';";
    eval "\${".$BRIDGE1{'Order'}{'Status'}."}{'NOTES'} = '$NOTE';";
    }
    else {
    eval "\${".$BRIDGE1{'Order'}{'Status'}."}{'RESULT'} = 'FAILED';";
    eval "\${".$BRIDGE1{'Order'}{'Status'}."}{'NOTES'} = '$NOTE';";
    }
  11. After traversing all of the branches of XML hash structure, open the report template and evaluate template, producing:

    StatusNotes
    <Order>
    <Number>01234</Number>PASSED 
    <Status>OPEN</Status>PASSED 
    </Order>


No comments: