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 BatchUpdateLineValidation extends Validation<BatchUpdateLine>{
029    
030        protected enum ErrorType {
031            EMPTYENTITY("empty entity", "the entity field is empty"),
032            NULLLINE("null line", "the line is empty (null)"),
033            EMPTYPARAMETERNAME("empty parameter name", "the parameter name field is empty"),
034            EMPTYVALUE("empty value", "the value field is empty"),
035            WRONGCOMMAS("malformed line", "the line format is wrong, expected three commas");
036    
037            private final String debugMessage;
038            private final String displayMessage;
039    
040            ErrorType(String debugMessage, String displayMessage) {
041                this.debugMessage = debugMessage;
042                this.displayMessage = displayMessage;
043            }
044    
045            public String getDebugMessage() {
046                return debugMessage;
047            }
048    
049            public String getDisplayMessage() {
050                return displayMessage;
051            }
052        }
053    
054        private ErrorType errorType;
055    
056        protected BatchUpdateLineValidation(AbstractValidator<BatchUpdateLine> validationContainer,
057                ValidationPurpose reasonForValidation) {
058            super(validationContainer, reasonForValidation);
059            severity = (reasonForValidation == ValidationPurpose.CERTIFY_READY_TO_USE) ? FailureSeverity.ERROR
060                    : FailureSeverity.WARNING;
061        }
062    
063        protected String debugMessage() {
064            return errorType.getDebugMessage();
065        }
066    
067        protected String displayMessage() {
068            return errorType.getDisplayMessage();
069        }
070    
071        protected boolean passesTest() {
072            BatchUpdateLine target = container.getTarget();
073    
074            if (target.getLine() == null) {
075                    errorType = ErrorType.NULLLINE;
076                    return false;
077            }
078            if (target.isComment()) {
079                return true;
080            }
081            if (target.numCommas() != 3) {
082                errorType = ErrorType.WRONGCOMMAS;
083                return false;
084            }
085            if (target.getEntity() == null) {
086                errorType = ErrorType.EMPTYENTITY;
087                return false;
088            }
089            if (target.getParameterName() == null) {
090                errorType = ErrorType.EMPTYPARAMETERNAME;
091                return false;
092            }
093            if (target.getValue() == null) {
094                errorType = ErrorType.EMPTYVALUE;
095                return false;
096            }
097            return true;
098        }
099    }