gov.nih.nci.mageom.util
Class MAGEUtilities

java.lang.Object
  extended by gov.nih.nci.mageom.util.MAGEUtilities

public class MAGEUtilities
extends java.lang.Object

This class defines functions that are useful throughout MAGE.

Version:
$Revision: 1.2 $
Author:
MAGE Team

Field Summary
protected static java.lang.String[] _beanPkgNames
           
protected static MAGEUtilities _instance
           
protected static java.util.Properties _roles
           
protected static java.lang.String[] _scPkgNames
           
static int URL_FILE
          Index for file in parsed URL results.
static int URL_HOST
          Index for host in parsed URL results.
static int URL_PASSWORD
          Index for password in parsed URL results.
static int URL_PATH
          Index for path in parsed URL results.
static int URL_USERNAME
          Index for username in parsed URL results.
 
Method Summary
static void appendOrderBy(java.lang.StringBuffer query, java.lang.String order)
          Appends an ORDER BY to an SQL query.
static void appendWhere(java.lang.StringBuffer where, java.lang.String exists)
          Appends an EXISTS subquery to an SQL WHERE clause.
static void appendWhere(java.lang.StringBuffer where, java.lang.String column, java.lang.Boolean bool)
          Appends an boolean to an SQL WHERE clause using 0/1 paradigm.
static void appendWhere(java.lang.StringBuffer where, java.lang.String column, java.lang.Long pattern)
          Appends an item comparing a column to a pattern to an SQL WHERE clause.
static void appendWhere(java.lang.StringBuffer where, java.lang.String column, java.lang.Long pattern, boolean caseInsensitive)
          Appends an item comparing a column to a pattern to an SQL WHERE clause.
static void appendWhere(java.lang.StringBuffer where, java.lang.String column, java.lang.String pattern)
          Appends an item comparing a column to a pattern to an SQL WHERE clause.
static void appendWhere(java.lang.StringBuffer where, java.lang.String column, java.lang.String[] items)
          Appends a list of items to an SQL WHERE clause.
static void appendWhere(java.lang.StringBuffer where, java.lang.String column, java.lang.String pattern, boolean caseInsensitive)
          Appends an item comparing a column to a pattern to an SQL WHERE clause.
static void appendWhere(java.lang.StringBuffer where, java.lang.String column1, java.lang.String criteria, java.lang.String column2)
          Appends an item comparing two columns to an SQL WHERE clause.
static void appendWhere(java.lang.StringBuffer where, java.lang.String column1, java.lang.String criteria, java.lang.String column2, boolean caseInsensitive)
          Appends an item comparing two columns to an SQL WHERE clause.
static void appendWhere(java.lang.StringBuffer where, java.lang.String column, java.lang.String year, java.lang.String month, java.lang.String day)
          Appends a date to an SQL WHERE clause.
static java.util.HashMap buildParamsMap(java.net.URL url)
           
static boolean containsWildcards(java.util.Collection values)
           
static boolean containsWildcards(java.lang.String str)
          Determines if a string contains wildcards.
static java.lang.Object convert(java.lang.String beanClassName, java.lang.String attName, java.lang.Object value)
           
static long convertToMilliseconds(int year, int month, int day)
          Converts a year, month, and day into milliseconds since whenever the time for the Date and Calendar classes start.
static java.util.Collection convertWildcards(java.util.Collection values)
           
static java.lang.String convertWildcards(java.lang.String str)
          Turns non-SQL wildcards into SQL wildcards: * becomes %, and ? becomes _.
static java.util.Collection escapeApostrophes(java.util.Collection values)
           
static java.lang.String escapeApostrophes(java.lang.String str)
          Escapes apostrophies.
static java.lang.reflect.Method findMethod(java.lang.Class klass, java.lang.String match)
           
static java.lang.String getAssociatedBeanName(java.lang.String beanDotRole)
           
static java.lang.String getBeanClassName(java.lang.String beanName)
           
static java.lang.String getBeanName(java.lang.String theName)
           
static java.lang.String getBeanNameIgnoreCase(java.lang.String beanName)
           
static java.lang.String getBeanPackageName(java.lang.String p_beanName)
           
static java.lang.String getHTTPQueryURL(java.lang.String fromBeanName, java.lang.Object fromObjId, java.lang.String role, java.lang.String toBeanName)
           
static java.lang.String getReciprocalRole(java.lang.String beanDotRole)
           
static java.lang.String getSCClassName(java.lang.String beanName)
           
static java.lang.String getSCPackageName(java.lang.String p_beanName)
           
static java.lang.String getShortName(java.lang.String theName)
           
