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