Coverage Report - org.kuali.student.contract.model.util.DictionaryMainTypeExpander
 
Classes in this File Line Coverage Branch Coverage Complexity
DictionaryMainTypeExpander
0%
0/29
0%
0/8
2.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.util;
 17  
 
 18  
 import org.kuali.student.contract.exception.DictionaryExecutionException;
 19  
 import org.kuali.student.contract.model.*;
 20  
 import org.kuali.student.contract.model.util.ModelFinder;
 21  
 import org.kuali.student.contract.model.validation.DictionaryValidationException;
 22  
 
 23  
 import java.util.ArrayList;
 24  
 import java.util.List;
 25  
 
 26  
 /**
 27  
  * A dictionary expander that expandds the main type
 28  
  * @author nwright
 29  
  */
 30  
 public class DictionaryMainTypeExpander implements DictionaryExpander
 31  
 {
 32  
 
 33  
  private List<Dictionary> oldDicts;
 34  
  private List<Dictionary> newDicts;
 35  
  private DictionaryModel spreadsheet;
 36  
  private ModelFinder finder;
 37  
 
 38  
  public DictionaryMainTypeExpander (DictionaryModel spreadsheet)
 39  0
  {
 40  0
   this.spreadsheet = spreadsheet;
 41  0
   this.oldDicts = spreadsheet.getDictionary ();
 42  0
   this.finder = new ModelFinder (this.spreadsheet);
 43  0
  }
 44  
 
 45  
  @Override
 46  
  public List<Dictionary> expand ()
 47  
  {
 48  0
   newDicts = new ArrayList (oldDicts.size () * 3);
 49  0
   expandMainType ();
 50  0
   return newDicts;
 51  
  }
 52  
 
 53  
  private void expandMainType ()
 54  
  {
 55  0
   for (Dictionary d : oldDicts)
 56  
   {
 57  0
    Type type = getMainType (d);
 58  0
    if (type.getStatus ().equals (Type.GROUPING))
 59  
    {
 60  0
     expandMainType (d, type);
 61  
    }
 62  
    else
 63  
    {
 64  0
     newDicts.add (d);
 65  
    }
 66  0
   }
 67  0
   return;
 68  
  }
 69  
 
 70  
  private Type getMainType (Dictionary dict)
 71  
  {
 72  0
   Dictionary root = finder.findRoot (dict);
 73  0
   Type type = finder.findType (root.getXmlObject (), dict.getType ());
 74  0
   if (type == null)
 75  
   {
 76  0
    throw new DictionaryValidationException ("Could not find main type for dictionary entry " +
 77  
     dict.getId () + ": " + root.getXmlObject () + "." + dict.getType ());
 78  
   }
 79  0
   return type;
 80  
  }
 81  
 
 82  
  private void expandMainType (Dictionary d, Type type)
 83  
  {
 84  0
   for (Type t : finder.expandType (type))
 85  
   {
 86  
    try
 87  
    {
 88  0
     System.out.println ("Expanding dictionary entry " + d.getId () +
 89  
      " with type " + type.getName () + "  to " + t.getName ());
 90  0
     Dictionary clone = (Dictionary) d.clone ();
 91  0
     clone.setType (t.getName ());
 92  0
     newDicts.add (clone);
 93  
    }
 94  0
    catch (CloneNotSupportedException ex)
 95  
    {
 96  0
     throw new DictionaryExecutionException (ex);
 97  0
    }
 98  
   }
 99  0
  }
 100  
 }
 101  
  
 102