Coverage Report - org.kuali.student.common.ui.client.configurable.mvc.multiplicity.MultiplicityConfiguration
 
Classes in this File Line Coverage Branch Coverage Complexity
MultiplicityConfiguration
0%
0/96
0%
0/14
1.206
MultiplicityConfiguration$MultiplicityType
0%
0/1
N/A
1.206
MultiplicityConfiguration$StyleType
0%
0/2
N/A
1.206
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  * <p/>
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  * <p/>
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"                       
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  * <p/>
 15  
  */
 16  
 package org.kuali.student.common.ui.client.configurable.mvc.multiplicity;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.HashMap;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 
 23  
 import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor;
 24  
 import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle;
 25  
 import org.kuali.student.common.ui.client.widgets.field.layout.element.MessageKeyInfo;
 26  
 import org.kuali.student.core.assembly.data.Metadata;
 27  
 import org.kuali.student.core.assembly.data.QueryPath;
 28  
 
 29  
 /**
 30  
   *
 31  
   * MultiplicityConfiguration is passed into a MultiplicitySection to control the creation of the multiplicity.
 32  
   *
 33  
   * This class is used to control the Multiplicity, e.g. if its updateable, has headers, min number of items, labels etc.
 34  
   * It also holds FieldDescriptors for the fields required to be included in the multiplicity. They are
 35  
   * held in a HashMap keyed by rows, i.e. Map<Integer, List<FieldDescriptor>> .
 36  
   *
 37  
   * The parent for the multiplicity refers to the path to the high level field that owns
 38  
   * the repeating elements, e.g. course/versions is the parent field for course version fields versionCode and
 39  
   * versionTitle
 40  
   *
 41  
   * For multiplicities nested inside another multiplicity, create a MultiplicityConfiguration for the nested
 42  
   * multiplicity and call setNestedConfig to add the lower level multiplicity
 43  
   *
 44  
   *
 45  
  */
 46  
 public class MultiplicityConfiguration {
 47  
 
 48  
     private MultiplicityType multiplicityType;
 49  
     
 50  
     private StyleType styleType;
 51  0
     private boolean updateable = false;
 52  
 
 53  
     private String itemLabel;
 54  
     private String addItemLabel;
 55  
     
 56  
     private Metadata metaData;
 57  
     
 58  
     private SectionTitle title;
 59  
 
 60  0
     int row = 0;
 61  
     
 62  
     private FieldDescriptor parentFd;
 63  0
     private Map<Integer, List<MultiplicityFieldConfiguration>> fields = new HashMap<Integer, List<MultiplicityFieldConfiguration>>();
 64  0
     private Map<String, FieldDescriptor> fieldMap = new HashMap<String, FieldDescriptor>();
 65  0
     protected Map<String, String> concatenatedFields = new HashMap<String, String>();
 66  
     
 67  
     private MultiplicityConfiguration nestedConfig ;
 68  
     private MultiplicityGroup customMultiplicityGroup;
 69  
     private boolean showHeaders;
 70  
 
 71  0
     public static enum MultiplicityType {GROUP, TABLE }
 72  0
     public static enum StyleType {
 73  0
         TOP_LEVEL_GROUP, SUB_LEVEL_GROUP, BORDERLESS_TABLE, BORDERED_TABLE
 74  
     }
 75  
 
 76  
 
 77  
      /**
 78  
      *
 79  
      * Creates a new MultiplicityConfiguration.
 80  
      *
 81  
      *
 82  
      * @param multiplicityType      the type of multiplicity required, MultiplicityType
 83  
      * @param styleType             the style type for this multiplicity, StyleType
 84  
      * @param metaData              the metadata for the parentFd
 85  
      */
 86  0
      public MultiplicityConfiguration(MultiplicityType multiplicityType, StyleType styleType, Metadata metaData) {
 87  0
         this.multiplicityType = multiplicityType;
 88  0
         this.styleType = styleType;
 89  0
         this.metaData = metaData;
 90  0
     }
 91  
 
 92  
     /**
 93  
      * Causes a line throw so that the subsequent fields are placed on the next line of the screen
 94  
      *
 95  
      */
 96  
     public void nextLine() {
 97  0
             row++;
 98  0
     }
 99  
 
 100  
     /**
 101  
      * Includes this field (as defined by the FieldDescriptor) on the current line at the next horizontal position
 102  
      *
 103  
      *
 104  
      * @param fieldDescriptor
 105  
      */
 106  
     public void addFieldConfiguration(MultiplicityFieldConfiguration fieldDescriptor) {
 107  
             
 108  
         List<MultiplicityFieldConfiguration> fds;
 109  0
         if (fields.containsKey(row)) {
 110  0
             fds = fields.get(row);
 111  
         }
 112  
         else {
 113  0
             fds = new ArrayList<MultiplicityFieldConfiguration>();
 114  
         }
 115  0
         fds.add(fieldDescriptor);
 116  0
         fields.put(row, fds);
 117  0
     }
 118  
     
 119  
     public void setFields(Map<Integer, List<MultiplicityFieldConfiguration>> fields) {
 120  0
         if (fields == null || fields.isEmpty()) {
 121  0
             row = 0;
 122  
         } else {
 123  0
             row = fields.size() - 1;
 124  
         }
 125  0
         this.fields = fields;
 126  0
     }
 127  
 
 128  
         /**
 129  
          * Concatenates multiple field values into a single table cell
 130  
      *
 131  
      *  !!!!!  Currently only implemented for MultiplicityTable !!!!!
 132  
      *
 133  
      * The parentField should define a field with a Data value. The fieldKey should resolve to a single value
 134  
          * field in that Data value. The binding will iterate the values in the Data object
 135  
          * and concatenate the named fieldKeys into the same cell in the current row of the table
 136  
          * separated by commas.
 137  
      *
 138  
      * e.g. parentFD = "course/versions", fieldKey = "versionCode", version codes will be concatenated to
 139  
      * display "code1, code2, code3" in a single table cell
 140  
          *
 141  
          * @param fieldKey
 142  
          */
 143  
         public void addConcatenatedField(MultiplicityFieldConfiguration parentField, String fieldKey){
 144  0
                 addFieldConfiguration(parentField);
 145  0
                 concatenatedFields.put(parentField.getFieldPath(), fieldKey);
 146  0
         }
 147  
 
 148  
     /**
 149  
      * Creates a copy of this MultiplicityConfiguration. Used when creating a new MultiplicityItem
 150  
      *
 151  
      * @return  MultiplicityConfiguration
 152  
      */
 153  
     public MultiplicityConfiguration copy() {
 154  0
         MultiplicityConfiguration copy = new MultiplicityConfiguration(getMultiplicityType(), getStyleType(), getMetaData());
 155  0
         copy.setAddItemLabel(getAddItemLabel());
 156  0
         copy.setItemLabel(getItemLabel());
 157  0
         copy.setMetaData(getMetaData());
 158  0
         copy.setUpdateable(isUpdateable());
 159  0
         if (getNestedConfig() != null) {
 160  0
             copy.setNestedConfig(getNestedConfig().copy());
 161  
         }
 162  0
         FieldDescriptor parent = new FieldDescriptor(getParentFd().getFieldKey(), getParentFd().getMessageKey(), getParentFd().getMetadata());
 163  0
         if(!getParentFd().isLabelShown()){
 164  0
                 parent.hideLabel();
 165  
         }
 166  0
         copy.setParent(parent);
 167  0
         for (Integer row  : getFields().keySet()) {
 168  0
             List<MultiplicityFieldConfiguration> fields = getFields().get(row);
 169  0
             for (MultiplicityFieldConfiguration fieldConfig : fields) {
 170  0
                 MultiplicityFieldConfiguration newfieldConfig = new MultiplicityFieldConfiguration(fieldConfig.getFieldPath(), fieldConfig.getMessageKeyInfo(), fieldConfig.getMetadata(), fieldConfig.getFieldWidgetInitializer());
 171  0
                 newfieldConfig.setOptional(fieldConfig.isOptional());
 172  0
                 newfieldConfig.setModelWidgetBinding(fieldConfig.getModelWidgetBinding());
 173  0
                 copy.addFieldConfiguration(newfieldConfig);
 174  0
             }
 175  0
             copy.nextLine();
 176  0
         }
 177  0
         return copy;
 178  
     }
 179  
 
 180  
     public MultiplicityType getMultiplicityType() {
 181  0
         return multiplicityType;
 182  
     }
 183  
 
 184  
      /**
 185  
       * Sets the MultiplicityType required for this config
 186  
       *
 187  
       * Valid values are defined in {@link #MultiplicityConfiguration.MultiplicityType}
 188  
       * @param multiplicityType
 189  
       */
 190  
      public void setMultiplicityType(MultiplicityType multiplicityType) {
 191  0
         this.multiplicityType = multiplicityType;
 192  0
     }
 193  
 
 194  
         public FieldDescriptor getParentFd() {
 195  0
                 return parentFd;
 196  
         }
 197  
 
 198  
     /**
 199  
      * The parent fd defines the high level parent field that contains the repeating elements
 200  
      *
 201  
      * @param parentFd
 202  
      */
 203  
     public void setParent(FieldDescriptor parentFd) {
 204  0
                 this.parentFd = parentFd;
 205  0
         }
 206  
 
 207  
 //        public Map<Integer, List<FieldDescriptor>> getFields() {return null;}
 208  
 
 209  
         public Map<Integer, List<MultiplicityFieldConfiguration>> getFields() {
 210  0
                 return fields;
 211  
         }
 212  
 
 213  
     public String getItemLabel() {
 214  0
         return itemLabel;
 215  
     }
 216  
 
 217  
     /**
 218  
      * Sets text to be used as the header for each multiplicity item
 219  
      * @param itemLabel
 220  
      */
 221  
     public void setItemLabel(String itemLabel) {
 222  0
         this.itemLabel = itemLabel;
 223  0
     }
 224  
 
 225  
     public String getAddItemLabel() {
 226  0
         return addItemLabel;
 227  
     }
 228  
 
 229  
     /**
 230  
      * Sets text to be used as the button text to add a new empty item in the multiplicity
 231  
      * @param addItemLabel
 232  
      */
 233  
     public void setAddItemLabel(String addItemLabel) {
 234  0
         this.addItemLabel = addItemLabel;
 235  0
     }
 236  
 
 237  
         public StyleType getStyleType() {
 238  0
                 return styleType;
 239  
         }
 240  
 
 241  
         public void setStyleType(StyleType styleType) {
 242  0
                 this.styleType = styleType;
 243  0
         }
 244  
 
 245  
         public MultiplicityConfiguration getNestedConfig() {
 246  0
                 return nestedConfig;
 247  
         }
 248  
 
 249  
     /**
 250  
      * If this multiplicity is to contain other nested multiplicities, create a MultiplicityConfiguration for the child
 251  
      * multiplicity and set as a nested config in the parent.
 252  
      *
 253  
      * @param config
 254  
      */
 255  
     public void setNestedConfig(MultiplicityConfiguration config) {
 256  0
                 this.nestedConfig = config;
 257  0
         }
 258  
 
 259  
     public boolean isShowHeaders() {
 260  0
          return showHeaders;
 261  
     }
 262  
 
 263  
     /**
 264  
      *
 265  
      * @param showHeaders
 266  
      */
 267  
     public void setShowHeaders(boolean showHeaders) {
 268  0
         this.showHeaders = showHeaders;
 269  0
     }
 270  
 
 271  
         public Map<String, String> getConcatenatedFields() {
 272  0
                 return concatenatedFields;
 273  
         }
 274  
 
 275  
     public boolean isUpdateable() {
 276  0
         return updateable;
 277  
     }
 278  
 
 279  
     /**
 280  
      * Sets if this multiplicity will be updateable or display.
 281  
      * Add delete buttons/links will not be shown for display multiplicities
 282  
      *
 283  
      * @param updateable
 284  
      */
 285  
     public void setUpdateable(boolean updateable) {
 286  0
         this.updateable = updateable;
 287  0
     }
 288  
 
 289  
     public MultiplicityType getLayoutType() {
 290  0
         return multiplicityType;
 291  
     }
 292  
 
 293  
     public void setLayoutType(MultiplicityType multiplicityType) {
 294  0
         this.multiplicityType = multiplicityType;
 295  0
     }
 296  
 
 297  
         public Metadata getMetaData() {
 298  0
                 return metaData;
 299  
         }
 300  
 
 301  
         public void setMetaData(Metadata metaData) {
 302  0
                 this.metaData = metaData;
 303  0
         }
 304  
 
 305  
 
 306  
      public SectionTitle getTitle() {
 307  0
         return title;
 308  
     }
 309  
 
 310  
     public void setTitle(SectionTitle title) {
 311  0
         this.title = title;
 312  0
     }
 313  
 
 314  
     /**
 315  
      * Creates a field descriptor for the parent for this multiplicity
 316  
      * This defines the high level parent field that contains the repeating elements
 317  
      * Will use default widget and binding. For more complex fields create your own
 318  
      * fieldDescriptor and use the other setParent method
 319  
      *
 320  
      * @param fieldKey
 321  
      * @param messageKey
 322  
      * @param parentPath
 323  
      * @param meta
 324  
      */
 325  
     public void setParent(String fieldKey, String messageKey,  String parentPath, Metadata meta) {
 326  
 
 327  0
         QueryPath path = QueryPath.concat(parentPath, fieldKey);
 328  0
         FieldDescriptor fd = new FieldDescriptor(path.toString(), new MessageKeyInfo(messageKey), meta);
 329  0
         fd.getFieldElement().setRequired(false);
 330  
 
 331  0
         setParent(fd);
 332  
 
 333  0
     }
 334  
 
 335  
     /**
 336  
      * Includes this field on the current line at the next horizontal position
 337  
      *
 338  
      * Will use default widget and binding. For more complex fields create your own
 339  
      * FieldDescriptor and pass it in using the other addField method
 340  
      *
 341  
      * @param fieldKey
 342  
      * @param messageKey
 343  
      * @param parentPath
 344  
      * @param meta
 345  
      */
 346  
     public void addField(String fieldKey, String messageKey,  String parentPath, Metadata meta) {
 347  
 
 348  0
         QueryPath path = QueryPath.concat(parentPath, QueryPath.getWildCard(), fieldKey);
 349  
 
 350  0
         MultiplicityFieldConfiguration fieldConfig = new MultiplicityFieldConfiguration(path.toString(), new MessageKeyInfo(messageKey), meta, null);
 351  0
         fieldConfig.setRequired(false);
 352  0
         addFieldConfiguration(fieldConfig);
 353  0
     }
 354  
 
 355  
 //    private String translatePath(String path, Metadata metadata) {
 356  
 //
 357  
 //        String result = path;
 358  
 //
 359  
 //        QueryPath qPath = QueryPath.parse(path);
 360  
 //
 361  
 //        if(metadata!=null&&metadata.getInitialLookup()!=null){
 362  
 //            QueryPath translationPath = qPath.subPath(0, qPath.size()-1);
 363  
 //            if (metadata.getDataType().equals(Data.DataType.STRING)) {
 364  
 //                translationPath.add(new Data.StringKey("_runtimeData"));
 365  
 //                translationPath.add(new Data.StringKey((String)qPath.get(qPath.size() - 1).get()));
 366  
 //                translationPath.add(new Data.StringKey("id-translation"));
 367  
 //                result = translationPath.toString();
 368  
 //            }
 369  
 //        }
 370  
 //        return result;
 371  
 //    }
 372  
 
 373  
     public MultiplicityGroup getCustomMultiplicityGroup() {
 374  0
         return customMultiplicityGroup;
 375  
     }
 376  
 
 377  
     public void setCustomMultiplicityGroup(MultiplicityGroup customMultiplicityGroup) {
 378  0
         this.customMultiplicityGroup = customMultiplicityGroup;
 379  0
     }
 380  
     
 381  
     
 382  
 }
 383