| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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 | |
|
| 28 | |
|
| 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 | |
|