Coverage Report - org.kuali.student.common.ui.client.configurable.mvc.binding.MultiplicityTableBinding
 
Classes in this File Line Coverage Branch Coverage Complexity
MultiplicityTableBinding
0%
0/77
0%
0/40
7.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  
  * <p/>
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  * <p/>
 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  
  * <p/>
 15  
  */
 16  
 package org.kuali.student.common.ui.client.configurable.mvc.binding;
 17  
 
 18  
 import java.util.Iterator;
 19  
 import java.util.List;
 20  
 
 21  
 import org.kuali.student.common.assembly.data.Data;
 22  
 import org.kuali.student.common.assembly.data.Metadata;
 23  
 import org.kuali.student.common.assembly.data.QueryPath;
 24  
 import org.kuali.student.common.assembly.data.Data.DataType;
 25  
 import org.kuali.student.common.ui.client.configurable.mvc.multiplicity.MultiplicityFieldConfiguration;
 26  
 import org.kuali.student.common.ui.client.configurable.mvc.multiplicity.MultiplicityTable;
 27  
 import org.kuali.student.common.ui.client.mvc.DataModel;
 28  
 
 29  
 import com.google.gwt.core.client.GWT;
 30  
 
 31  
 /**
 32  
  * This class handles transferring data between the model and the widgets 
 33  
  */
 34  0
 public class MultiplicityTableBinding extends ModelWidgetBindingSupport<MultiplicityTable> {
 35  
 
 36  
 
 37  
     /**
 38  
      * @see ModelWidgetBindingSupport#setModelValue(Object,
 39  
      *      org.kuali.student.common.ui.client.mvc.DataModel, String)
 40  
      */
 41  
     public void setModelValue(MultiplicityTable table, DataModel model, String path) {
 42  
         // Not required  - MultiplicityTable is read only
 43  0
         GWT.log("Method setModelValue not implemented for MultiplicityTable");
 44  0
     }
 45  
 
 46  
 
 47  
     /**
 48  
      * @see ModelWidgetBindingSupport#setWidgetValue(Object,
 49  
      *      org.kuali.student.common.ui.client.mvc.DataModel, String)
 50  
      */
 51  
     public void setWidgetValue(MultiplicityTable table, DataModel model, String path) {
 52  0
         table.initTable();
 53  
 
 54  0
         path = path.trim();
 55  0
         if (path.startsWith(QueryPath.getPathSeparator())) {
 56  0
             path = path.substring(QueryPath.getPathSeparator().length());
 57  
         }
 58  
 
 59  0
         QueryPath qPath = QueryPath.parse(path);
 60  0
         Data data = null;
 61  0
         if (model != null) {
 62  0
             data = model.get(qPath);
 63  
         }
 64  
 
 65  0
         if (data != null) {
 66  0
             Iterator<Data.Property> iter1 = data.iterator();
 67  0
             if (iter1.hasNext()) {
 68  0
                 table.buildHeaders();
 69  
 
 70  
                 // iterate through data
 71  0
                 while (iter1.hasNext()) {
 72  0
                     Data.Property prop = iter1.next();
 73  0
                     Object value = prop.getValue();
 74  
 
 75  0
                     if (value instanceof String) {
 76  
 
 77  0
                         Metadata metadata = model.getMetadata(qPath);
 78  0
                         String dataValue = null;
 79  0
                         if(metadata!=null&&metadata.getInitialLookup()!=null){
 80  0
                             QueryPath translatedPath = QueryPath.concat("_runtimeData", prop.getKey().toString(), "id-translation");
 81  0
                             dataValue = data.query(translatedPath);
 82  
                         }
 83  
 
 84  0
                         if (dataValue == null)
 85  0
                             dataValue = (String)value;
 86  
                         
 87  0
                         table.addNextCell(dataValue);                        
 88  0
                         table.nextRow();
 89  0
                     }
 90  
                     else {
 91  0
                         Data rowData = prop.getValue();
 92  
 
 93  
                         // iterate through the fields defined for this table
 94  0
                         for (Integer row  : table.getConfig().getFields().keySet()) {
 95  0
                             List<MultiplicityFieldConfiguration> fieldConfigs = table.getConfig().getFields().get(row);
 96  0
                             for (MultiplicityFieldConfiguration fieldConfig : fieldConfigs) {
 97  
 
 98  0
                                 QueryPath fullPath = QueryPath.parse(fieldConfig.getFieldPath());
 99  0
                                 QueryPath fieldPath = translatePath(path, fullPath, model);                           
 100  
 
 101  0
                                 Object o = rowData.query(fieldPath);
 102  0
                                 if (o != null) {
 103  
                                     // multiple values required in the table cell, concatenate values
 104  
                                     // with comma separator
 105  0
                                     if (o instanceof Data) {
 106  0
                                         Data cellData = (Data) o;
 107  
                                         // iterate through the field keys to produce a comma delimited list
 108  
                                         // of values in a single cell of the table
 109  0
                                         if (cellData != null && cellData.size() > 0) {
 110  0
                                             StringBuilder sb = new StringBuilder();
 111  0
                                             Iterator<Data.Property> iter = cellData.realPropertyIterator();
 112  0
                                             while (iter.hasNext()) {
 113  0
                                                 Data.Property p = iter.next();
 114  0
                                                 Data d = p.getValue();
 115  0
                                                 String key = table.getConfig().getConcatenatedFields().get(fullPath.toString());
 116  0
                                                 sb.append(d.get(key)).append(", ");
 117  0
                                             }
 118  0
                                             sb.deleteCharAt(sb.lastIndexOf(", "));
 119  0
                                             table.addNextCell((sb.toString()));
 120  0
                                         } else {
 121  0
                                             table.addEmptyCell();
 122  
                                         }
 123  0
                                     }
 124  
                                     // a single value required in a single table cell
 125  
                                     else {
 126  0
                                         table.addNextCell((o.toString()));
 127  
                                     }
 128  
                                 } else {
 129  0
                                     table.addEmptyCell();
 130  
                                 }                                
 131  0
                             }
 132  0
                             table.nextRow();
 133  0
                         }
 134  
                     }
 135  0
                 }
 136  
             }
 137  
         }
 138  0
     }
 139  
 
 140  
     /**
 141  
      * 
 142  
      * This method checks the meta data for an initial lookup for this field. If found, the field path
 143  
      * is translated to lookup the id_translation data in the _runtimeData structure in the model
 144  
      * 
 145  
      * @param path
 146  
      * @param fullPath
 147  
      * @param model
 148  
      * @return
 149  
      */
 150  
     private QueryPath translatePath(String path, QueryPath fullPath, DataModel model) {
 151  0
         QueryPath parentPath = QueryPath.parse(path);
 152  0
         int i = parentPath.size();
 153  
 
 154  0
         Metadata metadata = model.getMetadata(fullPath);
 155  0
         QueryPath fieldPath = null;
 156  
 
 157  0
         if(metadata!=null&&metadata.getInitialLookup()!=null){
 158  0
             if (metadata.getDataType().equals(DataType.STRING)) {
 159  0
                 QueryPath translatedPath = fullPath.subPath(0, fullPath.size()-1);
 160  0
                 translatedPath.add(new Data.StringKey("_runtimeData"));
 161  0
                 translatedPath.add(new Data.StringKey((String)fullPath.get(fullPath.size() - 1).get()));
 162  0
                 translatedPath.add(new Data.StringKey("id-translation"));
 163  0
                 fieldPath  =  translatedPath.subPath(i, translatedPath.size());
 164  0
             }
 165  
             else {
 166  0
                 fieldPath =  fullPath.subPath(i, fullPath.size());
 167  
             }
 168  
         }
 169  
         else {
 170  0
             fieldPath =  fullPath.subPath(i, fullPath.size());
 171  
         }
 172  
 
 173  0
         if (fieldPath.get(0).toString().equals(QueryPath.getWildCard())) {
 174  0
             fieldPath.remove(0);
 175  
         }
 176  0
         return fieldPath;
 177  
     }
 178  
 }