| 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 | |
private List<Dictionary> oldDicts; |
| 33 | |
private List<Dictionary> newDicts; |
| 34 | |
private DictionaryModel spreadsheet; |
| 35 | |
private ModelFinder finder; |
| 36 | |
|
| 37 | 0 | public DictionaryMainTypeExpander(DictionaryModel spreadsheet) { |
| 38 | 0 | this.spreadsheet = spreadsheet; |
| 39 | 0 | this.oldDicts = spreadsheet.getDictionary(); |
| 40 | 0 | this.finder = new ModelFinder(this.spreadsheet); |
| 41 | 0 | } |
| 42 | |
|
| 43 | |
@Override |
| 44 | |
public List<Dictionary> expand() { |
| 45 | 0 | newDicts = new ArrayList(oldDicts.size() * 3); |
| 46 | 0 | expandMainType(); |
| 47 | 0 | return newDicts; |
| 48 | |
} |
| 49 | |
|
| 50 | |
private void expandMainType() { |
| 51 | 0 | for (Dictionary d : oldDicts) { |
| 52 | 0 | Type type = getMainType(d); |
| 53 | 0 | if (type.getStatus().equals(Type.GROUPING)) { |
| 54 | 0 | expandMainType(d, type); |
| 55 | |
} else { |
| 56 | 0 | newDicts.add(d); |
| 57 | |
} |
| 58 | 0 | } |
| 59 | 0 | return; |
| 60 | |
} |
| 61 | |
|
| 62 | |
private Type getMainType(Dictionary dict) { |
| 63 | 0 | Dictionary root = finder.findRoot(dict); |
| 64 | 0 | Type type = finder.findType(root.getXmlObject(), dict.getType()); |
| 65 | 0 | if (type == null) { |
| 66 | 0 | throw new DictionaryValidationException("Could not find main type for dictionary entry " |
| 67 | |
+ dict.getId() + ": " + root.getXmlObject() + "." + dict.getType()); |
| 68 | |
} |
| 69 | 0 | return type; |
| 70 | |
} |
| 71 | |
|
| 72 | |
private void expandMainType(Dictionary d, Type type) { |
| 73 | 0 | for (Type t : finder.expandType(type)) { |
| 74 | |
try { |
| 75 | 0 | System.out.println("Expanding dictionary entry " + d.getId() |
| 76 | |
+ " with type " + type.getName() + " to " + t.getName()); |
| 77 | 0 | Dictionary clone = (Dictionary) d.clone(); |
| 78 | 0 | clone.setType(t.getName()); |
| 79 | 0 | newDicts.add(clone); |
| 80 | 0 | } catch (CloneNotSupportedException ex) { |
| 81 | 0 | throw new DictionaryExecutionException(ex); |
| 82 | 0 | } |
| 83 | |
} |
| 84 | 0 | } |
| 85 | |
} |
| 86 | |
|
| 87 | |
|