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