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