static java.lang.String HTTPdecode(java.lang.String s)
          Deprecated. use HTTPdecode(String s, String enc)
static java.lang.String HTTPdecode(java.lang.String s, java.lang.String enc)
          Decodes a application/x-www-form-urlencoded string using a specific encoding scheme.
static java.lang.String HTTPencode(java.lang.String s)
          Deprecated. use HTTPencode(String s, String enc)
static java.lang.String HTTPencode(java.lang.String s, java.lang.String enc)
          Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme.
static java.lang.Object invoke(java.lang.Object obj, java.lang.String methodName, java.lang.Object[] args)
           
static java.lang.Object invoke(java.lang.String className, java.lang.String methodName, java.lang.Object[] args)
           
static void loadProperties(java.lang.String propertyFile)
           
static java.lang.String[] parseURL(java.lang.String sUrl)
          Parses a URL into its component pieces.
static java.util.Collection quote(java.util.Collection values)
           
static java.util.HashMap readManagerDirectory(java.lang.String dirFileName)
          Reads the contents of a MAGE manager directory, returning the contents as a HashMap relating managers to machine addresses.
static java.util.Properties readPropertyFile(java.lang.String fileName)
          Reads the contents of a properties file, returning the contents as a Properties object.
static java.lang.Long runGetId(java.lang.Object bean)
           
static java.lang.Object runMethod(java.lang.Object bean, java.lang.String attributeName)
           
static void runSetId(java.lang.Object bean, java.lang.Object theValue)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_beanPkgNames

protected static java.lang.String[] _beanPkgNames

_scPkgNames

protected static java.lang.String[] _scPkgNames

_roles

protected static java.util.Properties _roles

_instance

protected static MAGEUtilities _instance

URL_USERNAME

public static final int URL_USERNAME
Index for username in parsed URL results.

See Also:
Constant Field Values

URL_PASSWORD

public static final int URL_PASSWORD
Index for password in parsed URL results.

See Also:
Constant Field Values

URL_HOST

public static final int URL_HOST
Index for host in parsed URL results.

See Also:
Constant Field Values

URL_PATH

public static final int URL_PATH
Index for path in parsed URL results.

See Also:
Constant Field Values

URL_FILE

public static final int URL_FILE
Index for file in parsed URL results.

See Also:
Constant Field Values
Method Detail

HTTPencode

public static java.lang.String HTTPencode(java.lang.String s)
                                   throws java.io.UnsupportedEncodingException
Deprecated. use HTTPencode(String s, String enc)

Encodes a string so that it will be valid for use in a URL.

Parameters:
s - string to encode
Returns:
encoded string
Throws:
java.io.UnsupportedEncodingException
See Also:
HTTPdecode(String)

HTTPencode

public static java.lang.String HTTPencode(java.lang.String s,
                                          java.lang.String enc)
                                   throws java.io.UnsupportedEncodingException
Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme. This method uses the supplied encoding scheme to obtain the bytes for unsafe characters.

Note: The World Wide Web Consortium Recommendation states that UTF-8 should be used. Not doing so may introduce incompatibilites.

Parameters:
s - String to be translated.
enc - The name of a supported character encoding.
Returns:
the translated String.
Throws:
java.io.UnsupportedEncodingException - If the named encoding is not supported

HTTPdecode

public static java.lang.String HTTPdecode(java.lang.String s)
Deprecated. use HTTPdecode(String s, String enc)

Decodes a HTTP encoded string.

Parameters:
s - string to decode
Returns:
decoded string
See Also:
HTTPencode(String)

HTTPdecode

public static java.lang.String HTTPdecode(java.lang.String s,
                                          java.lang.String enc)
Decodes a application/x-www-form-urlencoded string using a specific encoding scheme. The supplied encoding is used to determine what characters are represented by any consecutive sequences of the form "%xy".

Note: The World Wide Web Consortium Recommendation states that UTF-8 should be used. Not doing so may introduce incompatibilites.

Parameters:
s - string to decode
enc - The name of a supported character encoding.
Returns:
the newly decoded String

parseURL

public static java.lang.String[] parseURL(java.lang.String sUrl)
Parses a URL into its component pieces. If the given URL is malformed, returns null.

Parameters:
sUrl - a URL as a String
Returns:
an array of Strings for the following, in order: username, password, host, path, and file

convertToMilliseconds

public static long convertToMilliseconds(int year,
                                         int month,
                                         int day)
Converts a year, month, and day into milliseconds since whenever the time for the Date and Calendar classes start.

Parameters:
year - the year to convert
month - the month to convert, starting from zero
day - the day to convert
Returns:
a millisecond time value
See Also:
Date, Calendar

