1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.ui.client.configurable.mvc.sections; |
17 | |
|
18 | |
import java.util.Date; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.List; |
21 | |
import java.util.Map; |
22 | |
|
23 | |
import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor; |
24 | |
import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptorReadOnly; |
25 | |
import org.kuali.student.common.ui.client.configurable.mvc.binding.ModelWidgetBinding; |
26 | |
import org.kuali.student.common.ui.client.configurable.mvc.binding.ModelWidgetBindingSupport; |
27 | |
import org.kuali.student.common.ui.client.mvc.DataModel; |
28 | |
import org.kuali.student.core.assembly.data.Data; |
29 | |
import org.kuali.student.core.assembly.data.QueryPath; |
30 | |
|
31 | |
import com.google.gwt.core.client.GWT; |
32 | |
import com.google.gwt.user.client.ui.Widget; |
33 | |
|
34 | 0 | public class SectionBinding extends ModelWidgetBindingSupport<Section> { |
35 | 0 | public static SectionBinding INSTANCE = new SectionBinding(); |
36 | |
|
37 | 0 | private SectionBinding() {}; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
@Override |
44 | |
public void setModelValue(Section section, DataModel model, String path) { |
45 | |
|
46 | 0 | List<FieldDescriptor> fields = section.getUnnestedFields(); |
47 | 0 | Map<String, Object> selectedData = null; |
48 | 0 | for (int i = 0; i < fields.size(); i++) { |
49 | 0 | if(!(fields.get(i) instanceof FieldDescriptorReadOnly)){ |
50 | 0 | FieldDescriptor field = (FieldDescriptor) fields.get(i); |
51 | |
|
52 | 0 | String fieldPath = path + QueryPath.getPathSeparator() + field.getFieldKey(); |
53 | 0 | fieldPath = fieldPath.trim(); |
54 | 0 | if (fieldPath.startsWith(QueryPath.getPathSeparator())) { |
55 | 0 | fieldPath = fieldPath.substring(QueryPath.getPathSeparator().length()); |
56 | |
} |
57 | 0 | ModelWidgetBinding binding = field.getModelWidgetBinding(); |
58 | 0 | if (binding != null) { |
59 | 0 | Widget w = field.getFieldWidget(); |
60 | 0 | binding.setModelValue(w, model, fieldPath); |
61 | 0 | } else { |
62 | 0 | GWT.log(field.getFieldKey() + " has no widget binding.", null); |
63 | |
} |
64 | |
} |
65 | |
} |
66 | |
|
67 | 0 | if(section instanceof HasSectionDeletion){ |
68 | 0 | List<Section> deleted =((HasSectionDeletion) section).getDeletedSections(); |
69 | 0 | for(int i =0; i < deleted.size(); i++){ |
70 | 0 | Section deletedSection = deleted.get(i); |
71 | |
|
72 | 0 | List<FieldDescriptor> deletedFields = deletedSection.getFields(); |
73 | 0 | for(int j = 0; j < deletedFields.size(); j++){ |
74 | 0 | FieldDescriptor field = deletedFields.get(j); |
75 | 0 | model.remove(QueryPath.parse(field.getFieldKey())); |
76 | |
} |
77 | |
} |
78 | |
} |
79 | |
|
80 | 0 | for (Section s : section.getSections()) { |
81 | |
|
82 | 0 | if(section instanceof HasSectionDeletion){ |
83 | 0 | List<Section> deleted =((HasSectionDeletion) section).getDeletedSections(); |
84 | 0 | List<String> deletionParentKeys = ((HasSectionDeletion) section).getDeletionParentKeys(); |
85 | 0 | if (deletionParentKeys != null) { |
86 | 0 | for (String deletionParentKey : deletionParentKeys) { |
87 | 0 | model.remove(QueryPath.parse(deletionParentKey)); |
88 | |
} |
89 | |
} |
90 | |
|
91 | |
|
92 | 0 | if(!deleted.contains(s)){ |
93 | 0 | s.updateModel(model); |
94 | 0 | if (deletionParentKeys != null) { |
95 | 0 | for (String deletionParentKey : deletionParentKeys) { |
96 | 0 | selectedData = (selectedData == null)? |
97 | |
new HashMap<String, Object>() : selectedData; |
98 | 0 | selectedData.put(deletionParentKey, model.get(deletionParentKey)); |
99 | |
} |
100 | |
} |
101 | 0 | } else if (selectedData != null && !selectedData.isEmpty()) { |
102 | 0 | for (String key : selectedData.keySet()) { |
103 | 0 | setValue(model, key, selectedData.get(key)); |
104 | |
} |
105 | |
} |
106 | 0 | } |
107 | |
else{ |
108 | 0 | s.updateModel(model); |
109 | |
} |
110 | |
} |
111 | 0 | } |
112 | |
|
113 | |
private void setValue(DataModel model, String path, Object value) { |
114 | 0 | QueryPath qPath = QueryPath.parse(path); |
115 | 0 | if (value instanceof String) { |
116 | 0 | model.set(qPath, (String) value); |
117 | 0 | } else if (value instanceof Integer) { |
118 | 0 | model.set(qPath, (Integer) value); |
119 | 0 | } else if (value instanceof Long) { |
120 | 0 | model.set(qPath, (Long) value); |
121 | 0 | } else if (value instanceof Float) { |
122 | 0 | model.set(qPath, (Float) value); |
123 | 0 | } else if (value instanceof Double) { |
124 | 0 | model.set(qPath, (Double) value); |
125 | 0 | } else if (value instanceof Boolean) { |
126 | 0 | model.set(qPath, (Boolean) value); |
127 | 0 | } else if (value instanceof Date) { |
128 | 0 | model.set(qPath, (Date) value); |
129 | 0 | } else if (value instanceof Data) { |
130 | 0 | model.set(qPath, (Data) value); |
131 | |
} |
132 | 0 | } |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
@Override |
139 | |
public void setWidgetValue(Section section, DataModel model, String path) { |
140 | 0 | List<FieldDescriptor> fields = section.getUnnestedFields(); |
141 | |
|
142 | 0 | for (int i = 0; i < fields.size(); i++) { |
143 | 0 | FieldDescriptor field = fields.get(i); |
144 | |
|
145 | 0 | ModelWidgetBinding binding = field.getModelWidgetBinding(); |
146 | 0 | String fieldPath = path + QueryPath.getPathSeparator() + field.getFieldKey(); |
147 | 0 | if (binding != null) { |
148 | 0 | Widget w = field.getFieldWidget(); |
149 | 0 | binding.setWidgetValue(w, model, fieldPath); |
150 | 0 | } else { |
151 | 0 | GWT.log(field.getFieldKey() + " has no widget binding.", null); |
152 | |
} |
153 | |
} |
154 | 0 | for (Section s : section.getSections()) { |
155 | 0 | s.updateWidgetData(model); |
156 | |
} |
157 | |
|
158 | 0 | } |
159 | |
} |