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 | ||
22 | private DictionaryModel model; | |
23 | private ModelFinder finder; | |
24 | ||
25 | public DictionaryParentSetter (DictionaryModel model) | |
26 | 0 | { |
27 | 0 | this.model = model; |
28 | 0 | this.finder = new ModelFinder (model); |
29 | 0 | } |
30 | ||
31 | public void set () | |
32 | { | |
33 | 0 | for (int i = 1; i < model.getDictionary ().size (); i ++) |
34 | { | |
35 | 0 | Dictionary child = model.getDictionary ().get (i); |
36 | 0 | Dictionary parent = calcParent (i, child); |
37 | 0 | child.setParent (parent); |
38 | } | |
39 | 0 | } |
40 | ||
41 | private Dictionary calcParent (int index, Dictionary child) | |
42 | { | |
43 | // if first field the parent is by default null | |
44 | // course.official | |
45 | 0 | if (index == 0) |
46 | { | |
47 | 0 | return null; |
48 | } | |
49 | // if first field of the defnition of a new main type | |
50 | // then the parent is null | |
51 | // i.e. program.official is the start of a new cluInfo definition | |
52 | 0 | XmlType xmlType = finder.findXmlType (child.getXmlObject ()); |
53 | 0 | if (xmlType == null) |
54 | { | |
55 | 0 | throw new DictionaryExecutionException ("child.getXmlObject ()=" + child.getXmlObject ()); |
56 | } | |
57 | 0 | if (xmlType.hasOwnCreateUpdate ()) |
58 | { | |
59 | 0 | List<Field> fields = finder.findFields (child.getXmlObject ()); |
60 | 0 | if (fields.get (0).getShortName ().equalsIgnoreCase (child.getShortName ())) |
61 | { | |
62 | 0 | return null; |
63 | } | |
64 | } | |
65 | 0 | Dictionary prev = model.getDictionary ().get (index - 1); |
66 | // if this is just another field on same object as prev | |
67 | // then they have the same parent | |
68 | // course.official.no cluIdentifierInfo | |
69 | // course.official.transcriptTitle cluIdentifierInfo << this has the same parent as previous | |
70 | 0 | if (prev.getXmlObject ().equalsIgnoreCase (child.getXmlObject ())) |
71 | { | |
72 | 0 | return prev.getParent (); |
73 | } | |
74 | // objects are different so check if we are going down or up | |
75 | // if going down the hierarchy | |
76 | 0 | Field prevField = finder.findField (prev); |
77 | 0 | if (prevField == null) |
78 | { | |
79 | 0 | throw new DictionaryExecutionException |
80 | ("Could not find field associated with dictionary entry with id =" + prev.getId ()); | |
81 | } | |
82 | // going down heirarchy if this sub-object is the same type of the previous field | |
83 | // because that means this field is the 1st field of the sub-type definition | |
84 | 0 | if (calcType (prevField.getXmlType ()).equalsIgnoreCase (child.getXmlObject ())) |
85 | { | |
86 | // loop back to find the first (default) definition for that field -- that is the real parent | |
87 | // not the state override | |
88 | // course.desc <<<< Make this the parent | |
89 | // course.desc.draft.private | |
90 | // course.desc.template | |
91 | // course.desc.draft.public <<<< not this | |
92 | // course.desc.plain <<<< of this | |
93 | 0 | for (int i = index - 2; i > -1; i --) |
94 | { | |
95 | 0 | Dictionary prev2 = model.getDictionary ().get (i); |
96 | 0 | if (prev2.getXmlObject ().equalsIgnoreCase (prev.getXmlObject ())) |
97 | { | |
98 | 0 | if (prev2.getShortName ().equalsIgnoreCase (prev.getShortName ())) |
99 | { | |
100 | 0 | prev = prev2; |
101 | 0 | continue; |
102 | } | |
103 | } | |
104 | break; | |
105 | } | |
106 | 0 | return prev; |
107 | } | |
108 | // we are popping up from down in the heirarchy | |
109 | // have to go back to find the previous item at the same level | |
110 | // course.desc cluInfo | |
111 | // course.desc.draft.private cluInfo | |
112 | // course.desc.template cluInfo | |
113 | // course.desc.draft.public cluInfo << use this parent | |
114 | // course.desc.plain richTextInfo | |
115 | // course.desc.plain.draft.private richTextInfo | |
116 | // course.desc.plain.draft.public richTextInfo | |
117 | // course.desc.formatted richTextInfo | |
118 | // course.desc.formatted.draft.private richTextInfo | |
119 | // course.desc.formatted.draft.public richTextInfo | |
120 | // course.rationale cluInfo << as this field's parent | |
121 | 0 | for (int i = index - 1; i > -1; i --) |
122 | { | |
123 | 0 | Dictionary dict = model.getDictionary ().get (i); |
124 | 0 | if (dict.getXmlObject ().equalsIgnoreCase (child.getXmlObject ())) |
125 | { | |
126 | 0 | return dict.getParent (); |
127 | } | |
128 | } | |
129 | 0 | throw new DictionaryExecutionException ("dictionary entry " + child.getId () |
130 | + " could not calculate the parent"); | |
131 | } | |
132 | ||
133 | private String calcType (String type) | |
134 | { | |
135 | 0 | if (type.endsWith ("List")) |
136 | { | |
137 | 0 | type = type.substring (0, type.length () - "List".length ()); |
138 | } | |
139 | ||
140 | 0 | return type; |
141 | } | |
142 | ||
143 | } |