Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BindingInfo |
|
| 1.9444444444444444;1.944 |
1 | /* | |
2 | * Copyright 2007 The Kuali Foundation Licensed under the Educational Community | |
3 | * License, Version 1.0 (the "License"); you may not use this file except in | |
4 | * compliance with the License. You may obtain a copy of the License at | |
5 | * http://www.opensource.org/licenses/ecl1.php Unless required by applicable law | |
6 | * or agreed to in writing, software distributed under the License is | |
7 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
8 | * KIND, either express or implied. See the License for the specific language | |
9 | * governing permissions and limitations under the License. | |
10 | */ | |
11 | package org.kuali.rice.krad.uif.core; | |
12 | ||
13 | import org.apache.commons.lang.StringUtils; | |
14 | import org.kuali.rice.krad.uif.UifConstants; | |
15 | import org.kuali.rice.krad.uif.container.View; | |
16 | import org.kuali.rice.krad.util.ObjectUtils; | |
17 | ||
18 | import java.io.Serializable; | |
19 | ||
20 | /** | |
21 | * Provides binding configuration for an DataBinding component (attribute or | |
22 | * collection) | |
23 | * | |
24 | * <p> | |
25 | * From the binding configuration the binding path is determined (if not | |
26 | * manually set) and used to set the path in the UI or to get the value from the | |
27 | * model | |
28 | * </p> | |
29 | * | |
30 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
31 | */ | |
32 | public class BindingInfo implements Serializable { | |
33 | private static final long serialVersionUID = -7389398061672136091L; | |
34 | ||
35 | private boolean bindToForm; | |
36 | private boolean bindToMap; | |
37 | ||
38 | private String bindingName; | |
39 | private String bindByNamePrefix; | |
40 | private String bindingObjectPath; | |
41 | ||
42 | private String collectionPath; | |
43 | ||
44 | private String bindingPath; | |
45 | ||
46 | 0 | public BindingInfo() { |
47 | 0 | bindToForm = false; |
48 | 0 | bindToMap = false; |
49 | 0 | } |
50 | ||
51 | /** | |
52 | * Sets up some default binding properties based on the view configuration | |
53 | * and the component's property name | |
54 | * | |
55 | * <p> | |
56 | * Sets the bindingName (if not set) to the given property name, and if the | |
57 | * binding object path has not been set uses the default binding object path | |
58 | * setup for the view | |
59 | * </p> | |
60 | * | |
61 | * @param view | |
62 | * - the view instance the component belongs to | |
63 | * @param propertyName | |
64 | * - name of the property (relative to the parent object) the | |
65 | * component binds to | |
66 | */ | |
67 | public void setDefaults(View view, String propertyName) { | |
68 | 0 | if (StringUtils.isBlank(bindingName)) { |
69 | 0 | bindingName = propertyName; |
70 | } | |
71 | ||
72 | 0 | if (StringUtils.isBlank(bindingObjectPath)) { |
73 | 0 | bindingObjectPath = view.getDefaultBindingObjectPath(); |
74 | } | |
75 | 0 | } |
76 | ||
77 | /** | |
78 | * Path to the property on the model the component binds to. Uses standard | |
79 | * dot notation for nested properties. If the binding path was manually set | |
80 | * it will be returned as it is, otherwise the path will be formed by using | |
81 | * the binding object path and the bind prefix | |
82 | * | |
83 | * <p> | |
84 | * e.g. Property name 'foo' on a model would have binding path "foo", while | |
85 | * property name 'name' of the nested model property 'account' would have | |
86 | * binding path "account.name" | |
87 | * </p> | |
88 | * | |
89 | * @return String binding path | |
90 | */ | |
91 | public String getBindingPath() { | |
92 | 0 | if (StringUtils.isNotBlank(bindingPath)) { |
93 | 0 | return bindingPath; |
94 | } | |
95 | ||
96 | 0 | String formedBindingPath = ""; |
97 | ||
98 | 0 | if (!bindToForm && StringUtils.isNotBlank(bindingObjectPath)) { |
99 | 0 | formedBindingPath = bindingObjectPath; |
100 | } | |
101 | ||
102 | 0 | if (StringUtils.isNotBlank(bindByNamePrefix)) { |
103 | 0 | if (!bindByNamePrefix.startsWith("[") && StringUtils.isNotBlank(formedBindingPath)) { |
104 | 0 | formedBindingPath += "."; |
105 | } | |
106 | 0 | formedBindingPath += bindByNamePrefix; |
107 | } | |
108 | ||
109 | 0 | if (bindToMap) { |
110 | 0 | formedBindingPath += "['" + bindingName + "']"; |
111 | } else { | |
112 | 0 | if (StringUtils.isNotBlank(formedBindingPath)) { |
113 | 0 | formedBindingPath += "."; |
114 | } | |
115 | 0 | formedBindingPath += bindingName; |
116 | } | |
117 | ||
118 | 0 | return formedBindingPath; |
119 | } | |
120 | ||
121 | /** | |
122 | * Returns the binding prefix string that can be used to setup the binding | |
123 | * on <code>DataBinding</code> components that are children of the component | |
124 | * that contains the <code>BindingInfo</code>. The binding prefix is formed | |
125 | * like the binding path but without including the object path | |
126 | * | |
127 | * @return String binding prefix for nested components | |
128 | */ | |
129 | public String getBindingPrefixForNested() { | |
130 | 0 | String bindingPrefix = ""; |
131 | ||
132 | 0 | if (StringUtils.isNotBlank(bindByNamePrefix)) { |
133 | 0 | bindingPrefix = bindByNamePrefix; |
134 | } | |
135 | ||
136 | 0 | if (bindToMap) { |
137 | 0 | bindingPrefix += "['" + bindingName + "']"; |
138 | } else { | |
139 | 0 | if (StringUtils.isNotBlank(bindingPrefix)) { |
140 | 0 | bindingPrefix += "."; |
141 | } | |
142 | 0 | bindingPrefix += bindingName; |
143 | } | |
144 | ||
145 | 0 | return bindingPrefix; |
146 | } | |
147 | ||
148 | /** | |
149 | * Returns the binding path that is formed by taking the binding configuration | |
150 | * of this <code>BindingInfo</code> instance with the given property path as the | |
151 | * binding name. This can be used to get the binding path when just a property | |
152 | * name is given that is assumed to be on the same parent object of the field with | |
153 | * the configured binding info | |
154 | * | |
155 | * <p> | |
156 | * Special check is done for org.kuali.rice.krad.uif.UifConstants#NO_BIND_ADJUST_PREFIX prefix | |
157 | * on the property name which indicates the property path is the full path and should | |
158 | * not be adjusted. Also, if the property is prefixed with | |
159 | * org.kuali.rice.krad.uif.UifConstants#DATA_OBJECT_BIND_ADJUST_PREFIX, this indicates we should only append the | |
160 | * binding object path | |
161 | * </p> | |
162 | * | |
163 | * @param propertyPath - path for property to return full binding path for | |
164 | * @return String full binding path | |
165 | */ | |
166 | public String getPropertyAdjustedBindingPath(String propertyPath) { | |
167 | 0 | if (propertyPath.startsWith(UifConstants.NO_BIND_ADJUST_PREFIX)) { |
168 | 0 | propertyPath = StringUtils.removeStart(propertyPath, UifConstants.NO_BIND_ADJUST_PREFIX); |
169 | 0 | return propertyPath; |
170 | } | |
171 | ||
172 | 0 | BindingInfo bindingInfoCopy = (BindingInfo) ObjectUtils.deepCopy(this); |
173 | ||
174 | 0 | if (propertyPath.startsWith(UifConstants.DATA_OBJECT_BIND_ADJUST_PREFIX)) { |
175 | 0 | bindingInfoCopy.setBindByNamePrefix(""); |
176 | 0 | propertyPath = StringUtils.removeStart(propertyPath, UifConstants.DATA_OBJECT_BIND_ADJUST_PREFIX); |
177 | } | |
178 | 0 | bindingInfoCopy.setBindingName(propertyPath); |
179 | ||
180 | 0 | return bindingInfoCopy.getBindingPath(); |
181 | } | |
182 | ||
183 | /** | |
184 | * Setter for the binding path. Can be left blank in which the path will be | |
185 | * determined from the binding configuration | |
186 | * | |
187 | * @param bindingPath | |
188 | */ | |
189 | public void setBindingPath(String bindingPath) { | |
190 | 0 | this.bindingPath = bindingPath; |
191 | 0 | } |
192 | ||
193 | /** | |
194 | * Indicates whether the component binds directly to the form (that is its | |
195 | * bindingName gives a property available through the form), or whether is | |
196 | * binds through a nested form object. If bindToForm is false, it is assumed | |
197 | * the component binds to the object given by the form property whose path | |
198 | * is configured by bindingObjectPath. | |
199 | * | |
200 | * @return boolean true if component binds directly to form, false if it | |
201 | * binds to a nested object | |
202 | */ | |
203 | public boolean isBindToForm() { | |
204 | 0 | return this.bindToForm; |
205 | } | |
206 | ||
207 | /** | |
208 | * Setter for the bind to form indicator | |
209 | * | |
210 | * @param bindToForm | |
211 | */ | |
212 | public void setBindToForm(boolean bindToForm) { | |
213 | 0 | this.bindToForm = bindToForm; |
214 | 0 | } |
215 | ||
216 | /** | |
217 | * Gives the name of the property that the component binds to. The name can | |
218 | * be nested but not the full path, just from the parent object or in the | |
219 | * case of binding directly to the form from the form object | |
220 | * | |
221 | * <p> | |
222 | * If blank this will be set from the name field of the component | |
223 | * </p> | |
224 | * | |
225 | * @return String name of the bind property | |
226 | */ | |
227 | public String getBindingName() { | |
228 | 0 | return this.bindingName; |
229 | } | |
230 | ||
231 | /** | |
232 | * Setter for the bind property name | |
233 | * | |
234 | * @param bindingName | |
235 | */ | |
236 | public void setBindingName(String bindingName) { | |
237 | 0 | this.bindingName = bindingName; |
238 | 0 | } |
239 | ||
240 | /** | |
241 | * Prefix that will be used to form the binding path from the component | |
242 | * name. Typically used for nested collection properties | |
243 | * | |
244 | * @return String binding prefix | |
245 | */ | |
246 | public String getBindByNamePrefix() { | |
247 | 0 | return this.bindByNamePrefix; |
248 | } | |
249 | ||
250 | /** | |
251 | * Setter for the prefix to use for forming the binding path by name | |
252 | * | |
253 | * @param bindByNamePrefix | |
254 | */ | |
255 | public void setBindByNamePrefix(String bindByNamePrefix) { | |
256 | 0 | this.bindByNamePrefix = bindByNamePrefix; |
257 | 0 | } |
258 | ||
259 | /** | |
260 | * If field is part of a collection field, gives path to collection | |
261 | * | |
262 | * <p> | |
263 | * This is used for metadata purposes when getting finding the attribute | |
264 | * definition from the dictionary and is not used in building the final | |
265 | * binding path | |
266 | * </p> | |
267 | * | |
268 | * @return String path to collection | |
269 | */ | |
270 | public String getCollectionPath() { | |
271 | 0 | return this.collectionPath; |
272 | } | |
273 | ||
274 | /** | |
275 | * Setter for the field's collection path (if part of a collection) | |
276 | * | |
277 | * @param collectionPath | |
278 | */ | |
279 | public void setCollectionPath(String collectionPath) { | |
280 | 0 | this.collectionPath = collectionPath; |
281 | 0 | } |
282 | ||
283 | /** | |
284 | * For attribute fields that do not belong to the default form object (given | |
285 | * by the view), this field specifies the path to the object (on the form) | |
286 | * the attribute does belong to. | |
287 | * | |
288 | * <p> | |
289 | * e.g. Say we have an attribute field with property name 'number', that | |
290 | * belongs to the object given by the 'account' property on the form. The | |
291 | * form object path would therefore be set to 'account'. If the property | |
292 | * belonged to the object given by the 'document.header' property of the | |
293 | * form, the binding object path would be set to 'document.header'. Note if | |
294 | * the binding object path is not set for an attribute field (or any | |
295 | * <code>DataBinding</code> component), the binding object path configured | |
296 | * on the <code>View</code> will be used (unless bindToForm is set to true, | |
297 | * where is assumed the property is directly available from the form). | |
298 | * </p> | |
299 | * | |
300 | * @return String path to object from form | |
301 | */ | |
302 | public String getBindingObjectPath() { | |
303 | 0 | return this.bindingObjectPath; |
304 | } | |
305 | ||
306 | /** | |
307 | * Setter for the object path on the form | |
308 | * | |
309 | * @param bindingObjectPath | |
310 | */ | |
311 | public void setBindingObjectPath(String bindingObjectPath) { | |
312 | 0 | this.bindingObjectPath = bindingObjectPath; |
313 | 0 | } |
314 | ||
315 | /** | |
316 | * Indicates whether the parent object for the property that we are binding | |
317 | * to is a Map. If true the binding path will be adjusted to use the map key | |
318 | * syntax | |
319 | * | |
320 | * @return boolean true if the property binds to a map, false if it does not | |
321 | */ | |
322 | public boolean isBindToMap() { | |
323 | 0 | return this.bindToMap; |
324 | } | |
325 | ||
326 | /** | |
327 | * Setter for the bind to map indicator | |
328 | * | |
329 | * @param bindToMap | |
330 | */ | |
331 | public void setBindToMap(boolean bindToMap) { | |
332 | 0 | this.bindToMap = bindToMap; |
333 | 0 | } |
334 | ||
335 | } |