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