public class OpenmrsUtil
extends java.lang.Object
Constructor and Description |
---|
OpenmrsUtil() |
Modifier and Type | Method and Description |
---|---|
static <K,V> void |
addToListMap(java.util.Map<K,java.util.List<V>> map,
K key,
V obj) |
static <K,V> void |
addToSetMap(java.util.Map<K,java.util.Set<V>> map,
K key,
V obj)
Allows easy manipulation of a Map, Set>
|
static java.lang.Integer |
ageFromBirthdate(java.util.Date birthdate)
Deprecated.
this method is not currently used within OpenMRS and is a duplicate of
Person.getAge(Date) |
static void |
applyLogLevel(java.lang.String logClass,
java.lang.String logLevel)
Set the log4j log level for class
logClass to logLevel . |
static void |
applyLogLevels()
Set the org.openmrs log4j logger's level if global property log.level.openmrs (
OpenmrsConstants.GLOBAL_PROPERTY_LOG_LEVEL ) exists.
|
static void |
closeStream(java.io.Closeable closableStream)
A null-safe and exception safe way to close an inputstream or an outputstream
|
static boolean |
collectionContains(java.util.Collection<?> objects,
java.lang.Object obj)
Loops over the collection to check to see if the given object is in that collection.
|
static int |
compare(java.util.Date d1,
java.util.Date d2)
Compares two java.util.Date objects, but handles java.sql.Timestamp (which is not directly
comparable to a date) by dropping its nanosecond value.
|
static <E> java.util.Collection<java.util.Collection<E>> |
compareLists(java.util.Collection<E> origList,
java.util.Collection<E> newList)
Compares origList to newList returning map of differences
|
static int |
compareWithNullAsEarliest(java.util.Date d1,
java.util.Date d2)
Compares two Date/Timestamp objects, treating null as the earliest possible date.
|
static <E extends java.lang.Comparable<E>> |
compareWithNullAsGreatest(E c1,
E c2) |
static int |
compareWithNullAsLatest(java.util.Date d1,
java.util.Date d2)
Compares two Date/Timestamp objects, treating null as the earliest possible date.
|
static <E extends java.lang.Comparable<E>> |
compareWithNullAsLowest(E c1,
E c2) |
static java.util.List<Concept> |
conceptListHelper(java.lang.String descriptor) |
static java.util.Set<Concept> |
conceptSetHelper(java.lang.String descriptor) |
static <T> boolean |
containsAny(java.util.Collection<T> collection,
java.util.Collection<T> elements) |
static boolean |
containsDigit(java.lang.String test) |
static boolean |
containsOnlyDigits(java.lang.String test) |
static boolean |
containsUpperAndLowerCase(java.lang.String test) |
static java.lang.Integer |
convertToInteger(java.lang.Long longValue)
This method converts the given Long value to an Integer.
|
static void |
copyFile(java.io.InputStream inputStream,
java.io.OutputStream outputStream)
Copy file from inputStream onto the outputStream inputStream is not closed in this method
outputStream /is/ closed at completion of this method
|
static boolean |
deleteDirectory(java.io.File dir)
Recursively deletes files in the given
dir folder |
static java.util.List<Concept> |
delimitedStringToConceptList(java.lang.String delimitedString,
java.lang.String delimiter)
Parses and loads a delimited list of concept ids or names
|
static java.util.List<Concept> |
delimitedStringToConceptList(java.lang.String delimitedString,
java.lang.String delimiter,
Context context)
Deprecated.
replaced by
delimitedStringToConceptList(String,String) |
static java.util.Map<java.lang.String,Concept> |
delimitedStringToConceptMap(java.lang.String delimitedString,
java.lang.String delimiter) |
static java.util.List<java.lang.Integer> |
delimitedStringToIntegerList(java.lang.String delimitedString,
java.lang.String delimiter) |
static java.util.Date |
firstSecondOfDay(java.util.Date date)
Return a date that is the same day as the passed in date, but the hours and seconds are the
earliest possible for that day.
|
static boolean |
folderContains(java.io.File folder,
java.lang.String filename)
Look for a file named
filename in folder |
static java.util.Date |
fromDateHelper(java.util.Date comparisonDate,
java.lang.Integer withinLastDays,
java.lang.Integer withinLastMonths,
java.lang.Integer untilDaysAgo,
java.lang.Integer untilMonthsAgo,
java.util.Date sinceDate,
java.util.Date untilDate) |
static java.lang.String |
generateUid()
Creates a uid of length 20
|
static java.lang.String |
generateUid(java.lang.Integer size)
Creates a relatively acceptable unique string of the give size
|
static java.lang.String |
getApplicationDataDirectory()
Returns the application data directory.
|
static int |
getCheckDigit(java.lang.String idWithoutCheckdigit)
Deprecated.
|
static Concept |
getConceptByIdOrName(java.lang.String idOrName) |
static java.util.Map<java.lang.String,java.lang.String> |
getCorePrivileges()
These are the privileges that are required by OpenMRS.
|
static java.util.Map<java.lang.String,java.lang.String> |
getCoreRoles()
All roles returned by this method are inserted into the database if they do not exist
already.
|
static java.text.SimpleDateFormat |
getDateFormat()
Deprecated.
use
Context.getDateFormat() or #getDateFormat(Context#getLocale()) instead |
static java.text.SimpleDateFormat |
getDateFormat(java.util.Locale locale)
Get the current user's date format Will look similar to "mm-dd-yyyy".
|
static java.text.SimpleDateFormat |
getDateTimeFormat(java.util.Locale locale)
Get the current user's datetime format Will look similar to "mm-dd-yyyy hh:mm a".
|
static java.io.File |
getDirectoryInApplicationDataDirectory(java.lang.String folderName)
Find the given folderName in the application data directory.
|
static byte[] |
getFileAsBytes(java.io.File file)
Return a byte array representation of the given file
|
static java.lang.String |
getFileAsString(java.io.File file)
Return a string representation of the given file
|
static java.util.Date |
getLastMomentOfDay(java.util.Date day)
Gets the date having the last millisecond of a given day.
|
static java.lang.String |
getMessage(java.lang.String code,
java.lang.Object... args)
Utility method for getting the translation for the passed code
|
static java.lang.String |
getOpenmrsLogLocation()
Returns the location of the OpenMRS log file.
|
static java.lang.String |
getOpenMRSVersionInTestMode()
Gets OpenMRS version name under test mode.
|
static java.io.File |
getOutFile(java.io.File dir,
java.util.Date date,
User user)
Gets an out File object.
|
static java.io.InputStream |
getResourceInputStream(java.net.URL url)
Opens input stream for given resource.
|
static java.util.Properties |
getRuntimeProperties(java.lang.String applicationName)
Finds and loads the runtime properties file for a specific OpenMRS application.
|
static java.lang.String |
getRuntimePropertiesFileNameInTestMode() |
static java.lang.String |
getRuntimePropertiesFilePathName(java.lang.String applicationName)
Gets the full path and name of the runtime properties file.
|
static org.simpleframework.xml.Serializer |
getSerializer()
Deprecated.
- Use OpenmrsSerializer from
Context.getSerializationService.getDefaultSerializer() Note, this uses a
different Serialization mechanism, so you may need to use this for conversion
|
static org.simpleframework.xml.Serializer |
getShortSerializer()
Deprecated.
- Use OpenmrsSerializer from
Context.getSerializationService.getDefaultSerializer() Note, this uses a
different Serialization mechanism, so you may need to use this for conversion
|
static java.text.SimpleDateFormat |
getTimeFormat(java.util.Locale locale)
Get the current user's time format Will look similar to "hh:mm a".
|
static boolean |
isConceptInList(Concept concept,
java.util.List<Concept> list) |
static java.lang.Boolean |
isInAbsoluteNumericRange(java.lang.Float value,
ConceptNumeric concept) |
static java.lang.Boolean |
isInCriticalNumericRange(java.lang.Float value,
ConceptNumeric concept) |
static java.lang.Boolean |
isInNormalNumericRange(java.lang.Float value,
ConceptNumeric concept) |
static boolean |
isShortSerialization(java.util.Map<?,?> sessionMap)
Deprecated.
- use SerializationService and OpenmrsSerializer implementation for Serialization
|
static boolean |
isStringInArray(java.lang.String str,
java.lang.String[] arr) |
static boolean |
isTestMode()
Checks whether the system is running in test mode
|
static boolean |
isValidCheckDigit(java.lang.String id)
Deprecated.
Should be using
PatientService.getIdentifierValidator(String) |
static java.lang.Boolean |
isValidNumericValue(java.lang.Float value,
ConceptNumeric concept) |
static <E> java.lang.String |
join(java.util.Collection<E> c,
java.lang.String separator)
Converts a collection to a String with a specified separator between all elements
|
static java.util.Date |
lastSecondOfDay(java.util.Date date)
Deprecated.
|
static void |
loadProperties(java.util.Properties props,
java.io.File propertyFile)
Convenience method used to load properties from the given file.
|
static void |
loadProperties(java.util.Properties props,
java.io.InputStream input)
Deprecated.
|
static <Arg1,Arg2 extends Arg1> |
nullSafeEquals(Arg1 d1,
Arg2 d2) |
static boolean |
nullSafeEqualsIgnoreCase(java.lang.String s1,
java.lang.String s2)
Performs a case insensitive Comparison of two strings taking care of null values
|
static java.lang.Object |
parse(java.lang.String string,
java.lang.Class clazz)
Takes a String (e.g.
|
static java.util.Map<java.lang.String,java.lang.String> |
parseParameterList(java.lang.String paramList)
Takes a String like "size=compact|order=date" and returns a Map
|
static java.lang.String |
postToUrl(java.lang.String urlString,
java.util.Map<java.lang.String,java.lang.String> dataToPost)
Deprecated.
use org.openmrs.util.HttpClient
|
static java.util.Date |
safeDate(java.util.Date d1) |
static void |
saveDocument(org.w3c.dom.Document doc,
java.io.File outFile)
Save the given xml document to the given outfile
|
static void |
setupLogAppenders()
Setup root level log appenders.
|
static java.lang.String |
shortenedStackTrace(java.lang.String stackTrace)
Convert a stack trace into a shortened version for easier viewing and data storage, excluding
those lines we are least concerned with; should average about 60% reduction in stack trace
length
|
static void |
startup(java.util.Properties p)
Initialize global settings Find and load modules
|
static void |
storeProperties(java.util.Properties properties,
java.io.File file,
java.lang.String comment)
Convenience method to replace Properties.store(), which isn't UTF-8 compliant
NOTE: In Java 6, you will be able to pass the load() and store() methods a UTF-8 Reader/Writer object as an argument, making this method unnecessary. |
static void |
storeProperties(java.util.Properties properties,
java.io.OutputStream outStream,
java.lang.String comment)
Convenience method to replace Properties.store(), which isn't UTF-8 compliant NOTE: In Java
6, you will be able to pass the load() and store() methods a UTF-8 Reader/Writer object as an
argument.
|
static boolean |
stringStartsWith(java.lang.String str,
java.lang.String[] prefixes)
Tests if the given String starts with any of the specified prefixes
|
static java.util.Date |
toDateHelper(java.util.Date comparisonDate,
java.lang.Integer withinLastDays,
java.lang.Integer withinLastMonths,
java.lang.Integer untilDaysAgo,
java.lang.Integer untilMonthsAgo,
java.util.Date sinceDate,
java.util.Date untilDate) |
static PatientFilter |
toPatientFilter(PatientSearch search,
CohortSearchHistory history)
Deprecated.
see reportingcompatibility module
|
static PatientFilter |
toPatientFilter(PatientSearch search,
CohortSearchHistory history,
EvaluationContext evalContext)
Deprecated.
see reportingcompatibility module
|
static java.io.File |
url2file(java.net.URL url)
Utility method to convert local URL to a File object.
|
static void |
validateJavaVersion()
Checks whether the current JVM version is at least Java 6.
|
static void |
validatePassword(java.lang.String username,
java.lang.String password,
java.lang.String systemId)
Utility to check the validity of a password for a certain
User . |
@Deprecated public static int getCheckDigit(java.lang.String idWithoutCheckdigit) throws java.lang.Exception
PatientService.getIdentifierValidator(String)
idWithoutCheckdigit
- java.lang.Exception
@Deprecated public static boolean isValidCheckDigit(java.lang.String id) throws java.lang.Exception
PatientService.getIdentifierValidator(String)
id
- java.lang.Exception
- on invalid characters and invalid id formationpublic static <E> java.util.Collection<java.util.Collection<E>> compareLists(java.util.Collection<E> origList, java.util.Collection<E> newList)
origList
- newList
- public static boolean isStringInArray(java.lang.String str, java.lang.String[] arr)
public static java.lang.Boolean isInNormalNumericRange(java.lang.Float value, ConceptNumeric concept)
public static java.lang.Boolean isInCriticalNumericRange(java.lang.Float value, ConceptNumeric concept)
public static java.lang.Boolean isInAbsoluteNumericRange(java.lang.Float value, ConceptNumeric concept)
public static java.lang.Boolean isValidNumericValue(java.lang.Float value, ConceptNumeric concept)
public static java.lang.String getFileAsString(java.io.File file) throws java.io.IOException
file
- java.io.IOException
public static byte[] getFileAsBytes(java.io.File file) throws java.io.IOException
file
- java.io.IOException
public static void copyFile(java.io.InputStream inputStream, java.io.OutputStream outputStream) throws java.io.IOException
inputStream
- Stream to copy fromoutputStream
- Stream/location to copy tojava.io.IOException
- thrown if an error occurs during read/writepublic static boolean folderContains(java.io.File folder, java.lang.String filename)
filename
in folderfolder
- filename
- public static java.util.Map<java.lang.String,java.lang.String> getCorePrivileges()
AddOnStartup
to know which privs, upon startup or loading of a module, to insert into
the database if they do not exist already. These privileges are not allowed to be deleted.
They are marked as 'locked' in the administration screens.PrivilegeConstants
,
Context.checkCoreDataset()
public static java.util.Map<java.lang.String,java.lang.String> getCoreRoles()
public static void startup(java.util.Properties p)
p
- properties from runtime configurationpublic static void applyLogLevels()
public static void setupLogAppenders()
public static void applyLogLevel(java.lang.String logClass, java.lang.String logLevel)
logClass
to logLevel
.logClass
- optional string giving the class level to change. Defaults to
OpenmrsConstants.LOG_CLASS_DEFAULT . Should be something like org.openmrs.___logLevel
- one of OpenmrsConstants.LOG_LEVEL_*public static java.util.Map<java.lang.String,java.lang.String> parseParameterList(java.lang.String paramList)
paramList
- String
with a list of parameterspublic static <Arg1,Arg2 extends Arg1> boolean nullSafeEquals(Arg1 d1, Arg2 d2)
public static int compare(java.util.Date d1, java.util.Date d2)
public static int compareWithNullAsEarliest(java.util.Date d1, java.util.Date d2)
public static int compareWithNullAsLatest(java.util.Date d1, java.util.Date d2)
public static <E extends java.lang.Comparable<E>> int compareWithNullAsLowest(E c1, E c2)
public static <E extends java.lang.Comparable<E>> int compareWithNullAsGreatest(E c1, E c2)
@Deprecated public static java.lang.Integer ageFromBirthdate(java.util.Date birthdate)
Person.getAge(Date)
public static <E> java.lang.String join(java.util.Collection<E> c, java.lang.String separator)
c
- Collection to be joinedseparator
- string to put between all elementspublic static java.util.Set<Concept> conceptSetHelper(java.lang.String descriptor)
@Deprecated public static java.util.List<Concept> delimitedStringToConceptList(java.lang.String delimitedString, java.lang.String delimiter, Context context)
delimitedStringToConceptList(String,String)
public static java.util.List<Concept> delimitedStringToConceptList(java.lang.String delimitedString, java.lang.String delimiter)
delimitedString
- the delimited list of concept ids or namesdelimiter
- the delimiter, e.g. ","public static java.util.Map<java.lang.String,Concept> delimitedStringToConceptMap(java.lang.String delimitedString, java.lang.String delimiter)
public static Concept getConceptByIdOrName(java.lang.String idOrName)
public static java.util.List<Concept> conceptListHelper(java.lang.String descriptor)
@Deprecated public static java.util.Date lastSecondOfDay(java.util.Date date)
getLastMomentOfDay(Date)
date
- date to adjustpublic static java.util.Date getLastMomentOfDay(java.util.Date day)
day
- the day.public static java.util.Date firstSecondOfDay(java.util.Date date)
date
- date to adjustpublic static java.util.Date safeDate(java.util.Date d1)
public static boolean deleteDirectory(java.io.File dir) throws java.io.IOException
dir
folderdir
- File directory to deletejava.io.IOException
- if dir
is not a directorypublic static java.io.File url2file(java.net.URL url)
url
- an URLnull
if URL is not localpublic static java.io.InputStream getResourceInputStream(java.net.URL url) throws java.io.IOException
url
- resource URLjava.io.IOException
- if any I/O error has occurredpublic static java.lang.String getApplicationDataDirectory()
Returns the application data directory. Searches for the value first in the "application_data_directory" runtime property, then in the servlet init parameter "application.data.directory." If not found, returns: a) "{user.home}/.OpenMRS" on UNIX-based systems b) "{user.home}\Application Data\OpenMRS" on Windows Path can be set via systemproperty OPENMRS_APPLICATION_DATA_DIRECTORY for development purposes.
public static java.lang.String getOpenmrsLogLocation()
public static void validateJavaVersion()
org.springframework.context.ApplicationContextException
- if the current JVM version is earlier than Java 6public static java.io.File getDirectoryInApplicationDataDirectory(java.lang.String folderName) throws APIException
folderName
- APIException
public static void saveDocument(org.w3c.dom.Document doc, java.io.File outFile)
doc
- Document to be savedoutFile
- file pointer to the location the xml file is to be saved topublic static java.util.List<java.lang.Integer> delimitedStringToIntegerList(java.lang.String delimitedString, java.lang.String delimiter)
public static boolean stringStartsWith(java.lang.String str, java.lang.String[] prefixes)
str
- the string to testprefixes
- an array of prefixes to test againstpublic static boolean isConceptInList(Concept concept, java.util.List<Concept> list)
public static java.util.Date fromDateHelper(java.util.Date comparisonDate, java.lang.Integer withinLastDays, java.lang.Integer withinLastMonths, java.lang.Integer untilDaysAgo, java.lang.Integer untilMonthsAgo, java.util.Date sinceDate, java.util.Date untilDate)
public static java.util.Date toDateHelper(java.util.Date comparisonDate, java.lang.Integer withinLastDays, java.lang.Integer withinLastMonths, java.lang.Integer untilDaysAgo, java.lang.Integer untilMonthsAgo, java.util.Date sinceDate, java.util.Date untilDate)
public static <T> boolean containsAny(java.util.Collection<T> collection, java.util.Collection<T> elements)
collection
- elements
- public static <K,V> void addToSetMap(java.util.Map<K,java.util.Set<V>> map, K key, V obj)
public static <K,V> void addToListMap(java.util.Map<K,java.util.List<V>> map, K key, V obj)
@Deprecated public static java.text.SimpleDateFormat getDateFormat()
Context.getDateFormat()
or #getDateFormat(Context#getLocale())
insteadpublic static java.text.SimpleDateFormat getDateFormat(java.util.Locale locale)
public static java.text.SimpleDateFormat getTimeFormat(java.util.Locale locale)
public static java.text.SimpleDateFormat getDateTimeFormat(java.util.Locale locale)
@Deprecated public static PatientFilter toPatientFilter(PatientSearch search, CohortSearchHistory history)
public static java.lang.Object parse(java.lang.String string, java.lang.Class clazz)
string
- clazz
- clazz
with the data from string
@Deprecated public static PatientFilter toPatientFilter(PatientSearch search, CohortSearchHistory history, EvaluationContext evalContext)
public static boolean collectionContains(java.util.Collection<?> objects, java.lang.Object obj)
obj
will return falseobjects
- collection to loop overobj
- Object to look for in the objects
@Deprecated public static org.simpleframework.xml.Serializer getSerializer()
@Deprecated public static org.simpleframework.xml.Serializer getShortSerializer()
OpenmrsConstants.SHORT_SERIALIZATION
@Deprecated public static boolean isShortSerialization(java.util.Map<?,?> sessionMap)
Map
parameter.sessionMap
- current serialization sessionpublic static java.io.File getOutFile(java.io.File dir, java.util.Date date, User user)
dir
- directory to make the random filename indate
- optional Date object used for the nameuser
- optional User creating this file objectpublic static java.lang.String generateUid(java.lang.Integer size)
public static java.lang.String generateUid()
generateUid(Integer)
@Deprecated public static java.lang.String postToUrl(java.lang.String urlString, java.util.Map<java.lang.String,java.lang.String> dataToPost)
urlString
- valid http url to post data todataToPost
- Mappublic static void storeProperties(java.util.Properties properties, java.io.File file, java.lang.String comment)
properties
- file
- comment
- public static void storeProperties(java.util.Properties properties, java.io.OutputStream outStream, java.lang.String comment)
properties
- file
- comment
- (which appears in comments in properties file)public static void loadProperties(java.util.Properties props, java.io.InputStream input)
loadProperties(Properties, File)
props
- the properties object to write intoinput
- the input stream to read frompublic static void loadProperties(java.util.Properties props, java.io.File propertyFile)
props
- the properties object to be loaded intopropertyFile
- the properties file to readpublic static java.lang.String getMessage(java.lang.String code, java.lang.Object... args)
code
- the message key to lookupargs
- the replacement values for the translation stringpublic static void validatePassword(java.lang.String username, java.lang.String password, java.lang.String systemId) throws PasswordException
User
. Passwords must be
non-null. Their required strength is configured via global properties:
Description | Property | Default Value |
---|---|---|
Require that it not match the User 's username or system id
|
OpenmrsConstants.GP_PASSWORD_CANNOT_MATCH_USERNAME_OR_SYSTEMID |
true |
Require a minimum length | OpenmrsConstants.GP_PASSWORD_MINIMUM_LENGTH |
8 |
Require both an upper and lower case character | OpenmrsConstants.GP_PASSWORD_REQUIRES_UPPER_AND_LOWER_CASE |
true |
Require at least one numeric character | OpenmrsConstants.GP_PASSWORD_REQUIRES_DIGIT |
true |
Require at least one non-numeric character | OpenmrsConstants.GP_PASSWORD_REQUIRES_NON_DIGIT |
true |
Require a match on the specified regular expression | OpenmrsConstants.GP_PASSWORD_CUSTOM_REGEX |
null |
username
- user name of the user with password to validatedpassword
- string that will be validatedsystemId
- system id of the user with password to be validatedPasswordException
public static boolean containsUpperAndLowerCase(java.lang.String test)
test
- the string to testpublic static boolean containsOnlyDigits(java.lang.String test)
test
- the string to testpublic static boolean containsDigit(java.lang.String test)
test
- the string to testpublic static void closeStream(java.io.Closeable closableStream)
closableStream
- an InputStream or OutputStream to closepublic static java.lang.String shortenedStackTrace(java.lang.String stackTrace)
stackTrace
- original stack trace from an errorpublic static java.util.Properties getRuntimeProperties(java.lang.String applicationName)
Finds and loads the runtime properties file for a specific OpenMRS application. Searches for the file in this order: 1) {current directory}/{applicationname}_runtime.properties 2) an environment variable called "{APPLICATIONNAME}_RUNTIME_PROPERTIES_FILE" 3) {openmrs_app_dir}/{applicationName}_runtime.properties // openmrs_app_dir is typically {user_home}/.OpenMRS
applicationName
- (defaults to "openmrs") the name of the running OpenMRS application,
e.g. if you have deployed OpenMRS as a web application you would give the deployed
context path heregetApplicationDataDirectory()
public static boolean isTestMode()
public static java.lang.String getRuntimePropertiesFilePathName(java.lang.String applicationName)
applicationName
- (defaults to "openmrs") the name of the running OpenMRS application,
e.g. if you have deployed OpenMRS as a web application you would give the deployed
context path herepublic static java.lang.String getRuntimePropertiesFileNameInTestMode()
public static java.lang.String getOpenMRSVersionInTestMode()
public static boolean nullSafeEqualsIgnoreCase(java.lang.String s1, java.lang.String s2)
s1
- the string to compares2
- the string to comparepublic static java.lang.Integer convertToInteger(java.lang.Long longValue)
longValue
- the value to convertjava.lang.IllegalArgumentException
- if the long value does not fit into an integerCopyright © 2018 OpenMRS Inc.. All Rights Reserved.