readManagerDirectory

public static java.util.HashMap readManagerDirectory(java.lang.String dirFileName)
Reads the contents of a MAGE manager directory, returning the contents as a HashMap relating managers to machine addresses.

The directory should be used to determine which machines in the MAGE system host which managers. The format of the file has each line contain the name of a manager as it it given to RMI and the IP address or DNS name of the machine it should run on, separated by whitespace.

 LibraryManager      165.112.113.1
 TissueManager       165.112.113.2
 ...
If one of the lines is formatted wrong, the directory will not include the manager on that line.

The hashtable returned uses the manager names as keys.

Parameters:
dirFileName - name of the file where the directory is stored
Returns:
a hashtable representing the directory, or null if there was a problem accessing the file

readPropertyFile

public static java.util.Properties readPropertyFile(java.lang.String fileName)
Reads the contents of a properties file, returning the contents as a Properties object.

Parameters:
fileName - name of the file where the proteries are stored
Returns:
a Properties object , or null if there was a problem accessing the file

containsWildcards

public static boolean containsWildcards(java.lang.String str)
Determines if a string contains wildcards. Wildcards detected are %, *, _, and ?.

Parameters:
str - string to examine
Returns:
true if the string contains wildcards, false otherwise

convertWildcards

public static java.lang.String convertWildcards(java.lang.String str)
Turns non-SQL wildcards into SQL wildcards: * becomes %, and ? becomes _.

Parameters:
str - string to convert
Returns:
new string with converted wildcards

escapeApostrophes

public static java.lang.String escapeApostrophes(java.lang.String str)
Escapes apostrophies.

Parameters:
str - string to escape
Returns:
new string with escaped apostrophes

appendWhere

public static void appendWhere(java.lang.StringBuffer where,
                               java.lang.String column,
                               java.lang.String pattern)
Appends an item comparing a column to a pattern to an SQL WHERE clause. If passed a null or blank item, does nothing. The item is case-insensitive.

Parameters:
where - WHERE clause so far, should be blank to start
column - name of column
pattern - pattern to compare against
See Also:
appendWhere(StringBuffer,String,String,boolean)

appendWhere

public static void appendWhere(java.lang.StringBuffer where,
                               java.lang.String column,
                               java.lang.Long pattern)
Appends an item comparing a column to a pattern to an SQL WHERE clause. If passed a null or blank item, does nothing. The item is case-insensitive.

Parameters:
where - WHERE clause so far, should be blank to start
column - name of column
pattern - pattern to compare against
See Also:
appendWhere(StringBuffer,String ,String,boolean)

appendWhere

public static void appendWhere(java.lang.StringBuffer where,
                               java.lang.String column,
                               java.lang.Long pattern,
                               boolean caseInsensitive)
Appends an item comparing a column to a pattern to an SQL WHERE clause. If passed a null or blank item, does nothing.

Parameters:
where - WHERE clause so far, should be blank to start
column - name of column
pattern - pattern to compare against
caseInsensitive - whether to make it case insensitive

appendWhere

public static void appendWhere(java.lang.StringBuffer where,
                               java.lang.String column,
                               java.lang.String pattern,
                               boolean caseInsensitive)
Appends an item comparing a column to a pattern to an SQL WHERE clause. If passed a null or blank item, does nothing.

Parameters:
where - WHERE clause so far, should be blank to start
column - name of column
pattern - pattern to compare against
caseInsensitive - whether to make it case insensitive

appendWhere

public static void appendWhere(java.lang.StringBuffer where,
                               java.lang.String column1,
                               java.lang.String criteria,
                               java.lang.String column2)
Appends an item comparing two columns to an SQL WHERE clause. If passed a null or blank item, does nothing. The item is case-insensitive.

Parameters:
where - WHERE clause so far, should be blank to start
column1 - name of the first column
criteria - name of criteria, ie. = or LIKE
column2 - name of the second column

appendWhere

public static void appendWhere(java.lang.StringBuffer where,
                               java.lang.String column1,
                               java.lang.String criteria,
                               java.lang.String column2,
                               boolean caseInsensitive)
Appends an item comparing two columns to an SQL WHERE clause. If passed a null or blank item, does nothing.

Parameters:
where - WHERE clause so far, should be blank to start
column1 - name of the first column
criteria - name of criteria, ie. = or LIKE
column2 - name of the second column
caseInsensitive - whether to make it case insensitive

appendOrderBy

public static void appendOrderBy(java.lang.StringBuffer query,
                                 java.lang.String order)
Appends an ORDER BY to an SQL query. If passed a null or blank item, does nothing.

