1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.kuali.student.contract.model.util;
21
22 import java.util.List;
23
24 import org.kuali.student.contract.exception.DictionaryExecutionException;
25 import org.kuali.student.contract.model.Dictionary;
26 import org.kuali.student.contract.model.DictionaryModel;
27 import org.kuali.student.contract.model.Field;
28 import org.kuali.student.contract.model.XmlType;
29
30
31
32
33
34 public class DictionaryParentSetter {
35
36 private DictionaryModel model;
37 private ModelFinder finder;
38
39 public DictionaryParentSetter(DictionaryModel model) {
40 this.model = model;
41 this.finder = new ModelFinder(model);
42 }
43
44 public void set() {
45 for (int i = 1; i < model.getDictionary().size(); i++) {
46 Dictionary child = model.getDictionary().get(i);
47 Dictionary parent = calcParent(i, child);
48 child.setParent(parent);
49 }
50 }
51
52 private Dictionary calcParent(int index, Dictionary child) {
53
54
55 if (index == 0) {
56 return null;
57 }
58
59
60
61 XmlType xmlType = finder.findXmlType(child.getXmlObject());
62 if (xmlType == null) {
63 throw new DictionaryExecutionException("child.getXmlObject ()=" + child.getXmlObject());
64 }
65 if (xmlType.hasOwnCreateUpdate()) {
66 List<Field> fields = finder.findFields(child.getXmlObject());
67 if (fields.get(0).getShortName().equalsIgnoreCase(child.getShortName())) {
68 return null;
69 }
70 }
71 Dictionary prev = model.getDictionary().get(index - 1);
72
73
74
75
76 if (prev.getXmlObject().equalsIgnoreCase(child.getXmlObject())) {
77 return prev.getParent();
78 }
79
80
81 Field prevField = finder.findField(prev);
82 if (prevField == null) {
83 throw new DictionaryExecutionException("Could not find field associated with dictionary entry with id =" + prev.getId());
84 }
85
86
87 if (calcType(prevField.getXmlType()).equalsIgnoreCase(child.getXmlObject())) {
88
89
90
91
92
93
94
95 for (int i = index - 2; i > -1; i--) {
96 Dictionary prev2 = model.getDictionary().get(i);
97 if (prev2.getXmlObject().equalsIgnoreCase(prev.getXmlObject())) {
98 if (prev2.getShortName().equalsIgnoreCase(prev.getShortName())) {
99 prev = prev2;
100 continue;
101 }
102 }
103 break;
104 }
105 return prev;
106 }
107
108
109
110
111
112
113
114
115
116
117
118
119
120 for (int i = index - 1; i > -1; i--) {
121 Dictionary dict = model.getDictionary().get(i);
122 if (dict.getXmlObject().equalsIgnoreCase(child.getXmlObject())) {
123 return dict.getParent();
124 }
125 }
126 throw new DictionaryExecutionException("dictionary entry " + child.getId()
127 + " could not calculate the parent");
128 }
129
130 private String calcType(String type) {
131 if (type.endsWith("List")) {
132 type = type.substring(0, type.length() - "List".length());
133 }
134
135 return type;
136 }
137 }