Coverage Report - org.kuali.rice.krad.util.KRADUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
KRADUtils
0%
0/84
0%
0/36
2.625
 
 1  
 /*
 2  
  * Copyright 2007-2009 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.krad.util;
 17  
 
 18  
 import java.lang.reflect.Constructor;
 19  
 import java.text.NumberFormat;
 20  
 import java.util.ArrayList;
 21  
 import java.util.Arrays;
 22  
 import java.util.HashMap;
 23  
 import java.util.List;
 24  
 import java.util.Map;
 25  
 
 26  
 import org.apache.commons.lang.StringUtils;
 27  
 import org.kuali.rice.core.util.AttributeSet;
 28  
 import org.kuali.rice.core.util.type.KualiDecimal;
 29  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 30  
 import org.kuali.rice.krad.service.KualiModuleService;
 31  
 import org.kuali.rice.krad.service.ModuleService;
 32  
 
 33  
 /**
 34  
  * Miscellaneous Utility Methods.
 35  
  * 
 36  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 37  
  */
 38  
 public final class KRADUtils {
 39  
         private static KualiModuleService kualiModuleService;
 40  
         
 41  0
         private KRADUtils() {
 42  0
                 throw new UnsupportedOperationException("do not call");
 43  
         }
 44  
     
 45  
     public final static String getBusinessTitleForClass(Class<? extends Object> clazz) {
 46  0
         if (clazz == null) {
 47  0
             throw new IllegalArgumentException("The getBusinessTitleForClass method of KRADUtils requires a non-null class");
 48  
         }
 49  0
         String className = clazz.getSimpleName();
 50  
     
 51  0
         StringBuffer label = new StringBuffer(className.substring(0, 1));
 52  0
         for (int i = 1; i < className.length(); i++) {
 53  0
             if (Character.isLowerCase(className.charAt(i))) {
 54  0
                 label.append(className.charAt(i));
 55  
             } else {
 56  0
                 label.append(" ").append(className.charAt(i));
 57  
             }
 58  
         }
 59  0
         return label.toString().trim();
 60  
     }
 61  
 
 62  
     /**
 63  
      * Picks off the filename from the full path. Takes care of different OS seperators.
 64  
      */
 65  
     public final static List<String> getFileNameFromPath(List<String> fullFileNames) {
 66  0
         List<String> fileNameList = new ArrayList<String>();
 67  
 
 68  0
         for (String fullFileName : fullFileNames) {
 69  0
             if (StringUtils.contains(fullFileName, "/")) {
 70  0
                 fileNameList.add(StringUtils.substringAfterLast(fullFileName, "/"));
 71  
             }
 72  
             else {
 73  0
                 fileNameList.add(StringUtils.substringAfterLast(fullFileName, "\\"));
 74  
             }
 75  
         }
 76  
 
 77  0
         return fileNameList;
 78  
     }
 79  
 
 80  0
     private static final KualiDecimal ONE_HUNDRED = new KualiDecimal("100.00");
 81  
     /**
 82  
      * Convert the given monney amount into an interger string. Since the return string cannot have decimal point, multiplies the
 83  
      * amount by 100 so the decimal places are not lost, for example, 320.15 is converted into 32015. 
 84  
      * 
 85  
      * @return an integer string of the given money amount through multiplicating by 100 and removing the fraction portion.
 86  
      */
 87  
     public final static String convertDecimalIntoInteger(KualiDecimal decimalNumber) {
 88  0
         KualiDecimal decimalAmount = decimalNumber.multiply(ONE_HUNDRED);
 89  0
         NumberFormat formatter = NumberFormat.getIntegerInstance();
 90  0
         String formattedAmount = formatter.format(decimalAmount);
 91  
 
 92  0
         return StringUtils.replace(formattedAmount, ",", "");
 93  
     }
 94  
     
 95  
         public static Integer getIntegerValue(String numberStr){
 96  0
                 Integer numberInt = null;
 97  
                 try{
 98  0
                         numberInt = new Integer(numberStr);
 99  0
                 } catch(NumberFormatException nfe){
 100  0
                         Double numberDbl = new Double(numberStr);
 101  0
                         numberInt = new Integer(numberDbl.intValue());
 102  0
                 }
 103  0
                 return numberInt;
 104  
         }
 105  
 
 106  
         public static Object createObject(Class<?> clazz, Class<?>[] argumentClasses, Object[] argumentValues) {
 107  0
                 if(clazz==null)
 108  0
                         return null;
 109  
                 try {
 110  0
                         Constructor<?> constructor = clazz.getConstructor(argumentClasses);
 111  0
                         return constructor.newInstance(argumentValues);
 112  0
             } catch (Exception e) {
 113  0
                       return null;
 114  
             }
 115  
         }
 116  
 
 117  
         public static String joinWithQuotes(List<String> list){
 118  0
                 if(list==null || list.size()==0) return "";
 119  
 
 120  0
                 return KRADConstants.SINGLE_QUOTE+
 121  
                         StringUtils.join(list.iterator(), KRADConstants.SINGLE_QUOTE+","+ KRADConstants.SINGLE_QUOTE)+
 122  
                         KRADConstants.SINGLE_QUOTE;
 123  
         }
 124  
         
 125  
         private static KualiModuleService getKualiModuleService() {
 126  0
                 if (kualiModuleService == null) {
 127  0
                         kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
 128  
                 }
 129  0
                 return kualiModuleService;
 130  
         }
 131  
         
 132  
         
 133  
         /**
 134  
          * TODO this method will probably need to be exposed in a public KRADUtils class as it is used
 135  
          * by several different modules.  That will have to wait until ModuleService and KualiModuleService are moved
 136  
          * to core though.
 137  
          */
 138  
         public static String getNamespaceCode(Class<? extends Object> clazz) {
 139  0
                 ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
 140  0
                 if (moduleService == null) {
 141  0
                         return KRADConstants.DEFAULT_NAMESPACE;
 142  
                 }
 143  0
                 return moduleService.getModuleConfiguration().getNamespaceCode();
 144  
         }
 145  
         
 146  
         public static AttributeSet getNamespaceAndComponentSimpleName( Class<? extends Object> clazz) {
 147  0
                 AttributeSet attributeSet = new AttributeSet();
 148  0
                 attributeSet.put(KRADConstants.NAMESPACE_CODE, getNamespaceCode(clazz));
 149  0
                 attributeSet.put(KRADConstants.COMPONENT_NAME, getComponentSimpleName(clazz));
 150  0
                 return attributeSet;
 151  
         }
 152  
 
 153  
         public static AttributeSet getNamespaceAndComponentFullName( Class<? extends Object> clazz) {
 154  0
                 AttributeSet attributeSet = new AttributeSet();
 155  0
                 attributeSet.put(KRADConstants.NAMESPACE_CODE, getNamespaceCode(clazz));
 156  0
                 attributeSet.put(KRADConstants.COMPONENT_NAME, getComponentFullName(clazz));
 157  0
                 return attributeSet;
 158  
         }
 159  
 
 160  
         public static AttributeSet getNamespaceAndActionClass( Class<? extends Object> clazz) {
 161  0
                 AttributeSet attributeSet = new AttributeSet();
 162  0
                 attributeSet.put(KRADConstants.NAMESPACE_CODE, getNamespaceCode(clazz));
 163  0
                 attributeSet.put(KRADConstants.ACTION_CLASS, clazz.getName());
 164  0
                 return attributeSet;
 165  
         }
 166  
         
 167  
         private static String getComponentSimpleName(Class<? extends Object> clazz) {
 168  0
                 return clazz.getSimpleName();
 169  
         }
 170  
 
 171  
         private static String getComponentFullName(Class<? extends Object> clazz) {
 172  0
                 return clazz.getName();
 173  
         }
 174  
 
 175  
     /**
 176  
      * Parses a string that is in map format (commas separating map entries, colon separates
 177  
      * map key/value) to a new map instance
 178  
      *
 179  
      * @param parameter - string parameter to parse
 180  
      * @return Map<String, String> instance populated from string parameter
 181  
      */
 182  
     public static Map<String, String> convertStringParameterToMap(String parameter) {
 183  0
         Map<String, String> map = new HashMap<String, String>();
 184  
 
 185  0
         if (StringUtils.isNotBlank(parameter)) {
 186  0
             if (StringUtils.contains(parameter, ",")) {
 187  0
                 String[] fieldConversions = StringUtils.split(parameter, ",");
 188  
 
 189  0
                 for (int i = 0; i < fieldConversions.length; i++) {
 190  0
                     String fieldConversionStr = fieldConversions[i];
 191  0
                     if (StringUtils.isNotBlank(fieldConversionStr)) {
 192  0
                         if (StringUtils.contains(fieldConversionStr, ":")) {
 193  0
                             String[] fieldConversion = StringUtils.split(fieldConversionStr, ":");
 194  0
                             map.put(fieldConversion[0], fieldConversion[1]);
 195  0
                         } else {
 196  0
                             map.put(fieldConversionStr, fieldConversionStr);
 197  
                         }
 198  
                     }
 199  
                 }
 200  0
             } else if (StringUtils.contains(parameter, ":")) {
 201  0
                 String[] fieldConversion = StringUtils.split(parameter, ":");
 202  0
                 map.put(fieldConversion[0], fieldConversion[1]);
 203  0
             } else {
 204  0
                 map.put(parameter, parameter);
 205  
             }
 206  
         }
 207  
 
 208  0
         return map;
 209  
     }
 210  
 
 211  
     /**
 212  
      * Parses a string that is in list format (commas separating list entries) to a new List instance
 213  
      *
 214  
      * @param parameter - string parameter to parse
 215  
      * @return List<String> instance populated from string parameter
 216  
      */
 217  
     public static List<String> convertStringParameterToList(String parameter) {
 218  0
         List<String> list = new ArrayList<String>();
 219  
 
 220  0
         if (StringUtils.isNotBlank(parameter)) {
 221  0
             if (StringUtils.contains(parameter, ",")) {
 222  0
                 String[] parameters = StringUtils.split(parameter, ",");
 223  0
                 list = Arrays.asList(parameters);
 224  0
             } else {
 225  0
                 list.add(parameter);
 226  
             }
 227  
         }
 228  
 
 229  0
         return list;
 230  
     }
 231  
 }