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