| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |  package org.kuali.student.common.ui.client.configurable.mvc.sections; | 
  | 17 |  |   | 
  | 18 |  |  import java.util.Date; | 
  | 19 |  |  import java.util.HashMap; | 
  | 20 |  |  import java.util.List; | 
  | 21 |  |  import java.util.Map; | 
  | 22 |  |   | 
  | 23 |  |  import org.kuali.student.common.assembly.data.Data; | 
  | 24 |  |  import org.kuali.student.common.assembly.data.QueryPath; | 
  | 25 |  |  import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor; | 
  | 26 |  |  import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptorReadOnly; | 
  | 27 |  |  import org.kuali.student.common.ui.client.configurable.mvc.binding.ModelWidgetBinding; | 
  | 28 |  |  import org.kuali.student.common.ui.client.configurable.mvc.binding.ModelWidgetBindingSupport; | 
  | 29 |  |  import org.kuali.student.common.ui.client.mvc.DataModel; | 
  | 30 |  |   | 
  | 31 |  |  import com.google.gwt.core.client.GWT; | 
  | 32 |  |  import com.google.gwt.user.client.ui.Widget; | 
  | 33 |  |   | 
  | 34 |  |   | 
  | 35 |  |   | 
  | 36 |  |   | 
  | 37 |  |   | 
  | 38 |  |   | 
  | 39 |  |   | 
  | 40 |  |   | 
  | 41 | 0 |  public class SectionBinding extends ModelWidgetBindingSupport<Section> { | 
  | 42 | 0 |          public static SectionBinding INSTANCE = new SectionBinding(); | 
  | 43 |  |   | 
  | 44 | 0 |      private SectionBinding() {}; | 
  | 45 |  |   | 
  | 46 |  |       | 
  | 47 |  |   | 
  | 48 |  |   | 
  | 49 |  |   | 
  | 50 |  |      @Override | 
  | 51 |  |      public void setModelValue(Section section, DataModel model, String path) { | 
  | 52 |  |           | 
  | 53 | 0 |          List<FieldDescriptor> fields = section.getUnnestedFields(); | 
  | 54 | 0 |          Map<String, Object> selectedData = null; | 
  | 55 | 0 |          for (int i = 0; i < fields.size(); i++) { | 
  | 56 | 0 |                  if(!(fields.get(i) instanceof FieldDescriptorReadOnly)){ | 
  | 57 | 0 |                          FieldDescriptor field = (FieldDescriptor) fields.get(i); | 
  | 58 |  |               | 
  | 59 | 0 |                      String fieldPath = path + QueryPath.getPathSeparator() + field.getFieldKey(); | 
  | 60 | 0 |                      fieldPath = fieldPath.trim(); | 
  | 61 | 0 |                      if (fieldPath.startsWith(QueryPath.getPathSeparator())) { | 
  | 62 | 0 |                          fieldPath = fieldPath.substring(QueryPath.getPathSeparator().length()); | 
  | 63 |  |                      } | 
  | 64 | 0 |                      ModelWidgetBinding binding = field.getModelWidgetBinding(); | 
  | 65 | 0 |                      if (binding != null) { | 
  | 66 | 0 |                          Widget w = field.getFieldWidget(); | 
  | 67 | 0 |                          binding.setModelValue(w, model, fieldPath); | 
  | 68 | 0 |                      } else { | 
  | 69 | 0 |                          GWT.log(field.getFieldKey() + " has no widget binding.", null); | 
  | 70 |  |                      } | 
  | 71 |  |              } | 
  | 72 |  |          } | 
  | 73 |  |           | 
  | 74 | 0 |          if(section instanceof HasSectionDeletion){ | 
  | 75 | 0 |                  List<Section> deleted =((HasSectionDeletion) section).getDeletedSections(); | 
  | 76 | 0 |                  for(int i =0; i < deleted.size(); i++){ | 
  | 77 | 0 |                          Section deletedSection = deleted.get(i); | 
  | 78 |  |                           | 
  | 79 | 0 |                          List<FieldDescriptor> deletedFields = deletedSection.getFields(); | 
  | 80 | 0 |                          for(int j = 0; j < deletedFields.size(); j++){ | 
  | 81 | 0 |                                  FieldDescriptor field = deletedFields.get(j); | 
  | 82 | 0 |                                  model.remove(QueryPath.parse(field.getFieldKey())); | 
  | 83 |  |                          } | 
  | 84 |  |                  } | 
  | 85 |  |          } | 
  | 86 |  |           | 
  | 87 | 0 |          for (Section s : section.getSections()) { | 
  | 88 |  |               | 
  | 89 | 0 |                  if(section instanceof HasSectionDeletion){ | 
  | 90 | 0 |                          List<Section> deleted =((HasSectionDeletion) section).getDeletedSections(); | 
  | 91 | 0 |                          List<String> deletionParentKeys = ((HasSectionDeletion) section).getDeletionParentKeys(); | 
  | 92 | 0 |                          if (deletionParentKeys != null) { | 
  | 93 | 0 |                              for (String deletionParentKey : deletionParentKeys) { | 
  | 94 | 0 |                                  model.remove(QueryPath.parse(deletionParentKey)); | 
  | 95 |  |                              } | 
  | 96 |  |                          } | 
  | 97 |  |                           | 
  | 98 |  |                           | 
  | 99 | 0 |                          if(!deleted.contains(s)){ | 
  | 100 | 0 |                                  s.updateModel(model); | 
  | 101 | 0 |                      if (deletionParentKeys != null) { | 
  | 102 | 0 |                          for (String deletionParentKey : deletionParentKeys) { | 
  | 103 | 0 |                              selectedData = (selectedData == null)?  | 
  | 104 |  |                                      new HashMap<String, Object>() : selectedData; | 
  | 105 | 0 |                              selectedData.put(deletionParentKey, model.get(deletionParentKey)); | 
  | 106 |  |                          } | 
  | 107 |  |                      } | 
  | 108 | 0 |                          } else if (selectedData != null && !selectedData.isEmpty()) { | 
  | 109 | 0 |                              for (String key : selectedData.keySet()) { | 
  | 110 | 0 |                                  setValue(model, key, selectedData.get(key)); | 
  | 111 |  |                              } | 
  | 112 |  |                          } | 
  | 113 | 0 |                  } | 
  | 114 |  |                  else{ | 
  | 115 | 0 |                          s.updateModel(model); | 
  | 116 |  |                  }       | 
  | 117 |  |          } | 
  | 118 | 0 |      } | 
  | 119 |  |       | 
  | 120 |  |      private void setValue(DataModel model, String path, Object value) { | 
  | 121 | 0 |          QueryPath qPath = QueryPath.parse(path); | 
  | 122 | 0 |          if (value instanceof String) { | 
  | 123 | 0 |              model.set(qPath, (String) value); | 
  | 124 | 0 |          } else if (value instanceof Integer) { | 
  | 125 | 0 |              model.set(qPath, (Integer) value); | 
  | 126 | 0 |          } else if (value instanceof Long) { | 
  | 127 | 0 |              model.set(qPath, (Long) value); | 
  | 128 | 0 |          } else if (value instanceof Float) { | 
  | 129 | 0 |              model.set(qPath, (Float) value); | 
  | 130 | 0 |          } else if (value instanceof Double) { | 
  | 131 | 0 |              model.set(qPath, (Double) value); | 
  | 132 | 0 |          } else if (value instanceof Boolean) { | 
  | 133 | 0 |              model.set(qPath, (Boolean) value); | 
  | 134 | 0 |          } else if (value instanceof Date) { | 
  | 135 | 0 |              model.set(qPath, (Date) value); | 
  | 136 | 0 |          } else if (value instanceof Data) { | 
  | 137 | 0 |              model.set(qPath, (Data) value); | 
  | 138 |  |          } | 
  | 139 | 0 |      } | 
  | 140 |  |   | 
  | 141 |  |       | 
  | 142 |  |   | 
  | 143 |  |   | 
  | 144 |  |   | 
  | 145 |  |          @Override | 
  | 146 |  |          public void setWidgetValue(Section section, DataModel model, String path) { | 
  | 147 | 0 |          List<FieldDescriptor> fields = section.getUnnestedFields(); | 
  | 148 |  |   | 
  | 149 | 0 |          for (int i = 0; i < fields.size(); i++) { | 
  | 150 | 0 |              FieldDescriptor field = fields.get(i); | 
  | 151 |  |   | 
  | 152 | 0 |              ModelWidgetBinding binding = field.getModelWidgetBinding(); | 
  | 153 | 0 |              String fieldPath = path + QueryPath.getPathSeparator() + field.getFieldKey(); | 
  | 154 | 0 |              if (binding != null) { | 
  | 155 | 0 |                  Widget w = field.getFieldWidget(); | 
  | 156 | 0 |                  binding.setWidgetValue(w, model, fieldPath); | 
  | 157 | 0 |              } else { | 
  | 158 | 0 |                  GWT.log(field.getFieldKey() + " has no widget binding.", null); | 
  | 159 |  |              } | 
  | 160 |  |          } | 
  | 161 | 0 |          for (Section s : section.getSections()) { | 
  | 162 | 0 |              s.updateWidgetData(model); | 
  | 163 |  |          } | 
  | 164 |  |                   | 
  | 165 | 0 |          } | 
  | 166 |  |  } |