Parameters:
query - query so far
order - the ORDER BY list

appendWhere

public static void appendWhere(java.lang.StringBuffer where,
                               java.lang.String column,
                               java.lang.String[] items)
Appends a list of items to an SQL WHERE clause. If the list only has one element, calls another appendWhere to do the work. If passed a null or empty list, does nothing.

Parameters:
where - WHERE clause so far, should be blank to start
column - name of column
items - array of items to look for

appendWhere

public static void appendWhere(java.lang.StringBuffer where,
                               java.lang.String column,
                               java.lang.String year,
                               java.lang.String month,
                               java.lang.String day)
Appends a date to an SQL WHERE clause. If year is null or empty but month or day are not, assumes current year. If month is null or empty but day is not, assumes current month. If all three are null or empty, does nothing.

Parameters:
where - WHERE clause so far, should be blank to start
column - name of date column to compare
year - String representation of numeric year
month - String representation of three-letter month
day - String representation of numeric day

appendWhere

public static void appendWhere(java.lang.StringBuffer where,
                               java.lang.String exists)
Appends an EXISTS subquery to an SQL WHERE clause. If passed a null or blank item, does nothing.

Parameters:
where - WHERE clause so far, should be blank to start
exists - the EXISTS subquery

appendWhere

public static void appendWhere(java.lang.StringBuffer where,
                               java.lang.String column,
                               java.lang.Boolean bool)
Appends an boolean to an SQL WHERE clause using 0/1 paradigm. If passed a null or blank item, does nothing.

Parameters:
where - WHERE clause so far, should be blank to start
column - name of column
bool - the Boolean

getShortName

public static java.lang.String getShortName(java.lang.String theName)

runGetId

public static java.lang.Long runGetId(java.lang.Object bean)
                               throws java.lang.Exception
Throws:
java.lang.Exception

runSetId

public static void runSetId(java.lang.Object bean,
                            java.lang.Object theValue)
                     throws java.lang.Exception
Throws:
java.lang.Exception

getBeanName

public static java.lang.String getBeanName(java.lang.String theName)

runMethod

public static java.lang.Object runMethod(java.lang.Object bean,
                                         java.lang.String attributeName)
                                  throws java.lang.Exception
Throws:
java.lang.Exception

loadProperties

public static void loadProperties(java.lang.String propertyFile)

invoke

public static java.lang.Object invoke(java.lang.String className,
                                      java.lang.String methodName,
                                      java.lang.Object[] args)
                               throws java.lang.Exception
Throws:
java.lang.Exception

invoke

public static java.lang.Object invoke(java.lang.Object obj,
                                      java.lang.String methodName,
                                      java.lang.Object[] args)
                               throws java.lang.Exception
Throws:
java.lang.Exception

getBeanPackageName

public static java.lang.String getBeanPackageName(java.lang.String p_beanName)

getSCPackageName

public static java.lang.String getSCPackageName(java.lang.String p_beanName)

containsWildcards

public static boolean containsWildcards(java.util.Collection values)

escapeApostrophes

public static java.util.Collection escapeApostrophes(java.util.Collection values)

convertWildcards

public static java.util.Collection convertWildcards(java.util.Collection values)

quote

public static java.util.Collection quote(java.util.Collection values)

getAssociatedBeanName

public static java.lang.String getAssociatedBeanName(java.lang.String beanDotRole)
                                              throws java.lang.Exception
Throws:
java.lang.Exception

getSCClassName

public static java.lang.String getSCClassName(java.lang.String beanName)

getBeanClassName

public static java.lang.String getBeanClassName(java.lang.String beanName)

buildParamsMap

public static java.util.HashMap buildParamsMap(java.net.URL url)

convert

public static java.lang.Object convert(java.lang.String beanClassName,
                                       java.lang.String attName,
                                       java.lang.Object value)
                                throws java.lang.Exception
Throws:
java.lang.Exception

getReciprocalRole

public static java.lang.String getReciprocalRole(java.lang.String beanDotRole)

getHTTPQueryURL

public static java.lang.String getHTTPQueryURL(java.lang.String fromBeanName,
                                               java.lang.Object fromObjId,
                                               java.lang.String role,
                                               java.lang.String toBeanName)
                                        throws java.lang.Exception
Throws:
java.lang.Exception

findMethod

public static java.lang.reflect.Method findMethod(java.lang.Class klass,
                                                  java.lang.String match)
                                           throws java.lang.Exception
Throws:
java.lang.Exception

getBeanNameIgnoreCase

public static java.lang.String getBeanNameIgnoreCase(java.lang.String beanName)