Coverage Report - org.kuali.student.common.ui.client.configurable.mvc.binding.HasDataValueBinding
 
Classes in this File Line Coverage Branch Coverage Complexity
HasDataValueBinding
0%
0/77
0%
0/50
9.667
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 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.ui.client.mvc.DataModel;
 26  
 import org.kuali.student.common.ui.client.mvc.HasDataValue;
 27  
 import org.kuali.student.common.ui.client.mvc.TranslatableValueWidget;
 28  
 import org.kuali.student.common.ui.client.widgets.list.KSSelectedList;
 29  
 import org.kuali.student.core.assembly.data.Data;
 30  
 import org.kuali.student.core.assembly.data.Metadata;
 31  
 import org.kuali.student.core.assembly.data.MetadataInterrogator;
 32  
 import org.kuali.student.core.assembly.data.QueryPath;
 33  
 import org.kuali.student.core.assembly.data.Data.BooleanValue;
 34  
 import org.kuali.student.core.assembly.data.Data.DataValue;
 35  
 import org.kuali.student.core.assembly.data.Data.DateValue;
 36  
 import org.kuali.student.core.assembly.data.Data.DoubleValue;
 37  
 import org.kuali.student.core.assembly.data.Data.FloatValue;
 38  
 import org.kuali.student.core.assembly.data.Data.IntegerValue;
 39  
 import org.kuali.student.core.assembly.data.Data.LongValue;
 40  
 import org.kuali.student.core.assembly.data.Data.Property;
 41  
 import org.kuali.student.core.assembly.data.Data.ShortValue;
 42  
 import org.kuali.student.core.assembly.data.Data.StringValue;
 43  
 import org.kuali.student.core.assembly.data.Data.TimeValue;
 44  
 import org.kuali.student.core.assembly.data.Data.TimestampValue;
 45  
 import org.kuali.student.core.assembly.data.Data.Value;
 46  
 
 47  
 import com.google.gwt.core.client.GWT;
 48  
 
 49  0
 public class HasDataValueBinding extends ModelWidgetBindingSupport<HasDataValue>{
 50  
 
 51  0
         public static HasDataValueBinding INSTANCE = new HasDataValueBinding();
 52  
         
 53  0
         private HasDataValueBinding(){}
 54  
         
 55  
         @Override
 56  
         public void setModelValue(HasDataValue widget, DataModel model, String path) {
 57  0
                 QueryPath qPath = QueryPath.parse(path);
 58  0
         Value value = widget.getValue();
 59  0
         if (!nullsafeEquals(model.get(qPath), value)) {
 60  0
             setDirtyFlag(model, qPath);
 61  
         }
 62  0
         if(value != null){
 63  0
                 if (widget instanceof KSSelectedList ){
 64  
                         //This gets a value with _runtimeData translations for all selected items,
 65  
                         //otherwise translations get lost and KSSelectedList would have to lookup values via a search call.
 66  0
                         model.set(qPath, ((KSSelectedList)widget).getValueWithTranslations());
 67  
                 } else {
 68  0
                         model.set(qPath, value);
 69  
                 }
 70  
         }
 71  0
         }
 72  
 
 73  
         @Override
 74  
         public void setWidgetValue(HasDataValue widget, DataModel model, String path) {
 75  
                         
 76  0
                 QueryPath qPath = QueryPath.parse(path);
 77  0
                 Object value = null;
 78  0
                 if(model!=null){
 79  0
                 value = model.get(qPath);
 80  
         }
 81  
         
 82  
         
 83  0
         if (value != null && widget != null) {
 84  
            
 85  0
             if (value instanceof Data) {
 86  0
                 DataValue dv = new DataValue((Data) value);
 87  0
                 if (widget instanceof TranslatableValueWidget) {
 88  0
                     Metadata fieldMetadata = model.getMetadata(qPath);
 89  0
                     if (MetadataInterrogator.isRepeating(fieldMetadata)) {
 90  0
                         Map<String, String> translations = new HashMap<String, String>();
 91  0
                         Iterator<Property> iter = ((Data) value).iterator();
 92  0
                         while (iter.hasNext()) {
 93  0
                             Property p = iter.next();
 94  0
                             if(!"_runtimeData".equals(p.getKey())){
 95  0
                                     QueryPath translationPath = new QueryPath();
 96  0
                                     translationPath.add(new Data.StringKey(qPath.toString()));
 97  0
                                     translationPath.add(new Data.StringKey("_runtimeData"));
 98  0
                                     translationPath.add(new Data.IntegerKey((Integer)p.getKey()));
 99  0
                                     translationPath.add(new Data.StringKey("id-translation"));
 100  0
                                     String translation = model.get(translationPath.toString());
 101  0
                                     String id = p.getValue().toString();
 102  0
                                     translations.put(id, translation);
 103  
                             }
 104  0
                         }
 105  0
                         ((TranslatableValueWidget)widget).setValue(translations);
 106  
                     }
 107  0
                 } else {
 108  0
                     widget.setValue(dv);
 109  
                 }
 110  0
             } else if (value instanceof String) {
 111  0
                     if(widget instanceof TranslatableValueWidget) {
 112  0
                             QueryPath translationPath = qPath.subPath(0, qPath.size()-1);
 113  0
                         translationPath.add(new Data.StringKey("_runtimeData"));
 114  0
                         translationPath.add(new Data.StringKey((String)qPath.get(qPath.size() - 1).get()));
 115  0
                         translationPath.add(new Data.StringKey("id-translation"));
 116  
                         
 117  0
                         String translation = model.get(translationPath.toString());
 118  0
                         if(translation != null && !translation.isEmpty()) {
 119  0
                             ((TranslatableValueWidget)widget).setValue((String)value, translation);
 120  
                         } else {
 121  0
                             widget.setValue(new StringValue((String)value));
 122  
                         }
 123  0
                     } else {
 124  0
                             widget.setValue(new StringValue((String)value));
 125  
                     }
 126  
             }
 127  0
             else if(value instanceof Boolean){
 128  0
                     widget.setValue(new BooleanValue((Boolean)value));
 129  
             }
 130  0
             else if(value instanceof Integer){
 131  0
                     widget.setValue(new IntegerValue((Integer)value));
 132  
             }
 133  0
             else if(value instanceof Double){
 134  0
                     widget.setValue(new DoubleValue((Double)value));
 135  
             }
 136  0
             else if(value instanceof Float){
 137  0
                     widget.setValue(new FloatValue((Float)value));
 138  
             }
 139  0
             else if(value instanceof Date){
 140  0
                     widget.setValue(new DateValue((Date)value));
 141  
             }
 142  0
             else if(value instanceof Long){
 143  0
                     widget.setValue(new LongValue((Long)value));
 144  
             }
 145  0
             else if(value instanceof Short){
 146  0
                     widget.setValue(new ShortValue((Short)value));
 147  
             }
 148  0
             else if(value instanceof Time){
 149  0
                     widget.setValue(new TimeValue((Time)value));
 150  
             }
 151  0
             else if(value instanceof Timestamp){
 152  0
                     widget.setValue(new TimestampValue((Timestamp)value));
 153  
             }
 154  
             else{
 155  0
                     widget.setValue(null);
 156  0
                     GWT.log("Warning: a valid Data.Value datatype was not provided in HasDataValueBinding setWidget", null);
 157  
             }
 158  
 
 159  0
         } else if (widget != null) {
 160  
             try {
 161  0
                 widget.setValue(null);
 162  0
             } catch (RuntimeException e) {
 163  0
                 GWT.log("Warning: Ignoring error attempting to setValue for " + widget.getClass().getName(), e);
 164  0
             }
 165  
         }
 166  0
         }
 167  
 
 168  
 }