Coverage Report - org.kuali.rice.krad.uif.container.CollectionGroupBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
CollectionGroupBuilder
0%
0/124
0%
0/78
5.333
 
 1  
 /*
 2  
  * Copyright 2011 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.krad.uif.container;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 20  
 import org.kuali.rice.krad.uif.UifConstants;
 21  
 import org.kuali.rice.krad.uif.UifParameters;
 22  
 import org.kuali.rice.krad.uif.UifPropertyPaths;
 23  
 import org.kuali.rice.krad.uif.control.Control;
 24  
 import org.kuali.rice.krad.uif.core.DataBinding;
 25  
 import org.kuali.rice.krad.uif.field.ActionField;
 26  
 import org.kuali.rice.krad.uif.field.AttributeField;
 27  
 import org.kuali.rice.krad.uif.field.Field;
 28  
 import org.kuali.rice.krad.uif.field.GroupField;
 29  
 import org.kuali.rice.krad.uif.layout.CollectionLayoutManager;
 30  
 import org.kuali.rice.krad.uif.service.ExpressionEvaluatorService;
 31  
 import org.kuali.rice.krad.uif.util.ComponentUtils;
 32  
 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
 33  
 import org.kuali.rice.krad.util.KRADUtils;
 34  
 import org.kuali.rice.krad.util.ObjectUtils;
 35  
 import org.kuali.rice.krad.web.form.UifFormBase;
 36  
 
 37  
 import java.io.Serializable;
 38  
 import java.util.ArrayList;
 39  
 import java.util.HashMap;
 40  
 import java.util.List;
 41  
 import java.util.Map;
 42  
 
 43  
 /**
 44  
  * Builds out the <code>Field</code> instances for a collection group with a
 45  
  * series of steps that interact with the configured
 46  
  * <code>CollectionLayoutManager</code> to assemble the fields as necessary for
 47  
  * the layout
 48  
  * 
 49  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 50  
  */
 51  0
 public class CollectionGroupBuilder implements Serializable {
 52  
         private static final long serialVersionUID = -4762031957079895244L;
 53  
 
 54  
         /**
 55  
          * Creates the <code>Field</code> instances that make up the table
 56  
          * 
 57  
          * <p>
 58  
          * The corresponding collection is retrieved from the model and iterated
 59  
          * over to create the necessary fields. The binding path for fields that
 60  
          * implement <code>DataBinding</code> is adjusted to point to the collection
 61  
          * line it is apart of. For example, field 'number' of collection 'accounts'
 62  
          * for line 1 will be set to 'accounts[0].number', and for line 2
 63  
          * 'accounts[1].number'. Finally parameters are set on the line's action
 64  
          * fields to indicate what collection and line they apply to.
 65  
          * </p>
 66  
          * 
 67  
          * @param view
 68  
          *            - View instance the collection belongs to
 69  
          * @param model
 70  
          *            - Top level object containing the data
 71  
          * @param collectionGroup
 72  
          *            - CollectionGroup component for the collection
 73  
          */
 74  
     public void build(View view, Object model, CollectionGroup collectionGroup) {
 75  
         // create add line
 76  0
         if (collectionGroup.isRenderAddLine() && !collectionGroup.isReadOnly()) {
 77  0
             buildAddLine(view, model, collectionGroup);
 78  
         }
 79  
 
 80  
         // get the collection for this group from the model
 81  0
         List<Object> modelCollection = ObjectPropertyUtils.getPropertyValue(model, ((DataBinding) collectionGroup)
 82  
                 .getBindingInfo().getBindingPath());
 83  
 
 84  
         // filter inactive model
 85  0
         List<Integer> showIndexes = collectionGroup.performCollectionFiltering(view, model);
 86  
         
 87  
         // for each collection row build the line fields
 88  0
         if (modelCollection != null) {
 89  0
             for (int index = 0; index < modelCollection.size(); index++) {
 90  
                 
 91  
                 // Display only records that passed filtering
 92  0
                 if (showIndexes == null || showIndexes.contains(index)) {
 93  0
                     String bindingPathPrefix = collectionGroup.getBindingInfo().getBindingName() + "[" + index + "]";
 94  0
                     if (StringUtils.isNotBlank(collectionGroup.getBindingInfo().getBindByNamePrefix())) {
 95  0
                         bindingPathPrefix = collectionGroup.getBindingInfo().getBindByNamePrefix() + "."
 96  
                                 + bindingPathPrefix;
 97  
                     }
 98  
 
 99  0
                     Object currentLine = modelCollection.get(index);
 100  
 
 101  0
                     List<ActionField> actions = getLineActions(view, model, collectionGroup, currentLine, index);
 102  0
                     buildLine(view, model, collectionGroup, bindingPathPrefix, actions, false, currentLine, index);
 103  
                 }
 104  
             }
 105  
         }
 106  0
     }
 107  
 
 108  
         /**
 109  
          * Builds the fields for holding the collection add line and if necessary
 110  
          * makes call to setup the new line instance
 111  
          * 
 112  
          * @param view
 113  
          *            - view instance the collection belongs to
 114  
          * @param collectionGroup
 115  
          *            - collection group the layout manager applies to
 116  
          * @param model
 117  
          *            - Object containing the view data, should extend UifFormBase
 118  
          *            if using framework managed new lines
 119  
          */
 120  
     protected void buildAddLine(View view, Object model, CollectionGroup collectionGroup) {
 121  0
         boolean addLineBindsToForm = false;
 122  
 
 123  
         // initialize new line if one does not already exist
 124  0
         initializeNewCollectionLine(view, model, collectionGroup, false);
 125  
 
 126  
         // determine whether the add line binds to the generic form map or a
 127  
         // specified property
 128  0
         if (StringUtils.isBlank(collectionGroup.getAddLinePropertyName())) {
 129  0
             addLineBindsToForm = true;
 130  
         }
 131  
 
 132  0
         String addLineBindingPath = collectionGroup.getAddLineBindingInfo().getBindingPath();
 133  0
         List<ActionField> actions = getAddLineActions(view, model, collectionGroup);
 134  
 
 135  0
         Object addLine = ObjectPropertyUtils.getPropertyValue(model, addLineBindingPath);
 136  0
         buildLine(view, model, collectionGroup, addLineBindingPath, actions, addLineBindsToForm, addLine, -1);
 137  0
     }
 138  
 
 139  
         /**
 140  
          * Builds the field instances for the collection line. A copy of the
 141  
          * configured items on the <code>CollectionGroup</code> is made and adjusted
 142  
          * for the line (id and binding). Then a call is made to the
 143  
          * <code>CollectionLayoutManager</code> to assemble the line as necessary
 144  
          * for the layout
 145  
          * 
 146  
          * @param view
 147  
          *            - view instance the collection belongs to
 148  
          * @param model
 149  
          *            - top level object containing the data
 150  
          * @param collectionGroup
 151  
          *            - collection group component for the collection
 152  
          * @param bindingPath
 153  
          *            - binding path for the line fields (if DataBinding)
 154  
          * @param actions
 155  
          *            - List of actions to set in the lines action column
 156  
          * @param bindLineToForm
 157  
          *            - whether the bindToForm property on the items bindingInfo
 158  
          *            should be set to true (needed for add line)
 159  
          * @param currentLine
 160  
          *            - object instance for the current line, or null if add line
 161  
          * @param lineIndex
 162  
          *            - index of the line in the collection, or -1 if we are
 163  
          *            building the add line
 164  
          */
 165  
         @SuppressWarnings("unchecked")
 166  
         protected void buildLine(View view, Object model, CollectionGroup collectionGroup, String bindingPath,
 167  
                         List<ActionField> actions, boolean bindToForm, Object currentLine, int lineIndex) {
 168  0
                 CollectionLayoutManager layoutManager = (CollectionLayoutManager) collectionGroup.getLayoutManager();
 169  
 
 170  
                 // copy group items for new line
 171  0
                 List<Field> lineFields = null;
 172  0
         if (lineIndex == -1) {
 173  0
             lineFields = (List<Field>) ComponentUtils.copyFieldList(collectionGroup.getAddLineFields(), bindingPath,
 174  
                     "_add");
 175  
         } else {
 176  0
             lineFields = (List<Field>) ComponentUtils.copyFieldList(collectionGroup.getItems(), bindingPath, "_"
 177  
                     + Integer.toString(lineIndex));
 178  
         }
 179  
         
 180  0
                 if(lineIndex == -1 && !lineFields.isEmpty()){
 181  0
                     for(Field f: lineFields){
 182  0
                         if(f instanceof AttributeField){
 183  
                             //sets up - skipping these fields in add area during standard form validation calls
 184  
                             //custom addLineToCollection js call will validate these fields manually on an add
 185  0
                                 Control control = ((AttributeField) f).getControl();
 186  0
                                 if (control != null) {
 187  0
                                     control.addStyleClass(collectionGroup.getBaseId() + "-addField");
 188  0
                                         control.addStyleClass("ignoreValid");
 189  
                                 }
 190  0
                         }
 191  
                     }
 192  0
                     for(ActionField action: actions){
 193  0
                         if(action.getActionParameter(UifParameters.ACTION_TYPE).equals(UifParameters.ADD_LINE)){
 194  0
                             action.setFocusOnAfterSubmit(lineFields.get(0).getId());
 195  
                         }
 196  
                     }
 197  
                 }
 198  
                 
 199  0
                 ComponentUtils.updateContextsForLine(lineFields, currentLine, lineIndex);
 200  
 
 201  0
                 if (bindToForm) {
 202  0
                         ComponentUtils.setComponentsPropertyDeep(lineFields, UifPropertyPaths.BIND_TO_FORM, new Boolean(true));
 203  
                 }                
 204  
                 
 205  
         // remove fields from the line that have render false
 206  0
         lineFields = removeNonRenderLineFields(view, model, collectionGroup, lineFields, currentLine, lineIndex);
 207  
 
 208  
                 // if not add line build sub-collection field groups
 209  0
                 List<GroupField> subCollectionFields = new ArrayList<GroupField>();
 210  0
                 if ((lineIndex != -1) && (collectionGroup.getSubCollections() != null)) {
 211  0
                         for (int subLineIndex = 0; subLineIndex < collectionGroup.getSubCollections().size(); subLineIndex++) {
 212  0
                                 CollectionGroup subCollectionPrototype = collectionGroup.getSubCollections().get(subLineIndex);
 213  0
                                 CollectionGroup subCollectionGroup = ComponentUtils.copy(subCollectionPrototype, collectionGroup.getId() + "s" + subLineIndex);
 214  
                                 
 215  
                 // verify the sub-collection should be rendered
 216  0
                 boolean renderSubCollection = checkSubCollectionRender(view, model, collectionGroup, subCollectionGroup);
 217  0
                 if (!renderSubCollection) {
 218  0
                     continue;
 219  
                 }
 220  
 
 221  0
                                 subCollectionGroup.getBindingInfo().setBindByNamePrefix(bindingPath);
 222  0
                                 subCollectionGroup.getAddLineBindingInfo().setBindByNamePrefix(bindingPath);
 223  
 
 224  0
                                 GroupField groupFieldPrototype = layoutManager.getSubCollectionGroupFieldPrototype();
 225  0
                                 GroupField subCollectionGroupField = ComponentUtils.copy(groupFieldPrototype, collectionGroup.getId() + "s" + subLineIndex);
 226  0
                                 subCollectionGroupField.setGroup(subCollectionGroup);
 227  
 
 228  0
                                 subCollectionFields.add(subCollectionGroupField);
 229  
                         }
 230  
                 }
 231  
                 
 232  
                 
 233  
                 // invoke layout manager to build the complete line
 234  0
                 layoutManager.buildLine(view, model, collectionGroup, lineFields, subCollectionFields, bindingPath, actions,
 235  
                                 "_l" + lineIndex, currentLine, lineIndex);
 236  0
         }
 237  
 
 238  
         
 239  
     /**
 240  
      * Evaluates the render property for the given list of <code>Field</code>
 241  
      * instances for the line and removes any fields from the returned list that
 242  
      * have render false. The conditional render string is also taken into
 243  
      * account. This needs to be done here as opposed to during the normal
 244  
      * condition evaluation so the the fields are not used while building the
 245  
      * collection lines
 246  
      * 
 247  
      * @param view
 248  
      *            - view instance the collection group belongs to
 249  
      * @param model
 250  
      *            - object containing the view data
 251  
      * @param collectionGroup
 252  
      *            - collection group for the line fields
 253  
      * @param lineFields
 254  
      *            - list of fields configured for the line
 255  
      * @param currentLine
 256  
      *            - object containing the line data
 257  
      * @param lineIndex
 258  
      *            - index of the line in the collection
 259  
      * @return List<Field> list of field instances that should be rendered
 260  
      */
 261  
     protected List<Field> removeNonRenderLineFields(View view, Object model, CollectionGroup collectionGroup,
 262  
             List<Field> lineFields, Object currentLine, int lineIndex) {
 263  0
         List<Field> fields = new ArrayList<Field>();
 264  
 
 265  0
         for (Field lineField : lineFields) {
 266  
             // evaluate conditional render string if set
 267  0
             if (StringUtils.isNotBlank(lineField.getConditionalRender())) {
 268  0
                 Map<String, Object> context = new HashMap<String, Object>();
 269  0
                 context.putAll(view.getContext());
 270  0
                 context.put(UifConstants.ContextVariableNames.PARENT, collectionGroup);
 271  0
                 context.put(UifConstants.ContextVariableNames.COMPONENT, lineField);
 272  0
                 context.put(UifConstants.ContextVariableNames.LINE, currentLine);
 273  0
                 context.put(UifConstants.ContextVariableNames.INDEX, new Integer(lineIndex));
 274  0
                 context.put(UifConstants.ContextVariableNames.IS_ADD_LINE, new Boolean(lineIndex == -1));
 275  
 
 276  0
                 Boolean render = (Boolean) getExpressionEvaluatorService().evaluateExpression(model, context,
 277  
                         lineField.getConditionalRender());
 278  0
                 lineField.setRender(render);
 279  
             }
 280  
 
 281  
             // only add line field if set to render or if it is hidden by
 282  
             // progressive render
 283  0
             if (lineField.isRender()
 284  
                     || (!lineField.isRender() && !StringUtils.isEmpty(lineField.getProgressiveRender()))) {
 285  0
                 fields.add(lineField);
 286  
             }
 287  
         }
 288  
 
 289  0
         return fields;
 290  
     }
 291  
     
 292  
     /**
 293  
      * Checks whether the given sub-collection should be rendered, any
 294  
      * conditional render string is evaluated
 295  
      * 
 296  
      * @param view
 297  
      *            - view instance the sub collection belongs to
 298  
      * @param model
 299  
      *            - object containing the view data
 300  
      * @param collectionGroup
 301  
      *            - collection group the sub collection belongs to
 302  
      * @param subCollectionGroup
 303  
      *            - sub collection group to check render status for
 304  
      * @return boolean true if sub collection should be rendered, false if it
 305  
      *         should not be rendered
 306  
      */
 307  
     protected boolean checkSubCollectionRender(View view, Object model, CollectionGroup collectionGroup,
 308  
             CollectionGroup subCollectionGroup) {
 309  
         // evaluate conditional render string if set
 310  0
         if (StringUtils.isNotBlank(subCollectionGroup.getConditionalRender())) {
 311  0
             Map<String, Object> context = new HashMap<String, Object>();
 312  0
             context.putAll(view.getContext());
 313  0
             context.put(UifConstants.ContextVariableNames.PARENT, collectionGroup);
 314  0
             context.put(UifConstants.ContextVariableNames.COMPONENT, subCollectionGroup);
 315  
 
 316  0
             Boolean render = (Boolean) getExpressionEvaluatorService().evaluateExpression(model, context,
 317  
                     subCollectionGroup.getConditionalRender());
 318  0
             subCollectionGroup.setRender(render);
 319  
         }
 320  
 
 321  0
         return subCollectionGroup.isRender();
 322  
     }
 323  
 
 324  
         /**
 325  
          * Creates new <code>ActionField</code> instances for the line
 326  
          * 
 327  
          * <p>
 328  
          * Adds context to the action fields for the given line so that the line the
 329  
          * action was performed on can be determined when that action is selected
 330  
          * </p>
 331  
          * 
 332  
          * @param view
 333  
          *            - view instance the collection belongs to
 334  
          * @param model
 335  
          *            - top level object containing the data
 336  
          * @param collectionGroup
 337  
          *            - collection group component for the collection
 338  
          * @param collectionLine
 339  
          *            - object instance for the current line
 340  
          * @param lineIndex
 341  
          *            - index of the line the actions should apply to
 342  
          */
 343  
         protected List<ActionField> getLineActions(View view, Object model, CollectionGroup collectionGroup,
 344  
                         Object collectionLine, int lineIndex) {
 345  0
                 List<ActionField> lineActions = ComponentUtils.copyFieldList(collectionGroup.getActionFields(), Integer.toString(lineIndex));
 346  0
                 for (ActionField actionField : lineActions) {
 347  0
                         actionField.addActionParameter(UifParameters.SELLECTED_COLLECTION_PATH, collectionGroup.getBindingInfo()
 348  
                                         .getBindingPath());
 349  0
                         actionField.addActionParameter(UifParameters.SELECTED_LINE_INDEX, Integer.toString(lineIndex));
 350  0
                         actionField.setJumpToIdAfterSubmit(collectionGroup.getId() + "_div");
 351  0
                         actionField.setClientSideJs("performCollectionAction('"+collectionGroup.getId()+"');");
 352  
                 }
 353  
 
 354  0
                 ComponentUtils.updateContextsForLine(lineActions, collectionLine, lineIndex);
 355  
 
 356  0
                 return lineActions;
 357  
         }
 358  
 
 359  
         /**
 360  
          * Creates new <code>ActionField</code> instances for the add line
 361  
          * 
 362  
          * <p>
 363  
          * Adds context to the action fields for the add line so that the collection
 364  
          * the action was performed on can be determined
 365  
          * </p>
 366  
          * 
 367  
          * @param view
 368  
          *            - view instance the collection belongs to
 369  
          * @param model
 370  
          *            - top level object containing the data
 371  
          * @param collectionGroup
 372  
          *            - collection group component for the collection
 373  
          */
 374  
         protected List<ActionField> getAddLineActions(View view, Object model, CollectionGroup collectionGroup) {
 375  0
                 List<ActionField> lineActions = ComponentUtils.copyFieldList(collectionGroup.getAddLineActionFields(), "_add");
 376  0
                 for (ActionField actionField : lineActions) {
 377  0
                         actionField.addActionParameter(UifParameters.SELLECTED_COLLECTION_PATH, collectionGroup.getBindingInfo()
 378  
                                         .getBindingPath());
 379  
                         //actionField.addActionParameter(UifParameters.COLLECTION_ID, collectionGroup.getId());
 380  0
                         actionField.setJumpToIdAfterSubmit(collectionGroup.getId() + "_div");
 381  0
                         actionField.addActionParameter(UifParameters.ACTION_TYPE, UifParameters.ADD_LINE);
 382  0
                         actionField.setClientSideJs("addLineToCollection('"+collectionGroup.getId()+"', '"+ collectionGroup.getBaseId() +"');");
 383  
                 }
 384  
 
 385  
                 // get add line for context
 386  0
                 String addLinePath = collectionGroup.getAddLineBindingInfo().getBindingPath();
 387  0
                 Object addLine = ObjectPropertyUtils.getPropertyValue(model, addLinePath);
 388  
 
 389  0
                 ComponentUtils.updateContextsForLine(lineActions, addLine, -1);
 390  
 
 391  0
                 return lineActions;
 392  
         }
 393  
 
 394  
     /**
 395  
      * Initializes a new instance of the collection class
 396  
      * 
 397  
      * <p>
 398  
      * If the add line property was not specified for the collection group the
 399  
      * new lines will be added to the generic map on the
 400  
      * <code>UifFormBase</code>, else it will be added to the property given by
 401  
      * the addLineBindingInfo
 402  
      * </p>
 403  
      * 
 404  
      * <p>
 405  
      * New line will only be created if the current line property is null or
 406  
      * clearExistingLine is true. In the case of a new line default values are
 407  
      * also applied
 408  
      * </p>
 409  
      * 
 410  
      * @see org.kuali.rice.krad.uif.container.CollectionGroup.
 411  
      *      initializeNewCollectionLine(View, Object, CollectionGroup, boolean)
 412  
      */
 413  
     public void initializeNewCollectionLine(View view, Object model, CollectionGroup collectionGroup,
 414  
             boolean clearExistingLine) {
 415  0
         Object newLine = null;
 416  
 
 417  
         // determine if we are binding to generic form map or a custom property
 418  0
         if (StringUtils.isBlank(collectionGroup.getAddLinePropertyName())) {
 419  
             // bind to form map
 420  0
             if (!(model instanceof UifFormBase)) {
 421  0
                 throw new RuntimeException("Cannot create new collection line for group: "
 422  
                         + collectionGroup.getPropertyName() + ". Model does not extend " + UifFormBase.class.getName());
 423  
             }
 424  
 
 425  
             // get new collection line map from form
 426  0
             Map<String, Object> newCollectionLines = ObjectPropertyUtils.getPropertyValue(model,
 427  
                     UifPropertyPaths.NEW_COLLECTION_LINES);
 428  0
             if (newCollectionLines == null) {
 429  0
                 newCollectionLines = new HashMap<String, Object>();
 430  0
                 ObjectPropertyUtils.setPropertyValue(model, UifPropertyPaths.NEW_COLLECTION_LINES, newCollectionLines);
 431  
             }
 432  
             
 433  
             // set binding path for add line
 434  0
             String newCollectionLineKey = KRADUtils
 435  
                     .translateToMapSafeKey(collectionGroup.getBindingInfo().getBindingPath());
 436  0
             String addLineBindingPath = UifPropertyPaths.NEW_COLLECTION_LINES + "['" + newCollectionLineKey + "']";
 437  0
             collectionGroup.getAddLineBindingInfo().setBindingPath(addLineBindingPath);
 438  
 
 439  
             // if there is not an instance available or we need to clear create
 440  
             // a new instance
 441  0
             if (!newCollectionLines.containsKey(newCollectionLineKey)
 442  
                     || (newCollectionLines.get(newCollectionLineKey) == null) || clearExistingLine) {
 443  
                 // create new instance of the collection type for the add line
 444  0
                 newLine = ObjectUtils.newInstance(collectionGroup.getCollectionObjectClass());
 445  0
                 newCollectionLines.put(newCollectionLineKey, newLine);
 446  
             }
 447  0
         } else {
 448  
             // bind to custom property
 449  0
             Object addLine = ObjectPropertyUtils.getPropertyValue(model, collectionGroup.getAddLineBindingInfo()
 450  
                     .getBindingPath());
 451  0
             if ((addLine == null) || clearExistingLine) {
 452  0
                 newLine = ObjectUtils.newInstance(collectionGroup.getCollectionObjectClass());
 453  0
                 ObjectPropertyUtils.setPropertyValue(model, collectionGroup.getAddLineBindingInfo().getBindingPath(),
 454  
                         newLine);
 455  
             }
 456  
         }
 457  
 
 458  
         // apply default values if a new line was created
 459  0
         if (newLine != null) {
 460  0
             view.getViewHelperService().applyDefaultValuesForCollectionLine(view, model, collectionGroup, newLine);
 461  
         }
 462  0
     }
 463  
     
 464  
     protected ExpressionEvaluatorService getExpressionEvaluatorService() {
 465  0
         return KRADServiceLocatorWeb.getExpressionEvaluatorService();
 466  
     }
 467  
 
 468  
 }