001 package edu.nrao.sss.model.project; 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.FailureSeverity; 008 import edu.nrao.sss.validation.Validation; 009 import edu.nrao.sss.validation.ValidationPurpose; 010 import edu.nrao.sss.validation.Validator; 011 012 /** 013 * A validator of {@link ProgramBlock program blocks}. 014 * <p> 015 * <u>Validations Performed for All Purposes</u> 016 * <ol> 017 * <li>Short name and/or long name was set.</li> 018 * <li>Program block has at least one scheduling block.</li> 019 * <li>Contained scheduling blocks 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 scheduling 023 * blocks</i>.</p> 024 * <p> 025 * <b>Version Info:</b> 026 * <table style="margin-left:2em"> 027 * <tr><td>$Revision: 397 $</td></tr> 028 * <tr><td>$Date: 2007-02-26 16:11:19 -0700 (Mon, 26 Feb 2007) $</td></tr> 029 * <tr><td>$Author: dharland $</td></tr> 030 * </table></p> 031 * 032 * @author David M. Harland 033 * @since 2007-02-09 034 */ 035 public class ProgramBlockValidator 036 extends AbstractValidator<ProgramBlock> 037 { 038 private Validator schedBlockValidator; 039 040 /** Creates a new instance. */ 041 public ProgramBlockValidator() 042 { 043 super(ProgramBlockValidator.class.getName(), ProgramBlock.class); 044 } 045 046 /* (non-Javadoc) 047 * @see AbstractValidator#makeValidationList(ValidationPurpose) 048 */ 049 @Override 050 protected List<Validation<ProgramBlock>> 051 makeValidationList(ValidationPurpose purpose) 052 { 053 List<Validation<ProgramBlock>> validations = 054 new ArrayList<Validation<ProgramBlock>>(); 055 056 validations.add(new HasName(this, purpose)); 057 validations.add(new HasSchedulingBlock(this, purpose)); 058 059 return validations; 060 } 061 062 /* (non-Javadoc) 063 * @see edu.nrao.sss.model.validation.AbstractValidator#validate() 064 */ 065 @Override 066 protected void validate() 067 { 068 super.validate(); 069 070 //Validate the program blocks 071 Validator sbVal = getSchedulingBlockValidator(); 072 073 for (SchedulingBlock sb : target.getSchedulingBlocks()) 074 failures.addAll(sbVal.validate(sb, purpose)); 075 } 076 077 //============================================================================ 078 // COMPONENT VALIDATORS 079 //============================================================================ 080 081 /** 082 * Sets the validator to use for the scheduling blocks of the target program 083 * block. It is acceptable to use a <i>null</i> parameter, in which case 084 * this validator will contact its manager for a scheduling block validator 085 * or create one itself. 086 * 087 * @param newValidator a validator to use on the scheduling blocks of this 088 * validator's target program block. 089 */ 090 public void setSchedulingBlockValidator(Validator newValidator) 091 { 092 schedBlockValidator = newValidator; 093 } 094 095 /** Returns a validator for program blocks. */ 096 public Validator getSchedulingBlockValidator() 097 { 098 Validator validator = schedBlockValidator; 099 100 if (validator == null) 101 { 102 if (manager != null) 103 validator = manager.getValidator(SchedulingBlock.class); 104 105 if (validator == null) 106 validator = new SchedulingBlockValidator(); 107 } 108 109 return validator; 110 } 111 112 //============================================================================ 113 // 114 //============================================================================ 115 116 /** Make sure one or both of shortName & longName have been entered. */ 117 class HasName extends HasNameValidation<ProgramBlock> 118 { 119 protected HasName(AbstractValidator<ProgramBlock> validationContainer, 120 ValidationPurpose reasonForValidation) 121 { 122 super(validationContainer, reasonForValidation, "program blocks"); 123 } 124 125 /** This test is passed if at least one name is not in default state. */ 126 @Override 127 protected boolean passesTest() 128 { 129 return !target.getName().equals(ProgramBlock.DEFAULT_NAME); 130 } 131 } 132 133 //============================================================================ 134 // ENSURE PROGRAM BLOCK HAS SCHEDULING BLOCKS 135 //============================================================================ 136 137 /** Make sure program block has SBs. */ 138 class HasSchedulingBlock extends Validation<ProgramBlock> 139 { 140 protected HasSchedulingBlock( 141 AbstractValidator<ProgramBlock> validationContainer, 142 ValidationPurpose reasonForValidation) 143 { 144 super(validationContainer, reasonForValidation); 145 146 severity = 147 (reasonForValidation == ValidationPurpose.CERTIFY_READY_TO_USE) ? 148 FailureSeverity.ERROR : FailureSeverity.WARNING; 149 } 150 151 /** This test is passed if PB has > 0 SBs. */ 152 @Override 153 protected boolean passesTest() 154 { 155 return target.getSchedulingBlocks().size() > 0; 156 } 157 158 /* (non-Javadoc) 159 * @see edu.nrao.sss.validation.Validation#displayMessage() 160 */ 161 @Override 162 protected String displayMessage() 163 { 164 StringBuilder buff = new StringBuilder("Program block '"); 165 166 buff.append(target.getName()) 167 .append("' has no scheduling blocks. "); 168 169 if (purpose == ValidationPurpose.CERTIFY_READY_TO_USE) 170 { 171 buff.append("A program block must have at least one scheduling block") 172 .append(" in order to be executed. Please add a scheduling block") 173 .append(" to this program block, or remove this program block from") 174 .append(" project '").append(target.getProject().getTitle()) 175 .append("'."); 176 } 177 else 178 { 179 buff.append("This is fine for now, but you will need to add at least") 180 .append(" one scheduling block before this program block is ready") 181 .append(" for execution."); 182 } 183 184 return buff.toString(); 185 } 186 187 /* (non-Javadoc) 188 * @see edu.nrao.sss.validation.Validation#debugMessage() 189 */ 190 @Override 191 protected String debugMessage() 192 { 193 StringBuilder buff = new StringBuilder("ProgBlock '"); 194 195 buff.append(target.getName()).append("' has no scheduling blocks."); 196 197 return buff.toString(); 198 } 199 } 200 }