1 14 package org.compiere.util; 15 16 import java.awt.*; 17 import javax.swing.*; 18 import java.net.*; 19 import java.util.*; 20 import java.sql.*; 21 22 import org.compiere.Compiere; 23 import org.compiere.model.*; 24 import org.compiere.db.CConnection; 25 26 27 33 public final class Env 34 { 35 36 private static Logger s_log = Logger.getCLogger(Env.class); 37 38 45 public static Properties initTest (int traceLevel, boolean isClient) 46 { 47 org.compiere.Compiere.startupClient(); 49 Log.setTraceLevel(traceLevel); 50 Properties ctx = Env.getCtx(); 52 KeyNamePair[] roles = DB.login(ctx, CConnection.get(), 53 "System", "System", true); 54 if (roles != null && roles.length > 0) 56 { 57 KeyNamePair[] clients = DB.loadClients (ctx, roles[0]); 58 if (clients != null && clients.length > 0) 60 { 61 KeyNamePair[] whs = DB.loadWarehouses(ctx, clients[0]); 62 KeyNamePair[] orgs = DB.loadOrgs(ctx, clients[0]); 64 if (orgs != null && orgs.length > 0) 66 { 67 DB.loadPreferences(ctx, orgs[0], null, null, null); 68 } 69 } 70 } 71 Env.setContext(ctx, "#Date", "2000-01-01"); 73 return ctx; 75 } 77 81 public static boolean isJavaOK() 82 { 83 String jVersion = System.getProperty("java.version"); 85 if (jVersion.startsWith("1.4.1") || jVersion.startsWith("1.4.2")) return true; 87 boolean ok = false; 89 if (jVersion.startsWith("1.4")) ok = true; 91 92 StringBuffer msg = new StringBuffer (); 94 msg.append(System.getProperty("java.vm.name")).append(" - ").append(jVersion); 95 if (ok) 96 msg.append("(untested)"); 97 msg.append(" <> 1.4.1/2"); 98 JOptionPane.showMessageDialog(null, msg.toString(), 100 org.compiere.Compiere.getName() + " - Java Version Check", 101 ok ? JOptionPane.WARNING_MESSAGE : JOptionPane.ERROR_MESSAGE); 102 return ok; 103 } 105 109 public static void exitEnv (int status) 110 { 111 reset(true); 112 s_log.info("exit"); 113 Logger.shutdownAll(); 114 if (Ini.isClient()) 115 System.exit (status); 116 } 118 122 public static void reset (boolean all) 123 { 124 s_log.info("reset - all=" + all); 125 126 139 s_windows.clear(); 140 141 if (all) 143 s_ctx.clear(); 144 else 145 { 146 Object [] keys = s_ctx.keySet().toArray(); 147 for (int i = 0; i < keys.length; i++) 148 { 149 String tag = keys[i].toString(); 150 if (Character.isDigit(tag.charAt(0))) 151 s_ctx.remove(keys[i]); 152 } 153 } 154 155 CacheMgt.get().reset(); 157 DB.closeTarget(); 158 MRole defaultRole = MRole.getDefault(s_ctx, false); 160 if (defaultRole != null) 161 defaultRole.loadAccess(true); } 164 165 166 167 170 private static Properties s_ctx = new Properties(); 171 172 public static final int WINDOW_FIND = 1110; 173 174 public static final int WINDOW_MLOOKUP = 1111; 175 176 public static final int WINDOW_CUSTOMIZE = 1112; 177 178 public static final int WINDOW_INFO = 1113; 179 180 181 public static final int TAB_INFO = 1113; 182 183 187 public static final Properties getCtx() 188 { 189 return s_ctx; 190 } 192 196 public static void setCtx (Properties ctx) 197 { 198 if (ctx == null) 199 throw new IllegalArgumentException ("Env.setCtx - require Context"); 200 s_ctx.clear(); 201 s_ctx = ctx; 202 } 204 210 public static void setContext (Properties ctx, String context, String value) 211 { 212 if (ctx == null || context == null) 213 return; 214 Log.trace(7, "Context " + context + "==" + value); 215 if (value == null || value.length() == 0) 217 ctx.remove(context); 218 else 219 ctx.setProperty(context, value); 220 } 222 228 public static void setContext (Properties ctx, String context, int value) 229 { 230 if (ctx == null || context == null) 231 return; 232 Log.trace(7, "Context " + context + "==" + value); 233 ctx.setProperty(context, String.valueOf(value)); 235 } 237 243 public static void setContext (Properties ctx, String context, boolean value) 244 { 245 setContext (ctx, context, value ? "Y" : "N"); 246 } 248 255 public static void setContext (Properties ctx, int WindowNo, String context, String value) 256 { 257 if (ctx == null || context == null) 258 return; 259 if (WindowNo != WINDOW_FIND && WindowNo != WINDOW_MLOOKUP) 260 Log.trace(8, "Context("+WindowNo+") " + context + "==" + value); 261 if (value == null || value.equals("")) 263 ctx.remove(WindowNo+"|"+context); 264 else 265 ctx.setProperty(WindowNo+"|"+context, value); 266 } 268 275 public static void setContext (Properties ctx, int WindowNo, String context, int value) 276 { 277 if (ctx == null || context == null) 278 return; 279 if (WindowNo != WINDOW_FIND && WindowNo != WINDOW_MLOOKUP) 280 Log.trace(8, "Context("+WindowNo+") " + context + "==" + value); 281 ctx.setProperty(WindowNo+"|"+context, String.valueOf(value)); 283 } 285 292 public static void setContext (Properties ctx, int WindowNo, String context, boolean value) 293 { 294 setContext (ctx, WindowNo, context, value ? "Y" : "N"); 295 } 297 305 public static void setContext (Properties ctx, int WindowNo, int TabNo, String context, String value) 306 { 307 if (ctx == null || context == null) 308 return; 309 if (WindowNo != WINDOW_FIND && WindowNo != WINDOW_MLOOKUP) 310 Log.trace(9, "Context("+WindowNo+","+TabNo+") " + context + "==" + value); 311 if (value == null || value.equals("")) 313 ctx.remove(WindowNo+"|"+TabNo+"|"+context); 314 else 315 ctx.setProperty(WindowNo+"|"+TabNo+"|"+context, value); 316 } 318 323 public static void setAutoCommit (Properties ctx, boolean autoCommit) 324 { 325 if (ctx == null) 326 return; 327 ctx.setProperty("AutoCommit", autoCommit ? "Y" : "N"); 328 } 330 336 public static void setAutoCommit (Properties ctx, int WindowNo, boolean autoCommit) 337 { 338 if (ctx == null) 339 return; 340 ctx.setProperty(WindowNo+"|AutoCommit", autoCommit ? "Y" : "N"); 341 } 343 349 public static String getContext (Properties ctx, String context) 350 { 351 if (ctx == null || context == null) 352 throw new IllegalArgumentException ("Env.getContext - require Context"); 353 return ctx.getProperty(context, ""); 354 } 356 365 public static String getContext (Properties ctx, int WindowNo, String context, boolean onlyWindow) 366 { 367 if (ctx == null) 368 throw new IllegalArgumentException ("Env.getContext - No Ctx"); 369 if (context == null) 370 throw new IllegalArgumentException ("Env.getContext - require Context"); 371 String s = ctx.getProperty(WindowNo+"|"+context); 372 if (s == null) 373 { 374 if (context.startsWith("#") || context.startsWith("$")) 376 return getContext(ctx, context); 377 if (onlyWindow) return ""; 379 return getContext(ctx, "#" + context); 380 } 381 return s; 382 } 384 392 public static String getContext (Properties ctx, int WindowNo, String context) 393 { 394 return getContext(ctx, WindowNo, context, false); 395 } 397 406 public static String getContext (Properties ctx, int WindowNo, int TabNo, String context) 407 { 408 if (ctx == null || context == null) 409 throw new IllegalArgumentException ("Env.getContext - require Context"); 410 String s = ctx.getProperty(WindowNo+"|"+TabNo+"|"+context); 411 if (s == null) 412 return getContext(ctx, WindowNo, context, false); 413 return s; 414 } 416 422 public static int getContextAsInt(Properties ctx, String context) 423 { 424 if (ctx == null || context == null) 425 throw new IllegalArgumentException ("Env.getContext - require Context"); 426 String s = getContext(ctx, context); 427 if (s.length() == 0) 428 s = getContext(ctx, 0, context, false); if (s.length() == 0) 430 return 0; 431 try 433 { 434 return Integer.parseInt(s); 435 } 436 catch (NumberFormatException e) 437 { 438 s_log.error("getContextAsInt (" + context + ") = " + s, e); 439 } 440 return 0; 441 } 443 450 public static int getContextAsInt(Properties ctx, int WindowNo, String context) 451 { 452 String s = getContext(ctx, WindowNo, context, false); 453 if (s.length() == 0) 454 return 0; 455 try 457 { 458 return Integer.parseInt(s); 459 } 460 catch (NumberFormatException e) 461 { 462 s_log.error("getContextAsInt (" + context + ") = " + s, e); 463 } 464 return 0; 465 } 467 475 public static int getContextAsInt (Properties ctx, int WindowNo, int TabNo, String context) 476 { 477 String s = getContext(ctx, WindowNo, TabNo, context); 478 if (s.length() == 0) 479 return 0; 480 try 482 { 483 return Integer.parseInt(s); 484 } 485 catch (NumberFormatException e) 486 { 487 s_log.error("getContextAsInt (" + context + ") = " + s, e); 488 } 489 return 0; 490 } 492 497 public static boolean isAutoCommit (Properties ctx) 498 { 499 if (ctx == null) 500 throw new IllegalArgumentException ("Env.getContext - require Context"); 501 String s = getContext(ctx, "AutoCommit"); 502 if (s != null && s.equals("Y")) 503 return true; 504 return false; 505 } 507 513 public static boolean isAutoCommit (Properties ctx, int WindowNo) 514 { 515 if (ctx == null) 516 throw new IllegalArgumentException ("Env.getContext - require Context"); 517 String s = getContext(ctx, WindowNo, "AutoCommit", false); 518 if (s != null) 519 { 520 if (s.equals("Y")) 521 return true; 522 else 523 return false; 524 } 525 return isAutoCommit(ctx); 526 } 528 535 public static Timestamp getContextAsDate(Properties ctx, String context) 536 { 537 return getContextAsDate(ctx, 0, context); 538 } 540 548 public static Timestamp getContextAsDate(Properties ctx, int WindowNo, String context) 549 { 550 if (ctx == null || context == null) 551 throw new IllegalArgumentException ("Env.getContext - require Context"); 552 String s = getContext(ctx, WindowNo, context, false); 553 if (s == null || s.equals("")) 555 { 556 s_log.error("getContextAsDate - No value for: " + context); 557 return new Timestamp(System.currentTimeMillis()); 558 } 559 560 if (s.trim().length() == 10) 562 s = s.trim() + " 00:00:00.0"; 563 564 return Timestamp.valueOf(s); 565 } 567 572 public static int getAD_Client_ID (Properties ctx) 573 { 574 return Env.getContextAsInt(ctx, "#AD_Client_ID"); 575 } 577 582 public static int getAD_Org_ID (Properties ctx) 583 { 584 return Env.getContextAsInt(ctx, "#AD_Org_ID"); 585 } 587 588 589 604 public static String getPreference (Properties ctx, int AD_Window_ID, String context, boolean system) 605 { 606 if (ctx == null || context == null) 607 throw new IllegalArgumentException ("Env.getPreference - require Context"); 608 String retValue = null; 609 if (!system) { 612 retValue = ctx.getProperty("P"+AD_Window_ID+"|"+context); if (retValue == null) 614 retValue = ctx.getProperty("P|"+context); } 616 else { 618 retValue = ctx.getProperty("#"+context); if (retValue == null) 620 retValue = ctx.getProperty("$"+context); } 622 return (retValue == null ? "" : retValue); 624 } 626 629 630 631 static public final String LANG = "#AD_Language"; 632 633 639 public static boolean isBaseLanguage (Properties ctx, String TableName) 640 { 641 if (TableName.startsWith("AD") || TableName.equals("C_UOM")) 642 Language.isBaseLanguage (getAD_Language(ctx)); 643 else if (!isMultiLingualDocument(ctx)) 645 return true; return Language.isBaseLanguage (getAD_Language(ctx)); 647 } 649 655 public static boolean isBaseLanguage (String AD_Language, String TableName) 656 { 657 if (TableName.startsWith("AD") || TableName.equals("C_UOM")) 658 Language.isBaseLanguage (AD_Language); 659 else if (!isMultiLingualDocument(s_ctx)) return true; return Language.isBaseLanguage (AD_Language); 663 } 665 671 public static boolean isBaseLanguage (Language language, String TableName) 672 { 673 if (TableName.startsWith("AD") || TableName.equals("C_UOM")) 674 language.isBaseLanguage(); 675 else if (!isMultiLingualDocument(s_ctx)) return true; return language.isBaseLanguage(); 679 } 681 687 public static boolean isMultiLingualDocument (Properties ctx) 688 { 689 return "Y".equals(Env.getContext(ctx, "#IsMultiLingualDocument")); 690 } 692 697 public static String getAD_Language (Properties ctx) 698 { 699 if (ctx != null) 700 { 701 String lang = getContext(ctx, LANG); 702 if (lang != null || lang.length() > 0) 703 return lang; 704 } 705 return Language.getBaseAD_Language(); 706 } 708 713 public static Language getLanguage (Properties ctx) 714 { 715 if (ctx != null) 716 { 717 String lang = getContext(ctx, LANG); 718 if (lang != null || lang.length() > 0) 719 return Language.getLanguage(lang); 720 } 721 return Language.getLanguage(); 722 } 724 730 public static void verifyLanguage (Properties ctx, Language language) 731 { 732 if (language.isBaseLanguage()) 733 return; 734 boolean IsSystemLanguage = false; 735 String sql = "SELECT IsSystemLanguage FROM AD_Language WHERE AD_Language=?"; 736 try 737 { 738 PreparedStatement pstmt = DB.prepareStatement(sql); 739 pstmt.setString(1, language.getAD_Language()); 740 ResultSet rs = pstmt.executeQuery(); 741 if (rs.next()) 742 IsSystemLanguage = "Y".equals(rs.getString(1)); 743 rs.close(); 744 pstmt.close(); 745 } 746 catch (SQLException e) 747 { 748 s_log.error("verifyLanguage (1)", e); 749 } 750 if (IsSystemLanguage && !"0".equals(Msg.getMsg(ctx, "0"))) 752 return; 753 s_log.warn ("verifyLanguage - Not System Language=" + language); 755 756 sql = "SELECT AD_Language FROM AD_Language WHERE AD_Language LIKE ?" 758 + " AND (IsSystemLanguage='Y' OR IsBaseLanguage='Y')"; 759 String newLanguage = null; 760 try 761 { 762 PreparedStatement pstmt = DB.prepareStatement(sql); 763 pstmt.setString(1, language.getAD_Language().substring(0,2) + "%"); ResultSet rs = pstmt.executeQuery(); 765 if (rs.next()) 766 newLanguage = rs.getString(1); 767 rs.close(); 768 pstmt.close(); 769 } 770 catch (SQLException e) 771 { 772 s_log.error("verifyLanguage (2)", e); 773 } 774 if (newLanguage != null && !"0".equals(Msg.getMsg(newLanguage, "0"))) 776 { 777 s_log.info("verifyLanguage - Found similar Language " + newLanguage); 778 language.setAD_Language(newLanguage); 779 Env.setContext(ctx, Env.LANG, newLanguage); 780 return; 781 } 782 783 s_log.info("verifyLanguage - Set to Base Language " + Language.getBaseAD_Language()); 785 language.setAD_Language(Language.getBaseAD_Language()); 786 Env.setContext(ctx, Env.LANG, Language.getBaseAD_Language()); 787 Msg.getMsg(ctx, "0"); 789 } 791 792 793 798 public static String [] getEntireContext(Properties ctx) 799 { 800 if (ctx == null) 801 throw new IllegalArgumentException ("Env.getEntireContext - require Context"); 802 Iterator keyIterator = ctx.keySet().iterator(); 803 String [] sList = new String [ctx.size()]; 804 int i = 0; 805 while (keyIterator.hasNext()) 806 { 807 Object key = keyIterator.next(); 808 sList[i++] = key.toString() + " == " + ctx.get(key).toString(); 809 } 810 811 return sList; 812 } 814 820 public static String getHeader(Properties ctx, int WindowNo) 821 { 822 StringBuffer sb = new StringBuffer (); 823 if (WindowNo > 0) 824 sb.append(getContext(ctx, WindowNo, "WindowName", false)).append(" "); 825 sb.append(getContext(ctx, "#AD_User_Name")).append("@") 826 .append(getContext(ctx, "#AD_Client_Name")).append(".") 827 .append(getContext(ctx, "#AD_Org_Name")) 828 .append(" [").append(CConnection.get().toString()).append("]"); 829 return sb.toString(); 830 } 832 837 public static void clearWinContext(Properties ctx, int WindowNo) 838 { 839 if (ctx == null) 840 throw new IllegalArgumentException ("Env.clearWinContext - require Context"); 841 Object [] keys = ctx.keySet().toArray(); 843 for (int i = 0; i < keys.length; i++) 844 { 845 String tag = keys[i].toString(); 846 if (tag.startsWith(WindowNo+"|")) 847 ctx.remove(keys[i]); 848 } 849 MLookupCache.cacheReset(WindowNo); 851 removeWindow(WindowNo); 854 } 856 860 public static void clearContext(Properties ctx) 861 { 862 if (ctx == null) 863 throw new IllegalArgumentException ("Env.clearContext - require Context"); 864 ctx.clear(); 865 } 867 868 878 public static String parseContext (Properties ctx, int WindowNo, String value, 879 boolean onlyWindow, boolean ignoreUnparsable) 880 { 881 if (value == null) 882 return ""; 883 884 String token; 885 String inStr = new String (value); 886 StringBuffer outStr = new StringBuffer (); 887 888 int i = inStr.indexOf("@"); 889 while (i != -1) 890 { 891 outStr.append(inStr.substring(0, i)); inStr = inStr.substring(i+1, inStr.length()); 894 int j = inStr.indexOf("@"); if (j < 0) 896 { 897 s_log.error("parseContext - no second tag: " + inStr); 898 return ""; } 900 901 token = inStr.substring(0, j); 902 903 String ctxInfo = getContext(ctx, WindowNo, token, onlyWindow); if (ctxInfo.length() == 0 && (token.startsWith("#") || token.startsWith("$")) ) 905 ctxInfo = getContext(ctx, token); if (ctxInfo.length() == 0) 907 { 908 Log.trace(Log.l5_DData, "Env.parseContext - no context (" + WindowNo + ") for: " + token); 909 if (!ignoreUnparsable) 910 return ""; } 912 else 913 outStr.append(ctxInfo); 915 inStr = inStr.substring(j+1, inStr.length()); i = inStr.indexOf("@"); 917 } 918 outStr.append(inStr); 920 return outStr.toString(); 921 } 923 932 public static String parseContext (Properties ctx, int WindowNo, String value, 933 boolean onlyWindow) 934 { 935 return parseContext(ctx, WindowNo, value, onlyWindow, false); 936 } 938 939 940 private static ArrayList s_windows = new ArrayList(20); 941 942 948 public static int createWindowNo(Container win) 949 { 950 int retValue = s_windows.size(); 951 s_windows.add(win); 952 return retValue; 953 } 955 960 public static int getWindowNo (Container container) 961 { 962 if (container == null) 963 return 0; 964 JFrame winFrame = getFrame(container); 965 if (winFrame == null) 966 return 0; 967 968 for (int i = 0; i < s_windows.size(); i++) 970 { 971 Container cmp = (Container)s_windows.get(i); 972 if (cmp != null) 973 { 974 JFrame cmpFrame = getFrame(cmp); 975 if (winFrame.equals(cmpFrame)) 976 return i; 977 } 978 } 979 return 0; 980 } 982 987 public static JFrame getWindow (int WindowNo) 988 { 989 JFrame retValue = null; 990 try 991 { 992 retValue = getFrame ((Container)s_windows.get(WindowNo)); 993 } 994 catch (Exception e) 995 { 996 System.err.println("Env.getWindow - " + e); 997 } 998 return retValue; 999 } 1001 1005 private static void removeWindow (int WindowNo) 1006 { 1007 if (WindowNo <= s_windows.size()) 1008 s_windows.set(WindowNo, null); 1009 } 1011 1015 public static void clearWinContext(int WindowNo) 1016 { 1017 clearWinContext (s_ctx, WindowNo); 1018 } 1020 1023 public static void clearContext() 1024 { 1025 s_ctx.clear(); 1026 } 1028 1029 1030 1031 1036 public static JFrame getFrame (Container container) 1037 { 1038 Container element = container; 1039 while (element != null) 1040 { 1041 if (element instanceof JFrame) 1042 return (JFrame)element; 1043 element = element.getParent(); 1044 } 1045 return null; 1046 } 1048 1055 public static Graphics getGraphics (Container container) 1056 { 1057 Container element = container; 1058 while (element != null) 1059 { 1060 Graphics g = element.getGraphics(); 1061 if (g != null) 1062 return g; 1063 element = element.getParent(); 1064 } 1065 return null; 1066 } 1068 1073 public static Window getParent (Container container) 1074 { 1075 Container element = container; 1076 while (element != null) 1077 { 1078 if (element instanceof JDialog || element instanceof JFrame) 1079 return (Window)element; 1080 element = element.getParent(); 1081 } 1082 return null; 1083 } 1085 1086 1087 1093 public static Image getImage (String fileNameInImageDir) 1094 { 1095 URL url = Compiere.class.getResource("images/" + fileNameInImageDir); 1096 if (url == null) 1097 return null; 1098 Toolkit tk = Toolkit.getDefaultToolkit(); 1099 return tk.getImage(url); 1100 } 1102 1108 public static ImageIcon getImageIcon (String fileNameInImageDir) 1109 { 1110 URL url = Compiere.class.getResource("images/" + fileNameInImageDir); 1111 if (url == null) 1112 return null; 1113 return new ImageIcon(url); 1114 } 1116 1117 1118 1119 1123 public static void startBrowser (String url) 1124 { 1125 Log.trace(Log.l1_User, "Env.startBrowser", url); 1126 String cmd = "explorer "; 1128 if (!System.getProperty("os.name").startsWith("Win")) 1129 cmd = "netscape "; 1130 String execute = cmd + url; 1132 try 1133 { 1134 Runtime.getRuntime().exec(execute); 1135 } 1136 catch (Exception e) 1137 { 1138 System.err.println("Env.startBrowser - " + execute + " - " + e); 1139 } 1140 } 1142 1145 1146 1149 static final public java.math.BigDecimal ZERO = new java.math.BigDecimal (0.0); 1150 static final public java.math.BigDecimal ONE = new java.math.BigDecimal (1.0); 1151 1152 1155 public static final String NL = System.getProperty("line.separator"); 1156 1157 1158 1161 static 1162 { 1163 s_ctx.put(LANG, Language.getBaseAD_Language()); 1165 } 1167} | Popular Tags |