View Javadoc

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  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          GWT.log("Method setModelValue not implemented for MultiplicityTable");
44      }
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          table.initTable();
53  
54          path = path.trim();
55          if (path.startsWith(QueryPath.getPathSeparator())) {
56              path = path.substring(QueryPath.getPathSeparator().length());
57          }
58  
59          QueryPath qPath = QueryPath.parse(path);
60          Data data = null;
61          if (model != null) {
62              data = model.get(qPath);
63          }
64  
65          if (data != null) {
66              Iterator<Data.Property> iter1 = data.iterator();
67              if (iter1.hasNext()) {
68                  table.buildHeaders();
69  
70                  // iterate through data
71                  while (iter1.hasNext()) {
72                      Data.Property prop = iter1.next();
73                      Object value = prop.getValue();
74  
75                      if (value instanceof String) {
76  
77                          Metadata metadata = model.getMetadata(qPath);
78                          String dataValue = null;
79                          if(metadata!=null&&metadata.getInitialLookup()!=null){
80                              QueryPath translatedPath = QueryPath.concat("_runtimeData", prop.getKey().toString(), "id-translation");
81                              dataValue = data.query(translatedPath);
82                          }
83  
84                          if (dataValue == null)
85                              dataValue = (String)value;
86                          
87                          table.addNextCell(dataValue);                        
88                          table.nextRow();
89                      }
90                      else {
91                          Data rowData = prop.getValue();
92  
93                          // iterate through the fields defined for this table
94                          for (Integer row  : table.getConfig().getFields().keySet()) {
95                              List<MultiplicityFieldConfiguration> fieldConfigs = table.getConfig().getFields().get(row);
96                              for (MultiplicityFieldConfiguration fieldConfig : fieldConfigs) {
97  
98                                  QueryPath fullPath = QueryPath.parse(fieldConfig.getFieldPath());
99                                  QueryPath fieldPath = translatePath(path, fullPath, model);                           
100 
101                                 Object o = rowData.query(fieldPath);
102                                 if (o != null) {
103                                     // multiple values required in the table cell, concatenate values
104                                     // with comma separator
105                                     if (o instanceof Data) {
106                                         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                                         if (cellData != null && cellData.size() > 0) {
110                                             StringBuilder sb = new StringBuilder();
111                                             Iterator<Data.Property> iter = cellData.realPropertyIterator();
112                                             while (iter.hasNext()) {
113                                                 Data.Property p = iter.next();
114                                                 Data d = p.getValue();
115                                                 String key = table.getConfig().getConcatenatedFields().get(fullPath.toString());
116                                                 sb.append(d.get(key)).append(", ");
117                                             }
118                                             sb.deleteCharAt(sb.lastIndexOf(", "));
119                                             table.addNextCell((sb.toString()));
120                                         } else {
121                                             table.addEmptyCell();
122                                         }
123                                     }
124                                     // a single value required in a single table cell
125                                     else {
126                                         table.addNextCell((o.toString()));
127                                     }
128                                 } else {
129                                     table.addEmptyCell();
130                                 }                        	
131                             }
132                             table.nextRow();
133                         }
134                     }
135                 }
136             }
137         }
138     }
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         QueryPath parentPath = QueryPath.parse(path);
152         int i = parentPath.size();
153 
154         Metadata metadata = model.getMetadata(fullPath);
155         QueryPath fieldPath = null;
156 
157         if(metadata!=null&&metadata.getInitialLookup()!=null){
158             if (metadata.getDataType().equals(DataType.STRING)) {
159                 QueryPath translatedPath = fullPath.subPath(0, fullPath.size()-1);
160                 translatedPath.add(new Data.StringKey("_runtimeData"));
161                 translatedPath.add(new Data.StringKey((String)fullPath.get(fullPath.size() - 1).get()));
162                 translatedPath.add(new Data.StringKey("id-translation"));
163                 fieldPath  =  translatedPath.subPath(i, translatedPath.size());
164             }
165             else {
166                 fieldPath =  fullPath.subPath(i, fullPath.size());
167             }
168         }
169         else {
170             fieldPath =  fullPath.subPath(i, fullPath.size());
171         }
172 
173         if (fieldPath.get(0).toString().equals(QueryPath.getWildCard())) {
174             fieldPath.remove(0);
175         }
176         return fieldPath;
177     }
178 }