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