001    package edu.nrao.sss.model.source;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    import edu.nrao.sss.validation.AbstractValidator;
007    import edu.nrao.sss.validation.Validation;
008    import edu.nrao.sss.validation.ValidationPurpose;
009    import edu.nrao.sss.validation.Validator;
010    
011    /**
012     * A validator of {@link Source sources}.
013     * <p>
014     * <u>Validations Performed for All Purposes</u>
015     * <ol>
016     *   <li>Validation of contained subsources.<sup>1</sup></li>
017     * </ol>
018     * <sup>1</sup><i>The particular validations performed depend on the
019     * validator used by this one to operate on those contained subsources</i>.</p>
020     * <p>
021     * <b>Version Info:</b>
022     * <table style="margin-left:2em">
023     *   <tr><td>$Revision: 332 $</td></tr>
024     *   <tr><td>$Date: 2007-02-08 16:49:14 -0700 (Thu, 08 Feb 2007) $</td></tr>
025     *   <tr><td>$Author: dharland $</td></tr>
026     * </table></p>
027     * 
028     * @author David M. Harland
029     * @since 2007-02-05
030     */
031    public class SourceValidator
032      extends AbstractValidator<Source>
033    {
034      private Validator subsourceValidator;
035      
036      /** Creates a new instance. */
037      public SourceValidator()
038      {
039        super(SourceValidator.class.getName(), Source.class);
040      }
041    
042      /* (non-Javadoc)
043       * @see AbstractValidator#makeValidationList(ValidationPurpose)
044       */
045      @Override
046      protected List<Validation<Source>>
047        makeValidationList(ValidationPurpose purpose)
048      {
049        return new ArrayList<Validation<Source>>();
050      }
051    
052      /* (non-Javadoc)
053       * @see edu.nrao.sss.model.validation.AbstractValidator#validate()
054       */
055      @Override
056      protected void validate()
057      {
058        super.validate();
059        
060        //Validate the contained subsources
061        Validator subsourceVal = getSubsourceValidator();
062        
063        for (Subsource ss : target.getSubsources())
064          failures.addAll(subsourceVal.validate(ss, purpose));
065      }
066      
067      //============================================================================
068      // COMPONENT VALIDATORS
069      //============================================================================
070    
071      /**
072       * Sets the validator to use for the subsources of the target source.
073       * It is acceptable to use a <i>null</i> parameter, in which case
074       * this validator will contact its manager for a subsource validator
075       * or create one itself.
076       * 
077       * @param newValidator a validator to use on the subsources of this
078       *                     validator's target source.
079       */
080      public void setSubsourceValidator(Validator newValidator)
081      {
082        subsourceValidator = newValidator;
083      }
084    
085      /** Returns a validator for sources. */
086      public Validator getSubsourceValidator()
087      {
088        Validator validator = subsourceValidator;
089        
090        if (validator == null)
091        {
092          if (manager != null)
093            validator = manager.getValidator(Subsource.class);
094        
095          if (validator == null)
096            validator = new SubsourceValidator();
097        }
098        
099        return validator;
100      }
101      
102      //============================================================================
103      //
104      //============================================================================
105      /*
106      public static void main(String[] args)
107      {
108        SourceBuilder builder = new SourceBuilder();
109        
110        Source source = builder.makeSource("Deep Throat");
111        Subsource ss  = new Subsource("Xyz");
112        ss.resetPosition();
113        ss.removeAllPolynomialPositions();
114        source.addSubsource(ss);
115        
116        ValidationManager validator = new ValidationManager();
117        
118        if (validator.validate(source, ValidationPurpose.CERTIFY_READY_TO_USE))
119          System.out.println("Source is valid.");
120        else
121        {
122          System.out.println("Source NOT valid.");
123          System.out.println();
124          
125          for (ValidationFailure failure : validator.getFailures())
126            System.out.println("Display Msg: " + failure.getDisplayMessage());
127          
128          System.out.println();
129          
130          for (ValidationFailure failure : validator.getNonDisplayableFailures())
131            System.out.println("Debug Msg: " + failure.getDebugMessage());
132        }
133      }
134      */
135    }