1 14 package org.compiere.wstore; 15 16 import java.util.*; 17 import java.sql.*; 18 19 import org.apache.log4j.Logger; 20 21 import org.compiere.model.*; 22 import org.compiere.util.DB; 23 import org.compiere.util.Env; 24 import org.compiere.www.*; 25 26 33 public class WebUser 34 { 35 41 public static WebUser get (Properties ctx, String email) 42 { 43 return get (ctx, email, null, true); 44 } 46 54 public static WebUser get (Properties ctx, String email, String password, boolean useCache) 55 { 56 if (!useCache) 57 s_cache = null; 58 if (s_cache != null && email != null && email.equals(s_cache.getEmail())) 59 { 60 if (password == null || password.equals(s_cache.getPassword())) 62 return s_cache; 63 s_cache.setPasswordOK(false, null); 64 return s_cache; 65 } 66 s_cache = new WebUser (ctx, email, password); 67 return s_cache; 68 } 70 76 public static WebUser get (Properties ctx, int AD_User_ID) 77 { 78 if (s_cache != null && s_cache.getAD_User_ID() == AD_User_ID) 79 return s_cache; 80 s_cache = new WebUser (ctx, AD_User_ID); 81 return s_cache; 82 } 84 85 private static WebUser s_cache = null; 86 87 88 89 90 public static final String NAME = "webUser"; 91 92 private Logger log = Logger.getLogger(getClass()); 93 94 100 private WebUser (Properties ctx, String email, String password) 101 { 102 m_ctx = ctx; 103 m_AD_Client_ID = Env.getContextAsInt(ctx, "#AD_Client_ID"); 104 load (email, password); 105 } 107 112 private WebUser (Properties ctx, int AD_User_ID) 113 { 114 m_ctx = ctx; 115 m_AD_Client_ID = Env.getContextAsInt(ctx, "#AD_Client_ID"); 116 load (AD_User_ID); 117 } 119 private Properties m_ctx; 120 private MBPartner m_bp; 122 private MUser m_bpc; 123 private MBPartner_Location m_bpl; 124 private MLocation m_loc; 125 private boolean m_passwordOK = false; 127 private String m_passwordMessage; 128 private String m_saveErrorMessage; 129 private int m_AD_Client_ID = 0; 131 private boolean m_loggedIn = false; 132 133 134 139 private void load (String email, String password) 140 { 141 log.info("load -" + email + "- AD_Client_ID=" + m_AD_Client_ID); 142 String sql = "SELECT * " 143 + "FROM AD_User " 144 + "WHERE AD_Client_ID=?" 145 + " AND TRIM(EMail)=?"; 146 if (email == null) 147 email = ""; 148 149 PreparedStatement pstmt = null; 150 try 151 { 152 pstmt = DB.prepareStatement(sql); 153 pstmt.setInt(1, m_AD_Client_ID); 154 pstmt.setString(2, email.trim()); 155 ResultSet rs = pstmt.executeQuery(); 156 if (rs.next()) 157 { 158 m_bpc = new MUser (m_ctx, rs); 159 log.debug("load - found BPC=" + m_bpc); 160 } 161 rs.close(); 162 pstmt.close(); 163 pstmt = null; 164 } 165 catch (Exception e) 166 { 167 log.error("load", e); 168 } 169 finally 170 { 171 try 172 { 173 if (pstmt != null) 174 pstmt.close (); 175 } 176 catch (Exception e) 177 {} 178 pstmt = null; 179 } 180 181 m_passwordOK = false; 183 if (m_bpc != null && password != null && password.equals(m_bpc.getPassword())) 184 m_passwordOK = true; 185 if (m_passwordOK || m_bpc == null) 186 m_passwordMessage = null; 187 else 188 setPasswordOK (false, password); 189 190 if (m_bpc != null) 192 { 193 m_bp = new MBPartner (m_ctx, m_bpc.getC_BPartner_ID ()); 194 log.debug("load - found BP=" + m_bp); 195 } 196 else 197 m_bp = null; 198 if (m_bpc != null) 200 { 201 if (m_bpc.getC_BPartner_Location_ID() != 0) 202 { 203 m_bpl = new MBPartner_Location (m_ctx, m_bpc.getC_BPartner_Location_ID ()); 204 log.debug("load - found BPL=" + m_bpl); 205 } 206 else 207 { 208 MBPartner_Location[] bpls = m_bp.getLocations(); 209 if (bpls != null && bpls.length > 0) 210 { 211 m_bpl = bpls[0]; 212 log.debug ("load - found BPL=" + m_bpl); 213 } 214 } 215 if (m_bpl != null) 216 { 217 m_loc = new MLocation (m_ctx, 0); 218 m_loc.load(m_bpl.getC_Location_ID()); 219 log.debug ("load - found LOC=" + m_loc); 220 } 221 else 222 m_loc = null; 223 } 224 else 225 { 226 m_bpl = null; 227 m_loc = null; 228 } 229 230 if (m_bpc == null) 232 { 233 m_bpc = new MUser (m_ctx, 0, 0); 234 m_bpc.setEmail(email); 235 m_bpc.setPassword(password); 236 } 237 if (m_bp == null) 238 { 239 m_bp = new MBPartner (m_ctx); m_bp.setIsCustomer(true); 241 } 242 if (m_bpl == null) 243 m_bpl = new MBPartner_Location (m_ctx, 0, 0); 244 if (m_loc == null) 245 { 246 m_loc = new MLocation (m_ctx, 0); 247 m_loc.load(0); 248 } 249 log.info("load complete - " + m_bp + " - " + m_bpc); 251 } 253 257 private void load (int AD_User_ID) 258 { 259 log.info("load ID=" + AD_User_ID + ", AD_Client_ID=" + m_AD_Client_ID); 260 String sql = "SELECT * " 261 + "FROM AD_User " 262 + "WHERE AD_Client_ID=?" 263 + " AND AD_User_ID=?"; 264 265 PreparedStatement pstmt = null; 266 try 267 { 268 pstmt = DB.prepareStatement(sql); 269 pstmt.setInt(1, m_AD_Client_ID); 270 pstmt.setInt(2, AD_User_ID); 271 ResultSet rs = pstmt.executeQuery(); 272 if (rs.next()) 273 { 274 m_bpc = new MUser (m_ctx, rs); 275 log.debug("load = found BPC=" + m_bpc); 276 } 277 rs.close(); 278 pstmt.close(); 279 pstmt = null; 280 } 281 catch (Exception e) 282 { 283 log.error("load", e); 284 } 285 finally 286 { 287 try 288 { 289 if (pstmt != null) 290 pstmt.close (); 291 } 292 catch (Exception e) 293 {} 294 pstmt = null; 295 } 296 297 m_passwordOK = false; 299 m_loggedIn = false; 300 301 if (m_bpc != null) 303 { 304 m_bp = new MBPartner (m_ctx, m_bpc.getC_BPartner_ID ()); 305 log.debug("load = found BP=" + m_bp); 306 } 307 else 308 m_bp = null; 309 if (m_bpc != null) 311 { 312 if (m_bpc.getC_BPartner_Location_ID() != 0) 313 { 314 m_bpl = new MBPartner_Location (m_ctx, m_bpc.getC_BPartner_Location_ID ()); 315 log.debug("load = found BPL=" + m_bpl); 316 } 317 else 318 { 319 MBPartner_Location[] bpls = m_bp.getLocations(); 320 if (bpls != null && bpls.length > 0) 321 { 322 m_bpl = bpls[0]; 323 log.debug ("load = found BPL=" + m_bpl); 324 } 325 } 326 if (m_bpl != null) 327 { 328 m_loc = new MLocation (m_ctx, 0); 329 m_loc.load(m_bpl.getC_Location_ID()); 330 log.debug ("load = found LOC=" + m_loc); 331 } 332 else 333 m_loc = null; 334 } 335 else 336 { 337 m_bpl = null; 338 m_loc = null; 339 } 340 341 if (m_bpc == null) 343 { 344 m_bpc = new MUser (m_ctx, 0, 0); 345 m_bpc.setEmail("?"); 346 m_bpc.setPassword("?"); 347 } 348 if (m_bp == null) 349 { 350 m_bp = new MBPartner (m_ctx); m_bp.setIsCustomer(true); 352 } 353 if (m_bpl == null) 354 m_bpl = new MBPartner_Location (m_ctx, 0, 0); 355 if (m_loc == null) 356 { 357 m_loc = new MLocation (m_ctx, 0); 358 m_loc.load(0); 359 } 360 log.info("load = " + m_bp + " - " + m_bpc); 362 } 364 368 public boolean isValid() 369 { 370 if (m_bpc == null) 371 return false; 372 boolean ok = m_bpc.getAD_User_ID() != 0; 373 return ok; 374 } 376 380 public boolean isEMailValid() 381 { 382 if (m_bpc == null || !WUtil.exists(getEmail())) 383 { 384 log.debug("isEMailValid - " + getEmail() + "bpc=" + m_bpc); 385 return false; 386 } 387 boolean ok = m_bpc.getAD_User_ID() != 0 389 && m_bpc.isOnline() 390 && m_bpc.isEMailValid(); 391 if (!ok) 392 log.debug("isEMailValid - " + getEmail() 393 + ", ID=" + m_bpc.getAD_User_ID() 394 + ", Online=" + m_bpc.isOnline() 395 + ", EMailValid=" + m_bpc.isEMailValid()); 396 return ok; 397 } 399 403 public boolean setEMailValid() 404 { 405 if (isEMailValid()) 406 return true; 407 m_passwordMessage = "EMail invalid"; 408 return false; 409 } 411 415 public String toString () 416 { 417 StringBuffer sb = new StringBuffer ("WebUser["); 418 sb.append(getEmail()) 419 .append(",LoggedIn=").append(m_loggedIn) 420 .append(",").append(m_bpc) 421 .append(",PasswordOK=").append(m_passwordOK) 422 .append(",Valid=").append(isValid()) 423 .append(" - ").append(m_bp).append("Customer=").append(isCustomer()) 424 .append("]"); 425 return sb.toString(); 426 } 428 429 430 434 public boolean save() 435 { 436 m_saveErrorMessage = null; 437 log.info("save - BP.Value=" + m_bp.getValue() + ", Name=" + m_bp.getName()); 438 try 439 { 440 if (m_bp.getC_BPartner_ID() == 0) 442 { 443 String sql = "SELECT * FROM C_BPartner WHERE AD_Client_ID=? AND Value=?"; 444 PreparedStatement pstmt = null; 445 try 446 { 447 pstmt = DB.prepareStatement(sql); 448 pstmt.setInt (1, m_AD_Client_ID); 449 pstmt.setString (2, m_bp.getValue()); 450 ResultSet rs = pstmt.executeQuery(); 451 if (rs.next()) 452 { 453 m_bp = new MBPartner (m_ctx, m_bpc.getC_BPartner_ID ()); 454 log.debug ("save - BP loaded =" + m_bp); 455 } 456 rs.close(); 457 pstmt.close(); 458 pstmt = null; 459 } 460 catch (Exception e) 461 { 462 log.error("save-check", e); 463 } 464 finally 465 { 466 try 467 { 468 if (pstmt != null) 469 pstmt.close (); 470 } 471 catch (Exception e) 472 {} 473 pstmt = null; 474 } 475 } 476 477 if (m_bp.getName () == null || m_bp.getName().length() == 0) 479 m_bp.setName (m_bpc.getName()); 480 if (m_bp.getValue() == null || m_bp.getValue().length() == 0) 481 m_bp.setValue(m_bpc.getEmail()); 482 log.debug ("save - BP=" + m_bp); 483 if (!m_bp.save ()) 484 { 485 m_saveErrorMessage = "Could not save Business Partner"; 486 return false; 487 } 488 489 log.debug ("save - LOC=" + m_loc); 491 m_loc.save (); 492 493 if (m_bpl.getC_BPartner_ID () != m_bp.getC_BPartner_ID()) 495 m_bpl.setC_BPartner_ID (m_bp.getC_BPartner_ID()); 496 if (m_bpl.getC_Location_ID () != m_loc.getC_Location_ID()) 497 m_bpl.setC_Location_ID (m_loc.getC_Location_ID()); 498 log.debug ("save - BPL=" + m_bpl); 499 if (!m_bpl.save ()) 500 { 501 m_saveErrorMessage = "Could not save Location"; 502 return false; 503 } 504 505 if (m_bpc.getC_BPartner_ID () != m_bp.getC_BPartner_ID()) 507 m_bpc.setC_BPartner_ID (m_bp.getC_BPartner_ID()); 508 if (m_bpc.getC_BPartner_Location_ID () != m_bpl.getC_BPartner_Location_ID ()) 509 m_bpc.setC_BPartner_Location_ID (m_bpl.getC_BPartner_Location_ID ()); 510 log.debug ("save - BPC=" + m_bpc); 511 if (!m_bpc.save ()) 512 { 513 m_saveErrorMessage = "Could not save Contact"; 514 return false; 515 } 516 } 517 catch (Exception ex) 518 { 519 log.error("save", ex); 520 m_saveErrorMessage = ex.toString(); 521 return false; 522 } 523 return true; 525 } 527 public void setSaveErrorMessage(String msg) 528 { 529 m_saveErrorMessage = msg; 530 } 531 public String getSaveErrorMessage() 532 { 533 return m_saveErrorMessage; 534 } 535 536 537 538 542 public String getEmail () 543 { 544 return m_bpc.getEmail(); 545 } 546 547 public void setEmail (String email) 548 { 549 m_bpc.setEmail(email); 550 } 551 552 public String getName () 553 { 554 return m_bpc.getName(); 555 } 556 557 public void setName (String name) 558 { 559 m_bpc.setName(name); 560 } 561 562 public String getTitle () 563 { 564 return m_bpc.getTitle(); 565 } 566 567 public void setTitle (String title) 568 { 569 m_bpc.setTitle(title); 570 } 571 572 576 public String getPassword () 577 { 578 String pwd = m_bpc.getPassword(); 579 if (pwd == null || pwd.length() == 0) pwd = String.valueOf(System.currentTimeMillis()); 581 return pwd; 582 } 584 587 public void setPassword () 588 { 589 String pwd = m_bpc.getPassword(); 590 if ((pwd == null || pwd.length() == 0) && m_bpc.getC_BPartner_ID() != 0 && m_bpc.getAD_User_ID() != 0 ) { 593 pwd = String.valueOf (System.currentTimeMillis ()); 594 m_bpc.setPassword (pwd); 595 m_bpc.save(); 596 } 597 } 599 603 public void setPassword (String password) 604 { 605 if (password == null || password.length() == 0) 606 m_passwordMessage = "Enter Password"; 607 m_bpc.setPassword (password); 608 } 610 615 private void setPasswordOK (boolean ok, String password) 616 { 617 m_passwordOK = ok; 618 if (ok) 619 m_passwordMessage = null; 620 else if (password == null || password.length() == 0) 621 m_passwordMessage = "Enter Password"; 622 else 623 m_passwordMessage = "Invalid Password"; 624 } 626 630 public boolean isPasswordOK() 631 { 632 if (m_bpc == null || !WUtil.exists(m_bpc.getPassword())) 633 return false; 634 return m_passwordOK; 635 } 637 641 public String getPasswordMessage () 642 { 643 return m_passwordMessage; 644 } 646 650 protected void setPasswordMessage (String passwordMessage) 651 { 652 m_passwordMessage = passwordMessage; 653 } 655 660 public boolean login (String password) 661 { 662 m_loggedIn = isValid () && WUtil.exists (password) && password.equals (getPassword ()); 665 setPasswordOK (m_loggedIn, password); 666 log.debug("login - success=" + m_loggedIn); 667 return m_loggedIn; 668 } 670 673 public void logout () 674 { 675 m_loggedIn = false; 676 } 678 682 public boolean isLoggedIn () 683 { 684 return m_loggedIn; 685 } 687 public String getPhone () 688 { 689 return m_bpc.getPhone(); 690 } 691 692 public void setPhone (String phone) 693 { 694 m_bpc.setPhone(phone); 695 } 696 697 public String getPhone2 () 698 { 699 return m_bpc.getPhone2(); 700 } 701 702 public void setPhone2 (String phone2) 703 { 704 m_bpc.setPhone2(phone2); 705 } 706 707 public String getFax () 708 { 709 return m_bpc.getFax(); 710 } 711 712 public void setFax (String fax) 713 { 714 m_bpc.setFax(fax); 715 } 716 717 public Timestamp getBirthday () 718 { 719 return m_bpc.getBirthday(); 720 } 721 722 public void setBirthday (Timestamp birthday) 723 { 724 m_bpc.setBirthday(birthday); 725 } 726 727 public String getTaxID () 728 { 729 return m_bp.getTaxID(); 730 } 731 732 public void setTaxID (String taxID) 733 { 734 m_bp.setTaxID(taxID); 735 } 736 737 738 public int getAD_User_ID () 739 { 740 return m_bpc.getAD_User_ID(); 741 } 742 743 public int getContactID () 744 { 745 return getAD_User_ID(); 746 } 747 748 749 750 754 public String getCompany() 755 { 756 return m_bp.getName(); 757 } 758 759 public void setCompany(String company) 760 { 761 m_bp.setName(company); 762 } 763 764 public int getC_BPartner_ID () 765 { 766 return m_bp.getC_BPartner_ID(); 767 } 768 769 public int getM_PriceList_ID () 770 { 771 return m_bp.getM_PriceList_ID(); 772 } 773 774 775 776 777 781 public int getC_BPartner_Location_ID () 782 { 783 return m_bpl.getC_BPartner_Location_ID(); 784 } 785 786 787 788 792 public String getAddress () 793 { 794 return m_loc.getAddress1(); 795 } 796 797 public void setAddress (String address) 798 { 799 m_loc.setAddress1(address); 800 } 801 802 public String getAddress2 () 803 { 804 return m_loc.getAddress2(); 805 } 806 807 public void setAddress2 (String address2) 808 { 809 m_loc.setAddress2(address2); 810 } 811 812 public String getCity () 813 { 814 return m_loc.getCity(); 815 } 816 817 public void setCity (String city) 818 { 819 m_loc.setCity(city); 820 } 821 822 public String getPostal () 823 { 824 return m_loc.getPostal(); 825 } 826 827 public void setPostal (String postal) 828 { 829 m_loc.setPostal(postal); 830 } 831 832 833 834 838 public String getRegionName () 839 { 840 return m_loc.getRegionName(); 841 } 842 843 public void setRegionName (String region) 844 { 845 m_loc.setRegionName(region); 846 } 847 848 public int getC_Region_ID () 849 { 850 return m_loc.getC_Region_ID(); 851 } 852 853 public String getRegionID () 854 { 855 return String.valueOf(getC_Region_ID()); 856 } 857 858 public void setC_Region_ID (int C_Region_ID) 859 { 860 m_loc.setC_Region_ID(C_Region_ID); 861 } 862 863 public void setC_Region_ID (String C_Region_ID) 864 { 865 try 866 { 867 if (C_Region_ID == null || C_Region_ID.length() == 0) 868 setC_Region_ID(0); 869 else 870 setC_Region_ID(Integer.parseInt(C_Region_ID)); 871 } 872 catch (Exception e) 873 { 874 setC_Region_ID(0); 875 log.warn("setC_Region_ID", e); 876 } 877 } 878 879 public String getCountryName () 880 { 881 return m_loc.getCountryName(); 882 } 883 884 public void setCountryName (String country) 885 { 886 log.warn("setCountryName=" + country + " Ignored - C_Country_ID=" + m_loc.getC_Country_ID()); 887 } 889 890 public int getC_Country_ID () 891 { 892 return m_loc.getC_Country_ID(); 893 } 894 895 public String getCountryID () 896 { 897 return String.valueOf(getC_Country_ID()); 898 } 899 900 public void setC_Country_ID (int C_Country_ID) 901 { 902 m_loc.setC_Country_ID(C_Country_ID); 903 } 904 905 public void setC_Country_ID (String C_Country_ID) 906 { 907 try 908 { 909 if (C_Country_ID == null || C_Country_ID.length() == 0) 910 setC_Country_ID(0); 911 else 912 setC_Country_ID(Integer.parseInt(C_Country_ID)); 913 } 914 catch (Exception e) 915 { 916 setC_Country_ID(0); 917 log.warn("setC_Country_ID", e); 918 } 919 } 920 921 public boolean isEmployee() 922 { 923 return m_bp.isEmployee(); 924 } 925 926 public boolean isSalesRep() 927 { 928 return m_bp.isSalesRep(); 929 } 930 931 public boolean isCustomer() 932 { 933 return m_bp.isCustomer(); 934 } 935 936 public void setIsCustomer(boolean isCustomer) 937 { 938 m_bp.setIsCustomer(isCustomer); 939 } 940 941 public boolean isVendor() 942 { 943 return m_bp.isVendor(); 944 } 945 946 public int getSalesRep_ID() 947 { 948 return m_bp.getSalesRep_ID(); 949 } 950 951 952 953 954 958 public MBP_BankAccount getBankAccount() 959 { 960 MBP_BankAccount retValue = null; 961 MBP_BankAccount[] bas = m_bp.getBankAccounts(); 962 if (bas == null || bas.length == 0) 964 { 965 retValue = new MBP_BankAccount (m_ctx, m_bp, m_bpc, m_loc); 966 retValue.save(); 967 } 968 else 969 retValue = bas[0]; return retValue; 971 } 973 } | Popular Tags |