I am trying to find a specific header string from different Maps (LEDES1998Bheaders
, LEDES98BIheaders
and LEDES98BI_V2headers
) in an errorMessage
depends on the parcel type and if the errorMessage
has the specific header string I need to replace it with the corresponding value.
public class ErrorMessageConverter {
private static Map<String, String> LEDES1998Bheaders = new HashMap<>();
private static Map<String, String> LEDES98BIheaders = new HashMap<>();
private static Map<String, String> LEDES98BI_V2headers = new HashMap<>();
static {
LEDES1998Bheaders.put("(\\binv_date\\b|\\bINVOICE DATE\\b)", "INVOICE DATE");
LEDES1998Bheaders.put("\\binv_id\\b", "INVOICE NUMBER");
LEDES1998Bheaders.put("\\bcl_id\\b", "CLIENT ID");
LEDES1998Bheaders.put("\\blf_matter_id\\b", "LAW FIRM MATTER ID");
LEDES1998Bheaders.put("\\bUNITS\\b", "LINE ITEM NUMBER OF UNITS");
LEDES1998Bheaders.put("(\\bBaseRate\\b|\\bRate\\b|\\btk_rate\\b)", "LINE ITEM UNIT COST");
LEDES98BIheaders.put("\\blf_address/address_info/address_1\\b", "LAW FIRM ADDRESS 1");
LEDES98BIheaders.put("\\blf_address/address_info/city\\b", "LAW FIRM CITY");
LEDES98BIheaders.put("\\btax_rate\\b", "LINE ITEM TAX RATE");
LEDES98BIheaders.put("\\btax_on_charge\\b", "LINE ITEM TAX TOTAL");
LEDES98BIheaders.put("\\btax_type\\b", "LINE ITEM TAX TYPE");
LEDES98BI_V2headers.put("\\binv_reported_tax_total\\b", "INVOICE REPORTED TAX TOTAL");
LEDES98BI_V2headers.put("\\binv_reported_tax_currency\\b", "INVOICE TAX CURRENCY");
}
public static String toUserFriendlyErrorMessage(String parcelType, String message) {
if (parcelType.equals("LEDES1998B")) {
return updateErrorMessage(message, LEDES1998Bheaders);
}
else if (parcelType.equals("LEDES98BI")) {
message = updateErrorMessage(message, LEDES1998Bheaders);
return updateErrorMessage(message, LEDES98BIheaders);
}
else if (parcelType.equals("LEDES98BI V2")) {
message = updateErrorMessage(message, LEDES1998Bheaders);
message = updateErrorMessage(message, LEDES98BIheaders);
return updateErrorMessage(message, LEDES98BI_V2headers);
}
return message;
}
private static String updateErrorMessage(String msg, Map<String, String> invHeaders) {
Pattern pattern;
for (String key : invHeaders.keySet()) {
pattern = Pattern.compile(key);
if (pattern.matcher(msg).find()) {
msg = msg.replaceAll(key, invHeaders.get(key));
}
}
return msg;
}
}
Below are couple of sample error messages:
String errorMesage1 = "Line 3 : Could not parse inv_date value"
String errorMesage2 = "Line : 1 BaseRate is a required field"
Can this method simplified further in java 8 using filters/lambda expressions?