| 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.util.Date; | 
  | 19 |  |   | 
  | 20 |  |  import org.kuali.student.common.assembly.data.QueryPath; | 
  | 21 |  |  import org.kuali.student.common.assembly.data.Data.DataType; | 
  | 22 |  |  import org.kuali.student.common.ui.client.mvc.DataModel; | 
  | 23 |  |  import org.kuali.student.common.ui.client.validator.ClientDateParser; | 
  | 24 |  |   | 
  | 25 |  |  import com.google.gwt.core.client.GWT; | 
  | 26 |  |  import com.google.gwt.user.client.ui.HasText; | 
  | 27 |  |   | 
  | 28 |  |   | 
  | 29 |  |   | 
  | 30 |  |   | 
  | 31 |  |   | 
  | 32 |  |   | 
  | 33 |  |   | 
  | 34 |  |   | 
  | 35 |  |   | 
  | 36 | 0 |  public class HasTextBinding extends ModelWidgetBindingSupport<HasText> { | 
  | 37 | 0 |      public static HasTextBinding INSTANCE = new HasTextBinding(); | 
  | 38 |  |   | 
  | 39 | 0 |      private ClientDateParser dateParser = new ClientDateParser(); | 
  | 40 |  |   | 
  | 41 | 0 |      private HasTextBinding() {} | 
  | 42 |  |   | 
  | 43 |  |      @Override | 
  | 44 |  |      public void setModelValue(HasText object, DataModel model, String path) { | 
  | 45 |  |          try { | 
  | 46 | 0 |              QueryPath qPath = QueryPath.parse(path); | 
  | 47 | 0 |              DataType type = model.getType(qPath); | 
  | 48 |  |               | 
  | 49 | 0 |              String newValue = null; | 
  | 50 | 0 |              if(object.getText() != null) { | 
  | 51 | 0 |                  newValue = object.getText().trim(); | 
  | 52 |  |              } | 
  | 53 |  |   | 
  | 54 |  |               | 
  | 55 | 0 |              boolean skipUpdatingModel = model.get(qPath) == null && (newValue == null || newValue.isEmpty()); | 
  | 56 |  |   | 
  | 57 | 0 |              if (!skipUpdatingModel){ | 
  | 58 |  |                      try { | 
  | 59 | 0 |                          switch (type) { | 
  | 60 |  |                              case STRING: | 
  | 61 | 0 |                                  if (!nullsafeEquals(model.get(qPath), newValue)) { | 
  | 62 | 0 |                                      model.set(qPath, newValue); | 
  | 63 | 0 |                                      setDirtyFlag(model, qPath); | 
  | 64 |  |                                  } | 
  | 65 |  |                                  break; | 
  | 66 |  |                              case INTEGER: | 
  | 67 | 0 |                                          if(newValue != null && newValue.isEmpty()){ | 
  | 68 | 0 |                                                  Integer value = null; | 
  | 69 | 0 |                                                  model.set(qPath, value); | 
  | 70 | 0 |                                                  setDirtyFlag(model, qPath); | 
  | 71 | 0 |                                          } | 
  | 72 |  |                                          else{ | 
  | 73 | 0 |                                      int intValue = Integer.parseInt(newValue); | 
  | 74 | 0 |                                      if (!nullsafeEquals(model.get(qPath), intValue)) { | 
  | 75 | 0 |                                          model.set(qPath, intValue); | 
  | 76 | 0 |                                          setDirtyFlag(model, qPath); | 
  | 77 |  |                                      } | 
  | 78 |  |                                          } | 
  | 79 | 0 |                                  break; | 
  | 80 |  |                              case LONG: | 
  | 81 | 0 |                                  long longValue = Long.parseLong(newValue); | 
  | 82 | 0 |                                  if (!nullsafeEquals(model.get(qPath), longValue)) { | 
  | 83 | 0 |                                      model.set(qPath, longValue); | 
  | 84 | 0 |                                      setDirtyFlag(model, qPath); | 
  | 85 |  |                                  } | 
  | 86 |  |                                  break; | 
  | 87 |  |                              case FLOAT: | 
  | 88 | 0 |                                  float floatValue = Float.parseFloat(newValue); | 
  | 89 | 0 |                                  if (!nullsafeEquals(model.get(qPath), floatValue)) { | 
  | 90 | 0 |                                      model.set(qPath, floatValue); | 
  | 91 | 0 |                                      setDirtyFlag(model, qPath); | 
  | 92 |  |                                  } | 
  | 93 |  |                                  break; | 
  | 94 |  |                              case DOUBLE: | 
  | 95 | 0 |                                  double doubleValue = Double.parseDouble(newValue); | 
  | 96 | 0 |                                  if (!nullsafeEquals(model.get(qPath), doubleValue)) { | 
  | 97 | 0 |                                      model.set(qPath, doubleValue); | 
  | 98 | 0 |                                      setDirtyFlag(model, qPath); | 
  | 99 |  |                                  } | 
  | 100 |  |                                  break; | 
  | 101 |  |                              case BOOLEAN: | 
  | 102 | 0 |                                  if (newValue.equalsIgnoreCase("true") || newValue.equalsIgnoreCase("false")) { | 
  | 103 | 0 |                                      boolean booleanValue = Boolean.parseBoolean(newValue); | 
  | 104 | 0 |                                      if (!nullsafeEquals(model.get(qPath), booleanValue)) { | 
  | 105 | 0 |                                          model.set(qPath, booleanValue); | 
  | 106 | 0 |                                          setDirtyFlag(model, qPath); | 
  | 107 |  |                                      } | 
  | 108 | 0 |                                  } else { | 
  | 109 | 0 |                                      throw new UnsupportedOperationException("BooleanTypes can only be set with true or false"); | 
  | 110 |  |                                  } | 
  | 111 |  |                                  break; | 
  | 112 |  |                              case DATE: | 
  | 113 | 0 |                                  Date dateValue = dateParser.parseDate(newValue); | 
  | 114 | 0 |                                  if (!nullsafeEquals(model.get(qPath), dateValue)) { | 
  | 115 | 0 |                                      model.set(qPath, dateValue); | 
  | 116 | 0 |                                      setDirtyFlag(model, qPath); | 
  | 117 |  |                                  } | 
  | 118 |  |                                  break; | 
  | 119 |  |                          } | 
  | 120 | 0 |                      } catch (Exception e) { | 
  | 121 | 0 |                          GWT.log("Unable to coerce type for " + path + ", falling back to String", e); | 
  | 122 | 0 |                          model.set(qPath, newValue); | 
  | 123 | 0 |                      } | 
  | 124 |  |              } | 
  | 125 | 0 |          } catch (Exception e) { | 
  | 126 | 0 |              GWT.log("Error setting model value for: " + path, e); | 
  | 127 | 0 |          } | 
  | 128 | 0 |      } | 
  | 129 |  |   | 
  | 130 |  |      @Override | 
  | 131 |  |      public void setWidgetValue(HasText object, DataModel model, String path) { | 
  | 132 |  |          try { | 
  | 133 | 0 |              QueryPath qPath = QueryPath.parse(path); | 
  | 134 |  |               | 
  | 135 | 0 |              Object value = null; | 
  | 136 | 0 |              if(model!=null){ | 
  | 137 | 0 |                      value = model.get(qPath); | 
  | 138 |  |              } | 
  | 139 |  |   | 
  | 140 | 0 |              if (value != null && object != null) { | 
  | 141 | 0 |                  if (value instanceof Date) { | 
  | 142 | 0 |                      object.setText(dateParser.toString((Date) value)); | 
  | 143 |  |                  } else { | 
  | 144 | 0 |                      object.setText(value.toString()); | 
  | 145 |  |                  } | 
  | 146 | 0 |              } else if (value == null && object != null) { | 
  | 147 | 0 |                  object.setText(""); | 
  | 148 |  |              } | 
  | 149 | 0 |          } catch (Exception e) { | 
  | 150 | 0 |              GWT.log("Error setting widget value for: " + path, e); | 
  | 151 | 0 |          } | 
  | 152 | 0 |      } | 
  | 153 |  |   | 
  | 154 |  |  } |