package com.tcs.hrr.util;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;
import org.w3c.dom.NodeList;
/**
* @author Kavin
*/
public class StrUtils {
public Logger LOG = Logger.getLogger(this.getClass().getName());
public String getValueOfKeyFormProperty(String key) {
String resultStr = "";
Properties prop = new Properties();
InputStream stream = null;
String path = getClass().getResource("/").getPath();
try {
stream = new BufferedInputStream(new FileInputStream(
new File(path + "/resources/application_en.properties")));
} catch (FileNotFoundException e1) {
LOG.error(e1.getMessage(), e1);
}
try {
prop.load(stream);
resultStr = prop.getProperty(key);
stream.close();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
return resultStr;
}
public String getValueOfKeyFormConfiguration(String key) {
String resultStr = "";
Properties prop = new Properties();
InputStream stream = null;
String path = getClass().getResource("/").getPath();
try {
stream = new BufferedInputStream(new FileInputStream(
new File(path + "/resources/configuration.properties")));
} catch (FileNotFoundException e1) {
LOG.error(e1.getMessage(), e1);
}
try {
prop.load(stream);
resultStr = prop.getProperty(key).trim();
stream.close();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
return resultStr;
}
static public boolean isInt(String val) {
try {
int intVal = Integer.parseInt(val);
} catch (Exception e) {
new StrUtils().LOG.error(e.getMessage(), e);
return false;
}
return true;
}
public static String checkString(String toCheck) {
if (toCheck != null) {
return toCheck.trim();
} else {
return "";
}
}
public static String checkForNull(String p_str){
return p_str==null?"":p_str;
}
public static boolean isNull(String p_str){
return (p_str==null)?true:false;
}
public static boolean isNullOrEmpty(String p_str){
boolean t_B = false;
if(p_str==null){
t_B = true;
}else{
if( p_str.equals("") )
t_B= true;
}
return t_B;
}
public static boolean isNotNullOrEmpty(String p_str){
boolean t_B = true;
if(p_str==null){
t_B = false;
}else{
if( p_str.equals("") )
t_B= false;
}
return t_B;
}
public static String checkForNullEmpty(String p_str){
if(p_str!=null && !"".equals(p_str)){
return p_str;
} else {
return "";
}
}
public static String getStringFromXml(String filePath) {
String s = null;
try {
File ff = new File(filePath);
FileInputStream fis = new FileInputStream(ff);
int length = fis.available();
byte[] b = new byte[length];
fis.read(b);
s = new String(b);
new StrUtils().LOG.info(s);
} catch (IOException e) {
new StrUtils().LOG.error(e.getMessage(), e);
System.exit(1);
}
return s;
}
public static Date convertStringToDate(String strDate)
throws ParseException {
Date aDate = null;
try {
aDate = convertStringToDate("dd-MM-yyyy", strDate);
new StrUtils().LOG.info("aDate: " + aDate);
} catch (ParseException pe) {
new StrUtils().LOG.error(pe.getMessage(), pe);
throw new ParseException(pe.getMessage(), pe.getErrorOffset());
}
return aDate;
}
public static final Date convertStringToDate(String aMask, String strDate)
throws ParseException {
SimpleDateFormat df = null;
Date date = null;
df = new SimpleDateFormat(aMask);
try {
date = df.parse(strDate);
} catch (ParseException pe) {
new StrUtils().LOG.error(pe.getMessage(), pe);
throw new ParseException(pe.getMessage(), pe.getErrorOffset());
}
return (date);
}
public static String convertNumberToCurrency(double param, String formatType) {
NumberFormat formatter = new DecimalFormat(formatType);
String str_Result = formatter.format(new Double(param));
return str_Result;
}
public static final String convertDateToString(Date aDate) {
return getDateTime("yyyy-MM-dd", aDate);
}
public static final String getDateTime(String aMask, Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate != null) {
df = new SimpleDateFormat(aMask);
returnValue = df.format(aDate);
}
return (returnValue);
}
//Filter String For fortify
public static final String filterStr(String p_Str) {
//String t_Str = "";
// if ((p_Str.indexOf('(') == -1) && (p_Str.indexOf(')') == -1)) {
// return p_Str;
// } else {
// return "";
// }
return ((p_Str==null)?"":p_Str);
//return t_Str;
}
//Filter Object For fortify
public static final Object filterObj(Object p_Obj) {
return p_Obj;
}
/**
* parse xml
* @param inXml
* @param targetName
* @return
* @throws Exception
*/
public static String getValueOfXmlTagertName(String inXml, String targetName)
throws Exception {
String resultStr = "";
try {
Document doc = DocumentHelper.parseText(inXml);
Node node = doc.selectSingleNode(targetName);
resultStr = node.getText();
} catch (Exception e) {
new StrUtils().LOG.error(e.getMessage(), e);
}
return resultStr;
}
public static String getValueOfXmlTagertNameParameter(String inXml, String targetName,String targetName2,String targetParameter)
throws Exception {
String resultStr = "DCL";
try {
//Document doc = DocumentHelper.parseText(inXml);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document doc2 = db.parse(new ByteArrayInputStream(inXml.getBytes()));
doc2.getDocumentElement().normalize();
NodeList listOfPersons = doc2.getElementsByTagName(targetName);
NodeList listOfPersons2 = ((org.w3c.dom.Element)listOfPersons.item(0)).getElementsByTagName(targetName2);
org.w3c.dom.Element node = (org.w3c.dom.Element)listOfPersons2.item(0);
resultStr = node.getAttribute(targetParameter);
} catch (Exception e) {
new StrUtils().LOG.error(e.getMessage(), e);
}
return resultStr;
}
public static String toTitleCase(String str) {
if(str!=null){
Pattern pTitleCase = Pattern.compile("\\b\\p{Lower}");
str = str.toLowerCase();
Matcher m = pTitleCase.matcher(str);
while (m.find()) {
String regex = "\\b" + m.group(0);
str = str.replaceAll(regex, m.group(0).toUpperCase());
}
}
return str;
}
/**
* substr function
* @param originStr
* @param length
* @return
*/
public static String getSubString(String originStr, int length) {
String resultStr = "";
if(!originStr.equals("") && originStr.length() > length) {
resultStr = originStr.substring(0, length);
} else {
resultStr = originStr;
}
return resultStr;
}
public static double getPremiumOfOtherFrequency(double annualPremium, double modalFactor, double gst) {
BigDecimal b_annualPremium = new BigDecimal(String.valueOf(annualPremium));
BigDecimal b_modalFactor = new BigDecimal(String.valueOf(modalFactor));
BigDecimal b_gst = new BigDecimal(String.valueOf(gst));
BigDecimal result = b_annualPremium.multiply(b_modalFactor).multiply(b_gst).setScale(2, BigDecimal.ROUND_HALF_UP);
return result.doubleValue();
}
public static double getDoubleValueWith2Decimal(double val) {
NumberFormat decimalformatter = new DecimalFormat("######0.00");
return Double.parseDouble(decimalformatter.format(val));
}
public static String formatDouble2Integer(String str) {
String resultStr = "";
if(!checkForNull(str).equals("")) {
int index = str.indexOf('.');
if(index != -1) {
resultStr = str.substring(0, index).replaceAll(",", "");
} else {
resultStr = str;
}
} else {
resultStr = "0";
}
return resultStr;
}
public static List removeDuplicateWithOrder(List list) {
Set set = new HashSet();
List newList = new ArrayList();
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
Object element = iter.next();
if (set.add(element))
newList.add(element);
}
list.clear();
list.addAll(newList);
return list;
}
public static boolean isExist(List list, String str[], String flag) {
boolean isExist = false;
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
String[] element = (String[])iter.next();
if(flag.equals("cover")) {
if(String.valueOf(element[0]).equals(String.valueOf(str[0]))) {
isExist = true;
break;
}
} else if(flag.equals("cost")) {
if(String.valueOf(element[1]).equals(String.valueOf(str[1]))) {
isExist = true;
break;
}
}
}
new StrUtils().LOG.info("isExist: " + isExist);
return isExist;
}
public static String formatSentence(String str) {
if(str.indexOf(',') != -1) {
int indexI = str.lastIndexOf(',');
str = str.substring(0, indexI) + str.substring(indexI, str.length()).replaceAll(",", " and");
}
return str;
}
public static String[] splitStr(String str,String split)
{
String[] rStrs ;
String s = str.replaceAll(split, " " + split) ;
rStrs = s.split(split);
for(int i = 0 ; i < rStrs.length ; i++)
{
if(rStrs[i] == null || rStrs[i].equals(""))
rStrs[i] = "";
else if(rStrs[i].equals(" "))
rStrs[i] = rStrs[i].trim() ;
else
rStrs[i] = rStrs[i].substring(0,rStrs[i].length()-1);
}
return rStrs ;
}
}