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  
 /**
 50  
  * Model widget binding for HasDataValue widgets.  These are widgets which deal with KS data types
 51  
  * directly.
 52  
  * 
 53  
  * @author Kuali Student Team
 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  
                         //This gets a value with _runtimeData translations for all selected items,
 72  
                         //otherwise translations get lost and KSSelectedList would have to lookup values via a search call.
 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  
 }