Coverage Report - org.kuali.student.common.ui.client.configurable.mvc.binding.DynamicAttributeListBinding
 
Classes in this File Line Coverage Branch Coverage Complexity
DynamicAttributeListBinding
0%
0/39
0%
0/18
2.5
 
 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.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  
  * Binding which can be used to bind a comma separated dynamic attribute list with KSAbstractSelectItem widgets
 32  
  * List widgets only work with lists represented by a DataValue in the data model.  This binding converts the
 33  
  * list values between a DataValue expected by the widget to/from comma separated string value expected by the
 34  
  * model when using a dynamic attribute.  
 35  
  * 
 36  
  * @author Kuali Student Team
 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  
                         //This gets a value with _runtimeData translations for all selected items,
 56  
                         //otherwise translations get lost and KSSelectedList would have to lookup values via a search call.
 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  
                 // TODO Auto-generated method stub
 85  0
                 return null;
 86  
         }        
 87  
         
 88  
         /**
 89  
          * Converts a list represented by DataValue to a list of values as a comma delimited StringValue
 90  
 
 91  
          * @param dataValue DataValue representing a list object
 92  
          * @return the list converted to a comma delimited StringValue
 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  
          * Converts a list represented by a comma delimited string so to a DataValue so it can be added used to
 114  
          * set into a list widget.
 115  
          * 
 116  
          * @param stringValue a comma separated list of values
 117  
          * @return DataValue representation of stringValue
 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  
 }