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