001    package edu.nrao.sss.model.project.scan;
002    
003    import java.util.List;
004    
005    import edu.nrao.sss.validation.AbstractValidator;
006    import edu.nrao.sss.validation.DataNotEnteredValidation;
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 FocusScan focus scans}.
013     * <p>
014     * <u>Validations Performed for All Purposes</u>
015     * <ol>
016     *   <li>All validations performed by {@link SimpleScanValidator}.</li>
017     *   <li>Scan has at least one offset.</li>
018     *   <li>Contained offsets are valid.<sup>1</sup></li>
019     * </ol>
020     * <sup>1</sup><i>The particular validations performed depend on the
021     * validator used by this one to operate on those contained offsets.
022     * </i></p>
023     * <p>
024     * <b>Version Info:</b>
025     * <table style="margin-left:2em">
026     *   <tr><td>$Revision: 397 $</td></tr>
027     *   <tr><td>$Date: 2007-02-26 16:11:19 -0700 (Mon, 26 Feb 2007) $</td></tr>
028     *   <tr><td>$Author: dharland $</td></tr>
029     * </table></p>
030     * 
031     * @author David M. Harland
032     * @since 2007-02-13
033     */
034    public class FocusScanValidator
035      extends ScanValidator<FocusScan>
036    {
037      private Validator focusOffsetValidator;
038      
039      /** Creates a new instance. */
040      public FocusScanValidator()
041      {
042        super(FocusScanValidator.class.getName(), FocusScan.class);
043      }
044      
045      /* (non-Javadoc)
046       * @see AbstractValidator#makeValidationList(ValidationPurpose)
047       */
048      @Override
049      protected List<Validation<FocusScan>>
050        makeValidationList(ValidationPurpose purpose)
051      {
052        List<Validation<FocusScan>> validations = super.makeValidationList(purpose);
053        
054        validations.add(new HasOffset(this, purpose));
055        
056        return validations; 
057      }
058    
059      /* (non-Javadoc)
060       * @see edu.nrao.sss.model.validation.AbstractValidator#validate()
061       */
062      @Override
063      protected void validate()
064      {
065        super.validate();
066    
067        //Validate the contained settings
068        Validator foVal = getFocusOffsetValidator();
069        
070        for (FocusOffset fo : target.getOffsets())
071          failures.addAll(foVal.validate(fo, purpose));
072      }
073      
074      //============================================================================
075      // COMPONENT VALIDATORS
076      //============================================================================
077    
078      /**
079       * Sets the validator to use for the focus offsets of the target scan.
080       * It is acceptable to use a <i>null</i> parameter, in which case
081       * this validator will contact its manager for a focus-offset validator
082       * or create one itself.
083       * 
084       * @param newValidator a validator to use on the focus offsets of this
085       *                     validator's target scan.
086       */
087      public void setFocusOffsetValidator(Validator newValidator)
088      {
089        focusOffsetValidator = newValidator;
090      }
091    
092      /** Returns a validator for focus offsets. */
093      public Validator getFocusOffsetValidator()
094      {
095        Validator validator = focusOffsetValidator;
096        
097        if (validator == null)
098        {
099          if (manager != null)
100            validator = manager.getValidator(FocusOffset.class);
101          
102          if (validator == null)
103            validator = new FocusOffsetValidator();
104        }
105        
106        return validator;
107      }
108      
109      //============================================================================
110      // UNITIALIZED FIELD VALIDATIONS
111      //============================================================================
112      
113      class HasOffset extends DataNotEnteredValidation<FocusScan>
114      {
115        protected HasOffset(AbstractValidator<FocusScan> validationContainer,
116                            ValidationPurpose            reasonForValidation)
117        {
118          super(validationContainer, reasonForValidation,
119                "focus scan", "focus offset", "0");
120        }
121        
122        @Override
123        protected String getCurrentValue()
124        {
125          specificTargetName = target.getName();
126    
127          return Integer.toString(target.getOffsets().size());
128        }
129      }
130    }