Summary Table
Categories |
Total Count |
PII |
0 |
URL |
0 |
DNS |
0 |
EKL |
0 |
IP |
0 |
PORT |
0 |
VsID |
0 |
CF |
0 |
AI |
0 |
VPD |
0 |
PL |
0 |
Other |
0 |
File Content
/*
* ApiUtil.java
* Copyright (c) 2017 Veterans Affairs.
*/
package gov.va.oneconsult.seoc.api.util;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import gov.va.oneconsult.seoc.api.model.ClinicalService;
/**
* Utility class for all API Utility methods
* @author Ablevets
*
*/
public class ApiUtil
{
public static final Logger log = LoggerFactory.getLogger(ApiUtil.class);
static MessageDigest md ;
private static String DATE_PATTERN = "MM-dd-yyyy hh:mm:ss a";
private static ZoneId UTC_ZONE = ZoneId.of("UTC");
private static DateTimeFormatter parser = DateTimeFormatter.ofPattern(DATE_PATTERN);
private static SimpleDateFormat formatter = new SimpleDateFormat(DATE_PATTERN);
static {
try
{
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e)
{
log.error("Failed to load the Algorithm.", e);
}
}
/**
* @param dateStr
* @return
*/
public static Date parseStringToDate(DateFormat formatter, String dateStr) {
Date date = null;
synchronized(formatter)
{
try
{
date = formatter.parse(dateStr);
} catch (ParseException e)
{
log.error("Unable to parse the Date value :" + dateStr);
}
return date;
}
}
/**
* @param date
* @return
*/
public static String formatDateToString(DateFormat formatter, Date date) {
String dateStr = null;
synchronized(formatter)
{
dateStr = formatter.format(date);
return dateStr;
}
}
/**
* @param dateStr
* @return
*/
public static Date parseStringToDate(String dateStr) {
Date date = null;
synchronized(formatter)
{
try
{
date = formatter.parse(dateStr);
} catch (ParseException e)
{
log.error("Unable to parse the Date : " + dateStr + ". Exception message: " + e.getMessage());
throw new IllegalArgumentException(
"Error converting String value " + dateStr + " to Date with format "
+ formatter );
}
return date;
}
}
/**
* @param date
* @return
*/
public static String formatDateToString(Date date) {
String dateStr = null;
synchronized(formatter)
{
dateStr = formatter.format(date);
return dateStr;
}
}
/**
* Description: Calculate hash value for the input
* @param input
* @return String
*/
public static String getMD5(String input)
{
byte[] messageDigest = md.digest(input.getBytes());
BigInteger number = new BigInteger(1, messageDigest);
String hashtext = number.toString(16);
// Now we need to zero pad it if you actually want the full 32 chars.
while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
return hashtext;
}
/**
* Description: Get current date in UTC format
* @return String
*/
public static Date today_UTC()
{
ZonedDateTime zdt = ZonedDateTime.now(UTC_ZONE);
String utcValue = DateTimeFormatter.ofPattern(DATE_PATTERN).format(zdt);
return convertStringToDate(utcValue);
}
/**
* Description: Get UTC time zone value for the String date passed
* @param dateStr
* @return String
*/
public static Date getUTCZoneDateTime(String dateStr)
{
if(dateStr==null || dateStr.isEmpty()) {
return null;
}
ZonedDateTime zdt = LocalDateTime.parse(dateStr, parser).atZone(UTC_ZONE);
String utcValue = DateTimeFormatter.ofPattern(DATE_PATTERN).format(zdt);
return convertStringToDate(utcValue) ;
}
/**
* Description: Get UTC time zone value for the String date passed
* @param date
* @return String
*/
public static Date getUTCZoneDateTime(Date date)
{
if(date==null) {
return null;
}
ZonedDateTime zdt = ZonedDateTime.ofInstant(date.toInstant(), UTC_ZONE);
String utcValue = DateTimeFormatter.ofPattern(DATE_PATTERN).format(zdt);
return convertStringToDate(utcValue) ;
}
/**
* Description:
* @param dateStr
* @return String
*/
public static Date convertStringToDate(String dateStr)
{
Date date = parseStringToDate(dateStr);
return date;
}
/**
* Description:
* @param date
* @return String
*/
public static String convertDateToString(Date date)
{
String dateStr = null;
dateStr = formatDateToString(date);
return dateStr;
}
/**
* Description: Given a date, returns Date to next day midnight = 0500 UTC
* @param date
* @return Date
*/
public static Date getUTCCalendarDate(Date date)
{
if(date==null) {
return date;
}
log.info("Input date " + date);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.setTimeZone(TimeZone.getTimeZone(UTC_ZONE));
Date dateUTC = getUTCZoneDateTime(formatDateToString(cal.getTime()));
log.info("UTC input date " + dateUTC);
//Set Hour,Minute,Second after converting to UTC.
cal = Calendar.getInstance();
cal.setTime(dateUTC);
cal.set(Calendar.HOUR, 5);
cal.set(Calendar.MINUTE, 1);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.AM_PM, Calendar.AM);
String dateUTC5AMStr = formatDateToString(cal.getTime());
Date dateUTC5AM = convertStringToDate(dateUTC5AMStr);
return dateUTC5AM;
}
/**
* Description: Returns true if the status is a valid status of SEOC i.e. Draft or DateHold
* @param status
* @return boolean
*/
public static boolean isValidStatus(String status)
{
if (status.equalsIgnoreCase(Constants.STATUS_INPROGRESS)
|| status.equalsIgnoreCase(Constants.STATUS_DATEHOLD))
{
return true;
}
return false;
}
/**
* Description:
* @param input
* @return String
*/
public static String removeTrailingCharacters(String input)
{
String output = input;
if(output!=null && !output.trim().isEmpty())
{
int endIndex = 0;
int length = 0;
while(output.endsWith("\n") || output.endsWith("\r")) {
endIndex = 0;
length = output.length();
if(output.endsWith("\n")) {
endIndex = output.lastIndexOf("\n");
}
if(output.endsWith("\r")) {
endIndex = output.lastIndexOf("\r");
}
if(endIndex == (length-1)) {
output = output.substring(0, endIndex);
}
}
}
return output;
}
/**
* Description: Strips medicare code from clinical service description, which is the first part of description up to separator -.
* @param clinicalServices - description of clinical services
* @return Set<String>
*/
public static Set<String> getMscFromClinicalServices(Set<ClinicalService> clinicalServices)
{
if (clinicalServices == null || clinicalServices.isEmpty())
{
log.debug("Cinical Services is empty.");
return null;
}
Set<String> csDesc = clinicalServices.stream()
.filter(cs -> cs.getDescription().indexOf(Constants.CLINICALSERVICE_SEPARATOR) > 0)
.map(fcs -> fcs.getDescription().substring(0, fcs.getDescription().indexOf(Constants.CLINICALSERVICE_SEPARATOR)).trim()).collect(Collectors.toSet());
return csDesc;
}
/**
* Description: Removes invalid chars from a given string for export to VistA
* @param string - string to update
* @return String
*/
public static String removeInvalidCharacters(String string) {
String newString = string.replaceAll("\r\n\n", "\n");
newString = newString.replaceAll("\r\n", "\n");
newString = newString.replaceAll("\r", "\n");
newString = newString.replaceAll("\n", "\r\n");
for(Map.Entry<Character, Character> entry : Constants.invalidChars.entrySet()) {
char invalidChar = entry.getKey();
char validChar = entry.getValue();
newString = newString.replace(invalidChar, validChar);
}
return newString;
}
}