| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DictionaryParentSetter |
|
| 6.5;6.5 |
| 1 | /* | |
| 2 | * To change this template, choose Tools | Templates | |
| 3 | * and open the template in the editor. | |
| 4 | */ | |
| 5 | package org.kuali.student.contract.model.util; | |
| 6 | ||
| 7 | import java.util.List; | |
| 8 | ||
| 9 | import org.kuali.student.contract.exception.DictionaryExecutionException; | |
| 10 | import org.kuali.student.contract.model.Dictionary; | |
| 11 | import org.kuali.student.contract.model.DictionaryModel; | |
| 12 | import org.kuali.student.contract.model.Field; | |
| 13 | import org.kuali.student.contract.model.XmlType; | |
| 14 | ||
| 15 | /** | |
| 16 | * | |
| 17 | * @author nwright | |
| 18 | */ | |
| 19 | public class DictionaryParentSetter { | |
| 20 | ||
| 21 | private DictionaryModel model; | |
| 22 | private ModelFinder finder; | |
| 23 | ||
| 24 | 0 | public DictionaryParentSetter(DictionaryModel model) { |
| 25 | 0 | this.model = model; |
| 26 | 0 | this.finder = new ModelFinder(model); |
| 27 | 0 | } |
| 28 | ||
| 29 | public void set() { | |
| 30 | 0 | for (int i = 1; i < model.getDictionary().size(); i++) { |
| 31 | 0 | Dictionary child = model.getDictionary().get(i); |
| 32 | 0 | Dictionary parent = calcParent(i, child); |
| 33 | 0 | child.setParent(parent); |
| 34 | } | |
| 35 | 0 | } |
| 36 | ||
| 37 | private Dictionary calcParent(int index, Dictionary child) { | |
| 38 | // if first field the parent is by default null | |
| 39 | // course.official | |
| 40 | 0 | if (index == 0) { |
| 41 | 0 | return null; |
| 42 | } | |
| 43 | // if first field of the defnition of a new main type | |
| 44 | // then the parent is null | |
| 45 | // i.e. program.official is the start of a new cluInfo definition | |
| 46 | 0 | XmlType xmlType = finder.findXmlType(child.getXmlObject()); |
| 47 | 0 | if (xmlType == null) { |
| 48 | 0 | throw new DictionaryExecutionException("child.getXmlObject ()=" + child.getXmlObject()); |
| 49 | } | |
| 50 | 0 | if (xmlType.hasOwnCreateUpdate()) { |
| 51 | 0 | List<Field> fields = finder.findFields(child.getXmlObject()); |
| 52 | 0 | if (fields.get(0).getShortName().equalsIgnoreCase(child.getShortName())) { |
| 53 | 0 | return null; |
| 54 | } | |
| 55 | } | |
| 56 | 0 | Dictionary prev = model.getDictionary().get(index - 1); |
| 57 | // if this is just another field on same object as prev | |
| 58 | // then they have the same parent | |
| 59 | // course.official.no cluIdentifierInfo | |
| 60 | // course.official.transcriptTitle cluIdentifierInfo << this has the same parent as previous | |
| 61 | 0 | if (prev.getXmlObject().equalsIgnoreCase(child.getXmlObject())) { |
| 62 | 0 | return prev.getParent(); |
| 63 | } | |
| 64 | // objects are different so check if we are going down or up | |
| 65 | // if going down the hierarchy | |
| 66 | 0 | Field prevField = finder.findField(prev); |
| 67 | 0 | if (prevField == null) { |
| 68 | 0 | throw new DictionaryExecutionException("Could not find field associated with dictionary entry with id =" + prev.getId()); |
| 69 | } | |
| 70 | // going down heirarchy if this sub-object is the same type of the previous field | |
| 71 | // because that means this field is the 1st field of the sub-type definition | |
| 72 | 0 | if (calcType(prevField.getXmlType()).equalsIgnoreCase(child.getXmlObject())) { |
| 73 | // loop back to find the first (default) definition for that field -- that is the real parent | |
| 74 | // not the state override | |
| 75 | // course.desc <<<< Make this the parent | |
| 76 | // course.desc.draft.private | |
| 77 | // course.desc.template | |
| 78 | // course.desc.draft.public <<<< not this | |
| 79 | // course.desc.plain <<<< of this | |
| 80 | 0 | for (int i = index - 2; i > -1; i--) { |
| 81 | 0 | Dictionary prev2 = model.getDictionary().get(i); |
| 82 | 0 | if (prev2.getXmlObject().equalsIgnoreCase(prev.getXmlObject())) { |
| 83 | 0 | if (prev2.getShortName().equalsIgnoreCase(prev.getShortName())) { |
| 84 | 0 | prev = prev2; |
| 85 | 0 | continue; |
| 86 | } | |
| 87 | } | |
| 88 | break; | |
| 89 | } | |
| 90 | 0 | return prev; |
| 91 | } | |
| 92 | // we are popping up from down in the heirarchy | |
| 93 | // have to go back to find the previous item at the same level | |
| 94 | // course.desc cluInfo | |
| 95 | // course.desc.draft.private cluInfo | |
| 96 | // course.desc.template cluInfo | |
| 97 | // course.desc.draft.public cluInfo << use this parent | |
| 98 | // course.desc.plain richTextInfo | |
| 99 | // course.desc.plain.draft.private richTextInfo | |
| 100 | // course.desc.plain.draft.public richTextInfo | |
| 101 | // course.desc.formatted richTextInfo | |
| 102 | // course.desc.formatted.draft.private richTextInfo | |
| 103 | // course.desc.formatted.draft.public richTextInfo | |
| 104 | // course.rationale cluInfo << as this field's parent | |
| 105 | 0 | for (int i = index - 1; i > -1; i--) { |
| 106 | 0 | Dictionary dict = model.getDictionary().get(i); |
| 107 | 0 | if (dict.getXmlObject().equalsIgnoreCase(child.getXmlObject())) { |
| 108 | 0 | return dict.getParent(); |
| 109 | } | |
| 110 | } | |
| 111 | 0 | throw new DictionaryExecutionException("dictionary entry " + child.getId() |
| 112 | + " could not calculate the parent"); | |
| 113 | } | |
| 114 | ||
| 115 | private String calcType(String type) { | |
| 116 | 0 | if (type.endsWith("List")) { |
| 117 | 0 | type = type.substring(0, type.length() - "List".length()); |
| 118 | } | |
| 119 | ||
| 120 | 0 | return type; |
| 121 | } | |
| 122 | } |