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    *
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.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.ui.client.configurable.mvc.FieldDescriptor;
24  import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptorReadOnly;
25  import org.kuali.student.common.ui.client.configurable.mvc.binding.ModelWidgetBinding;
26  import org.kuali.student.common.ui.client.configurable.mvc.binding.ModelWidgetBindingSupport;
27  import org.kuali.student.common.ui.client.mvc.DataModel;
28  import org.kuali.student.core.assembly.data.Data;
29  import org.kuali.student.core.assembly.data.QueryPath;
30  
31  import com.google.gwt.core.client.GWT;
32  import com.google.gwt.user.client.ui.Widget;
33  
34  public class SectionBinding extends ModelWidgetBindingSupport<Section> {
35  	public static SectionBinding INSTANCE = new SectionBinding();
36  
37      private SectionBinding() {};
38  
39      /**
40       * @see org.kuali.student.common.ui.client.configurable.mvc.binding.ModelWidgetBindingSupport#setModelValue(java.lang.Object,
41       *      org.kuali.student.common.ui.client.mvc.DataModel, java.lang.String)
42       */
43      @Override
44      public void setModelValue(Section section, DataModel model, String path) {
45          
46          List<FieldDescriptor> fields = section.getUnnestedFields();
47          Map<String, Object> selectedData = null;
48          for (int i = 0; i < fields.size(); i++) {
49          	if(!(fields.get(i) instanceof FieldDescriptorReadOnly)){
50          		FieldDescriptor field = (FieldDescriptor) fields.get(i);
51              
52  	            String fieldPath = path + QueryPath.getPathSeparator() + field.getFieldKey();
53  	            fieldPath = fieldPath.trim();
54  	            if (fieldPath.startsWith(QueryPath.getPathSeparator())) {
55  	                fieldPath = fieldPath.substring(QueryPath.getPathSeparator().length());
56  	            }
57  	            ModelWidgetBinding binding = field.getModelWidgetBinding();
58  	            if (binding != null) {
59  	                Widget w = field.getFieldWidget();
60  	                binding.setModelValue(w, model, fieldPath);
61  	            } else {
62  	                GWT.log(field.getFieldKey() + " has no widget binding.", null);
63  	            }
64              }
65          }
66          
67          if(section instanceof HasSectionDeletion){
68          	List<Section> deleted =((HasSectionDeletion) section).getDeletedSections();
69          	for(int i =0; i < deleted.size(); i++){
70          		Section deletedSection = deleted.get(i);
71          		//get all fields and delete them>
72          		List<FieldDescriptor> deletedFields = deletedSection.getFields();
73          		for(int j = 0; j < deletedFields.size(); j++){
74          			FieldDescriptor field = deletedFields.get(j);
75          			model.remove(QueryPath.parse(field.getFieldKey()));
76          		}
77          	}
78          }
79          
80          for (Section s : section.getSections()) {
81              // data of the HasSectionDeletion section that should be in the model 
82          	if(section instanceof HasSectionDeletion){
83          		List<Section> deleted =((HasSectionDeletion) section).getDeletedSections();
84          		List<String> deletionParentKeys = ((HasSectionDeletion) section).getDeletionParentKeys();
85          		if (deletionParentKeys != null) {
86          		    for (String deletionParentKey : deletionParentKeys) {
87          		        model.remove(QueryPath.parse(deletionParentKey));
88          		    }
89          		}
90          		// section is not deleted update model with the data from widget.  Keep
91          		// a record of the list of data so that they can be added back into the model
92          		if(!deleted.contains(s)){
93          			s.updateModel(model);
94                      if (deletionParentKeys != null) {
95                          for (String deletionParentKey : deletionParentKeys) {
96                              selectedData = (selectedData == null)? 
97                                      new HashMap<String, Object>() : selectedData;
98                              selectedData.put(deletionParentKey, model.get(deletionParentKey));
99                          }
100                     }
101         		} else if (selectedData != null && !selectedData.isEmpty()) {
102         		    for (String key : selectedData.keySet()) {
103         		        setValue(model, key, selectedData.get(key));
104         		    }
105         		}
106         	}
107         	else{
108         		s.updateModel(model);
109         	}      
110         }
111     }
112     
113     private void setValue(DataModel model, String path, Object value) {
114         QueryPath qPath = QueryPath.parse(path);
115         if (value instanceof String) {
116             model.set(qPath, (String) value);
117         } else if (value instanceof Integer) {
118             model.set(qPath, (Integer) value);
119         } else if (value instanceof Long) {
120             model.set(qPath, (Long) value);
121         } else if (value instanceof Float) {
122             model.set(qPath, (Float) value);
123         } else if (value instanceof Double) {
124             model.set(qPath, (Double) value);
125         } else if (value instanceof Boolean) {
126             model.set(qPath, (Boolean) value);
127         } else if (value instanceof Date) {
128             model.set(qPath, (Date) value);
129         } else if (value instanceof Data) {
130             model.set(qPath, (Data) value);
131         }
132     }
133 
134     /**
135      * @see org.kuali.student.common.ui.client.configurable.mvc.binding.ModelWidgetBindingSupport#setWidgetValue(java.lang.Object,
136      *      org.kuali.student.common.ui.client.mvc.DataModel, java.lang.String)
137      */
138 	@Override
139 	public void setWidgetValue(Section section, DataModel model, String path) {
140         List<FieldDescriptor> fields = section.getUnnestedFields();
141 
142         for (int i = 0; i < fields.size(); i++) {
143             FieldDescriptor field = fields.get(i);
144 
145             ModelWidgetBinding binding = field.getModelWidgetBinding();
146             String fieldPath = path + QueryPath.getPathSeparator() + field.getFieldKey();
147             if (binding != null) {
148                 Widget w = field.getFieldWidget();
149                 binding.setWidgetValue(w, model, fieldPath);
150             } else {
151                 GWT.log(field.getFieldKey() + " has no widget binding.", null);
152             }
153         }
154         for (Section s : section.getSections()) {
155             s.updateWidgetData(model);
156         }
157 		
158 	}
159 }