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 SwitchingScan switching 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 time on target.</li>
018     *   <li>Scan has at least one setting.</li>
019     *   <li>Contained settings are valid.<sup>1</sup></li>
020     * </ol>
021     * <sup>1</sup><i>The particular validations performed depend on the
022     * validator used by this one to operate on those contained settings.
023     * <p>
024     * <b>Version Info:</b>
025     * <table style="margin-left:2em">
026     *   <tr><td>$Revision: 1256 $</td></tr>
027     *   <tr><td>$Date: 2008-04-29 15:55:50 -0600 (Tue, 29 Apr 2008) $</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 SwitchingScanValidator
035      extends ScanValidator<SwitchingScan>
036    {
037      private Validator switchSettingValidator;
038      
039      /** Creates a new instance. */
040      public SwitchingScanValidator()
041      {
042        super(SwitchingScanValidator.class.getName(), SwitchingScan.class);
043      }
044      
045      /* (non-Javadoc)
046       * @see AbstractValidator#makeValidationList(ValidationPurpose)
047       */
048      @Override
049      protected List<Validation<SwitchingScan>>
050        makeValidationList(ValidationPurpose purpose)
051      {
052        List<Validation<SwitchingScan>> validations =
053          super.makeValidationList(purpose);
054        
055        validations.add(new HasTime   (this, purpose));
056        validations.add(new HasSetting(this, purpose));
057        
058        return validations; 
059      }
060    
061      /* (non-Javadoc)
062       * @see edu.nrao.sss.model.validation.AbstractValidator#validate()
063       */
064      @Override
065      protected void validate()
066      {
067        super.validate();
068    
069        //Validate the contained settings
070        Validator ssVal = getSwitchSettingValidator();
071        
072        for (SwitchSetting ss : target.getSwitchSettings())
073          failures.addAll(ssVal.validate(ss, purpose));
074      }
075      
076      //============================================================================
077      // COMPONENT VALIDATORS
078      //============================================================================
079    
080      /**
081       * Sets the validator to use for the switch settings of the target scan.
082       * It is acceptable to use a <i>null</i> parameter, in which case
083       * this validator will contact its manager for a switch-setting validator
084       * or create one itself.
085       * 
086       * @param newValidator a validator to use on the switch settings of this
087       *                     validator's target scan.
088       */
089      public void setSwitchSettingValidator(Validator newValidator)
090      {
091        switchSettingValidator = newValidator;
092      }
093    
094      /** Returns a validator for switch settings. */
095      public Validator getSwitchSettingValidator()
096      {
097        Validator validator = switchSettingValidator;
098        
099        if (validator == null)
100        {
101          if (manager != null)
102            validator = manager.getValidator(SwitchSetting.class);
103          
104          if (validator == null)
105            validator = new SwitchSettingValidator();
106        }
107        
108        return validator;
109      }
110      
111      //============================================================================
112      // UNITIALIZED FIELD VALIDATIONS
113      //============================================================================
114      
115      class HasSetting extends DataNotEnteredValidation<SwitchingScan>
116      {
117        protected HasSetting(AbstractValidator<SwitchingScan> validationContainer,
118                             ValidationPurpose                reasonForValidation)
119        {
120          super(validationContainer, reasonForValidation,
121                "switching scan", "switch setting", "0");
122        }
123        
124        @Override
125        protected String getCurrentValue()
126        {
127          specificTargetName = target.getName();
128    
129          return Integer.toString(target.getSwitchSettings().size());
130        }
131      }
132    
133      class HasTime extends DataNotEnteredValidation<SwitchingScan>
134      {
135        protected HasTime(AbstractValidator<SwitchingScan> validationContainer,
136                          ValidationPurpose               reasonForValidation)
137        {
138          super(validationContainer, reasonForValidation,
139                "switching scan", "time on target", "0.0");
140        }
141        
142        @Override
143        protected String getCurrentValue()
144        {
145          return target.getTimeOnTarget().getValue().toString();
146        }
147      }
148    }