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