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.binding;
17
18 import java.sql.Time;
19 import java.sql.Timestamp;
20 import java.util.Date;
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.Map;
24
25 import org.kuali.student.common.assembly.data.Data;
26 import org.kuali.student.common.assembly.data.Metadata;
27 import org.kuali.student.common.assembly.data.MetadataInterrogator;
28 import org.kuali.student.common.assembly.data.QueryPath;
29 import org.kuali.student.common.assembly.data.Data.BooleanValue;
30 import org.kuali.student.common.assembly.data.Data.DataValue;
31 import org.kuali.student.common.assembly.data.Data.DateValue;
32 import org.kuali.student.common.assembly.data.Data.DoubleValue;
33 import org.kuali.student.common.assembly.data.Data.FloatValue;
34 import org.kuali.student.common.assembly.data.Data.IntegerValue;
35 import org.kuali.student.common.assembly.data.Data.LongValue;
36 import org.kuali.student.common.assembly.data.Data.Property;
37 import org.kuali.student.common.assembly.data.Data.ShortValue;
38 import org.kuali.student.common.assembly.data.Data.StringValue;
39 import org.kuali.student.common.assembly.data.Data.TimeValue;
40 import org.kuali.student.common.assembly.data.Data.TimestampValue;
41 import org.kuali.student.common.assembly.data.Data.Value;
42 import org.kuali.student.common.ui.client.mvc.DataModel;
43 import org.kuali.student.common.ui.client.mvc.HasDataValue;
44 import org.kuali.student.common.ui.client.mvc.TranslatableValueWidget;
45 import org.kuali.student.common.ui.client.widgets.list.KSSelectedList;
46
47 import com.google.gwt.core.client.GWT;
48
49
50
51
52
53
54
55
56 public class HasDataValueBinding extends ModelWidgetBindingSupport<HasDataValue>{
57
58 public static HasDataValueBinding INSTANCE = new HasDataValueBinding();
59
60 private HasDataValueBinding(){}
61
62 @Override
63 public void setModelValue(HasDataValue widget, DataModel model, String path) {
64 QueryPath qPath = QueryPath.parse(path);
65 Value value = widget.getValue();
66 if (!nullsafeEquals(model.get(qPath), value)) {
67 setDirtyFlag(model, qPath);
68 }
69 if(value != null){
70 if (widget instanceof KSSelectedList ){
71
72
73 model.set(qPath, ((KSSelectedList)widget).getValueWithTranslations());
74 } else {
75 model.set(qPath, value);
76 }
77 }
78 }
79
80 @Override
81 public void setWidgetValue(HasDataValue widget, DataModel model, String path) {
82
83 QueryPath qPath = QueryPath.parse(path);
84 Object value = null;
85 if(model!=null){
86 value = model.get(qPath);
87 }
88
89
90 if (value != null && widget != null) {
91
92 if (value instanceof Data) {
93 DataValue dv = new DataValue((Data) value);
94 if (widget instanceof TranslatableValueWidget) {
95 Metadata fieldMetadata = model.getMetadata(qPath);
96 if (MetadataInterrogator.isRepeating(fieldMetadata)) {
97 Map<String, String> translations = new HashMap<String, String>();
98 Iterator<Property> iter = ((Data) value).iterator();
99 while (iter.hasNext()) {
100 Property p = iter.next();
101 if(!"_runtimeData".equals(p.getKey())){
102 QueryPath translationPath = new QueryPath();
103 translationPath.add(new Data.StringKey(qPath.toString()));
104 translationPath.add(new Data.StringKey("_runtimeData"));
105 translationPath.add(new Data.IntegerKey((Integer)p.getKey()));
106 translationPath.add(new Data.StringKey("id-translation"));
107 String translation = model.get(translationPath.toString());
108 String id = p.getValue().toString();
109 translations.put(id, translation);
110 }
111 }
112 ((TranslatableValueWidget)widget).setValue(translations);
113 }
114 } else {
115 widget.setValue(dv);
116 }
117 } else if (value instanceof String) {
118 if(widget instanceof TranslatableValueWidget) {
119 QueryPath translationPath = qPath.subPath(0, qPath.size()-1);
120 translationPath.add(new Data.StringKey("_runtimeData"));
121 translationPath.add(new Data.StringKey((String)qPath.get(qPath.size() - 1).get()));
122 translationPath.add(new Data.StringKey("id-translation"));
123
124 String translation = model.get(translationPath.toString());
125 if(translation != null && !translation.isEmpty()) {
126 ((TranslatableValueWidget)widget).setValue((String)value, translation);
127 } else {
128 widget.setValue(new StringValue((String)value));
129 }
130 } else {
131 widget.setValue(new StringValue((String)value));
132 }
133 }
134 else if(value instanceof Boolean){
135 widget.setValue(new BooleanValue((Boolean)value));
136 }
137 else if(value instanceof Integer){
138 widget.setValue(new IntegerValue((Integer)value));
139 }
140 else if(value instanceof Double){
141 widget.setValue(new DoubleValue((Double)value));
142 }
143 else if(value instanceof Float){
144 widget.setValue(new FloatValue((Float)value));
145 }
146 else if(value instanceof Date){
147 widget.setValue(new DateValue((Date)value));
148 }
149 else if(value instanceof Long){
150 widget.setValue(new LongValue((Long)value));
151 }
152 else if(value instanceof Short){
153 widget.setValue(new ShortValue((Short)value));
154 }
155 else if(value instanceof Time){
156 widget.setValue(new TimeValue((Time)value));
157 }
158 else if(value instanceof Timestamp){
159 widget.setValue(new TimestampValue((Timestamp)value));
160 }
161 else{
162 widget.setValue(null);
163 GWT.log("Warning: a valid Data.Value datatype was not provided in HasDataValueBinding setWidget", null);
164 }
165
166 } else if (widget != null) {
167 try {
168 widget.setValue(null);
169 } catch (RuntimeException e) {
170 GWT.log("Warning: Ignoring error attempting to setValue for " + widget.getClass().getName(), e);
171 }
172 }
173 }
174
175 }