Coverage Report - org.kuali.rice.kns.kim.type.DataDictionaryTypeServiceHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
DataDictionaryTypeServiceHelper
0%
0/138
0%
0/76
5.267
DataDictionaryTypeServiceHelper$1
0%
0/8
N/A
5.267
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.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/ecl2.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.kim.type;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.core.api.uif.Control;
 20  
 import org.kuali.rice.core.api.uif.RemotableAbstractControl;
 21  
 import org.kuali.rice.core.api.uif.RemotableAbstractWidget;
 22  
 import org.kuali.rice.core.api.uif.RemotableAttributeField;
 23  
 import org.kuali.rice.core.api.uif.RemotableCheckboxGroup;
 24  
 import org.kuali.rice.core.api.uif.RemotableDatepicker;
 25  
 import org.kuali.rice.core.api.uif.RemotableHiddenInput;
 26  
 import org.kuali.rice.core.api.uif.RemotableQuickFinder;
 27  
 import org.kuali.rice.core.api.uif.RemotableRadioButtonGroup;
 28  
 import org.kuali.rice.core.api.uif.RemotableSelect;
 29  
 import org.kuali.rice.core.api.uif.RemotableTextExpand;
 30  
 import org.kuali.rice.core.api.uif.RemotableTextInput;
 31  
 import org.kuali.rice.core.api.uif.RemotableTextarea;
 32  
 import org.kuali.rice.kim.api.KimConstants;
 33  
 import org.kuali.rice.kim.api.type.KimAttributeField;
 34  
 import org.kuali.rice.kns.datadictionary.control.CheckboxControlDefinition;
 35  
 import org.kuali.rice.kns.datadictionary.control.HiddenControlDefinition;
 36  
 import org.kuali.rice.kns.datadictionary.control.MultiselectControlDefinition;
 37  
 import org.kuali.rice.kns.datadictionary.control.RadioControlDefinition;
 38  
 import org.kuali.rice.kns.datadictionary.control.SelectControlDefinition;
 39  
 import org.kuali.rice.kns.datadictionary.control.TextControlDefinition;
 40  
 import org.kuali.rice.kns.datadictionary.control.TextareaControlDefinition;
 41  
 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
 42  
 import org.kuali.rice.krad.datadictionary.control.ControlDefinition;
 43  
 import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
 44  
 import org.kuali.rice.krad.datadictionary.validation.ValidationPattern;
 45  
 import org.kuali.rice.krad.keyvalues.KeyValuesFinder;
 46  
 import org.kuali.rice.krad.keyvalues.KeyValuesFinderFactory;
 47  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 48  
 
 49  
 import java.util.ArrayList;
 50  
 import java.util.Collection;
 51  
 import java.util.Collections;
 52  
 import java.util.List;
 53  
 import java.util.Map;
 54  
 import java.util.regex.Pattern;
 55  
 
 56  
 /**
 57  
  * @deprecated A krad integrated type service base class will be provided in the future.
 58  
  * This is only used for the legacy {@link DataDictionaryTypeServiceBase}.
 59  
  */
 60  
 @Deprecated
 61  
 public final class DataDictionaryTypeServiceHelper {
 62  
 
 63  0
     private DataDictionaryTypeServiceHelper() {
 64  0
         throw new UnsupportedOperationException("do not call");
 65  
     }
 66  
 
 67  
     public static String getKimBasePath(){
 68  0
             String kimBaseUrl = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(KimConstants.KimUIConstants.KIM_URL_KEY);
 69  0
             if (!kimBaseUrl.endsWith(KimConstants.KimUIConstants.URL_SEPARATOR)) {
 70  0
                     kimBaseUrl = kimBaseUrl + KimConstants.KimUIConstants.URL_SEPARATOR;
 71  
             }
 72  0
             return kimBaseUrl;
 73  
         }
 74  
 
 75  
     public static RemotableAbstractControl.Builder toRemotableAbstractControlBuilder(AttributeDefinition attr) {
 76  0
             ControlDefinition control = attr.getControl();
 77  
 
 78  0
             if (control.isCheckbox()) {
 79  0
                  return RemotableCheckboxGroup.Builder.create(getValues(attr));
 80  0
             } else if (control.isHidden()) {
 81  0
                 return RemotableHiddenInput.Builder.create();
 82  0
             } else if (control.isMultiselect()) {
 83  0
                 RemotableSelect.Builder b = RemotableSelect.Builder.create(getValues(attr));
 84  0
                 b.setMultiple(true);
 85  0
                 b.setSize(control.getSize());
 86  0
             } else if (control.isRadio()) {
 87  0
                 return RemotableRadioButtonGroup.Builder.create(getValues(attr));
 88  0
             } else if (control.isSelect()) {
 89  0
                 RemotableSelect.Builder b = RemotableSelect.Builder.create(getValues(attr));
 90  0
                 b.setMultiple(false);
 91  0
                 b.setSize(control.getSize());
 92  0
             } else if (control.isText()) {
 93  0
                 final RemotableTextInput.Builder b = RemotableTextInput.Builder.create();
 94  0
                 b.setSize(control.getSize());
 95  0
                 return b;
 96  0
             } else if (control.isTextarea()) {
 97  0
                 final RemotableTextarea.Builder b = RemotableTextarea.Builder.create();
 98  0
                 b.setCols(control.getCols());
 99  0
                 b.setRows(control.getRows());
 100  0
                 return b;
 101  
             }
 102  0
         return null;
 103  
     }
 104  
 
 105  
     /**
 106  
      * will first try to execute the values finder.  If that doesn't return any values then will try to use the optionfinder
 107  
      * on the AttributeDefinition.
 108  
      *
 109  
      * @param attr AttributeDefinition
 110  
      * @return a Map of key value pairs
 111  
      */
 112  
     private static Map<String, String> getValues(AttributeDefinition attr) {
 113  0
         ControlDefinition control = attr.getControl();
 114  
 
 115  
         try {
 116  0
             final Class<KeyValuesFinder> clazz = (Class<KeyValuesFinder>) Class.forName(control.getValuesFinderClass());
 117  0
             final KeyValuesFinder finder = clazz.newInstance();
 118  0
             final Map<String, String> values = finder.getKeyLabelMap();
 119  0
             if ((values != null) && !values.isEmpty()) {
 120  0
                return values;
 121  
             }
 122  0
             else if (attr.getOptionsFinder() != null) {
 123  0
                 return attr.getOptionsFinder().getKeyLabelMap();
 124  
             }
 125  0
         } catch (ClassNotFoundException e) {
 126  0
             throw new RuntimeException(e);
 127  0
         } catch (InstantiationException e) {
 128  0
             throw new RuntimeException(e);
 129  0
         } catch (IllegalAccessException e) {
 130  0
             throw new RuntimeException(e);
 131  0
         }
 132  0
         return Collections.emptyMap();
 133  
     }
 134  
 
 135  
     public static List<KimAttributeDefinition> toKimAttributeDefinitions(List<KimAttributeField> fields) {
 136  0
         if (fields == null) {
 137  0
             throw new IllegalArgumentException("fields was null");
 138  
         }
 139  
 
 140  0
         final List<KimAttributeDefinition> defns = new ArrayList<KimAttributeDefinition>();
 141  0
         for (KimAttributeField field : fields) {
 142  0
             defns.add(toKimAttributeDefinition(field));
 143  
         }
 144  
 
 145  0
         return Collections.unmodifiableList(defns);
 146  
     }
 147  
 
 148  
     public static KimAttributeDefinition toKimAttributeDefinition(KimAttributeField field) {
 149  0
         if (field == null) {
 150  0
             throw new IllegalArgumentException("field is null");
 151  
         }
 152  
 
 153  0
         KimAttributeDefinition ad = new KimAttributeDefinition();
 154  0
         ad.setKimAttrDefnId(field.getId());
 155  0
         ad.setUnique(field.isUnique());
 156  
 
 157  0
         final RemotableAttributeField attr = field.getAttributeField();
 158  0
         ad.setName(attr.getName());
 159  0
         ad.setDataType(attr.getDataType());
 160  0
         ad.setShortLabel(attr.getShortLabel());
 161  0
         ad.setLabel(attr.getLongLabel());
 162  0
         ad.setSummary(attr.getHelpSummary());
 163  0
         ad.setConstraintText(attr.getHelpConstraint());
 164  0
         ad.setDescription(attr.getHelpDescription());
 165  0
         ad.setForceUppercase(attr.isForceUpperCase());
 166  0
         ad.setMinLength(attr.getMinLength());
 167  0
         ad.setMaxLength(attr.getMaxLength());
 168  0
         ad.setExclusiveMin(attr.getMinValue() != null ? attr.getMinValue().toString() : null);
 169  0
         ad.setInclusiveMax(attr.getMaxValue() != null ? attr.getMaxValue().toString() : null);
 170  0
         if (StringUtils.isNotBlank(attr.getRegexConstraint())) {
 171  0
             ValidationPattern pattern = new ValidationPattern() {
 172  
 
 173  
                 @Override
 174  
                 public Pattern getRegexPattern() {
 175  0
                     return Pattern.compile(getRegexString());
 176  
                 }
 177  
 
 178  
                 @Override
 179  
                 protected String getRegexString() {
 180  0
                     return attr.getRegexConstraint();
 181  
                 }
 182  
 
 183  
                 @Override
 184  
                 public ExportMap buildExportMap(String exportKey) {
 185  0
                     ExportMap exportMap = new ExportMap(exportKey);
 186  0
                     exportMap.set("type", "regex");
 187  0
                     exportMap.set("pattern", getRegexString());
 188  
 
 189  0
                     return exportMap;
 190  
                 }
 191  
 
 192  
                 @Override
 193  
                 public String getValidationErrorMessageKey() {
 194  0
                     return attr.getRegexContraintMsg();
 195  
                 }
 196  
             };
 197  0
             ad.setValidationPattern(pattern);
 198  
         }
 199  0
         ad.setRequired(attr.isRequired());
 200  
 
 201  0
         final Control control = field.getAttributeField().getControl();
 202  
 
 203  0
         if (control != null) {
 204  0
             ControlDefinition d = toControlDefinition(control, ad);
 205  0
             for (RemotableAbstractWidget widget : field.getAttributeField().getWidgets()) {
 206  0
                 if(widget instanceof RemotableQuickFinder) {
 207  0
                     ad.setLookupBoClass(((RemotableQuickFinder) widget).getDataObjectClass());
 208  0
                     ad.setLookupInputPropertyConversions(((RemotableQuickFinder) widget).getLookupParameters());
 209  0
                     ad.setLookupReturnPropertyConversions(((RemotableQuickFinder) widget).getFieldConversions());
 210  0
                 } else if (widget instanceof RemotableDatepicker && d != null) {
 211  0
                     d.setDatePicker(true);
 212  0
                 } else if (widget instanceof RemotableTextExpand && d != null) {
 213  0
                     d.setExpandedTextArea(true);
 214  
                 }
 215  
             }
 216  0
             ad.setControl(d);
 217  
         }
 218  
 
 219  0
         return ad;
 220  
     }
 221  
 
 222  
     /** WARNING HACK! this may set the OptionsFinder instance on the passed in KimAttributeDefinition! */
 223  
     private static ControlDefinition toControlDefinition(Control control, KimAttributeDefinition containingAttribute) {
 224  0
         if (control instanceof RemotableCheckboxGroup) {
 225  0
             containingAttribute.setOptionsFinder(KeyValuesFinderFactory.fromMap(((RemotableCheckboxGroup) control).getKeyLabels()));
 226  0
             CheckboxControlDefinition checkbox = new CheckboxControlDefinition();
 227  0
             return checkbox;
 228  
 
 229  0
         } else if (control instanceof RemotableHiddenInput) {
 230  0
             HiddenControlDefinition hidden = new HiddenControlDefinition();
 231  0
             return hidden;
 232  0
         } else if (control instanceof RemotableRadioButtonGroup) {
 233  0
             containingAttribute.setOptionsFinder(KeyValuesFinderFactory.fromMap(((RemotableRadioButtonGroup) control).getKeyLabels()));
 234  0
             RadioControlDefinition radio = new RadioControlDefinition();
 235  0
             return radio;
 236  
 
 237  0
         } else if (control instanceof RemotableSelect) {
 238  0
             containingAttribute.setOptionsFinder(KeyValuesFinderFactory.fromMap(((RemotableSelect) control).getKeyLabels()));
 239  0
             if (((RemotableSelect) control).isMultiple()) {
 240  0
                 MultiselectControlDefinition multiSelect = new MultiselectControlDefinition();
 241  0
                 multiSelect.setSize(((RemotableSelect) control).getSize());
 242  0
                 return multiSelect;
 243  
             } else {
 244  0
                 SelectControlDefinition select = new SelectControlDefinition();
 245  0
                 select.setSize(((RemotableSelect) control).getSize());
 246  0
                 return select;
 247  
             }
 248  0
         } else if (control instanceof RemotableTextarea) {
 249  0
             TextareaControlDefinition textarea = new TextareaControlDefinition();
 250  0
             textarea.setRows(((RemotableTextarea) control).getRows());
 251  0
             textarea.setCols(((RemotableTextarea) control).getCols());
 252  0
             return textarea;
 253  0
         } else if (control instanceof RemotableTextInput) {
 254  0
             TextControlDefinition text = new TextControlDefinition();
 255  0
             text.setSize(((RemotableTextInput) control).getSize());
 256  0
             return text;
 257  
         }
 258  0
         return null;
 259  
     }
 260  
 
 261  
         /**
 262  
      * Utility method to search a collection of attribute fields and returns
 263  
      * a field for a give attribute name.
 264  
      *
 265  
      * @param attributeName the name of the attribute to search for.  Cannot be blank or null.
 266  
      * @param fields cannot be null.
 267  
      *
 268  
      * @return the attribute field or null if not found.
 269  
      */
 270  
     public static <T extends KimAttributeField> T findAttributeField(String attributeName, Collection<? extends T> fields) {
 271  0
         if (StringUtils.isBlank(attributeName)) {
 272  0
             throw new IllegalArgumentException("attributeName is blank");
 273  
         }
 274  
 
 275  0
         if (fields == null) {
 276  0
             throw new IllegalArgumentException("fields is null");
 277  
         }
 278  
 
 279  0
         for (T field : fields) {
 280  0
             if (attributeName.equals(field.getAttributeField().getName())) {
 281  0
                 return field;
 282  
             }
 283  
         }
 284  0
         return null;
 285  
     }
 286  
 
 287  
     public static String createErrorString(KimAttributeField definition) {
 288  0
         return definition.getAttributeField().getRegexContraintMsg();
 289  
     }
 290  
 
 291  
      /** will create a string like the following:
 292  
      * errorKey:param1;param2;param3;
 293  
      *
 294  
      * @param errorKey the errorKey
 295  
      * @param params the error params
 296  
      * @return error string
 297  
      */
 298  
     public static String createErrorString(String errorKey, String... params) {
 299  0
         final StringBuilder s = new StringBuilder(errorKey).append(':');
 300  0
         if (params != null) {
 301  0
             for (String p : params) {
 302  0
                 if (p != null) {
 303  0
                     s.append(p);
 304  0
                     s.append(';');
 305  
                 }
 306  
             }
 307  
         }
 308  0
         return s.toString();
 309  
     }
 310  
 
 311  
     public static String getAttributeErrorLabel(KimAttributeField definition) {
 312  0
         String longAttributeLabel = definition.getAttributeField().getLongLabel();
 313  0
         String shortAttributeLabel = definition.getAttributeField().getShortLabel();
 314  0
         return longAttributeLabel + " (" + shortAttributeLabel + ")";
 315  
     }
 316  
 }