Coverage Report - org.kuali.student.contract.model.validation.SearchModelValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
SearchModelValidator
0%
0/173
0%
0/122
4.667
 
 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 java.util.ArrayList;
 19  
 import java.util.Collection;
 20  
 import java.util.HashSet;
 21  
 import java.util.List;
 22  
 import java.util.Set;
 23  
 
 24  
 import org.kuali.student.contract.model.SearchCriteria;
 25  
 import org.kuali.student.contract.model.SearchCriteriaParameter;
 26  
 import org.kuali.student.contract.model.SearchModel;
 27  
 import org.kuali.student.contract.model.SearchResult;
 28  
 import org.kuali.student.contract.model.SearchResultColumn;
 29  
 import org.kuali.student.contract.model.SearchType;
 30  
 import org.kuali.student.contract.model.util.ModelFinder;
 31  
 
 32  
 /**
 33  
  * Validates the entire spreadsheet model
 34  
  * @author nwright
 35  
  */
 36  0
 public class SearchModelValidator implements ModelValidator
 37  
 {
 38  
 
 39  
  private SearchModel model;
 40  
 
 41  
  public SearchModelValidator (SearchModel model)
 42  0
  {
 43  0
   this.model = model;
 44  0
  }
 45  
 
 46  0
  List<String> errors = new ArrayList ();
 47  
 
 48  
  @Override
 49  
  public Collection<String> validate ()
 50  
  {
 51  0
   errors = new ArrayList ();
 52  0
   validateSearchTypes ();
 53  0
   return errors;
 54  
  }
 55  
 
 56  
  private void validateSearchTypes ()
 57  
  {
 58  0
   if (model.getSearchTypes ().size () == 0)
 59  
   {
 60  0
    addError ("No search types found");
 61  
   }
 62  0
   validateForDuplicates ();
 63  0
   for (SearchType st : model.getSearchTypes ())
 64  
   {
 65  0
    SearchTypeValidator stv = new SearchTypeValidator (st, model);
 66  0
    errors.addAll (stv.validate ());
 67  0
   }
 68  0
   getValidateSearchCriteria (true);
 69  0
   getValidateSearchCriteriaParameters (true);
 70  0
   getValidateSearchResults (true);
 71  0
   getValidateSearchResultColumns (true);
 72  0
  }
 73  
 
 74  
  private void validateForDuplicates ()
 75  
  {
 76  0
   Set<String> keys = new HashSet ();
 77  0
   for (SearchType st : model.getSearchTypes ())
 78  
   {
 79  0
    if ( ! keys.add (st.getKey ()))
 80  
    {
 81  0
     SearchType st2 = new ModelFinder (model).findSearchType (st.getKey ());
 82  0
     List<String> differences = findDifferences (st, st2);
 83  0
     if (differences.size () > 0)
 84  
     {
 85  0
      StringBuffer buf = new StringBuffer ();
 86  0
      String comma = "";
 87  0
      for (String val : differences)
 88  
      {
 89  0
       buf.append (comma);
 90  0
       buf.append (val);
 91  0
       comma = ", ";
 92  
      }
 93  0
      addError ("Search type [" + st.getKey () +
 94  
       "] is duplicated but differences were found: " + buf);
 95  
     }
 96  0
    }
 97  
   }
 98  0
  }
 99  
 
 100  
  private List<String> findDifferences (SearchType st1, SearchType st2)
 101  
  {
 102  0
   List<String> differences = new ArrayList ();
 103  0
   addIfDifferent (differences, st1.getKey (), st2.getKey (), "key");
 104  0
   addIfDifferent (differences, st1.getName (), st2.getName (), "name");
 105  0
   addIfDifferent (differences, st1.getDescription (), st2.getDescription (), "Description");
 106  0
   addIfDifferent (differences, st1.getService (), st2.getService (), "Service");
 107  
 
 108  0
   SearchCriteria sc1 = st1.getSearchCriteria ();
 109  0
   SearchCriteria sc2 = st2.getSearchCriteria ();
 110  0
   addIfDifferent (differences, sc1.getKey (), sc2.getKey (), "key");
 111  0
   addIfDifferent (differences, sc1.getName (), sc2.getName (), "name");
 112  0
   addIfDifferent (differences, sc1.getDescription (), sc2.getDescription (), "Description");
 113  
 
 114  0
   List<SearchCriteriaParameter> scp1s = sc1.getParameters ();
 115  0
   List<SearchCriteriaParameter> scp2s = sc2.getParameters ();
 116  0
   addIfDifferent (differences, scp1s.size (), scp2s.size (), "# of parameters");
 117  
 
 118  0
   if (scp1s.size () == scp2s.size ())
 119  
   {
 120  0
    for (int i = 0; i < scp1s.size (); i ++)
 121  
    {
 122  0
     SearchCriteriaParameter scp1 = scp1s.get (i);
 123  0
     SearchCriteriaParameter scp2 = scp2s.get (i);
 124  0
     addIfDifferent (differences, scp1.getKey (), scp2.getKey (), "key");
 125  0
     addIfDifferent (differences, scp1.getName (), scp2.getName (), "name");
 126  0
     addIfDifferent (differences, scp1.getDescription (), scp2.getDescription (), "Description");
 127  0
     addIfDifferent (differences, scp1.getDataType (), scp2.getDataType (), "Datatype");
 128  0
     addIfDifferent (differences, scp1.getOptional (), scp2.getOptional (), "Optional");
 129  0
     addIfDifferent (differences, scp1.getCaseSensitive (), scp2.getCaseSensitive (), "case");
 130  
    }
 131  
   }
 132  
 
 133  
 
 134  0
   SearchResult sr1 = st1.getSearchResult ();
 135  0
   SearchResult sr2 = st2.getSearchResult ();
 136  0
   addIfDifferent (differences, sr1.getKey (), sr2.getKey (), "key");
 137  0
   addIfDifferent (differences, sr1.getName (), sr2.getName (), "name");
 138  0
   addIfDifferent (differences, sr1.getDescription (), sr2.getDescription (), "Description");
 139  0
   List<SearchResultColumn> src1s = sr1.getResultColumns ();
 140  0
   List<SearchResultColumn> src2s = sr2.getResultColumns ();
 141  0
   addIfDifferent (differences, src1s.size (), src2s.size (), "# of result columns");
 142  0
   if (src1s.size () == src2s.size ())
 143  
   {
 144  0
    for (int i = 0; i < src1s.size (); i ++)
 145  
    {
 146  0
     SearchResultColumn src1 = src1s.get (i);
 147  0
     SearchResultColumn src2 = src2s.get (i);
 148  0
     addIfDifferent (differences, src1.getKey (), src2.getKey (), "key");
 149  0
     addIfDifferent (differences, src1.getName (), src2.getName (), "name");
 150  0
     addIfDifferent (differences, src1.getDescription (), src2.getDescription (), "Description");
 151  0
     addIfDifferent (differences, src1.getDataType (), src2.getDataType (), "Datatype");
 152  0
     addIfDifferent (differences, src1.getOptional (), src2.getOptional (), "Optional");
 153  0
     addIfDifferent (differences, src1.getCaseSensitive (), src2.getCaseSensitive (), "case");
 154  
    }
 155  
   }
 156  0
   return differences;
 157  
  }
 158  
 
 159  
  private void addIfDifferent (List<String> differences, Object val1, Object val2,
 160  
                               String difference)
 161  
  {
 162  0
   if (val1 == null && val2 == null)
 163  
   {
 164  0
    return;
 165  
   }
 166  0
   if (val1 == null)
 167  
   {
 168  0
    differences.add (difference);
 169  0
    return;
 170  
   }
 171  0
   if ( ! val1.equals (val2))
 172  
   {
 173  0
    differences.add (difference);
 174  
   }
 175  0
  }
 176  
 
 177  
  private List<SearchResult> getValidateSearchResults (boolean validate)
 178  
  {
 179  0
   List<SearchResult> list = new ArrayList ();
 180  0
   Set<String> keys = new HashSet ();
 181  0
   for (SearchType st : model.getSearchTypes ())
 182  
   {
 183  0
    if (keys.add (st.getSearchResult ().getKey ()))
 184  
    {
 185  0
     list.add (st.getSearchResult ());
 186  
    }
 187  
    else
 188  
    {
 189  0
     if (validate)
 190  
     {
 191  0
      for (SearchResult result : list)
 192  
      {
 193  0
       if (result.getKey ().equals (st.getSearchResult ().getKey ()))
 194  
       {
 195  0
        compareSearchResults (result, st.getSearchResult ());
 196  
       }
 197  
      }
 198  
     }
 199  
    }
 200  
   }
 201  0
   return list;
 202  
  }
 203  
 
 204  
  private void compareSearchResults (SearchResult result1, SearchResult result2)
 205  
  {
 206  0
   assert result1.getKey ().equals (result2.getKey ());
 207  0
   if ( ! result1.getName ().equals (result2.getName ()))
 208  
   {
 209  0
    addError ("two results with the same key have different names, one is on row " +
 210  
     result1.getRowNumber () + " the other is on row " + result2.getRowNumber ());
 211  
   }
 212  0
   if ( ! result1.getDescription ().equals (result2.getDescription ()))
 213  
   {
 214  0
    addError ("Two results with the same key have different descriptions, one is on row " +
 215  
     result1.getRowNumber () + " the other is on row " + result2.getRowNumber ());
 216  
   }
 217  0
   if (result1.getResultColumns ().size () != result2.getResultColumns ().size ())
 218  
   {
 219  0
    addError ("two results with the same key have different number of result columns, one is on row " +
 220  
     result1.getRowNumber () + " the other is on row " + result2.getRowNumber ());
 221  
   }
 222  0
   int i = 0;
 223  0
   for (SearchResultColumn col : result1.getResultColumns ())
 224  
   {
 225  0
    if ( ! col.getKey ().equals (result2.getResultColumns ().get (i).getKey ()))
 226  
    {
 227  0
     addError ("two results with the same key have different result columns, one is on row " +
 228  
      result1.getRowNumber () + " the other is on row " + result2.getRowNumber () +
 229  
      " the result columns that are different are on row " + col.getRowNumber ());
 230  
    }
 231  0
    i ++;
 232  
   }
 233  0
  }
 234  
 
 235  
  private List<SearchResultColumn> getValidateSearchResultColumns (
 236  
   boolean validate)
 237  
  {
 238  0
   List<SearchResultColumn> list = new ArrayList ();
 239  0
   Set<String> keys = new HashSet ();
 240  0
   for (SearchResult searchResult : getValidateSearchResults (false))
 241  
   {
 242  0
    for (SearchResultColumn col : searchResult.getResultColumns ())
 243  
    {
 244  0
     if (keys.add (col.getKey ()))
 245  
     {
 246  0
      list.add (col);
 247  
     }
 248  
     else
 249  
     {
 250  0
      if (validate)
 251  
      {
 252  0
       for (SearchResultColumn resultCol : list)
 253  
       {
 254  0
        if (resultCol.getKey ().equals (col.getKey ()))
 255  
        {
 256  0
         compareSearchResultColumns (resultCol, col);
 257  
        }
 258  
       }
 259  
      }
 260  
     }
 261  
    }
 262  
   }
 263  0
   return list;
 264  
  }
 265  
 
 266  
  private void compareSearchResultColumns (SearchResultColumn col1,
 267  
                                           SearchResultColumn col2)
 268  
  {
 269  0
   assert col1.getKey ().equals (col2.getKey ());
 270  0
   if ( ! col1.getName ().equals (col2.getName ()))
 271  
   {
 272  0
    addError ("two result columns with the same key have different names, one is on row " +
 273  
     col1.getRowNumber () + " the other is on row " +
 274  
     col2.getRowNumber ());
 275  
   }
 276  0
   if ( ! col1.getDescription ().equals (col2.getDescription ()))
 277  
   {
 278  0
    addError ("Two result columns with the same key have different descriptions, one is on row " +
 279  
     col1.getRowNumber () + " the other is on row " +
 280  
     col2.getRowNumber ());
 281  
   }
 282  0
   if ( ! col1.getDataType ().equals (col2.getDataType ()))
 283  
   {
 284  0
    addError ("Two result columns with the same key have different data type, one is on row " +
 285  
     col1.getRowNumber () + " the other is on row " +
 286  
     col2.getRowNumber ());
 287  
   }
 288  0
  }
 289  
 
 290  
  private List<SearchCriteria> getValidateSearchCriteria (boolean validate)
 291  
  {
 292  0
   List<SearchCriteria> list = new ArrayList ();
 293  0
   Set<String> keys = new HashSet ();
 294  0
   for (SearchType st : model.getSearchTypes ())
 295  
   {
 296  0
    if (keys.add (st.getSearchCriteria ().getKey ()))
 297  
    {
 298  0
     list.add (st.getSearchCriteria ());
 299  
    }
 300  
    else
 301  
    {
 302  0
     if (validate)
 303  
     {
 304  0
      for (SearchCriteria criteria : list)
 305  
      {
 306  0
       if (criteria.getKey ().equals (st.getSearchCriteria ().getKey ()))
 307  
       {
 308  0
        compareSearchCriteria (criteria, st.getSearchCriteria ());
 309  
       }
 310  
      }
 311  
     }
 312  
    }
 313  
   }
 314  0
   return list;
 315  
  }
 316  
 
 317  
  private void compareSearchCriteria (SearchCriteria criteria1,
 318  
                                      SearchCriteria criteria2)
 319  
  {
 320  0
   assert criteria1.getKey ().equals (criteria2.getKey ());
 321  0
   if ( ! criteria1.getName ().equals (criteria2.getName ()))
 322  
   {
 323  0
    addError ("two criteria with the same key have different names, one is on row " +
 324  
     criteria1.getRowNumber () + " the other is on row " +
 325  
     criteria2.getRowNumber ());
 326  
   }
 327  0
   if ( ! criteria1.getDescription ().equals (criteria2.getDescription ()))
 328  
   {
 329  0
    addError ("Two criteria with the same key have different descriptions, one is on row " +
 330  
     criteria1.getRowNumber () + " the other is on row " +
 331  
     criteria2.getRowNumber ());
 332  
   }
 333  0
   if (criteria1.getParameters ().size () != criteria2.getParameters ().size ())
 334  
   {
 335  0
    addError ("two results with the same key have different number of result columns, one is on row " +
 336  
     criteria1.getRowNumber () + " the other is on row " +
 337  
     criteria2.getRowNumber ());
 338  
   }
 339  0
   int i = 0;
 340  0
   for (SearchCriteriaParameter param : criteria1.getParameters ())
 341  
   {
 342  0
    if ( ! param.getKey ().equals (criteria2.getParameters ().get (i).getKey ()))
 343  
    {
 344  0
     addError ("two criteria with the same key have different parameters, one is on row " +
 345  
      criteria1.getRowNumber () + " the other is on row " + criteria2.
 346  
      getRowNumber () +
 347  
      " the parameters that are different are on row " + param.getRowNumber ());
 348  
    }
 349  0
    i ++;
 350  
   }
 351  0
  }
 352  
 
 353  
  private List<SearchCriteriaParameter> getValidateSearchCriteriaParameters (
 354  
   boolean validate)
 355  
  {
 356  0
   List<SearchCriteriaParameter> list = new ArrayList ();
 357  0
   Set<String> keys = new HashSet ();
 358  0
   for (SearchCriteria criteria : getValidateSearchCriteria (false))
 359  
   {
 360  0
    for (SearchCriteriaParameter parm : criteria.getParameters ())
 361  
    {
 362  0
     if (keys.add (parm.getKey ()))
 363  
     {
 364  0
      list.add (parm);
 365  
     }
 366  
     else
 367  
     {
 368  0
      if (validate)
 369  
      {
 370  0
       for (SearchCriteriaParameter param : list)
 371  
       {
 372  0
        if (param.getKey ().equals (parm.getKey ()))
 373  
        {
 374  0
         compareSearchCriteriaParameter (param, parm);
 375  
        }
 376  
       }
 377  
      }
 378  
     }
 379  
    }
 380  
   }
 381  0
   return list;
 382  
  }
 383  
 
 384  
  private void compareSearchCriteriaParameter (SearchCriteriaParameter param1,
 385  
                                               SearchCriteriaParameter param2)
 386  
  {
 387  0
   assert param1.getKey ().equals (param2.getKey ());
 388  0
   if ( ! param1.getName ().equals (param2.getName ()))
 389  
   {
 390  0
    addError ("two criteria parameters with the same key have different names, one is on row " +
 391  
     param1.getRowNumber () + " the other is on row " +
 392  
     param2.getRowNumber ());
 393  
   }
 394  0
   if ( ! param1.getDescription ().equals (param2.getDescription ()))
 395  
   {
 396  0
    addError ("Two criteria parameters with the same key have different descriptions, one is on row " +
 397  
     param1.getRowNumber () + " the other is on row " +
 398  
     param2.getRowNumber ());
 399  
   }
 400  0
   if ( ! param1.getDataType ().equals (param2.getDataType ()))
 401  
   {
 402  0
    addError ("Two criteria parameters with the same key have different data type, one is on row " +
 403  
     param1.getRowNumber () + " the other is on row " +
 404  
     param2.getRowNumber ());
 405  
   }
 406  0
  }
 407  
 
 408  
  private void addError (String msg)
 409  
  {
 410  0
   String error = "Error in overall spreadsheet: " + msg;
 411  0
   if ( ! errors.contains (error))
 412  
   {
 413  0
    errors.add (error);
 414  
   }
 415  0
  }
 416  
 
 417  
 }