001 /*----------------------------------------------------------------------- 002 * Copyright (C) 2006 003 * Associated Universities, Inc. Washington DC, USA. 004 * This program is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU General Public License as 006 * published by the Free Software Foundation; either version 2 of 007 * the License, or (at your option) any later version. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU General Public License for more details. 013 * 014 * Correspondence concerning this software should be addressed as follows: 015 * Internet email: switz@nrao.edu 016 * Postal address: SSS Software 017 * National Radio Astronomy Observatory 018 * Post Office Box 0 019 * Socorro, NM 87801 USA 020 *-----------------------------------------------------------------------*/ 021 package edu.nrao.sss.model.parameter; 022 023 import edu.nrao.sss.validation.AbstractValidator; 024 import edu.nrao.sss.validation.FailureSeverity; 025 import edu.nrao.sss.validation.Validation; 026 import edu.nrao.sss.validation.ValidationPurpose; 027 028 public class ParameterValidation extends Validation<Parameter> { 029 030 protected enum ErrorType { 031 BADDECINTVALUE("illegal value", "illegal value is set, expected a decimal integer"), 032 BADFLOATVALUE("illegal value", "illegal value is set, expected a floating point number"), 033 BADOCTINTVALUE("illegal value", "illegal value is set, expected an octal integer"), 034 BADPADNAMEVALUE("illegal value", "illegal value is set, expected a pad name"), 035 EMPTYEFFECTIVEDATE("empty effective date", "the effective date is empty"), 036 EMPTYENTITY("empty entity", "the entity field is empty"), 037 EMPTYMODIFIEDBY("empty modified by", "the modified by field is empty"), 038 EMPTYPARAMETERNAME("empty parameter name", "the parameter name field is empty"), 039 EMPTYVALUE("empty value", "the value field is empty"), 040 EMPTYVERSION("empty version", "the version field is emtpy"), 041 UNKNOWNBAND("unknown band", "the band is unknown"), 042 BANDREQUIRED("band required", "the band is required and not provided"), 043 UNKNOWNENTITY("unknown entity", "the entity is unknown"), 044 UNKNOWNPARAMETERNAME("unknown parameter name", "the parameter name is unknown"); 045 046 private final String debugMessage; 047 private final String displayMessage; 048 049 ErrorType(String debugMessage, String displayMessage) { 050 this.debugMessage = debugMessage; 051 this.displayMessage = displayMessage; 052 } 053 054 public String getDebugMessage() { 055 return debugMessage; 056 } 057 058 public String getDisplayMessage() { 059 return displayMessage; 060 } 061 } 062 063 private ErrorType errorType; 064 065 protected ParameterValidation(AbstractValidator<Parameter> validationContainer, 066 ValidationPurpose reasonForValidation) { 067 super(validationContainer, reasonForValidation); 068 severity = (reasonForValidation == ValidationPurpose.CERTIFY_READY_TO_USE) ? FailureSeverity.ERROR 069 : FailureSeverity.WARNING; 070 } 071 072 protected String debugMessage() { 073 return errorType.getDebugMessage(); 074 } 075 076 protected String displayMessage() { 077 return errorType.getDisplayMessage(); 078 } 079 080 protected boolean passesTest() { 081 ParameterRule parameterRule = null; 082 Parameter target = container.getTarget(); 083 errorType = null; 084 085 if (target.getEntity() == null) 086 errorType = ErrorType.EMPTYENTITY; 087 else if (target.getParameterName() == null) 088 errorType = ErrorType.EMPTYPARAMETERNAME; 089 else if (target.getValue() == null) 090 errorType = ErrorType.EMPTYVALUE; 091 else if (target.getEffectiveDate() == null) 092 errorType = ErrorType.EMPTYEFFECTIVEDATE; 093 else if (target.getVersion() == null) 094 errorType = ErrorType.EMPTYVERSION; 095 else if (target.getModifiedBy() == null) 096 errorType = ErrorType.EMPTYMODIFIEDBY; 097 098 if (errorType != null) 099 return false; 100 101 if (target.getEntityType() == null) { 102 errorType = ErrorType.UNKNOWNENTITY; 103 return false; 104 } 105 106 if ((parameterRule = target.getParameterRule()) == null) { 107 errorType = ErrorType.UNKNOWNPARAMETERNAME; 108 return false; 109 } 110 111 if (parameterRule.isBandRequired()) { 112 if (target.getBand() == null || target.getBand().length() == 0) { 113 errorType = ErrorType.BANDREQUIRED; 114 return false; 115 } else if (Bands.getInstance().getBand(target.getBand()) == null) { 116 errorType = ErrorType.UNKNOWNBAND; 117 return false; 118 } 119 } 120 121 switch (parameterRule.getValueType()) { 122 case DECIMAL_INT: 123 try { 124 target.getValueAsDecimalInt(); 125 } catch (NumberFormatException ex) { 126 errorType = ErrorType.BADDECINTVALUE; 127 return false; 128 } 129 break; 130 case OCTAL_INT: 131 try { 132 target.getValueAsOctalInt(); 133 } catch (NumberFormatException ex) { 134 errorType = ErrorType.BADOCTINTVALUE; 135 return false; 136 } 137 break; 138 case DOUBLE: 139 try { 140 target.getValueAsDouble(); 141 } catch (NumberFormatException ex) { 142 errorType = ErrorType.BADFLOATVALUE; 143 return false; 144 } 145 break; 146 case PAD_NAME: 147 if (!EntityType.PAD.getPattern().matcher(target.getValue()).matches()) { 148 errorType = ErrorType.BADPADNAMEVALUE; 149 return false; 150 } 151 break; 152 default: 153 // Should throw an exception here, means bad-juju, TBD. 154 break; 155 } 156 157 return true; 158 } 159 160 }