Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Building a Java servlet framework using reflection, Part 2

Gain greater functionality with less code using these reflective code samples

  • Print
  • Feedback

Page 4 of 7

Here's a code sample from the isValid method, illustrating how the validation might be performed:

        // Load the rules table from the business rules object
        // this method may be moved to a utility class later!
        hRulesTable = loadBusinessRules(sActionName);
        // Retrieve the keys from the businessData hashtable passed 
            // into the isValid method.
        Enumeration enumKeys = hBusinessData.keys();
        // Initialize the rule object
        Rule rule = null;
        // While there are still keys remaining in the business data
        while (enumKeys.hasMoreElements() ) {
            // Store the key name
            String fieldName = (String) enumKeys.nextElement();
            // Store the user-entered value
            String userValue = (String) hBusinessData.get( fieldName);
            // Retrieve the rule from the rules table
            Rule rule = (Rule) hRulesTable.get(fieldName);
            // the field is in the mandatory hash table
            if ( rule != null) {    
                  // the field is mandatory, either because it is required
                // retrieve rules
                String fieldType = rule.getValidation();
                boolean bFieldIsRequired = rule.isMandatory();
                        // trim the extra spaces from the field
                userValue = userValue.trim();
                // check if the field is blank
                boolean fieldNotBlank = (userValue.length() > 0);
                if ( fieldNotBlank) {
                    // if not blank, perform field-level validation
                    isFieldValid = 
                                validateField( fieldName, fieldType, userValue);
                } else if ( fieldIsRequired)    {
                    // if the field is blank, it may or may 
                              // not be valid, depending on
                    // whether or not it is required.
                    // If the field is required, it is not valid
                    // If the field is not required, it is valid
                    isFieldValid = false;
                }
                // If the field is not valid, a missing tag will appear
                if ( !isFieldValid) {
                    setError( fieldName, businessData);
                }
            } else { // the field is not contained in the hashtable
                // If this is not a field we care about
                if ( ignoreField( fieldName) )
                {
                    // just ignore here
                } else {
                    // the value is missing from the table
                    // there's a BIG problem!!!
                    throw new DataProcessingException( 
                                 "missing key value for key " 
                                 + fieldName 
                                 + " in rules table" );
                }
            }
            // if there's an error, add the error to the businessData
            // The return value is the previous value AND the current
                  // field-level value
            returnValue = returnValue && isFieldValid;
            // reset the field-level validation for the next iteration
            isFieldValid = true;
        }  
        return returnValue;
    }


That's a lot of code, but it is completely independent of the individual forms it validates. Let's step through it quickly. Initially, the business rules corresponding to the form are loaded using the previously described loadBusinessRules method. Having loaded the Hashtable containing the rules, you retrieve the keys corresponding to the business data that the form supplies. You'll loop over all the keys in the Enumeration and retrieve the name-value pairs for validation. Once the name is retrieved, you can use it to find the corresponding rule in the Hashtable. If you find the rule in the Hashtable, you can determine whether it is blank, or required. If the field is not blank, the validateField method is called to perform validation.

  • Print
  • Feedback

Resources