Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register
Show
Ignore:
Timestamp:
05/20/08 17:00:47 (8 months ago)
Author:
tmdugan
Message:

-- dss

* cleaned up utility methods

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openmrs-modules/dss/src/org/openmrs/module/dss/util/Util.java

    r3599 r4278  
    44package org.openmrs.module.dss.util; 
    55 
    6 import java.io.File; 
    76import java.util.Calendar; 
    87import java.util.Date; 
     
    1716public class Util 
    1817{ 
    19         public static final String YEAR_ABBR = "yo"; 
    20         public static final String MONTH_ABBR = "mo"; 
    21         public static final String WEEK_ABBR = "wk"; 
    22         public static final String DAY_ABBR = "do"; 
    23          
    2418        public static final String MEASUREMENT_LB = "lb"; 
    2519        public static final String MEASUREMENT_IN = "in"; 
    26          
    27         /** 
    28          * Returns a list of file names within a specific directory 
    29          *  
    30          * @param xmlDirectory specific directory 
    31          * @return String[] list of file names 
    32          */ 
    33         public static String[] getFilesInDirectory(String xmlDirectory) 
    34         { 
    35                 File dir = new File(xmlDirectory); 
    36  
    37                 return dir.list(); 
    38         } 
    39          
    40         /** 
    41          * Returns the file name without an extension or directory path 
    42          * @param filepath path to the file 
    43          * @return String file name without an extension or directory path 
    44          */ 
    45         public static String getFilenameWithoutExtension(String filepath) 
    46         { 
    47                 String filename = filepath; 
    48                 int index = filename.lastIndexOf("/"); 
    49                 int index2 = filename.lastIndexOf("\\"); 
    50                  
    51                 if(index2 > index) 
    52                 { 
    53                         index = index2; 
    54                 } 
    55                  
    56                 if(index>-1) 
    57                 { 
    58                         filename = filename.substring(index+1); 
    59                 } 
    60                                  
    61                 index = filename.lastIndexOf("."); 
    62                  
    63                 if(index > -1) 
    64                 { 
    65                         filename = filename.substring(0,index); 
    66                 } 
    67                  
    68                 return filename; 
    69         } 
    7020         
    7121        /** 
     
    9848         
    9949        /** 
    100          * Calculates age to a precision of days, weeks, months, or years based on a 
    101          * set of rules 
    102          *  
    103          * @param birthdate patient's birth date 
    104          * @param cutoff date to calculate age from 
    105          * @return String age with units  
    106          */ 
    107         public static String adjustAgeUnits(Date birthdate, Date cutoff) 
    108         { 
    109                 int years = getAgeInUnits(birthdate, cutoff, YEAR_ABBR); 
    110                 int months = getAgeInUnits(birthdate, cutoff, MONTH_ABBR); 
    111                 int weeks = getAgeInUnits(birthdate, cutoff, WEEK_ABBR); 
    112                 int days = getAgeInUnits(birthdate, cutoff, DAY_ABBR); 
    113  
    114                 if (years >= 2) 
    115                 { 
    116                         return years + " " + YEAR_ABBR; 
    117                 } 
    118  
    119                 if (months >= 2) 
    120                 { 
    121                         return months + " " + MONTH_ABBR; 
    122                 } 
    123  
    124                 if (days > 30) 
    125                 { 
    126                         return weeks + " " + WEEK_ABBR; 
    127                 } 
    128  
    129                 return days + " " + DAY_ABBR; 
    130         } 
    131  
    132         /** 
    133          * Returns a person's age in the specified units (days, weeks, months, years) 
    134          *  
    135          * @param birthdate person's date of birth 
    136          * @param today date to calculate age from 
    137          * @param unit unit to calculate age in (days, weeks, months, years) 
    138          * @return int age in the given units 
    139          */ 
    140         public static int getAgeInUnits(Date birthdate, Date today, String unit) 
    141         { 
    142                 if (birthdate == null) 
    143                 { 
    144                         return 0; 
    145                 } 
    146  
    147                 if (today == null) 
    148                 { 
    149                         today = new Date(); 
    150                 } 
    151  
    152                 int diffMonths = 0; 
    153                 int diffWeeks = 0; 
    154                 int diffDays = 0; 
    155                 int years = 0; 
    156                 int months = 0; 
    157                 int weeks = 0; 
    158                 int days = 0; 
    159  
    160                 Calendar birthdateCalendar = Calendar.getInstance(); 
    161                 birthdateCalendar.setTime(birthdate); 
    162                 Calendar todayCalendar = Calendar.getInstance(); 
    163                 todayCalendar.setTime(today); 
    164  
    165                 // return 0 if the birthdate is after today 
    166                 if (birthdate.compareTo(today) > 0) 
    167                 { 
    168                         return 0; 
    169                 } 
    170  
    171                 years = todayCalendar.get(Calendar.YEAR) 
    172                                 - birthdateCalendar.get(Calendar.YEAR); 
    173                 diffMonths = todayCalendar.get(Calendar.MONTH) 
    174                                 - birthdateCalendar.get(Calendar.MONTH); 
    175                 diffWeeks = todayCalendar.get(Calendar.WEEK_OF_YEAR) 
    176                                 - birthdateCalendar.get(Calendar.WEEK_OF_YEAR); 
    177                 diffDays = todayCalendar.get(Calendar.DAY_OF_YEAR) 
    178                                 - birthdateCalendar.get(Calendar.DAY_OF_YEAR); 
    179  
    180                 months = years * 12; 
    181                 months += diffMonths; 
    182  
    183                 weeks = years * 52; 
    184                 weeks += diffWeeks; 
    185  
    186                 days = years * 365; 
    187                 days += diffDays; 
    188  
    189                 if (unit.equalsIgnoreCase(YEAR_ABBR)) 
    190                 { 
    191                         if (diffMonths < 0) 
    192                         { 
    193                                 years--; 
    194                         } 
    195                         return years; 
    196                 } 
    197  
    198                 if (unit.equalsIgnoreCase(MONTH_ABBR)) 
    199                 { 
    200                         if (diffWeeks < 0) 
    201                         { 
    202                                 months--; 
    203                         } 
    204                         return months; 
    205                 } 
    206  
    207                 if (unit.equalsIgnoreCase(WEEK_ABBR)) 
    208                 { 
    209                         if (diffDays < 0) 
    210                         { 
    211                                 weeks--; 
    212                         } 
    213                         return weeks; 
    214                 } 
    215  
    216                 if (days < 0) 
    217                 { 
    218                         days = 0; 
    219                 } 
    220                 return days; 
    221         } 
    222          
    223         /** 
    22450         * Returns the numeric part of a string input as a string 
    22551         * @param input alphanumeric input 
     
    24066         
    24167        /** 
    242          * Adds slashes if needed to a file directory 
    243          * @param fileDirectory file directory path 
    244          * @return String formatted file directory path 
    245          */ 
    246         public static String processFileDirectory(String fileDirectory) 
    247         { 
    248                 if(!(fileDirectory.endsWith("/")||fileDirectory.endsWith("\\"))) 
    249                 { 
    250                         fileDirectory+="/"; 
    251                 } 
    252                 return fileDirectory; 
    253         } 
    254          
    255         /** 
    25668         * Adds period if necessary to a package prefix 
    25769         * @param packagePrefix a java package prefix 
    25870         * @return String formatted package prefix 
    25971         */ 
    260         public static String processPackagePrefix(String packagePrefix) 
     72        public static String formatPackagePrefix(String packagePrefix) 
    26173        { 
    26274                if (packagePrefix!=null&&!packagePrefix.endsWith(".")) 
     
    26678                return packagePrefix; 
    26779        } 
     80         
     81        public static String toProperCase(String str) 
     82        { 
     83                if(str == null || str.length()<1) 
     84                { 
     85                        return str; 
     86                } 
     87                return str.substring(0, 1).toUpperCase() 
     88                                + str.substring(1).toLowerCase(); 
     89        } 
    26890}