HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Implementing Fowler's Analysis Validator
 Pattern in Java"
Vol. 5, Issue 9, p. 52

	

Listing 1 

//Observation Class with a few pertinent methods 
public class Observation { 
   private java.util.Date recordedDate; 
   private java.util.Date observedDate; 
   private int duration; 
   private ObservationType type; 
   private String observationTypeName; 
   public boolean isValid (String obsValue) { 
    return getType().isValid( obsValue ); 
   } end isValid 
} // end Observation Class 

// Observation Type with pertinent methods 
public class ObservationType { 
   private String phenomenon; 
   private Validator dataElementValidator = null; 
   private String phenomenonType; 
   public ObservationType(String ddElementName) { 
     DataDictionaryRecord ddrecord = DDManager.getMember(dElementName ); 
     setPhenomenon( ddElementName ); 
     setPhenomenonType( ddrecord.getDataElementType()); 
     try { 
    // This uses the validator name stored in the data dictionary to    // 
build an instance of the needed validator (either a Range 
    // or Discrete Validator) using the Reflection pattern. 
    Class validatorClass = 
      Class.forName("fowlerspatterns."+ddrecord.getValidatorName() ); 
    setDataElementValidator ( (Validator) 
    validatorClass.newInstance()); 
    getDataElementValidator().setDDRecord( ddrecord); 
     } catch(Exception e) { e.printStackTrace(); } 
   } // end ObservationType constructor 
   public boolean isValid(String obsValue) { 
        return getDataElementValidator().isValid(obsValue ); 
   } // end isValid 
} // end Observation Type 

// Observation Panel 

public class ObservationPanel extends java.awt.Panel 
implements java.awt.event.ActionListener { 
    protected transient 
        java.beans.PropertyChangeSupport propertyChange; 
    private String fieldDataDictionaryElementName = new String(); 
    private java.awt.Label ivjObservationLabel = null; 
    private java.awt.TextField 
ivjObservationTextField = null; 
    private Observation dataElementObservation = null; 
    private java.awt.Label ivjErrorLabel = null; 
    private boolean observationValid = true; 
    private String panelObservation; 
    private String panelObservationText; 
    private String errorText; 

    public ObservationPanel() { 
    super();    initialize(); 
    } // end ObservationPanel constructor 

    public static void main(java.lang.String[] args) { 
    try {   java.awt.Frame frame; 
        try { 
           Class aFrameClass = 
            Class.forName("com.ibm.uvm.abt.edit.TestFrame"); 
          frame = (java.awt.Frame)aFrameClass.newInstance(); 
        } catch (java.lang.Throwable ivjExc) 
            {frame = new java.awt.Frame();} 
        ObservationPanel aObservationPanel; 
        aObservationPanel = new ObservationPanel(); 
        frame.add("Center", aObservationPanel); 
        frame.setSize(aObservationPanel.getSize()); 
        frame.setVisible(true); 
    } catch (Throwable exception) { 
        System.err.println("Exception occurred in main Panel"); 
        exception.printStackTrace(System.out); 
    } 
    }// end main 

    public void actionPerformed(java.awt.event.ActionEvent e) { 
    if ((e.getSource() == getObservationTextField()) ) { 
        validateThisObservation(e); 
    } 
    } // end actionPerformed 

    private void validateThisObservation 
        (java.awt.event.ActionEvent arg1) { 
    try {this.validateObservation();} 
    catch (java.lang.Throwable ivjExc) {handleException(ivjExc);} 
    } // end validateThisObservation 

    private java.awt.Label getErrorLabel() { 
    if (ivjErrorLabel == null) { 
        try { 
            ivjErrorLabel = new java.awt.Label(); 
            ivjErrorLabel.setName("ErrorLabel"); 
            ivjErrorLabel.setText(""); 
            ivjErrorLabel.setBounds(45, 61, 269, 23); 
        } catch (java.lang.Throwable ivjExc) 
            {handleException(ivjExc);} 
    }; // end if 
    return ivjErrorLabel; 
    } // end getErrorLabel 

    private java.awt.Label getObservationLabel() { 
    if (ivjObservationLabel == null) { 
        try { 
            ivjObservationLabel = new java.awt.Label(); 
            ivjObservationLabel.setName("ObservationLabel"); 
            ivjObservationLabel.setText("                     "); 
        ivjObservationLabel.setBackground(java.awt.Color.cyan); 
            ivjObservationLabel.setBounds(36, 29, 66, 23); 
        } catch (java.lang.Throwable ivjExc) 
                  {handleException(ivjExc);} 
    }; // end if 
    return ivjObservationLabel; 
    } // end getObservationLabel 

    private java.awt.TextField getObservationTextField() { 
    if (ivjObservationTextField == null) { 
        try { 
            ivjObservationTextField = new java.awt.TextField(); 
              ivjObservationTextField.setName 
                ("ObservationTextField"); 
            ivjObservationTextField.setBounds(108, 29, 188, 23); 
        } catch (java.lang.Throwable ivjExc) 
                {handleException(ivjExc);) 
    }; // end if 
    return ivjObservationTextField; 
    }// end getObservationTextField 

    protected java.beans.PropertyChangeSupport getPropertyChange() { 
    if (propertyChange == null) { 
        propertyChange = new 
            java.beans.PropertyChangeSupport(this); 
    }; // end if 
    return propertyChange; 
    } // end PropertyChangeSupport 

    private void initialize() { 
    setName("ObservationPanel"); setLayout(null); setSize(375, 88); 
    add(getObservationTextField(), 
getObservationTextField().getName()); 
    add(getObservationLabel(), getObservationLabel().getName()); 
    add(getErrorLabel(), getErrorLabel().getName()); 
    initConnections(); 
    DataDictionaryRecord ddrecord = 
        DDManager.getMember(getDataDictionaryElementName()); 
    getObservationLabel().setText( ddrecord.getLabelName() ); 
    setErrorText( ddrecord.getInvalidObservationLabel() ); 
    // The label size, textfield size, and panel size should 
    // be adjusted using the length of the label from the 
    // data dictionary. 
        setDataElementObservation( new Observation( 
getDataDictionaryElementName() ) ); 
    } // end initialize 

    public void setDataDictionaryElementName 
        (String dataDictionaryElementName) { 
    String oldValue = fieldDataDictionaryElementName; 
    fieldDataDictionaryElementName = dataDictionaryElementName; 
    firePropertyChange 
        ("dataDictionaryElementName", oldValue, 
        dataDictionaryElementName); 
    } // end setDataDictionaryElementName 

    public void validateObservation() { 
    /* Perform the validateObservation method. */ 
    getErrorLabel().setText(""); 
    setObservationValid(true); 
    setPanelObservationText( getObservationTextField().getText() ); 
    setObservationValid 
  (getDataElementObservation().isValid 
    (getPanelObservationText())); 
if( !getObservationValid() ) { 
    getErrorLabel().setText(getErrorText()); 
    } // end if 
    } // end validateObservation 
}end Observation Panel class 

// DataDictionary Record class an item in DD Vector 
public class DataDictionaryRecord { 
    private String dataElementName; 
    private Validator dataElementValidator; 
    private String min; 
    private String max; 
    private String tableName; 
    private java.util.Vector tableVector; 
    private String validatorName; 
    private int length; 
    private String labelName = new String(); 
    private String dataElementType; 
    private String invalidObservationLabel; 
} // end DDRecord class 
  

Listing 2 

// validator class 

public class Validator { 
    protected DataDictionaryRecord dDRecord; 
    public boolean isValid(String obs ) {   return false;} // end isValid 
} // end validator class 

// RangeValidator Class 
public class RangeValidator extends Validator { 
    public boolean isValid(String obs ) { 
    int obsInt; 
    if(getDDRecord().getDataElementType().equals("integer")) { 
       // validate that obs is an integer 
       // . 
       try {obsInt = Integer.parseInt( obs );} 
       catch(NumberFormatException e) {return false;} 
       // validate that obs meets the rules of min and max 
       int maxInt = Integer.parseInt(getDDRecord().getMax()); 
       int minInt = Integer.parseInt(getDDRecord().getMin()); 
       return (((minInt <= obsInt )&&(maxInt>=obsInt))); 
    } // end if 
    return false; 
    } // end isValid 
} // end rangevalidator 

  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.