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.binding;
17  
18  import java.util.Iterator;
19  
20  import org.kuali.student.common.ui.client.configurable.mvc.multiplicity.MultiplicityGroup;
21  import org.kuali.student.common.ui.client.configurable.mvc.multiplicity.MultiplicityGroupItem;
22  import org.kuali.student.common.ui.client.mvc.DataModel;
23  import org.kuali.student.r1.common.assembly.data.Data;
24  import org.kuali.student.r1.common.assembly.data.QueryPath;
25  import org.kuali.student.r1.common.assembly.data.Data.Property;
26  
27  /**
28   * This goes through each item in the MultiplicityGroup and calls it's binding
29   *
30   * @author Kuali Student Team
31   */
32  public class MultiplicityGroupBinding extends ModelWidgetBindingSupport<MultiplicityGroup> {
33      public static MultiplicityGroupBinding INSTANCE = new MultiplicityGroupBinding();
34  
35      public MultiplicityGroupBinding() {};
36  
37      /**
38       * @see ModelWidgetBindingSupport#setModelValue(Object,
39       *      org.kuali.student.common.ui.client.mvc.DataModel, String)
40       */
41      public void setModelValue(MultiplicityGroup mcWidget, DataModel model, String path) {
42          for (MultiplicityGroupItem item : mcWidget.getItems()) {
43              MultiplicityGroupItemBinding.INSTANCE.setModelValue(item, model, mcWidget.getParentPath());
44          }
45          for (MultiplicityGroupItem item : mcWidget.getRemovedItems()) {
46              //Check flag to add model binding for only those elements that are either added to the DB or
47              // loading frm the DB.
48              if(item.isCreated()==false){
49                  MultiplicityGroupItemBinding.INSTANCE.setModelValue(item, model, path);
50              }
51          }
52      }
53  
54      /**
55       * @see ModelWidgetBindingSupport#setWidgetValue(Object,
56       *      org.kuali.student.common.ui.client.mvc.DataModel, String)
57       */
58      public void setWidgetValue(MultiplicityGroup mg, DataModel model, String path) {
59          mg.clear();
60  
61          String fieldPath = mg.translatePath(path);
62   
63  		mg.setParentPath(fieldPath);
64  
65          QueryPath qPath = QueryPath.parse(fieldPath);
66          Data data = null;
67          if(model!=null){
68          	data = model.get(qPath);
69          }
70  
71          if (data != null) {
72              Iterator<Property> itr = data.iterator();
73              while (itr.hasNext()) {
74                  Property p = (Property) itr.next();
75  
76                  if (p.getKey() instanceof Integer && !isItemDeleted(model, path, (Integer)p.getKey(), mg)) {
77                  	MultiplicityGroupItem mgi = mg.createItem();
78                      mgi.setCreated(false);
79                      mgi.setItemKey((Integer) p.getKey());
80                      MultiplicityGroupItemBinding.INSTANCE.setWidgetValue(mgi, model, fieldPath);
81                  } else {
82                  	mg.incrementItemKey();
83                  }
84              }
85          }
86      }
87  
88      /**
89       * Checks to see if an item at the given index in the multiplicity has been deleted
90       *
91       * @param model
92       * @param path
93       * @param index
94       */
95      public boolean isItemDeleted(DataModel model, String path, Integer index, MultiplicityGroup mcWidget){
96      	boolean isDeleted = false;
97  
98      	// If multiplicity read only no point checking for deletions
99      	if (!mcWidget.getConfig().isUpdateable()) {
100 
101     		QueryPath runtimeDeletedPath = QueryPath.concat(path, String.valueOf(index), MultiplicityGroupItemBinding.RT_DELETED);
102 
103     		Boolean runtimeDeleted = model.get(runtimeDeletedPath);
104     		if (runtimeDeleted != null){
105     			isDeleted = runtimeDeleted;
106     		}
107     		return isDeleted;
108     	}
109 
110     	return isDeleted;
111     }
112 
113 
114 }