View Javadoc

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      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          // if first field the parent is by default null
39          // course.official
40          if (index == 0) {
41              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          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          // 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          if (prev.getXmlObject().equalsIgnoreCase(child.getXmlObject())) {
62              return prev.getParent();
63          }
64          // objects are different so check if we are going down or up
65          // if going down the hierarchy
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          // 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          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              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          // 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         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 }