Coverage Report - org.kuali.student.contract.model.validation.FieldValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldValidator
0%
0/36
0%
0/22
3.4
 
 1  
 /*
 2  
  * Copyright 2009 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may        obtain a copy of the License at
 7  
  *
 8  
  *         http://www.osedu.org/licenses/ECL-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.student.contract.model.validation;
 17  
 
 18  
 import org.kuali.student.contract.model.Constraint;
 19  
 import org.kuali.student.contract.model.DictionaryModel;
 20  
 import org.kuali.student.contract.model.Field;
 21  
 import org.kuali.student.contract.model.XmlType;
 22  
 import org.kuali.student.contract.model.util.ModelFinder;
 23  
 
 24  
 import java.util.Collection;
 25  
 
 26  
 /**
 27  
  * This validates a single field definition
 28  
  * @author nwright
 29  
  */
 30  
 public class FieldValidator implements ModelValidator
 31  
 {
 32  
 
 33  
  private Field field;
 34  
  private DictionaryModel model;
 35  
 
 36  
  public FieldValidator (Field field, DictionaryModel sheet)
 37  0
  {
 38  0
   this.field = field;
 39  0
   this.model = sheet;
 40  0
  }
 41  
 
 42  
  private Collection errors;
 43  
 
 44  
  @Override
 45  
  public Collection<String> validate ()
 46  
  {
 47  0
   ConstraintValidator cv =
 48  
    new ConstraintValidator (field.getInlineConstraint ());
 49  0
   errors = cv.validate ();
 50  0
   if (field.getPrimitive ().equalsIgnoreCase (XmlType.COMPLEX))
 51  
   {
 52  0
    validateComplexConstraint (field.getInlineConstraint ());
 53  0
    for (String id : field.getConstraintIds ())
 54  
    {
 55  0
     Constraint cons = findConstraint (id);
 56  0
     if (cons != null)
 57  
     {
 58  0
      validateComplexConstraint (cons);
 59  
     }
 60  0
    }
 61  
   }
 62  0
   return errors;
 63  
  }
 64  
 
 65  
  private Constraint findConstraint (String id)
 66  
  {
 67  0
   Constraint cons = new ModelFinder (model).findConstraint (id);
 68  0
   if (cons != null)
 69  
   {
 70  0
    return cons;
 71  
   }
 72  0
   addError ("Field constraint id, " + id +
 73  
    " is not defined in the bank of constraints");
 74  0
   return null;
 75  
  }
 76  
 
 77  
  private void validateComplexConstraint (Constraint cons)
 78  
  {
 79  0
   if ( ! cons.getMinLength ().equals (""))
 80  
   {
 81  0
    addError ("A minLength is not allowed on a complex field");
 82  
   }
 83  0
   if ( ! cons.getMaxLength ().equals (""))
 84  
   {
 85  0
    addError ("A maxLength is not allowed on a complex field");
 86  
   }
 87  0
   if ( ! cons.getMinValue ().equals (""))
 88  
   {
 89  0
    addError ("A minValue is not allowed on a complex field");
 90  
   }
 91  0
   if ( ! cons.getMaxValue ().equals (""))
 92  
   {
 93  0
    addError ("A maxValue is not allowed on a complex field");
 94  
   }
 95  0
   if ( ! cons.getValidChars ().equals (""))
 96  
   {
 97  0
    addError ("A validChars is not allowed on a complex field");
 98  
   }
 99  0
   if ( ! cons.getLookup ().equals (""))
 100  
   {
 101  0
    addError ("A lookup value is not allowed on a complex field");
 102  
   }
 103  0
  }
 104  
 
 105  
  private void addError (String msg)
 106  
  {
 107  0
   String error = "Error in field " + field.getId () + ": " + msg;
 108  0
   if ( ! errors.contains (error))
 109  
   {
 110  0
    errors.add (error);
 111  
   }
 112  0
  }
 113  
 
 114  
 }