Coverage Report - org.kuali.rice.krad.uif.core.BindingInfo
 
Classes in this File Line Coverage Branch Coverage Complexity
BindingInfo
0%
0/58
0%
0/28
1.889
 
 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
 159  
      * </p>
 160  
      *
 161  
      * @param propertyPath - path for property to return full binding path for
 162  
      * @return String full binding path
 163  
      */
 164  
     public String getPropertyAdjustedBindingPath(String propertyPath) {
 165  0
         if (propertyPath.startsWith(UifConstants.NO_BIND_ADJUST_PREFIX)) {
 166  0
             return propertyPath;
 167  
         }
 168  
 
 169  0
         BindingInfo bindingInfoCopy = (BindingInfo) ObjectUtils.deepCopy(this);
 170  0
         bindingInfoCopy.setBindingName(propertyPath);
 171  
 
 172  0
         return bindingInfoCopy.getBindingPath();
 173  
     }
 174  
 
 175  
     /**
 176  
      * Setter for the binding path. Can be left blank in which the path will be
 177  
      * determined from the binding configuration
 178  
      * 
 179  
      * @param bindingPath
 180  
      */
 181  
     public void setBindingPath(String bindingPath) {
 182  0
         this.bindingPath = bindingPath;
 183  0
     }
 184  
 
 185  
     /**
 186  
      * Indicates whether the component binds directly to the form (that is its
 187  
      * bindingName gives a property available through the form), or whether is
 188  
      * binds through a nested form object. If bindToForm is false, it is assumed
 189  
      * the component binds to the object given by the form property whose path
 190  
      * is configured by bindingObjectPath.
 191  
      * 
 192  
      * @return boolean true if component binds directly to form, false if it
 193  
      *         binds to a nested object
 194  
      */
 195  
     public boolean isBindToForm() {
 196  0
         return this.bindToForm;
 197  
     }
 198  
 
 199  
     /**
 200  
      * Setter for the bind to form indicator
 201  
      * 
 202  
      * @param bindToForm
 203  
      */
 204  
     public void setBindToForm(boolean bindToForm) {
 205  0
         this.bindToForm = bindToForm;
 206  0
     }
 207  
 
 208  
     /**
 209  
      * Gives the name of the property that the component binds to. The name can
 210  
      * be nested but not the full path, just from the parent object or in the
 211  
      * case of binding directly to the form from the form object
 212  
      * 
 213  
      * <p>
 214  
      * If blank this will be set from the name field of the component
 215  
      * </p>
 216  
      * 
 217  
      * @return String name of the bind property
 218  
      */
 219  
     public String getBindingName() {
 220  0
         return this.bindingName;
 221  
     }
 222  
 
 223  
     /**
 224  
      * Setter for the bind property name
 225  
      * 
 226  
      * @param bindingName
 227  
      */
 228  
     public void setBindingName(String bindingName) {
 229  0
         this.bindingName = bindingName;
 230  0
     }
 231  
 
 232  
     /**
 233  
      * Prefix that will be used to form the binding path from the component
 234  
      * name. Typically used for nested collection properties
 235  
      * 
 236  
      * @return String binding prefix
 237  
      */
 238  
     public String getBindByNamePrefix() {
 239  0
         return this.bindByNamePrefix;
 240  
     }
 241  
 
 242  
     /**
 243  
      * Setter for the prefix to use for forming the binding path by name
 244  
      * 
 245  
      * @param bindByNamePrefix
 246  
      */
 247  
     public void setBindByNamePrefix(String bindByNamePrefix) {
 248  0
         this.bindByNamePrefix = bindByNamePrefix;
 249  0
     }
 250  
 
 251  
     /**
 252  
      * If field is part of a collection field, gives path to collection
 253  
      * 
 254  
      * <p>
 255  
      * This is used for metadata purposes when getting finding the attribute
 256  
      * definition from the dictionary and is not used in building the final
 257  
      * binding path
 258  
      * </p>
 259  
      * 
 260  
      * @return String path to collection
 261  
      */
 262  
     public String getCollectionPath() {
 263  0
         return this.collectionPath;
 264  
     }
 265  
 
 266  
     /**
 267  
      * Setter for the field's collection path (if part of a collection)
 268  
      * 
 269  
      * @param collectionPath
 270  
      */
 271  
     public void setCollectionPath(String collectionPath) {
 272  0
         this.collectionPath = collectionPath;
 273  0
     }
 274  
 
 275  
     /**
 276  
      * For attribute fields that do not belong to the default form object (given
 277  
      * by the view), this field specifies the path to the object (on the form)
 278  
      * the attribute does belong to.
 279  
      * 
 280  
      * <p>
 281  
      * e.g. Say we have an attribute field with property name 'number', that
 282  
      * belongs to the object given by the 'account' property on the form. The
 283  
      * form object path would therefore be set to 'account'. If the property
 284  
      * belonged to the object given by the 'document.header' property of the
 285  
      * form, the binding object path would be set to 'document.header'. Note if
 286  
      * the binding object path is not set for an attribute field (or any
 287  
      * <code>DataBinding</code> component), the binding object path configured
 288  
      * on the <code>View</code> will be used (unless bindToForm is set to true,
 289  
      * where is assumed the property is directly available from the form).
 290  
      * </p>
 291  
      * 
 292  
      * @return String path to object from form
 293  
      */
 294  
     public String getBindingObjectPath() {
 295  0
         return this.bindingObjectPath;
 296  
     }
 297  
 
 298  
     /**
 299  
      * Setter for the object path on the form
 300  
      * 
 301  
      * @param bindingObjectPath
 302  
      */
 303  
     public void setBindingObjectPath(String bindingObjectPath) {
 304  0
         this.bindingObjectPath = bindingObjectPath;
 305  0
     }
 306  
 
 307  
     /**
 308  
      * Indicates whether the parent object for the property that we are binding
 309  
      * to is a Map. If true the binding path will be adjusted to use the map key
 310  
      * syntax
 311  
      * 
 312  
      * @return boolean true if the property binds to a map, false if it does not
 313  
      */
 314  
     public boolean isBindToMap() {
 315  0
         return this.bindToMap;
 316  
     }
 317  
 
 318  
     /**
 319  
      * Setter for the bind to map indicator
 320  
      * 
 321  
      * @param bindToMap
 322  
      */
 323  
     public void setBindToMap(boolean bindToMap) {
 324  0
         this.bindToMap = bindToMap;
 325  0
     }
 326  
 
 327  
 }