Changeset 1545 for contrib/psync/src/main/java/br/com
- Timestamp:
- 10/28/09 20:58:21 (15 years ago)
- Location:
- contrib/psync/src/main/java/br/com/prognus/psync
- Files:
-
- 2 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
contrib/psync/src/main/java/br/com/prognus/psync/engine/source/PIMCalendarSyncSource.java
r1103 r1545 77 77 this.manager = new PIMCalendarManager(JNDI_DATA_SOURCE_NAME, context.getPrincipal(), context.getSourceQuery()); 78 78 } catch (Exception e) { 79 // TODO Auto-generated catch block80 79 e.printStackTrace(); 81 80 } … … 453 452 private String hackFix(String text) { 454 453 455 String result, head, title, clas, description, tail, new_title; 456 int p_categories, p_classes, p_location, p_dstart, values1, values2, op1, op2, op3, op4, op5, op6, op7, op8, qt1, qt2; 457 458 qt1 = 0; 459 qt2 = 0; 460 461 // Pega as posicoes das tags 462 p_categories = text.indexOf("CATEGORIES"); 463 p_classes = text.indexOf("CLASS"); 464 465 p_location = text.indexOf("LOCATION"); 466 p_dstart = text.indexOf("DTSTART"); 467 468 // Pega as posicoes das varias formas da tag SUMMARY 469 op1 = text.indexOf("SUMMARY:"); 470 op2 = text.indexOf("SUMMARY;CHARSET=UTF-8:"); 471 op3 = text.indexOf("SUMMARY;ENCODING=QUOTED-PRINTABLE:"); 472 op4 = text.indexOf("SUMMARY;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:"); 473 474 // Pega as posicoes das varias formas da tag DESCRIPTION 475 op5 = text.indexOf("DESCRIPTION:"); 476 op6 = text.indexOf("DESCRIPTION;CHARSET=UTF-8:"); 477 op7 = text.indexOf("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:"); 478 op8 = text.indexOf("DESCRIPTION;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:"); 479 480 qt1 += (op1 == -1) ? 0 : op1 + 8; 481 qt1 += (op2 == -1) ? 0 : op2 + 22; 482 qt1 += (op3 == -1) ? 0 : op3 + 34; 483 qt1 += (op4 == -1) ? 0 : op4 + 48; 484 485 qt2 += (op5 == -1) ? 0 : op5 + 12; 486 qt2 += (op6 == -1) ? 0 : op6 + 26; 487 qt2 += (op7 == -1) ? 0 : op7 + 38; 488 qt2 += (op8 == -1) ? 0 : op8 + 52; 489 490 values1 = (p_categories == -1) ? p_classes : p_categories; 491 values2 = (p_location == -1) ? p_dstart : p_location; 492 493 head = text.substring(0, qt1); 494 title = text.substring(qt1, values1); 495 clas = text.substring(values1, qt2); 496 description = text.substring(qt2, values2); 497 tail = text.substring(values2); 498 499 new_title = title.replaceAll("\r\n ", " "); 500 501 // Retira os enters no final do titulo 502 int title_t, title_bl; 503 504 while(true){ 505 title_t = new_title.length(); 506 title_bl = new_title.lastIndexOf("=0D=0A=\r\n"); 507 508 if(title_bl != -1 && title_t == title_bl + 11){ 509 new_title = new_title.substring(0, title_bl) + "\r\n"; 510 } else { 511 break; 512 } 513 } 514 515 // Retira os enters no final da descricao 516 int description_t, description_bl; 517 518 while(true){ 519 description_t = description.length(); 520 description_bl = description.lastIndexOf("=0D=0A=\r\n"); 521 522 if(description_bl != -1 && description_t == description_bl + 11){ 523 description = description.substring(0, description_bl) + "\r\n"; 524 } else { 525 break; 526 } 527 } 528 529 result = head + new_title + clas + description + tail; 530 531 return result; 454 try { 455 456 String result, head, title, clas, description, tail, new_title; 457 int p_categories, p_classes, p_location, p_dstart, values1, values2, op1, op2, op3, op4, op5, op6, op7, op8, qt1, qt2; 458 459 qt1 = 0; 460 qt2 = 0; 461 462 // Pega as posicoes das tags 463 p_categories = text.indexOf("CATEGORIES"); 464 p_classes = text.indexOf("CLASS"); 465 466 p_location = text.indexOf("LOCATION"); 467 p_dstart = text.indexOf("DTSTART"); 468 469 // Pega as posicoes das varias formas da tag SUMMARY 470 op1 = text.indexOf("SUMMARY:"); 471 op2 = text.indexOf("SUMMARY;CHARSET=UTF-8:"); 472 op3 = text.indexOf("SUMMARY;ENCODING=QUOTED-PRINTABLE:"); 473 op4 = text.indexOf("SUMMARY;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:"); 474 475 // Pega as posicoes das varias formas da tag DESCRIPTION 476 op5 = text.indexOf("DESCRIPTION:"); 477 op6 = text.indexOf("DESCRIPTION;CHARSET=UTF-8:"); 478 op7 = text.indexOf("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:"); 479 op8 = text.indexOf("DESCRIPTION;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:"); 480 481 qt1 += (op1 == -1) ? 0 : op1 + 8; 482 qt1 += (op2 == -1) ? 0 : op2 + 22; 483 qt1 += (op3 == -1) ? 0 : op3 + 34; 484 qt1 += (op4 == -1) ? 0 : op4 + 48; 485 486 qt2 += (op5 == -1) ? 0 : op5 + 12; 487 qt2 += (op6 == -1) ? 0 : op6 + 26; 488 qt2 += (op7 == -1) ? 0 : op7 + 38; 489 qt2 += (op8 == -1) ? 0 : op8 + 52; 490 491 values1 = (p_categories == -1) ? p_classes : p_categories; 492 values2 = (p_location == -1) ? p_dstart : p_location; 493 494 head = text.substring(0, qt1); 495 title = text.substring(qt1, values1); 496 clas = text.substring(values1, qt2); 497 description = text.substring(qt2, values2); 498 tail = text.substring(values2); 499 500 new_title = title.replaceAll("\r\n ", " "); 501 502 // Retira os enters no final do titulo 503 int title_t, title_bl; 504 505 while(true){ 506 title_t = new_title.length(); 507 title_bl = new_title.lastIndexOf("=0D=0A=\r\n"); 508 509 if(title_bl != -1 && title_t == title_bl + 11){ 510 new_title = new_title.substring(0, title_bl) + "\r\n"; 511 } else { 512 break; 513 } 514 } 515 516 // Retira os enters no final da descricao 517 int description_t, description_bl; 518 519 while(true){ 520 description_t = description.length(); 521 description_bl = description.lastIndexOf("=0D=0A=\r\n"); 522 523 if(description_bl != -1 && description_t == description_bl + 11){ 524 description = description.substring(0, description_bl) + "\r\n"; 525 } else { 526 break; 527 } 528 } 529 530 result = head + new_title + clas + description + tail; 531 532 return result; 533 }catch (Exception e) { 534 if (log.isTraceEnabled()) { 535 log.trace(e); 536 } 537 return text; 538 } 532 539 } 533 540 -
contrib/psync/src/main/java/br/com/prognus/psync/items/dao/PIMContactDAO.java
r1103 r1545 32 32 import br.com.prognus.psync.exception.PIMDBAccessException; 33 33 import br.com.prognus.psync.items.model.ContactWrapper; 34 import br.com.prognus.psync.util.Country; 34 35 import br.com.prognus.psync.util.Def; 35 36 import br.com.prognus.psync.util.TypeAddress; 37 38 import com.funambol.common.pim.common.Property; 39 import com.funambol.common.pim.contact.Address; 36 40 import com.funambol.common.pim.contact.BusinessDetail; 37 41 import com.funambol.common.pim.contact.Contact; 38 42 import com.funambol.common.pim.contact.Email; 39 43 import com.funambol.common.pim.contact.Name; 44 import com.funambol.common.pim.contact.Note; 40 45 import com.funambol.common.pim.contact.PersonalDetail; 41 46 import com.funambol.common.pim.contact.Phone; 47 import com.funambol.common.pim.contact.Photo; 42 48 import com.funambol.framework.security.Sync4jPrincipal; 43 49 import com.funambol.framework.server.store.NotFoundException; … … 55 61 + "WHERE id_owner = ?"; 56 62 57 private static final String SQL_GET_CONTACT_BY_ID_USER = "SELECT id_contact, id_owner, last_update, last_status, given_names, family_names, birthdate, category FROM phpgw_cc_contact WHERE id_contact = ? AND id_owner = ? LIMIT 1";63 private static final String SQL_GET_CONTACT_BY_ID_USER = "SELECT id_contact, id_owner, last_update, last_status, given_names, family_names, birthdate, category, photo, notes, alias FROM phpgw_cc_contact WHERE id_contact = ? AND id_owner = ? LIMIT 1"; 58 64 59 65 private static final String SQL_GET_CONTACT_ITEM_BY_ID = "SELECT connection_name, connection_value, id_typeof_contact_connection FROM phpgw_cc_connections AS CO LEFT JOIN phpgw_cc_contact_conns AS CC ON CO.id_connection = CC.id_connection WHERE id_contact = ?"; … … 73 79 74 80 private static final String SQL_INSERT_INTO_FNBL_PIM_CONTACT = "INSERT INTO phpgw_cc_contact " 75 + "(id_contact, id_owner, id_status, given_names, family_names, names_ordered, birthdate, category, last_update, last_status ) "76 + "VALUES " + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) ";81 + "(id_contact, id_owner, id_status, given_names, family_names, names_ordered, birthdate, category, photo, notes, alias, last_update, last_status ) " 82 + "VALUES " + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "; 77 83 78 84 private static final String SQL_INSERT_INTO_CONTACT_ITEM = "INSERT INTO phpgw_cc_connections " … … 87 93 88 94 private static final String SQL_UPDATE_FNBL_PIM_CONTACT_END = " WHERE id_contact = ? AND id_owner = ? "; 89 95 96 97 private static final String SQL_GET_CONTACT_ADDRESS_ITEM = " select ad.complement, ad.address1, ad.postal_code, ca.id_typeof_contact_address, city_name, state_name, cy.id_country from phpgw_cc_addresses AS ad JOIN phpgw_cc_contact_addrs AS ca ON ad.id_address=ca.id_address, phpgw_cc_city AS cy JOIN phpgw_cc_state AS st ON cy.id_state=st.id_state where cy.id_city=ad.id_city and ad.id_address in (select min(ca.id_address) from phpgw_cc_contact AS cc JOIN phpgw_cc_contact_addrs AS ca ON cc.id_contact=ca.id_contact JOIN phpgw_cc_addresses AS ad ON ca.id_address=ad.id_address where cc.id_contact = ? AND cc.id_owner = ? and ca.id_typeof_contact_address = ? )"; 98 99 private static final String SQL_GET_STATE_ITEM = "SELECT id_state, id_country, state_name, state_symbol FROM phpgw_cc_state "; 100 101 private static final String SQL_GET_CITY_ITEM = "SELECT id_city, id_state, id_country, city_timezone, city_geo_location, city_name FROM phpgw_cc_city "; 102 103 private static final String SQL_INSERT_STATE = "INSERT INTO phpgw_cc_state(id_state, id_country, state_name, state_symbol) VALUES (?, ?, ?, ?)"; 104 105 private static final String SQL_INSERT_CITY = "INSERT INTO phpgw_cc_city(id_city, id_state, id_country, city_timezone, city_geo_location, city_name) VALUES (?, ?, ?, ?, ?, ?)"; 106 107 private static final String SQL_INSERT_ADDRESS = "INSERT INTO phpgw_cc_addresses(id_address, id_city, id_state, id_country, address1, complement, postal_code, address_is_default) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; 108 109 private static final String SQL_INSERT_CONTACT_ADDRESS = "INSERT INTO phpgw_cc_contact_addrs(id_contact, id_address, id_typeof_contact_address) VALUES (?, ?, ?)"; 110 111 private static final int SQL_ADDRESS_DIM = 60; 112 113 private static final String SQL_ID_MAX_ADDRESS = "select max(id_address) FROM phpgw_cc_addresses"; 114 115 private static final String SQL_ID_MAX_STATE = "select max(id_state) FROM phpgw_cc_state"; 116 117 private static final String SQL_ID_MAX_CITY = "select max(id_city) FROM phpgw_cc_city"; 118 90 119 private static final String SQL_EQUALS_QUESTIONMARK = " = ?"; 91 120 … … 111 140 112 141 protected static final String SQL_FIELD_CATEGORY = "category"; 142 143 protected static final String SQL_FIELD_PHOTO = "photo"; 144 145 protected static final String SQL_FIELD_NOTES = "notes"; 113 146 114 147 protected static final int SQL_FIRSTNAME_DIM = 49; … … 121 154 122 155 protected static final int SQL_CATEGORY_DIM = 20; 156 157 protected static final int SQL_NICK_DIM = 30; 123 158 124 159 protected static final String TYPE_EMAIL_1_ADDRESS = "Principal"; … … 159 194 160 195 protected static final String SQL_FIELD_ITEM_TYPE = "id_typeof_contact_connection"; 196 197 protected static final String SQL_FIELD_TYPEOF = "id_typeof_contact_address"; 198 199 protected static final String SQL_FIELD_COMPLEMENT = "complement"; 200 201 protected static final String SQL_FIELD_ADDRESS1 = "address1"; 202 203 protected static final String SQL_FIELD_POSTALCODE = "postal_code"; 204 205 protected static final String SQL_FIELD_CITY_NAME = "city_name"; 206 207 protected static final String SQL_FIELD_STATE_NAME = "state_name"; 208 209 protected static final String SQL_FIELD_COUNTRY_ID = "id_country"; 210 211 protected static final String SQL_FIELD_STATE_ID = "id_state"; 212 213 protected static final String SQL_FIELD_CITY_ID = "id_city"; 161 214 162 215 private Sync4jPrincipal principal = null; … … 193 246 * @see ContactWrapper 194 247 */ 248 @SuppressWarnings("unchecked") 195 249 public void addItem(ContactWrapper cw) throws PIMDBAccessException { 196 250 … … 223 277 StringBuffer fullName = null; 224 278 Date anniversary = null; 279 byte[] imagePhoto = null; 280 String note = null; 281 String alias = null; 225 282 226 283 try { … … 232 289 Timestamp lastUpdate = cw.getLastUpdate(); 233 290 291 try { 292 List<Note> notes = c.getNotes(); 293 int i = notes.size() - 1; 294 note = notes.get(i).getPropertyValueAsString(); 295 } catch (Exception e) { 296 // log.error(e.getMessage(),e); 297 } 298 if(note==null) { 299 note=""; 300 } 301 234 302 personalDetail = c.getPersonalDetail(); 235 303 businessDetail = c.getBusinessDetail(); … … 244 312 birthday = personalDetail.getBirthday(); 245 313 category = stringFrom(c.getCategories()); 314 imagePhoto = personalDetail.getPhotoObject().getImage(); 246 315 } 247 316 … … 254 323 firstName = stringFrom(name.getFirstName()); 255 324 lastName = stringFrom(name.getLastName()); 325 alias = stringFrom(name.getNickname()); 256 326 } 257 327 … … 297 367 ps.setDate(7, anniversary); // birthdate 298 368 ps.setString(8, truncate(category, SQL_CATEGORY_DIM)); // category 299 ps.setLong(9, lastUpdate.getTime()); // last_update 300 ps.setString(10, String.valueOf(Def.PIM_STATE_NEW)); // last_status 369 ps.setBytes(9, imagePhoto); // photo 370 ps.setString(10, note); // note 371 ps.setString(11, truncate(alias, SQL_NICK_DIM)); // alias 372 ps.setLong(12, lastUpdate.getTime()); // last_update 373 ps.setString(13, String.valueOf(Def.PIM_STATE_NEW)); // last_status 301 374 ps.executeUpdate(); 302 375 … … 483 556 String category = null; 484 557 Date anniversary = null; 558 byte[] imagePhoto = null; 559 String note = null; 560 String alias = null; 561 Address persoAddress = null; 562 Address businAddress = null; 485 563 486 564 try { … … 495 573 Contact c = cw.getContact(); 496 574 575 try { 576 List<Note> notes = c.getNotes(); 577 int i = notes.size() - 1; 578 note = notes.get(i).getPropertyValueAsString(); 579 } catch (Exception e) { 580 // log.error(e.getMessage(),e); 581 } 582 if(note==null) { 583 note=""; 584 } 585 497 586 personalDetail = c.getPersonalDetail(); 498 587 businessDetail = c.getBusinessDetail(); … … 505 594 birthday = personalDetail.getBirthday(); 506 595 category = stringFrom(c.getCategories()); 596 imagePhoto = personalDetail.getPhotoObject().getImage(); 597 persoAddress = personalDetail.getAddress(); 507 598 } 508 599 … … 510 601 emails.addAll(businessDetail.getEmails()); 511 602 phones.addAll(businessDetail.getPhones()); 603 businAddress = businessDetail.getAddress(); 512 604 } 513 605 … … 515 607 firstName = stringFrom(name.getFirstName()); 516 608 lastName = stringFrom(name.getLastName()); 609 alias = stringFrom(name.getNickname()); 517 610 } 518 611 … … 525 618 if (category != null && category.length() > SQL_CATEGORY_DIM) { 526 619 category = category.substring(0, SQL_CATEGORY_DIM); 620 } 621 if (alias != null && alias.length() > SQL_NICK_DIM) { 622 alias = alias.substring(0, SQL_NICK_DIM); 527 623 } 528 624 … … 563 659 queryUpdateFunPimContact.append(SQL_FIELD_CATEGORY 564 660 + SQL_EQUALS_QUESTIONMARK_COMMA); 661 queryUpdateFunPimContact.append(SQL_FIELD_PHOTO 662 + SQL_EQUALS_QUESTIONMARK_COMMA); 663 queryUpdateFunPimContact.append(SQL_FIELD_NOTES 664 + SQL_EQUALS_QUESTIONMARK_COMMA); 665 queryUpdateFunPimContact.append(SQL_FIELD_NICK 666 + SQL_EQUALS_QUESTIONMARK_COMMA); 565 667 queryUpdateFunPimContact 566 668 .append(SQL_FIELD_STATUS + SQL_EQUALS_QUESTIONMARK … … 575 677 ps.setDate(5, anniversary); // birthdate 576 678 ps.setString(6, category); // category 577 ps.setString(7, String.valueOf(Def.PIM_STATE_UPDATED)); // last_status 578 ps.setLong(8, id_contact); // id_contact 579 ps.setLong(9, Long.parseLong(userId)); // id_owner 679 ps.setBytes(7, imagePhoto); // photo 680 ps.setString(8, note); // notes 681 ps.setString(9, alias); // alias 682 ps.setString(10, String.valueOf(Def.PIM_STATE_UPDATED)); // last_status 683 ps.setLong(11, id_contact); // id_contact 684 ps.setLong(12, Long.parseLong(userId)); // id_owner 580 685 ps.executeUpdate(); 581 686 … … 730 835 } 731 836 837 838 // endereco 839 boolean syncAddress = true; 840 if(syncAddress) { 841 // Apaga os contatos na tabela phpgw_cc_addresses 842 ps = con 843 .prepareStatement("DELETE FROM phpgw_cc_addresses WHERE id_address IN (SELECT id_address FROM phpgw_cc_contact_addrs WHERE id_contact = ?)"); 844 ps.setLong(1, id_contact); 845 ps.executeUpdate(); 846 847 // Apaga os contatos na tabela phpgw_cc_contact_addrs 848 ps = con 849 .prepareStatement("DELETE FROM phpgw_cc_contact_addrs WHERE id_contact = ?"); 850 ps.setLong(1, id_contact); 851 ps.executeUpdate(); 852 853 // Para cadas tipo de endereco RESIDENCIAL | COMERCIAL 854 for (TypeAddress typeAddress: TypeAddress.values()) { 855 856 Address address = null; 857 boolean defaultAddress = false; 858 859 // se ambos sao nulos não faz nada aqui 860 if (persoAddress == null && businAddress == null) { 861 break; 862 } 863 864 if (typeAddress.equals(TypeAddress.RESIDENCIAL)) { 865 address=persoAddress; 866 defaultAddress = true; 867 } else if (typeAddress.equals(TypeAddress.COMERCIAL)) { 868 address=businAddress; 869 } 870 871 // Pula para o proximo 872 if(address == null) { 873 continue; 874 } 875 876 String addressValue = address.getStreet().getPropertyValueAsString(); 877 addressValue = truncate(addressValue, SQL_ADDRESS_DIM); 878 879 if (addressValue != null && addressValue.length() != 0) { 880 881 // Verifica o id maximo da tabela phpgw_cc_addresses 882 ps = con.prepareStatement(SQL_ID_MAX_ADDRESS); 883 rs = ps.executeQuery(); 884 long id_address = 0; 885 886 if (rs.next()) { 887 long id = rs.getInt(1); 888 id_address = (id == 0) ? 1 : ++id; 889 } 890 891 // Insere o endereco do contato na tabela 892 String idCountry; 893 try { 894 idCountry = Country.getCountryByName(address 895 .getCountry().getPropertyValueAsString()).getCode(); 896 } catch (Exception e) { 897 idCountry = "BR"; 898 } 899 long idState = searchStateId(con, address.getState().getPropertyValueAsString(), idCountry); 900 long idCity = searchCityId(con, address.getCity().getPropertyValueAsString(), idState, idCountry); 901 902 ps = con.prepareStatement(SQL_INSERT_ADDRESS); 903 ps.setLong(1, id_address); 904 ps.setLong(2, idCity); 905 ps.setLong(3, idState); 906 ps.setString(4, idCountry); 907 ps.setString(5, addressValue); 908 ps.setString(6, address.getExtendedAddress().getPropertyValueAsString()); 909 ps.setString(7, address.getPostalCode().getPropertyValueAsString()); 910 ps.setBoolean(8, defaultAddress); 911 ps.executeUpdate(); 912 913 ps = con.prepareStatement(SQL_INSERT_CONTACT_ADDRESS); 914 ps.setLong(1, id_contact); 915 ps.setLong(2, id_address); 916 ps.setInt(3, typeAddress.getId()); 917 ps.executeUpdate(); 918 } 919 } 920 } 732 921 con.commit(); 733 922 con.setAutoCommit(true); … … 743 932 744 933 return Long.toString(id_contact); 934 } 935 936 937 private long searchStateId(Connection con, String stateName, String idCountry) throws Exception { 938 939 PreparedStatement ps = null; 940 ResultSet rs = null; 941 long stateId = 0; 942 943 try { 944 String SQL_GET_STATE = SQL_GET_STATE_ITEM + " where upper(" 945 + SQL_FIELD_STATE_NAME + ")" + SQL_EQUALS_QUESTIONMARK 946 + " and " + SQL_FIELD_COUNTRY_ID + SQL_EQUALS_QUESTIONMARK; 947 ps = con.prepareStatement(SQL_GET_STATE); 948 ps.setString(1, stateName.toUpperCase()); 949 ps.setString(2, idCountry); 950 rs = ps.executeQuery(); 951 if (rs.next()) { 952 stateId = rs.getInt(SQL_FIELD_STATE_ID); 953 return stateId; 954 } 955 } catch (Exception e) { 956 e.printStackTrace(); 957 } 958 959 ps = con.prepareStatement(SQL_ID_MAX_STATE); 960 rs = ps.executeQuery(); 961 962 if (rs.next()) { 963 long id = rs.getInt(1); 964 stateId = (id == 0) ? 1 : ++id; 965 } 966 else { 967 return 0; 968 } 969 970 ps = con.prepareStatement(SQL_INSERT_STATE); 971 ps.setLong(1, stateId); 972 ps.setString(2, idCountry); 973 ps.setString(3, stateName); 974 ps.setString(4, stateName.substring(0, 2)); 975 if(ps.executeUpdate() > 0) { 976 return stateId; 977 } 978 979 return 0; 980 } 981 982 private long searchCityId(Connection con, String cityName, long idState, String idCountry) throws Exception { 983 984 PreparedStatement ps = null; 985 ResultSet rs = null; 986 long cityId = 0; 987 988 try { 989 String SQL_GET_CITY = SQL_GET_CITY_ITEM + " where " 990 + SQL_FIELD_STATE_ID + SQL_EQUALS_QUESTIONMARK + " and " 991 + SQL_FIELD_COUNTRY_ID + SQL_EQUALS_QUESTIONMARK 992 + " and upper(" + SQL_FIELD_CITY_NAME + ")" 993 + SQL_EQUALS_QUESTIONMARK; 994 ps = con.prepareStatement(SQL_GET_CITY); 995 ps.setLong(1, idState); 996 ps.setString(2, idCountry); 997 ps.setString(3, cityName.toUpperCase()); 998 rs = ps.executeQuery(); 999 if (rs.next()) { 1000 cityId = rs.getInt(SQL_FIELD_CITY_ID); 1001 return cityId; 1002 } 1003 } catch (Exception e) { 1004 e.printStackTrace(); 1005 } 1006 ps = con.prepareStatement(SQL_ID_MAX_CITY); 1007 rs = ps.executeQuery(); 1008 1009 if (rs.next()) { 1010 long id = rs.getInt(1); 1011 cityId = (id == 0) ? 1 : ++id; 1012 } 1013 else { 1014 return 0; 1015 } 1016 1017 ps = con.prepareStatement(SQL_INSERT_CITY); 1018 ps.setLong(1, cityId); 1019 ps.setLong(2, idState); 1020 ps.setString(3, idCountry); 1021 ps.setNull(4, 0); 1022 ps.setString(5, null); 1023 ps.setString(6, cityName); 1024 if(ps.executeUpdate() > 0) { 1025 return cityId; 1026 } 1027 1028 return 0; 745 1029 } 746 1030 … … 954 1238 .getSQLState()); 955 1239 } 1240 1241 try { 1242 ps = con.prepareStatement(SQL_GET_CONTACT_ADDRESS_ITEM); 1243 ps.setLong(1, Long.parseLong(uid)); 1244 ps.setLong(2, Long.parseLong(userId)); 1245 ps.setInt(3, TypeAddress.RESIDENCIAL.getId()); 1246 rs = ps.executeQuery(); 1247 1248 Address address = c.getContact().getPersonalDetail().getAddress(); 1249 addAddress(address, rs); 1250 } catch (Exception e) { 1251 throw new SQLException("\nError while adding personal address information"); 1252 } 1253 1254 try { 1255 ps = con.prepareStatement(SQL_GET_CONTACT_ADDRESS_ITEM); 1256 ps.setLong(1, Long.parseLong(uid)); 1257 ps.setLong(2, Long.parseLong(userId)); 1258 ps.setInt(3, TypeAddress.COMERCIAL.getId()); 1259 rs = ps.executeQuery(); 1260 1261 Address address = c.getContact().getBusinessDetail().getAddress(); 1262 addAddress(address, rs); 1263 } catch (Exception e) { 1264 throw new SQLException("\nError while adding business address information"); 1265 } 956 1266 957 1267 } catch (Exception e) { … … 963 1273 964 1274 return c; 1275 } 1276 1277 1278 private static void addAddress(Address address, ResultSet rs) throws Exception { 1279 1280 ResultSetMetaData rsmd = rs.getMetaData(); 1281 1282 String column = null; 1283 int columnCount = 0; 1284 1285 while (rs.next()) { 1286 1287 columnCount = rsmd.getColumnCount(); 1288 1289 if (rs.getString(SQL_FIELD_ADDRESS1)==null || (rs.getString(SQL_FIELD_ADDRESS1)!=null && "".equals(rs.getString(SQL_FIELD_ADDRESS1).trim()))) { 1290 break; 1291 } 1292 1293 for (int i = 1; i <= columnCount; ++i) { 1294 1295 column = rsmd.getColumnName(i); 1296 if (SQL_FIELD_TYPEOF.equalsIgnoreCase(column)) { 1297 continue; 1298 } else if (SQL_FIELD_COMPLEMENT.equalsIgnoreCase(column)) { 1299 address.getExtendedAddress().setPropertyValue(rs.getString(i)); 1300 } else if (SQL_FIELD_ADDRESS1.equalsIgnoreCase(column)) { 1301 address.getStreet().setPropertyValue(rs.getString(i)); 1302 } else if (SQL_FIELD_POSTALCODE.equalsIgnoreCase(column)) { 1303 address.getPostalCode().setPropertyValue(rs.getString(i)); 1304 } else if (SQL_FIELD_CITY_NAME.equalsIgnoreCase(column)) { 1305 address.getCity().setPropertyValue(rs.getString(i)); 1306 } else if (SQL_FIELD_STATE_NAME.equalsIgnoreCase(column)) { 1307 address.getState().setPropertyValue(rs.getString(i)); 1308 } else if (SQL_FIELD_COUNTRY_ID.equalsIgnoreCase(column)) { 1309 Country country = Country.getCountryByCode(rs.getString(i)); 1310 address.getCountry().setPropertyValue(country.getName()); 1311 } else { 1312 throw new SQLException("\n=> Unhandled column: " + column); 1313 } 1314 1315 } 1316 } 965 1317 } 966 1318 … … 1169 1521 } else if (SQL_FIELD_CATEGORY.equalsIgnoreCase(column)) { 1170 1522 c.getCategories().setPropertyValue(rs.getString(i)); 1523 } else if (SQL_FIELD_PHOTO.equalsIgnoreCase(column)) { 1524 if(c.getPersonalDetail()==null) { 1525 PersonalDetail pd = new PersonalDetail(); 1526 c.setPersonalDetail(pd); 1527 } 1528 Photo photo = new Photo(); 1529 photo.setImage(rs.getBytes(i)); 1530 c.getPersonalDetail().setPhotoObject(photo); 1531 } else if (SQL_FIELD_NOTES.equalsIgnoreCase(column)) { 1532 1533 try { 1534 if(rs.getString(i) != null) { 1535 if (c.getNotes() == null 1536 || (c.getNotes() != null && c.getNotes().size() != 1) 1537 || (c.getNotes() != null 1538 && c.getNotes().size() == 1 && c.getNotes() 1539 .get(0) == null)) { 1540 List<Note> l = new ArrayList<Note>(1); 1541 l.add(new Note()); 1542 c.setNotes(l); 1543 } 1544 Note n = (Note) c.getNotes().get(0); 1545 n.setPropertyValue(rs.getString(i)); 1546 n.setPropertyType("Body"); // HARD CODE 1547 } 1548 } catch (Exception e) { 1549 log.error(e.getMessage(),e); 1550 } 1551 1552 } else if (SQL_FIELD_NICK.equalsIgnoreCase(column)) { 1553 c.getName().getNickname().setPropertyValue(rs.getString(i)); 1171 1554 } else { 1172 1555 throw new SQLException("\n=> Unhandled column: " + column); -
contrib/psync/src/main/java/br/com/prognus/psync/items/dao/PIMEntityDAO.java
r1103 r1545 23 23 import java.sql.ResultSet; 24 24 import java.sql.Timestamp; 25 import java.util.Enumeration; 26 import java.util.Iterator; 25 import java.util.Hashtable; 27 26 import java.util.List; 28 27 28 import javax.naming.Context; 29 import javax.naming.NamingEnumeration; 29 30 import javax.naming.NamingException; 31 import javax.naming.directory.DirContext; 32 import javax.naming.directory.InitialDirContext; 33 import javax.naming.directory.SearchControls; 34 import javax.naming.directory.SearchResult; 30 35 import javax.sql.DataSource; 31 36 … … 38 43 import com.funambol.framework.tools.DBTools; 39 44 import com.funambol.framework.tools.DataSourceTools; 40 import com.novell.ldap.LDAPAttribute;41 import com.novell.ldap.LDAPAttributeSet;42 import com.novell.ldap.LDAPConnection;43 import com.novell.ldap.LDAPEntry;44 import com.novell.ldap.LDAPException;45 import com.novell.ldap.LDAPSearchResults;46 import com.novell.ldap.util.Base64;47 45 48 46 public abstract class PIMEntityDAO { … … 123 121 // -----------------------------------------------------------Public methods 124 122 123 @SuppressWarnings("unchecked") 125 124 protected String LdapUID(String server, String dc, String user) 126 125 throws Exception { 127 126 128 LDAPConnection ldap = new LDAPConnection(); 129 String searchAttrs[] = { "uidNumber" }; 130 String id_owner = null; 131 132 try { 133 134 ldap.connect(server, 389); 135 LDAPSearchResults searchResults = ldap.search(dc, 136 LDAPConnection.SCOPE_SUB, "(uid=" + user + ")", 137 searchAttrs, false); 138 139 while (searchResults.hasMore()) { 140 141 LDAPEntry nextEntry = null; 142 try { 143 nextEntry = searchResults.next(); 144 } catch (LDAPException e) { 145 if (e.getResultCode() == LDAPException.LDAP_TIMEOUT 146 || e.getResultCode() == LDAPException.CONNECT_ERROR) 147 break; 148 else 149 continue; 150 } 151 LDAPAttributeSet attributeSet = nextEntry.getAttributeSet(); 152 Iterator allAttributes = attributeSet.iterator(); 153 154 while (allAttributes.hasNext()) { 155 156 LDAPAttribute attribute = (LDAPAttribute) allAttributes 157 .next(); 158 String attributeName = attribute.getName(); 159 Enumeration allValues = attribute.getStringValues(); 160 161 if (allValues != null) { 162 163 while (allValues.hasMoreElements()) { 164 165 String Value = (String) allValues.nextElement(); 166 167 if (Base64.isLDIFSafe(Value)) { 168 } else { 169 Value = Base64.encode(Value.getBytes()); 170 } 171 172 if (attributeName.equalsIgnoreCase("uidNumber")) { 173 id_owner = Value; 174 break; 175 } 176 177 } 178 } 179 } 180 } 181 182 ldap.disconnect(); 183 184 if (id_owner == null) { 185 throw new LDAPException(); 186 } 187 188 } catch (LDAPException e) { 189 log.info("Error while connecting and binding to LDAP: " 190 + e.toString()); 191 } 192 193 return id_owner; 127 String uidNumber=null; 128 129 Hashtable env = new Hashtable(); 130 env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 131 env.put(Context.PROVIDER_URL, "ldap://" + server + ":" + 389); 132 133 DirContext ctx = new InitialDirContext(env); 134 // Searching User 135 SearchControls ctls = new SearchControls(); 136 ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); 137 ctls.setCountLimit(1); 138 ctls.setTimeLimit(10000); // Espera no max 10 segundos 139 String filter = "(uid=" + user + ")"; 140 141 NamingEnumeration answer = ctx.search(dc, filter, ctls); 142 if (answer.hasMore()) { 143 SearchResult sr = (SearchResult) answer.next(); 144 try { 145 // Getting User attributes 146 uidNumber = sr.getAttributes().get("uidNumber").get(0).toString(); 147 } catch (Exception e) { 148 e.printStackTrace(); 149 } 150 } 151 ctx.close(); 152 if (uidNumber == null) { 153 log.info("Error while connecting and binding to LDAP"); 154 } 155 156 return uidNumber; 157 158 159 // LDAPConnection ldap = new LDAPConnection(); 160 // String searchAttrs[] = { "uidNumber" }; 161 // String id_owner = null; 162 // 163 // try { 164 // 165 // ldap.connect(server, 389); 166 // LDAPSearchResults searchResults = ldap.search(dc, 167 // LDAPConnection.SCOPE_SUB, "(uid=" + user + ")", 168 // searchAttrs, false); 169 // 170 // while (searchResults.hasMore()) { 171 // 172 // LDAPEntry nextEntry = null; 173 // try { 174 // nextEntry = searchResults.next(); 175 // } catch (LDAPException e) { 176 // if (e.getResultCode() == LDAPException.LDAP_TIMEOUT 177 // || e.getResultCode() == LDAPException.CONNECT_ERROR) 178 // break; 179 // else 180 // continue; 181 // } 182 // LDAPAttributeSet attributeSet = nextEntry.getAttributeSet(); 183 // Iterator allAttributes = attributeSet.iterator(); 184 // 185 // while (allAttributes.hasNext()) { 186 // 187 // LDAPAttribute attribute = (LDAPAttribute) allAttributes 188 // .next(); 189 // String attributeName = attribute.getName(); 190 // Enumeration allValues = attribute.getStringValues(); 191 // 192 // if (allValues != null) { 193 // 194 // while (allValues.hasMoreElements()) { 195 // 196 // String Value = (String) allValues.nextElement(); 197 // 198 // if (Base64.isLDIFSafe(Value)) { 199 // } else { 200 // Value = Base64.encode(Value.getBytes()); 201 // } 202 // 203 // if (attributeName.equalsIgnoreCase("uidNumber")) { 204 // id_owner = Value; 205 // break; 206 // } 207 // 208 // } 209 // } 210 // } 211 // } 212 // 213 // ldap.disconnect(); 214 // 215 // if (id_owner == null) { 216 // throw new LDAPException(); 217 // } 218 // 219 // } catch (LDAPException e) { 220 // log.info("Error while connecting and binding to LDAP: " 221 // + e.toString()); 222 // } 223 // return id_owner; 194 224 } 195 225
Note: See TracChangeset
for help on using the changeset viewer.