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