1 | |
package org.kuali.student.lum.common.client.lo; |
2 | |
|
3 | |
import org.kuali.student.common.ui.client.configurable.mvc.binding.ModelWidgetBindingSupport; |
4 | |
import org.kuali.student.common.ui.client.mvc.DataModel; |
5 | |
import org.kuali.student.core.assembly.data.Data; |
6 | |
import org.kuali.student.core.assembly.data.QueryPath; |
7 | |
import org.kuali.student.lum.lo.dto.LoCategoryInfo; |
8 | |
|
9 | |
import java.util.*; |
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | 0 | public class LOBuilderBinding extends ModelWidgetBindingSupport<LOBuilder> { |
15 | |
|
16 | 0 | public static LOBuilderBinding INSTANCE = new LOBuilderBinding(); |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
@Override |
24 | |
public void setModelValue(LOBuilder builder, DataModel model, String path) { |
25 | 0 | Data losData = new Data(); |
26 | 0 | Map<Integer, Data> parentStore = new HashMap<Integer, Data>(); |
27 | 0 | int sequence = 0; |
28 | 0 | List<OutlineNode<LOPicker>> value = stripeOutEmptyInput(builder.getValue()); |
29 | 0 | if (value != null) { |
30 | 0 | for (OutlineNode<LOPicker> node : value) { |
31 | 0 | if (node.getIndentLevel() == 0) { |
32 | 0 | Data item = createLoDisplayInfoData(node, sequence); |
33 | 0 | parentStore.put(new Integer(0), item); |
34 | 0 | losData.add(item); |
35 | 0 | } else { |
36 | 0 | Data item = createLoDisplayInfoData(node, sequence); |
37 | 0 | LoDisplayInfoHelper parentItemHelper = null; |
38 | 0 | parentStore.put(node.getIndentLevel(), item); |
39 | 0 | parentItemHelper = new LoDisplayInfoHelper( |
40 | |
parentStore.get(node.getIndentLevel() - 1)); |
41 | 0 | parentItemHelper.getDisplayInfoList().add(item); |
42 | |
} |
43 | 0 | sequence++; |
44 | |
} |
45 | |
} |
46 | 0 | model.set(QueryPath.parse(path), losData); |
47 | 0 | } |
48 | |
|
49 | |
@Override |
50 | |
public void setWidgetValue(LOBuilder builder, DataModel model, String path) { |
51 | 0 | List<OutlineNode<LOPicker>> loOutlineNodes = new ArrayList<OutlineNode<LOPicker>>(); |
52 | |
|
53 | |
|
54 | 0 | QueryPath qPath = QueryPath.parse(path); |
55 | |
|
56 | 0 | Data data = null; |
57 | 0 | if (model != null) { |
58 | 0 | data = model.get(qPath); |
59 | |
} |
60 | |
|
61 | 0 | dataToOutlineNodes(data, loOutlineNodes, 0); |
62 | 0 | builder.setValue(loOutlineNodes); |
63 | 0 | } |
64 | |
|
65 | |
private List<OutlineNode<LOPicker>> stripeOutEmptyInput(List<OutlineNode<LOPicker>> input) { |
66 | 0 | List<OutlineNode<LOPicker>> value = new ArrayList<OutlineNode<LOPicker>>(); |
67 | 0 | boolean allEmptyNodes = true; |
68 | 0 | if (input != null) { |
69 | 0 | for (OutlineNode<LOPicker> node : input) { |
70 | 0 | String desc = node.getUserObject().getLOText(); |
71 | 0 | int identLevel = node.getIndentLevel(); |
72 | 0 | List<LoCategoryInfo> categories = node.getUserObject().getLoCategories(); |
73 | 0 | if (desc != null && desc.trim().length() > 0 || |
74 | |
identLevel > 0 || |
75 | |
categories != null && !categories.isEmpty()) { |
76 | 0 | allEmptyNodes = false; |
77 | 0 | value.add(node); |
78 | |
} |
79 | 0 | } |
80 | |
} |
81 | 0 | if (allEmptyNodes) { |
82 | 0 | value = null; |
83 | |
} |
84 | 0 | return value; |
85 | |
} |
86 | |
|
87 | |
private void dataToOutlineNodes(Data data, List<OutlineNode<LOPicker>> loOutlineNodes, int identLevel) { |
88 | 0 | if (data != null) { |
89 | 0 | LoDisplayInfoSortedSet sortedDisplayInfos = new LoDisplayInfoSortedSet(); |
90 | 0 | for (Data.Property property : data) { |
91 | 0 | Data loDisplayInfoData = property.getValue(); |
92 | 0 | LoDisplayInfoHelper loDisplayInfoHelper = new LoDisplayInfoHelper(loDisplayInfoData); |
93 | 0 | sortedDisplayInfos.add(loDisplayInfoHelper); |
94 | 0 | } |
95 | 0 | for (LoDisplayInfoHelper loDisplayInfoHelper : sortedDisplayInfos) { |
96 | 0 | LOPicker picker = new LOPicker(LOBuilder.getMessageGroup(), LOBuilder.getType(), LOBuilder.getState(), LOBuilder.getRepoKey()); |
97 | |
|
98 | 0 | LoInfoHelper loInfoHelper = new LoInfoHelper(loDisplayInfoHelper.getLoInfo()); |
99 | 0 | RichTextHelper descriptionHelper = new RichTextHelper(loInfoHelper.getDesc()); |
100 | 0 | picker.setLOText(descriptionHelper.getPlain()); |
101 | 0 | List<LoCategoryInfo> categories = getCategoryList(loDisplayInfoHelper); |
102 | 0 | picker.setLOCategories(categories); |
103 | 0 | picker.setMetaInfoData(loInfoHelper.getMetaInfo()); |
104 | 0 | OutlineNode<LOPicker> node = new OutlineNode<LOPicker>(); |
105 | |
|
106 | 0 | node.setUserObject(picker); |
107 | 0 | node.setOpaque(loInfoHelper.getId()); |
108 | 0 | node.setIndentLevel(identLevel); |
109 | 0 | loOutlineNodes.add(node); |
110 | |
|
111 | 0 | dataToOutlineNodes(loDisplayInfoHelper.getDisplayInfoList(), loOutlineNodes, identLevel + 1); |
112 | 0 | } |
113 | |
} |
114 | 0 | } |
115 | |
|
116 | |
private List<LoCategoryInfo> getCategoryList(LoDisplayInfoHelper loDisplayInfoHelper) { |
117 | 0 | List<LoCategoryInfo> categoryInfos = new ArrayList<LoCategoryInfo>(); |
118 | 0 | Data categoriesData = loDisplayInfoHelper.getCategoryInfoList(); |
119 | |
|
120 | 0 | if (null != categoriesData) { |
121 | 0 | Iterator<Data.Property> itr = categoriesData.realPropertyIterator(); |
122 | |
|
123 | 0 | while (itr.hasNext()) { |
124 | 0 | Data.Property catProp = itr.next(); |
125 | 0 | Data catData = catProp.getValue(); |
126 | 0 | LoCategoryInfo catInfo = CategoryDataUtil.toLoCategoryInfo(catData); |
127 | 0 | categoryInfos.add(catInfo); |
128 | 0 | } |
129 | |
} |
130 | 0 | return categoryInfos; |
131 | |
} |
132 | |
|
133 | |
private Data createLoDisplayInfoData(OutlineNode<LOPicker> node, int sequence) { |
134 | 0 | Data result = null; |
135 | 0 | LoDisplayInfoHelper loDisplayInfoDataHelper = new LoDisplayInfoHelper(new Data()); |
136 | 0 | LoInfoHelper loInfoHelper = new LoInfoHelper(new Data()); |
137 | |
|
138 | 0 | loInfoHelper.setId((String) node.getOpaque()); |
139 | |
|
140 | 0 | RichTextHelper richTextHelper = new RichTextHelper(); |
141 | 0 | String loDesc = node.getUserObject().getLOText(); |
142 | 0 | richTextHelper.setFormatted(loDesc); |
143 | 0 | richTextHelper.setPlain(loDesc); |
144 | 0 | loInfoHelper.setDesc(richTextHelper.getData()); |
145 | |
|
146 | 0 | if (null == loInfoHelper.getName() || loInfoHelper.getName().length() == 0) { |
147 | 0 | loInfoHelper.setName("SINGLE USE LO"); |
148 | |
} |
149 | |
|
150 | |
|
151 | 0 | Data categoriesData = new Data(); |
152 | 0 | for (LoCategoryInfo cat : node.getUserObject().getLoCategories()) { |
153 | 0 | categoriesData.add(CategoryDataUtil.toData(cat)); |
154 | |
} |
155 | |
|
156 | |
|
157 | 0 | loInfoHelper.setSequence(Integer.toString(sequence)); |
158 | |
|
159 | |
|
160 | 0 | loInfoHelper.setMetaInfo(node.getUserObject().getMetaInfoData()); |
161 | |
|
162 | 0 | loDisplayInfoDataHelper.setLoInfo(loInfoHelper.getData()); |
163 | 0 | loDisplayInfoDataHelper.setCategoryInfoList(categoriesData); |
164 | 0 | result = loDisplayInfoDataHelper.getData(); |
165 | 0 | return result; |
166 | |
} |
167 | |
|
168 | |
} |