| 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.Iterator; | 
  | 19 |  |   | 
  | 20 |  |  import org.kuali.student.common.assembly.data.Data; | 
  | 21 |  |  import org.kuali.student.common.assembly.data.Data.DataValue; | 
  | 22 |  |  import org.kuali.student.common.assembly.data.Data.Property; | 
  | 23 |  |  import org.kuali.student.common.assembly.data.Data.StringValue; | 
  | 24 |  |  import org.kuali.student.common.assembly.data.Data.Value; | 
  | 25 |  |  import org.kuali.student.common.assembly.data.QueryPath; | 
  | 26 |  |  import org.kuali.student.common.ui.client.mvc.DataModel; | 
  | 27 |  |  import org.kuali.student.common.ui.client.mvc.HasDataValue; | 
  | 28 |  |  import org.kuali.student.common.ui.client.widgets.list.KSSelectedList; | 
  | 29 |  |   | 
  | 30 |  |   | 
  | 31 |  |   | 
  | 32 |  |   | 
  | 33 |  |   | 
  | 34 |  |   | 
  | 35 |  |   | 
  | 36 |  |   | 
  | 37 |  |   | 
  | 38 |  |   | 
  | 39 | 0 |  public class DynamicAttributeListBinding extends ModelWidgetBindingSupport<HasDataValue>{ | 
  | 40 |  |   | 
  | 41 | 0 |          public static DynamicAttributeListBinding INSTANCE = new DynamicAttributeListBinding(); | 
  | 42 |  |           | 
  | 43 | 0 |          private DynamicAttributeListBinding(){} | 
  | 44 |  |           | 
  | 45 |  |          @Override | 
  | 46 |  |          public void setModelValue(HasDataValue widget, DataModel model, String path) { | 
  | 47 | 0 |                  QueryPath qPath = QueryPath.parse(path); | 
  | 48 | 0 |          Value value = widget.getValue(); | 
  | 49 | 0 |          if (!nullsafeEquals(model.get(qPath), value)) { | 
  | 50 | 0 |              setDirtyFlag(model, qPath); | 
  | 51 |  |          } | 
  | 52 | 0 |          if(value != null){                 | 
  | 53 |  |                  StringValue stringValue; | 
  | 54 | 0 |                  if (widget instanceof KSSelectedList ){ | 
  | 55 |  |                           | 
  | 56 |  |                           | 
  | 57 | 0 |                          stringValue = convertDataValueWithTranslationsToStringValue(((KSSelectedList)widget).getValueWithTranslations()); | 
  | 58 |  |                  } else { | 
  | 59 | 0 |                          stringValue = convertDataValueToStringValue((DataValue)value); | 
  | 60 |  |                  } | 
  | 61 |  |                   | 
  | 62 | 0 |                  model.set(qPath, stringValue);                 | 
  | 63 |  |          } | 
  | 64 | 0 |          } | 
  | 65 |  |           | 
  | 66 |  |          @Override | 
  | 67 |  |          public void setWidgetValue(HasDataValue widget, DataModel model, String path) { | 
  | 68 |  |                           | 
  | 69 | 0 |                  QueryPath qPath = QueryPath.parse(path); | 
  | 70 | 0 |                  Object value = null; | 
  | 71 | 0 |                  if(model!=null){ | 
  | 72 | 0 |                  value = model.get(qPath); | 
  | 73 |  |          } | 
  | 74 |  |                   | 
  | 75 | 0 |          if (value != null && widget != null && value instanceof String) { | 
  | 76 | 0 |                  DataValue dataValue = convertStringValueToDataValue((String)value); | 
  | 77 |  |                   | 
  | 78 | 0 |                  widget.setValue(dataValue); | 
  | 79 |  |          } | 
  | 80 | 0 |          } | 
  | 81 |  |   | 
  | 82 |  |          private StringValue convertDataValueWithTranslationsToStringValue( | 
  | 83 |  |                          Value valueWithTranslations) { | 
  | 84 |  |                   | 
  | 85 | 0 |                  return null; | 
  | 86 |  |          }         | 
  | 87 |  |           | 
  | 88 |  |           | 
  | 89 |  |   | 
  | 90 |  |   | 
  | 91 |  |   | 
  | 92 |  |   | 
  | 93 |  |   | 
  | 94 |  |          private StringValue convertDataValueToStringValue(DataValue dataValue) { | 
  | 95 | 0 |                  StringBuffer sbValue = new StringBuffer(); | 
  | 96 |  |                   | 
  | 97 | 0 |                  Data data = dataValue.get(); | 
  | 98 | 0 |                  Iterator<Property> propertyIterator = data.realPropertyIterator(); | 
  | 99 | 0 |                  while(propertyIterator.hasNext()){ | 
  | 100 | 0 |                          Property property = propertyIterator.next(); | 
  | 101 | 0 |                          String propertyValue = property.getValue(); | 
  | 102 | 0 |                          sbValue.append(","); | 
  | 103 | 0 |                          sbValue.append(propertyValue); | 
  | 104 | 0 |                  } | 
  | 105 |  |                   | 
  | 106 | 0 |                  StringValue stringValue = new StringValue(sbValue.toString().substring(1)); | 
  | 107 |  |                   | 
  | 108 | 0 |                  return stringValue;  | 
  | 109 |  |          } | 
  | 110 |  |   | 
  | 111 |  |   | 
  | 112 |  |           | 
  | 113 |  |   | 
  | 114 |  |   | 
  | 115 |  |   | 
  | 116 |  |   | 
  | 117 |  |   | 
  | 118 |  |   | 
  | 119 |  |          private DataValue convertStringValueToDataValue(String stringValue) { | 
  | 120 | 0 |                  Data data = new Data(); | 
  | 121 |  |                   | 
  | 122 | 0 |                  String[] stringValues = stringValue.split(","); | 
  | 123 | 0 |                  for (String value:stringValues){ | 
  | 124 | 0 |                          data.add(value); | 
  | 125 |  |                  } | 
  | 126 |  |                   | 
  | 127 | 0 |                  DataValue dataValue = new DataValue(data); | 
  | 128 |  |                   | 
  | 129 | 0 |                  return dataValue; | 
  | 130 |  |          } | 
  | 131 |  |   | 
  | 132 |  |  } |