Coverage Report - org.kuali.rice.kns.uif.field.AttributeQuery
 
Classes in this File Line Coverage Branch Coverage Complexity
AttributeQuery
0%
0/90
0%
0/22
1.367
 
 1  
 /*
 2  
  * Copyright 2007 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 1.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl1.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kns.uif.field;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.kns.uif.core.BindingInfo;
 20  
 import org.kuali.rice.kns.uif.core.MethodInvokerConfig;
 21  
 import org.kuali.rice.kns.util.KNSUtils;
 22  
 import org.kuali.rice.kns.util.ObjectUtils;
 23  
 import org.springframework.util.MethodInvoker;
 24  
 
 25  
 import java.io.Serializable;
 26  
 import java.util.ArrayList;
 27  
 import java.util.HashMap;
 28  
 import java.util.List;
 29  
 import java.util.Map;
 30  
 
 31  
 /**
 32  
  * Holds configuration for executing a dynamic query on an <code>AttributeField</code> to
 33  
  * pull data for updating the UI
 34  
  *
 35  
  * <p>
 36  
  * There are two types of query types that can be configured and executed. The first is provided
 37  
  * completely by the framework using the <code>LookupService</code> and will perform a query
 38  
  * against the configured dataObjectClassName using the query parameters and return field mapping.
 39  
  * The second type will invoke a method that will perform the query. This can be configured using the
 40  
  * queryMethodToCall (if the method is on the view helper service), or using the queryMethodInvoker if
 41  
  * the method is on another class or object.
 42  
  * </p>
 43  
  *
 44  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 45  
  */
 46  
 public class AttributeQuery implements Serializable {
 47  
     private static final long serialVersionUID = -4569905665441735255L;
 48  
 
 49  
     private String dataObjectClassName;
 50  
 
 51  
     private boolean renderNotFoundMessage;
 52  
     private String returnMessageText;
 53  
     private String returnMessageStyleClasses;
 54  
 
 55  
     private Map<String, String> queryFieldMapping;
 56  
     private Map<String, String> returnFieldMapping;
 57  
     private Map<String, String> additionalCriteria;
 58  
 
 59  
     private List<String> sortPropertyNames;
 60  
 
 61  
     private String queryMethodToCall;
 62  
     private List<String> queryMethodArgumentFieldList;
 63  
     private MethodInvokerConfig queryMethodInvokerConfig;
 64  
 
 65  0
     public AttributeQuery() {
 66  0
         renderNotFoundMessage = true;
 67  0
         queryFieldMapping = new HashMap<String, String>();
 68  0
         returnFieldMapping = new HashMap<String, String>();
 69  0
         additionalCriteria = new HashMap<String, String>();
 70  0
         sortPropertyNames = new ArrayList<String>();
 71  0
         queryMethodArgumentFieldList = new ArrayList<String>();
 72  0
     }
 73  
 
 74  
     /**
 75  
      * Adjusts the path on the query field mapping from property to match the binding
 76  
      * path prefix of the given <code>BindingInfo</code>
 77  
      *
 78  
      * @param bindingInfo - binding info instance to copy binding path prefix from
 79  
      */
 80  
     public void updateQueryFieldMapping(BindingInfo bindingInfo) {
 81  0
         Map<String, String> adjustedQueryFieldMapping = new HashMap<String, String>();
 82  0
         for (String toField : getQueryFieldMapping().keySet()) {
 83  0
             String fromFieldPath = getQueryFieldMapping().get(toField);
 84  0
             String adjustedFromFieldPath = bindingInfo.getPropertyAdjustedBindingPath(fromFieldPath);
 85  
 
 86  0
             adjustedQueryFieldMapping.put(toField, adjustedFromFieldPath);
 87  0
         }
 88  
 
 89  0
         this.queryFieldMapping = adjustedQueryFieldMapping;
 90  0
     }
 91  
 
 92  
     /**
 93  
      * Adjusts the path on the return field mapping to property to match the binding
 94  
      * path prefix of the given <code>BindingInfo</code>
 95  
      *
 96  
      * @param bindingInfo - binding info instance to copy binding path prefix from
 97  
      */
 98  
     public void updateReturnFieldMapping(BindingInfo bindingInfo) {
 99  0
         Map<String, String> adjustedReturnFieldMapping = new HashMap<String, String>();
 100  0
         for (String toFieldPath : getReturnFieldMapping().keySet()) {
 101  0
             String adjustedToFieldPath = bindingInfo.getPropertyAdjustedBindingPath(toFieldPath);
 102  
 
 103  0
             adjustedReturnFieldMapping.put(adjustedToFieldPath, getReturnFieldMapping().get(toFieldPath));
 104  0
         }
 105  
 
 106  0
         this.returnFieldMapping = adjustedReturnFieldMapping;
 107  0
     }
 108  
 
 109  
     /**
 110  
      * Adjusts the path on the query method arguments field list to match the binding
 111  
      * path prefix of the given <code>BindingInfo</code>
 112  
      *
 113  
      * @param bindingInfo - binding info instance to copy binding path prefix from
 114  
      */
 115  
     public void updateQueryMethodArgumentFieldList(BindingInfo bindingInfo) {
 116  0
         List<String> adjustedArgumentFieldList = new ArrayList<String>();
 117  0
         for (String argumentFieldPath : getQueryMethodArgumentFieldList()) {
 118  0
             String adjustedFieldPath = bindingInfo.getPropertyAdjustedBindingPath(argumentFieldPath);
 119  0
             adjustedArgumentFieldList.add(adjustedFieldPath);
 120  0
         }
 121  
 
 122  0
         this.queryMethodArgumentFieldList = adjustedArgumentFieldList;
 123  0
     }
 124  
 
 125  
     /**
 126  
      * Builds String for passing the queryFieldMapping Map as a Javascript object
 127  
      * parameter
 128  
      *
 129  
      * @return String js parameter string
 130  
      */
 131  
     public String getQueryFieldMappingJsString() {
 132  0
         String queryFieldMappingJs = "{";
 133  
 
 134  0
         for (String queryField : queryFieldMapping.keySet()) {
 135  0
             if (!StringUtils.equals(queryFieldMappingJs, "{")) {
 136  0
                 queryFieldMappingJs += ",";
 137  
             }
 138  
 
 139  0
             queryFieldMappingJs += "\"" + queryField + "\":\"" + queryFieldMapping.get(queryField) + "\"";
 140  
         }
 141  
 
 142  0
         queryFieldMappingJs += "}";
 143  
 
 144  0
         return queryFieldMappingJs;
 145  
     }
 146  
 
 147  
     /**
 148  
      * Builds String for passing the returnFieldMapping Map as a Javascript object
 149  
      * parameter
 150  
      *
 151  
      * @return String js parameter string
 152  
      */
 153  
     public String getReturnFieldMappingJsString() {
 154  0
         String returnFieldMappingJs = "{";
 155  
 
 156  0
         for (String returnField : returnFieldMapping.keySet()) {
 157  0
             if (!StringUtils.equals(returnFieldMappingJs, "{")) {
 158  0
                 returnFieldMappingJs += ",";
 159  
             }
 160  
 
 161  0
             returnFieldMappingJs += "\"" + returnField + "\":\"" + returnFieldMapping.get(returnField) + "\"";
 162  
         }
 163  
 
 164  0
         returnFieldMappingJs += "}";
 165  
 
 166  0
         return returnFieldMappingJs;
 167  
     }
 168  
 
 169  
     /**
 170  
      * Builds String for passing the queryMethodArgumentFieldList as a Javascript array
 171  
      *
 172  
      * @return String js parameter string
 173  
      */
 174  
     public String getQueryMethodArgumentFieldsJsString() {
 175  0
         String queryMethodArgsJs = "[";
 176  
 
 177  0
         for (String methodArg : queryMethodArgumentFieldList) {
 178  0
             if (!StringUtils.equals(queryMethodArgsJs, "{")) {
 179  0
                 queryMethodArgsJs += ",";
 180  
             }
 181  0
             queryMethodArgsJs += "\"" + methodArg + "\"";
 182  
         }
 183  
 
 184  0
         queryMethodArgsJs += "]";
 185  
 
 186  0
         return queryMethodArgsJs;
 187  
     }
 188  
 
 189  
     /**
 190  
      * Indicates whether this attribute query is configured to invoke a custom
 191  
      * method as opposed to running the general object query. If either the query method
 192  
      * to call is given, or the query method invoker is not null it is assumed the
 193  
      * intention is to call a custom method
 194  
      *
 195  
      * @return boolean true if a custom method is configured, false if not
 196  
      */
 197  
     public boolean hasConfiguredMethod() {
 198  0
         boolean configuredMethod = false;
 199  
 
 200  0
         if (StringUtils.isNotBlank(getQueryMethodToCall())) {
 201  0
             configuredMethod = true;
 202  0
         } else if (getQueryMethodInvokerConfig() != null) {
 203  0
             configuredMethod = true;
 204  
         }
 205  
 
 206  0
         return configuredMethod;
 207  
     }
 208  
 
 209  
     /**
 210  
      * Class name for the data object the query should be performed against
 211  
      *
 212  
      * @return String data object class name
 213  
      */
 214  
     public String getDataObjectClassName() {
 215  0
         return dataObjectClassName;
 216  
     }
 217  
 
 218  
     /**
 219  
      * Setter for the query data object class name
 220  
      *
 221  
      * @param dataObjectClassName
 222  
      */
 223  
     public void setDataObjectClassName(String dataObjectClassName) {
 224  0
         this.dataObjectClassName = dataObjectClassName;
 225  0
     }
 226  
 
 227  
     /**
 228  
      * Configures the query parameters by mapping fields in the view
 229  
      * to properties on the data object class for the query
 230  
      *
 231  
      * <p>
 232  
      * Each map entry configures one parameter for the query, where
 233  
      * the map key is the field name to pull the value from, and the
 234  
      * map value is the property name on the object the parameter should
 235  
      * populate.
 236  
      * </p>
 237  
      *
 238  
      * @return Map<String, String> mapping of query parameters
 239  
      */
 240  
     public Map<String, String> getQueryFieldMapping() {
 241  0
         return queryFieldMapping;
 242  
     }
 243  
 
 244  
     /**
 245  
      * Setter for the query parameter mapping
 246  
      *
 247  
      * @param queryFieldMapping
 248  
      */
 249  
     public void setQueryFieldMapping(Map<String, String> queryFieldMapping) {
 250  0
         this.queryFieldMapping = queryFieldMapping;
 251  0
     }
 252  
 
 253  
     /**
 254  
      * Maps properties from the result object of the query to
 255  
      * fields in the view
 256  
      *
 257  
      * <p>
 258  
      * Each map entry configures one return mapping, where the map
 259  
      * key is the field name for the field to populate, and the map
 260  
      * values is the name of the property on the result object to
 261  
      * pull the value from
 262  
      * </p>
 263  
      *
 264  
      * @return Map<String, String> return field mapping
 265  
      */
 266  
     public Map<String, String> getReturnFieldMapping() {
 267  0
         return returnFieldMapping;
 268  
     }
 269  
 
 270  
     /**
 271  
      * Setter for the return field mapping
 272  
      *
 273  
      * @param returnFieldMapping
 274  
      */
 275  
     public void setReturnFieldMapping(Map<String, String> returnFieldMapping) {
 276  0
         this.returnFieldMapping = returnFieldMapping;
 277  0
     }
 278  
 
 279  
     /**
 280  
      * Fixed criteria that will be appended to the dynamic criteria generated
 281  
      * for the query. Map key gives name of the property the criteria should
 282  
      * apply to, and the map value is the value (literal) for the criteria. Standard
 283  
      * lookup wildcards are allowed
 284  
      *
 285  
      * @return Map<String, String> field name/value pairs for query criteria
 286  
      */
 287  
     public Map<String, String> getAdditionalCriteria() {
 288  0
         return additionalCriteria;
 289  
     }
 290  
 
 291  
     /**
 292  
      * Setter for the query's additional criteria map
 293  
      *
 294  
      * @param additionalCriteria
 295  
      */
 296  
     public void setAdditionalCriteria(Map<String, String> additionalCriteria) {
 297  0
         this.additionalCriteria = additionalCriteria;
 298  0
     }
 299  
 
 300  
     /**
 301  
      * List of property names to sort the query results by. The sort
 302  
      * will be performed on each property in the order they are contained
 303  
      * within the list. Each property must be a valid property of the
 304  
      * return query object (the data object in case of the general query)
 305  
      *
 306  
      * @return List<String> property names
 307  
      */
 308  
     public List<String> getSortPropertyNames() {
 309  0
         return sortPropertyNames;
 310  
     }
 311  
 
 312  
     /**
 313  
      * Setter for the list of property names to sort results by
 314  
      *
 315  
      * @param sortPropertyNames
 316  
      */
 317  
     public void setSortPropertyNames(List<String> sortPropertyNames) {
 318  0
         this.sortPropertyNames = sortPropertyNames;
 319  0
     }
 320  
 
 321  
     /**
 322  
      * Indicates whether a message should be added to the query result
 323  
      * object and displayed when the query return object is null
 324  
      *
 325  
      * @return boolean true if not found message should be added, false otherwise
 326  
      */
 327  
     public boolean isRenderNotFoundMessage() {
 328  0
         return renderNotFoundMessage;
 329  
     }
 330  
 
 331  
     /**
 332  
      * Setter for the render not found message indicator
 333  
      *
 334  
      * @param renderNotFoundMessage
 335  
      */
 336  
     public void setRenderNotFoundMessage(boolean renderNotFoundMessage) {
 337  0
         this.renderNotFoundMessage = renderNotFoundMessage;
 338  0
     }
 339  
 
 340  
     /**
 341  
      * Message text to display along with the query result
 342  
      *
 343  
      * @return String literal message text
 344  
      */
 345  
     public String getReturnMessageText() {
 346  0
         return returnMessageText;
 347  
     }
 348  
 
 349  
     /**
 350  
      * Setter for the return message text
 351  
      *
 352  
      * @param returnMessageText
 353  
      */
 354  
     public void setReturnMessageText(String returnMessageText) {
 355  0
         this.returnMessageText = returnMessageText;
 356  0
     }
 357  
 
 358  
     /**
 359  
      * CSS Style classes that should be applied to the return message.
 360  
      * Multiple style classes should be delimited by a space
 361  
      *
 362  
      * @return String style classes
 363  
      */
 364  
     public String getReturnMessageStyleClasses() {
 365  0
         return returnMessageStyleClasses;
 366  
     }
 367  
 
 368  
     /**
 369  
      * Setter for the return messages style classes
 370  
      *
 371  
      * @param returnMessageStyleClasses
 372  
      */
 373  
     public void setReturnMessageStyleClasses(String returnMessageStyleClasses) {
 374  0
         this.returnMessageStyleClasses = returnMessageStyleClasses;
 375  0
     }
 376  
 
 377  
     /**
 378  
      * Configures the name of the method that should be invoked to perform
 379  
      * the query
 380  
      *
 381  
      * <p>
 382  
      * Should contain only the method name (no parameters or return type). If only
 383  
      * the query method name is configured it is assumed to be on the <code>ViewHelperService</code>
 384  
      * for the contained view.
 385  
      * </p>
 386  
      *
 387  
      * @return String query method name
 388  
      */
 389  
     public String getQueryMethodToCall() {
 390  0
         return queryMethodToCall;
 391  
     }
 392  
 
 393  
     /**
 394  
      * Setter for the query method name
 395  
      *
 396  
      * @param queryMethodToCall
 397  
      */
 398  
     public void setQueryMethodToCall(String queryMethodToCall) {
 399  0
         this.queryMethodToCall = queryMethodToCall;
 400  0
     }
 401  
 
 402  
     /**
 403  
      * List of field names that should be passed as arguments to the query method
 404  
      *
 405  
      * <p>
 406  
      * Each entry in the list maps to a method parameter, in the other contained within
 407  
      * the list. The value for the field within the view will be pulled and passed
 408  
      * to the query method as an argument
 409  
      * </p>
 410  
      *
 411  
      * @return List<String> query method argument list
 412  
      */
 413  
     public List<String> getQueryMethodArgumentFieldList() {
 414  0
         return queryMethodArgumentFieldList;
 415  
     }
 416  
 
 417  
     /**
 418  
      * Setter for the query method argument list
 419  
      *
 420  
      * @param queryMethodArgumentFieldList
 421  
      */
 422  
     public void setQueryMethodArgumentFieldList(List<String> queryMethodArgumentFieldList) {
 423  0
         this.queryMethodArgumentFieldList = queryMethodArgumentFieldList;
 424  0
     }
 425  
 
 426  
     /**
 427  
      * Configures the query method target class/object and method name
 428  
      *
 429  
      * <p>
 430  
      * When the query method is not contained on the <code>ViewHelperService</code>, this
 431  
      * can be configured for declaring the target class/object and method. The target class
 432  
      * can be set in which case a new instance will be created and the given method invoked.
 433  
      * Alternatively, the target object instance for the invocation can be given. Or finally
 434  
      * a static method can be configured
 435  
      * </p>
 436  
      *
 437  
      * @return MethodInvokerConfig query method config
 438  
      */
 439  
     public MethodInvokerConfig getQueryMethodInvokerConfig() {
 440  0
         return queryMethodInvokerConfig;
 441  
     }
 442  
 
 443  
     /**
 444  
      * Setter for the query method config
 445  
      *
 446  
      * @param queryMethodInvokerConfig
 447  
      */
 448  
     public void setQueryMethodInvokerConfig(MethodInvokerConfig queryMethodInvokerConfig) {
 449  0
         this.queryMethodInvokerConfig = queryMethodInvokerConfig;
 450  0
     }
 451  
 }