package th.co.muangthai.endrprint.util; import org.apache.log4j.Logger; import th.co.muangthai.endrprint.model.hibernate.HibernateUtil; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.*; public class VSMUtil { // not constant variable because might be used in MTRAMService public static String DEPLOY_NAME = "ENDRPRINT"; // Deployment Phase Versions public static final int DEPLOYMENT_PHASE = 1; public static final int DEPLOYMENT_LIVE = 1; public static final int DEPLOYMENT_UAT = 2; public static final int DEPLOYMENT_DEV = 3; // Deployment Mode public static final int DEPLOYMENT_MODE = DEPLOYMENT_UAT; // ---- Global Status // authenticate with muangthai active directory [ false = authenticate with database ] public static final boolean AUTHENTICATION_AD = false; // true //public static final boolean INITIALSCHEDULES = false; // true // ipAddress-printer public static final boolean IP_ADDR_PRINTER_PRO = false; // true = PRODUCTION , false = UAT // log private static Logger log = Logger.getLogger(th.co.muangthai.endrprint.util.VSMUtil.class); // ----------------- public static final String USER_SESSION = "USER_SESSION"; //public static final int UPDATE_FLAG = 1; //public static final int INSERT_FLAG = 0; public static final String NEW_LINE = System.getProperty("line.separator"); public static String FORMAT_DATE = "dd/MM/yyyy"; public static String FORMAT_DATE_TIME = "dd/MM/yyyy hh:mm:ss"; public static Locale LOCALE = new Locale("th", "TH"); public static Locale LOCALE_EN = new Locale("en", "EN"); public static boolean isEmpty(Object object) { if (object == null || "".equals(object.toString().trim()) || "null".equals(object.toString().trim())) { return true; } return false; } public static boolean isNotEmpty(Object object) { return !th.co.muangthai.endrprint.util.VSMUtil.isEmpty(object); } public static boolean isNotEmptyLst(List object) { if (null != object && object.size() > 0) { return true; } return false; } public static boolean isEmptyLst(List object) { if (null == object) { return true; } if (object.size() <= 0) { return true; } return false; } public static String nvl(Object obj, String text) { return obj == null ? text : obj.toString(); } public static BigDecimal long2BigDecimal(Long longValue) { return longValue == null ? null : new BigDecimal(longValue.toString()); } public static BigDecimal getBigDecimalByString(String val) { try { BigDecimal b = new BigDecimal(val); return b; } catch (Exception e) { return BigDecimal.ZERO; } } public static Long string2Long(String longValue) { return longValue == null ? null : new Long(longValue.toString()); } public static String toNumberFormat4Digit(BigDecimal value) { if (value == null) { return ""; } DecimalFormat dc = new DecimalFormat("#,##0.0000"); return dc.format(value.doubleValue()); } public static String toNumberFormat2Digit(Double value) { if (value == null) { return ""; } DecimalFormat dc = new DecimalFormat("#,##0.00"); return dc.format(value.doubleValue()); } public static String toNumberFormat4Digit(Integer value) { if (value == null) { return ""; } DecimalFormat dc = new DecimalFormat("#,##0"); return dc.format(value.doubleValue()); } public static String toNumberFormat10K(Integer value) { if (value == null) { return ""; } DecimalFormat dc = new DecimalFormat("###000"); return dc.format(value.doubleValue()); } public static String toNumberFormatDigit(BigDecimal value, int digit) { if (value == null) { return ""; } String digitS = ""; for (int i = 0; i < digit; i++) { digitS += "0"; } if (digit > 1) { digitS = "." + digitS; } DecimalFormat dc = new DecimalFormat("#,##0" + digitS); return dc.format(value.doubleValue()); } public static String formatStringEmpty(String s) { if (s == null) return ""; return s; } public static String formatStringEmpty(Object s) { if (s == null) return ""; return s.toString(); } public static String toNumberFormat(BigDecimal value) { if (value == null) { return ""; } DecimalFormat dc = new DecimalFormat("###,###,###"); return dc.format(value.doubleValue()); } public static void SystemPrintDebugMode(String str) { //System.out.println("LOF4POS : " + str); } public static String splitComma(String amount) { String result = ""; try { String[] tmp = amount.split(","); for (int aa = 0; aa < tmp.length; aa++) { result += tmp[aa]; } } catch (Exception e) { e.printStackTrace(); } return result; } public static boolean isProductionMode() { try { if (HibernateUtil.getHibernateProperties() == null) { try { HibernateUtil.closeSession(HibernateUtil.getCurrentSession()); } catch (Exception e) { } } String url = (String) HibernateUtil.getHibernateProperties().get("connection.url"); if (th.co.muangthai.endrprint.util.VSMUtil.isNotEmpty(url)) { return url.indexOf("10.200.53.37") > -1 || url.indexOf("mtlowdb") > -1; } String dsName = (String) HibernateUtil.getHibernateProperties().get("connection.datasource"); if (th.co.muangthai.endrprint.util.VSMUtil.isNotEmpty(dsName)) { return dsName.indexOf("jdbc/LFPOSORCL") > -1; } } catch (Exception e) { log.error(e.toString(), e); } return false; } public static String formatBankAccount(String accountNo) { if (th.co.muangthai.endrprint.util.VSMUtil.isEmpty(accountNo)) { return accountNo; } if (accountNo.length() != 10) { return accountNo; } return accountNo.substring(0, 3) + "-" + accountNo.substring(3, 4) + "-" + accountNo.substring(4, 9) + "-" + accountNo.substring(9, 10); } public static int length(Collection cols) { if (cols == null) { return 0; } return cols.size(); } public static List rollupSubTotal(List list, String rollupName, String... summaryName) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException { List newList = new ArrayList(); Map subTotalMap = new HashMap(); for (String summaryName1 : summaryName) { subTotalMap.put(summaryName1, BigDecimal.ZERO); } for (int i = 0; i < list.size(); i++) { Object o1 = list.get(i); Class clazzRoll = o1.getClass(); Method methodRoll = clazzRoll.getMethod("get" + rollupName); Object objectRoll = methodRoll.invoke(o1); newList.add(o1); for (String summaryName1 : summaryName) { Method methodCal = clazzRoll.getMethod("get" + summaryName1); BigDecimal cal1Value = null; try { cal1Value = (BigDecimal) methodCal.invoke(o1); } catch (ClassCastException e) { Integer cal1ValueInt = (Integer) methodCal.invoke(o1); if (cal1ValueInt != null) { cal1Value = new BigDecimal(cal1ValueInt); } } if (cal1Value == null) { cal1Value = BigDecimal.ZERO; } BigDecimal subTotal = subTotalMap.get(summaryName1); subTotal = subTotal.add(cal1Value); subTotalMap.put(summaryName1, subTotal); } Object value2 = null; if (i != list.size() - 1) { Object o2 = list.get(i + 1); value2 = methodRoll.invoke(o2); } if (!objectRoll.equals(value2) || i == list.size() - 1) { Object newObject = clazzRoll.newInstance(); methodRoll = clazzRoll.getMethod("set" + rollupName, objectRoll.getClass()); methodRoll.invoke(newObject, objectRoll); for (String summaryName1 : summaryName) { BigDecimal subTotal = subTotalMap.get(summaryName1); try { methodRoll = clazzRoll.getMethod("set" + summaryName1, BigDecimal.class); methodRoll.invoke(newObject, subTotal); } catch (NoSuchMethodException e) { methodRoll = clazzRoll.getMethod("set" + summaryName1, Integer.class); methodRoll.invoke(newObject, subTotal.intValue()); } } newList.add(newObject); for (String summaryName1 : summaryName) { BigDecimal subTotal = subTotalMap.get(summaryName1); // BigDecimal grandTotal = grandTotalMap.get(summaryName1); // grandTotal = grandTotal.add(subTotal); // grandTotalMap.put(summaryName1, grandTotal); subTotalMap.put(summaryName1, BigDecimal.ZERO); } } } return newList; } public static List summaryGrandTotal(List list, String... summaryName) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException { List newList = new ArrayList(); Map grandTotalMap = new HashMap(); for (String summaryName1 : summaryName) { grandTotalMap.put(summaryName1, BigDecimal.ZERO); } for (int i = 0; i < list.size(); i++) { Object o1 = list.get(i); Class clazzRoll = o1.getClass(); newList.add(o1); for (String summaryName1 : summaryName) { Method methodCal = clazzRoll.getMethod("get" + summaryName1); BigDecimal cal1Value = null; try { cal1Value = (BigDecimal) methodCal.invoke(o1); } catch (ClassCastException e) { Integer cal1ValueInt = (Integer) methodCal.invoke(o1); if (cal1ValueInt != null) { cal1Value = new BigDecimal(cal1ValueInt); } } if (cal1Value == null) { cal1Value = BigDecimal.ZERO; } BigDecimal subTotal = grandTotalMap.get(summaryName1); subTotal = subTotal.add(cal1Value); grandTotalMap.put(summaryName1, subTotal); } // Grand Total if (i == list.size() - 1) { Object newObject = clazzRoll.newInstance(); for (String summaryName1 : summaryName) { BigDecimal grandTotal = grandTotalMap.get(summaryName1); try { Method methodRoll = clazzRoll.getMethod("set" + summaryName1, BigDecimal.class); methodRoll.invoke(newObject, grandTotal); } catch (NoSuchMethodException e) { Method methodRoll = clazzRoll.getMethod("set" + summaryName1, Integer.class); methodRoll.invoke(newObject, grandTotal.intValue()); } } newList.add(newObject); } } return newList; } /** * @param list * @param rollupName group by field such as bank_account_no * @param summaryName summary field such as amount, feeAmount * @return * @throws NoSuchMethodException * @throws java.lang.reflect.InvocationTargetException * @throws IllegalAccessException * @throws InstantiationException */ public static List rollupGrandTotal(List list, String rollupName, String... summaryName) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException { List newList = new ArrayList(); Map subTotalMap = new HashMap(); for (String summaryName1 : summaryName) { subTotalMap.put(summaryName1, BigDecimal.ZERO); } Map grandTotalMap = new HashMap(); for (String summaryName1 : summaryName) { grandTotalMap.put(summaryName1, BigDecimal.ZERO); } for (int i = 0; i < list.size(); i++) { Object o1 = list.get(i); Class clazzRoll = o1.getClass(); Method methodRoll = clazzRoll.getMethod("get" + rollupName); Object objectRoll = methodRoll.invoke(o1); newList.add(o1); for (String summaryName1 : summaryName) { Method methodCal = clazzRoll.getMethod("get" + summaryName1); BigDecimal cal1Value = null; try { cal1Value = (BigDecimal) methodCal.invoke(o1); } catch (ClassCastException e) { Integer cal1ValueInt = (Integer) methodCal.invoke(o1); if (cal1ValueInt != null) { cal1Value = new BigDecimal(cal1ValueInt); } } if (cal1Value == null) { cal1Value = BigDecimal.ZERO; } BigDecimal subTotal = subTotalMap.get(summaryName1); subTotal = subTotal.add(cal1Value); subTotalMap.put(summaryName1, subTotal); } Object value2 = null; if (i != list.size() - 1) { Object o2 = list.get(i + 1); value2 = methodRoll.invoke(o2); } if (!objectRoll.equals(value2) || i == list.size() - 1) { Object newObject = clazzRoll.newInstance(); methodRoll = clazzRoll.getMethod("set" + rollupName, objectRoll.getClass()); methodRoll.invoke(newObject, objectRoll); for (String summaryName1 : summaryName) { BigDecimal subTotal = subTotalMap.get(summaryName1); try { methodRoll = clazzRoll.getMethod("set" + summaryName1, BigDecimal.class); methodRoll.invoke(newObject, subTotal); } catch (NoSuchMethodException e) { methodRoll = clazzRoll.getMethod("set" + summaryName1, Integer.class); methodRoll.invoke(newObject, subTotal.intValue()); } } newList.add(newObject); for (String summaryName1 : summaryName) { BigDecimal subTotal = subTotalMap.get(summaryName1); BigDecimal grandTotal = grandTotalMap.get(summaryName1); grandTotal = grandTotal.add(subTotal); grandTotalMap.put(summaryName1, grandTotal); subTotalMap.put(summaryName1, BigDecimal.ZERO); } } // Grand Total if (i == list.size() - 1) { Object newObject = clazzRoll.newInstance(); for (String summaryName1 : summaryName) { BigDecimal grandTotal = grandTotalMap.get(summaryName1); try { methodRoll = clazzRoll.getMethod("set" + summaryName1, BigDecimal.class); methodRoll.invoke(newObject, grandTotal); } catch (NoSuchMethodException e) { methodRoll = clazzRoll.getMethod("set" + summaryName1, Integer.class); methodRoll.invoke(newObject, grandTotal.intValue()); } } newList.add(newObject); } } return newList; } public static Object getDataInBeanByTxt(Object source, String beanName) { PropertyDescriptor[] sourceProperties = null; try { sourceProperties = Introspector.getBeanInfo(source.getClass()).getPropertyDescriptors(); } catch (IntrospectionException ex) { return ("Couldn't derive property descriptors."); } //StringBuffer results = new StringBuffer(); Object value = null; Source: for (int s = 0; s < sourceProperties.length; ++s) { String name = sourceProperties[s].getName(); try { if (beanName.equals(name)) { Method read = sourceProperties[s].getReadMethod(); if (read == null) break Source; value = read.invoke(source, null); break Source; } } catch (Exception ex) { //results.append("Exception transferring property ").append(name).append(": ").append(ex.getClass().getName()).append("\r\n"); return null; } } //return results.toString(); return value; } public static void dumpBeanLogValue(Logger log, Object bean) { if (bean == null) { //System.out.println("Bean is null."); log.info("*** Bean is null."); } try { Method[] methode = bean.getClass().getMethods(); for (int j = 0; j < methode.length; j++) { String methodeName = methode[j].getName(); methode[j].getName(); if (methodeName.startsWith("get")) { String msg = "\tMethodeName=[" + methodeName + "],Value=[" + methode[j].invoke(bean, null) + "]"; //log.info("*** " + msg); //System.out.println("\tMethodeName=[" + methodeName + "],Value=[" + methode[j].invoke(bean, null) + "]"); } } } catch (Exception ex) { ex.printStackTrace(); } } public static void dumpCollectionLog(Logger log, Collection coll) { if (coll != null) { Iterator iter = coll.iterator(); while (iter.hasNext()) { Object obj = iter.next(); dumpBeanLogValue(log, obj); } } else { //System.out.println("Collection is null."); log.info("*** Collection is null."); } } public static String getTxtByLengthF(String txt, int length, String pf) { String retData = ""; int numFor = 0; if (isNotEmpty(txt)) { numFor = length - txt.length(); retData = txt; } else { numFor = length; } for (int i = 0; i < numFor; i++) { if ("".equals(txt)) { retData = pf; } else { retData = pf + retData; } } return retData; } public static String getTxtByLengthB(String txt, int length, String pf) { String retData = ""; int numFor = 0; if (isNotEmpty(txt)) { numFor = length - txt.length(); retData = txt; } else { numFor = length; } for (int i = 0; i < numFor; i++) { if ("".equals(txt)) { retData = pf; } else { retData = retData + pf; } } return retData; } public static void main(String[] args) { try { /*if (new Boolean("true").getClass().equals("".getClass())) { }*/ //getTxtByLengthF("xxx",13,"0"); //log.info(getTxtByLengthF("xxx",13,"0")); //log.info(getTxtByLengthB("xxx",13,"0")); /*EappDataServiceInterface vvv = new EappDataServiceImp(); VwFaceAmountForm b = new VwFaceAmountForm(); b.setPlanCode("RAAN"); b.setRsCode("H"); List l = vvv.searchVwFaceAmountFormByForm(b); System.out.print(l.size());*/ //System.out.println("123456789 00000000232.22 ".substring("123456789 00000000232.22 ".trim().length()-14,"123456789 00000000232.22 ".trim().length())); //System.out.println( getTxtByLengthF("63500033015",13,"0")); } catch (Exception e) { log.error(e.toString(), e); } } public static String osbConfigSystem() { String system = "UAT"; if (isProductionMode()) { system = "PROD"; } return system; } }