-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContact.java
More file actions
1181 lines (981 loc) · 44.5 KB
/
Contact.java
File metadata and controls
1181 lines (981 loc) · 44.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package javaxt.exchange;
//******************************************************************************
//** Exchange Contact
//******************************************************************************
/**
* Used to represent a contact found in the Contacts Folder.
* http://msdn.microsoft.com/en-us/library/aa581315%28v=EXCHG.140%29.aspx
*
******************************************************************************/
public class Contact extends FolderItem {
/*
//This is an ordered list of all the contact properties
private String FileAs
private String FileAsMapping
private String DisplayName
private String GivenName
private String Initials
private String MiddleName
private String Nickname
private String CompleteName
private String CompanyName
private String EmailAddresses
private String PhysicalAddresses
private String PhoneNumbers
private String AssistantName
private String Birthday
private String BusinessHomePage
private String Children
private String Companies
private String ContactSource
private String Department
private String Generation
private String ImAddresses
private String JobTitle
private String Manager
private String Mileage
private String OfficeLocation
private String PostalAddressIndex
private String Profession
private String SpouseName
private String Surname
private String WeddingAnniversary
private String HasPicture
*/
private String firstName;
private String lastName;
private String fullName;
private String company;
private String title;
private java.util.ArrayList<EmailAddress> emailAddresses = new java.util.ArrayList<EmailAddress>();
private java.util.HashMap<String, PhoneNumber> phoneNumbers = new java.util.HashMap<String, PhoneNumber>();
private java.util.HashMap<String, PhysicalAddress> physicalAddresses = new java.util.HashMap<String, PhysicalAddress>();
private javaxt.utils.Date birthday;
//**************************************************************************
//** Constructor
//**************************************************************************
/** This constructor is provided for application developers who wish to
* extend this class.
*/
protected Contact(){}
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of this class using an existing Exchange Contact,
* effectively creating a clone.
*/
public Contact(javaxt.exchange.Contact contact){
//General information
this.id = contact.id;
this.parentFolderID = contact.parentFolderID;
this.categories = contact.categories;
this.hasAttachments = contact.hasAttachments;
this.attachments = contact.attachments;
this.updates = contact.updates;
this.lastModified = contact.lastModified;
this.additionalProperties = contact.additionalProperties;
this.extendedProperties = contact.extendedProperties;
//Contact specific information
this.firstName = contact.firstName;
this.lastName = contact.lastName;
this.fullName = contact.fullName;
this.company = contact.company;
this.title = contact.title;
this.emailAddresses = contact.emailAddresses;
this.phoneNumbers = contact.phoneNumbers;
this.physicalAddresses = contact.physicalAddresses;
this.birthday = contact.birthday;
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of this class
*/
public Contact(String exchangeID, Connection conn, ExtendedFieldURI[] AdditionalProperties) throws ExchangeException{
super(exchangeID, conn, AdditionalProperties);
parseContact();
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of this class
*/
public Contact(String exchangeID, Connection conn) throws ExchangeException{
this(exchangeID, conn, null);
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of this class with a name
*/
public Contact(String firstName, String lastName) {
this.setName(firstName, lastName);
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of Contact using a node from a
* FindItemResponseMessage.
*/
protected Contact(org.w3c.dom.Node contactNode) {
super(contactNode);
parseContact();
}
//**************************************************************************
//** parseContact
//**************************************************************************
/** Used to parse an xml node with contact information.
*/
private void parseContact(){
org.w3c.dom.NodeList outerNodes = this.getChildNodes();
for (int i=0; i<outerNodes.getLength(); i++){
org.w3c.dom.Node outerNode = outerNodes.item(i);
if (outerNode.getNodeType()==1){
String nodeName = outerNode.getNodeName();
if (nodeName.contains(":")) nodeName = nodeName.substring(nodeName.indexOf(":")+1);
if (nodeName.equalsIgnoreCase("CompanyName")){
company = javaxt.xml.DOM.getNodeValue(outerNode);
}
else if(nodeName.equalsIgnoreCase("JobTitle")){
title = javaxt.xml.DOM.getNodeValue(outerNode);
}
else if(nodeName.equalsIgnoreCase("Birthday")){
try{
birthday = new javaxt.utils.Date(javaxt.xml.DOM.getNodeValue(outerNode));
}
catch(java.text.ParseException e){}
}
else if (nodeName.equalsIgnoreCase("CompleteName")){
org.w3c.dom.NodeList childNodes = outerNode.getChildNodes();
for (int j=0; j<childNodes.getLength(); j++){
org.w3c.dom.Node childNode = childNodes.item(j);
if (childNode.getNodeType()==1){
String childNodeName = childNode.getNodeName();
if (childNodeName.contains(":")) childNodeName = childNodeName.substring(childNodeName.indexOf(":")+1);
if (childNodeName.equalsIgnoreCase("FirstName")){
firstName = javaxt.xml.DOM.getNodeValue(childNode);
}
else if (childNodeName.equalsIgnoreCase("LastName")){
lastName = javaxt.xml.DOM.getNodeValue(childNode);
}
else if (childNodeName.equalsIgnoreCase("FullName")){
fullName = javaxt.xml.DOM.getNodeValue(childNode);
}
}
}
}
else if (nodeName.equalsIgnoreCase("EmailAddresses")){
org.w3c.dom.NodeList childNodes = outerNode.getChildNodes();
for (int j=0; j<childNodes.getLength(); j++){
org.w3c.dom.Node childNode = childNodes.item(j);
if (childNode.getNodeType()==1){
String childNodeName = childNode.getNodeName();
if (childNodeName.contains(":")) childNodeName = childNodeName.substring(childNodeName.indexOf(":")+1);
if (childNodeName.equalsIgnoreCase("Entry")){
String email = javaxt.xml.DOM.getNodeValue(childNode);
try{
emailAddresses.add(new EmailAddress(email));
}
catch(ExchangeException e){}
}
}
}
}
else if (nodeName.equalsIgnoreCase("PhoneNumbers")){
org.w3c.dom.NodeList childNodes = outerNode.getChildNodes();
for (int j=0; j<childNodes.getLength(); j++){
try{
PhoneNumber phoneNumber = new PhoneNumber(childNodes.item(j));
phoneNumbers.put(phoneNumber.getType(), phoneNumber);
}
catch(ExchangeException e){}
}
}
else if (nodeName.equalsIgnoreCase("PhysicalAddresses")){
org.w3c.dom.NodeList childNodes = outerNode.getChildNodes();
for (int j=0; j<childNodes.getLength(); j++){
org.w3c.dom.Node childNode = childNodes.item(j);
if (childNode.getNodeType()==1){
PhysicalAddress address = new PhysicalAddress(childNode);
physicalAddresses.put(address.getType(), address);
}
}
}
}
}
}
//**************************************************************************
//** setName
//**************************************************************************
/** Used to set the first and last name for this contact.
*/
protected void setName(String firstName, String lastName) {
firstName = getValue(firstName);
lastName = getValue(lastName);
if (id!=null) {
this.firstName = getFirstName();
if (firstName==null && this.firstName!=null) updates.put("GivenName", null);
if (firstName!=null && !firstName.equals(this.firstName)) updates.put("GivenName", firstName);
this.lastName = getLastName();
if (lastName==null && this.lastName!=null) updates.put("Surname", null);
if (lastName!=null && !lastName.equals(this.lastName)) updates.put("Surname", lastName);
if (updates.containsKey("GivenName") || updates.containsKey("Surname")){
updates.put("FileAsMapping", "LastCommaFirst");
}
}
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName(){
firstName = getValue(firstName);
return firstName;
}
public String getLastName(){
lastName = getValue(lastName);
return lastName;
}
//**************************************************************************
//** setFullName
//**************************************************************************
/** Used to set the "Display Name" attribute for this contact.
*/
public void setFullName(String fullName){
fullName = getValue(fullName);
if (id!=null) {
this.fullName = getFullName();
if (fullName==null && this.fullName!=null) updates.put("DisplayName", null);
if (fullName!=null && !fullName.equals(this.fullName)) updates.put("DisplayName", fullName);
}
this.fullName = fullName;
}
//**************************************************************************
//** getFullName
//**************************************************************************
/** Returns the "FullName" attribute for this contact.
*/
public String getFullName(){
if (fullName==null){
String firstName = this.getFirstName();
if (firstName==null) firstName = "";
String lastName = this.getLastName();
if (lastName==null) lastName="";
fullName = (firstName + " " + lastName).trim();
if (fullName.length()==0) fullName = null;
}
return fullName;
}
//**************************************************************************
//** getPhysicalAddress
//**************************************************************************
/** Returns an array of mailing addresses associated with this contact.
*/
public PhysicalAddress[] getPhysicalAddresses(){
//Only include non-null values in the array
java.util.ArrayList<PhysicalAddress> arr = new java.util.ArrayList<PhysicalAddress>();
java.util.Iterator<String> it = physicalAddresses.keySet().iterator();
while (it.hasNext()){
String key = it.next();
PhysicalAddress val = physicalAddresses.get(key);
if (val!=null) arr.add(val);
}
if (arr.isEmpty()) return null;
else return arr.toArray(new PhysicalAddress[arr.size()]);
}
//**************************************************************************
//** setPhysicalAddresses
//**************************************************************************
/** Used to add phone numbers to a contact.
*/
public void setPhysicalAddresses(PhysicalAddress[] physicalAddresses) {
if (physicalAddresses==null || physicalAddresses.length==0) {
removePhysicalAddresses();
return;
}
//See whether any updates are required
int numMatches = 0;
int total = 0;
for (PhysicalAddress physicalAddress : physicalAddresses){
if (physicalAddress!=null){
if (!physicalAddress.isEmpty()){
total++;
String type = physicalAddress.getType();
if (this.physicalAddresses.containsKey(type)){
if (this.physicalAddresses.get(type).equals(physicalAddress)) numMatches++;
}
}
}
}
int numAddresses = 0;
if (this.getPhysicalAddresses()!=null) numAddresses = this.getPhysicalAddresses().length;
//If the input array equals the current list of physicalAddresses, do nothing...
if (numMatches==total && numMatches==numAddresses){
return;
}
else {
this.physicalAddresses.clear();
for (PhysicalAddress physicalAddress : physicalAddresses){
addPhysicalAddress(physicalAddress);
}
}
}
//**************************************************************************
//** addPhysicalAddress
//**************************************************************************
/** Used to associate a mailing address with this contact.
*/
public void addPhysicalAddress(PhysicalAddress address){
if (address.isEmpty()){
removePhysicalAddress(address);
}
else{
//Check whether this is a new address
boolean update = false;
if (physicalAddresses.containsKey(address.getType())){
java.util.Iterator<String> it = physicalAddresses.keySet().iterator();
while (it.hasNext()){
String key = it.next();
if (key.equals(address.getType())){
PhysicalAddress addr = physicalAddresses.get(key);
if (addr==null || !addr.equals(address)){
update = true;
}
break;
}
}
}
else{
update = true;
}
if (update){
physicalAddresses.put(address.getType(), address);
if (id!=null) updates.put("PhysicalAddresses", getAddressUpdates());
}
}
}
//**************************************************************************
//** removePhysicalAddress
//**************************************************************************
/** Deletes an address associated with this contact.
* @param str Complete mailing address or an address type/category.
*/
public void removePhysicalAddress(String str){
java.util.Iterator<String> it = physicalAddresses.keySet().iterator();
while (it.hasNext()){
String type = it.next();
if (str.equalsIgnoreCase(type)){
physicalAddresses.put(type, null);
if (id!=null) updates.put("PhysicalAddresses", getAddressUpdates());
break;
}
else{
PhysicalAddress address = physicalAddresses.get(type);
if (address.equals(type)){
physicalAddresses.put(type, null);
if (id!=null) updates.put("PhysicalAddresses", getAddressUpdates());
break;
}
}
}
}
//**************************************************************************
//** removePhysicalAddress
//**************************************************************************
/** Deletes an address associated with this contact.
* @param address PhysicalAddress. Must be an exact address match.
*/
public void removePhysicalAddress(PhysicalAddress address){
java.util.Iterator<String> it = physicalAddresses.keySet().iterator();
while (it.hasNext()){
String type = it.next();
if (physicalAddresses.get(type).equals(address)){
physicalAddresses.put(type, null);
if (id!=null) updates.put("PhysicalAddresses", getAddressUpdates());
break;
}
}
}
//**************************************************************************
//** removePhysicalAddresses
//**************************************************************************
/** Deletes all addresses associated with this contact.
*/
public void removePhysicalAddresses(){
PhysicalAddress[] addresses = getPhysicalAddresses();
if (addresses!=null){
for (PhysicalAddress address : addresses){
removePhysicalAddress(address);
}
}
}
//**************************************************************************
//** getAddressUpdates
//**************************************************************************
/** Returns an xml fragment with address updates or deletions. The xml
* fragment is later used in the updateContact() method.
*/
private String getAddressUpdates(){
if (physicalAddresses.isEmpty()) return "";
else{
StringBuffer xml = new StringBuffer();
java.util.Iterator<String> it = physicalAddresses.keySet().iterator();
while (it.hasNext()){
String type = it.next();
PhysicalAddress address = physicalAddresses.get(type);
if (address==null){
if (id!=null){
xml.append(new PhysicalAddress(type).toXML("t", false));
}
}
else{
xml.append(address.toXML("t", false));
}
}
return xml.toString();
}
}
//**************************************************************************
//** getPhoneNumbers
//**************************************************************************
/** Returns an array of PhoneNumbers associated with this contact.
*/
public PhoneNumber[] getPhoneNumbers(){
//Only include non-null values in the array
java.util.ArrayList<PhoneNumber> arr = new java.util.ArrayList<PhoneNumber>();
java.util.Iterator<String> it = phoneNumbers.keySet().iterator();
while (it.hasNext()){
String key = it.next();
PhoneNumber val = phoneNumbers.get(key);
if (val!=null) arr.add(val);
}
if (arr.isEmpty()) return null;
else return arr.toArray(new PhoneNumber[arr.size()]);
}
//**************************************************************************
//** setPhoneNumbers
//**************************************************************************
/** Used to add phone numbers to a contact.
*/
public void setPhoneNumbers(PhoneNumber[] phoneNumbers) {
if (phoneNumbers==null || phoneNumbers.length==0){
removePhoneNumbers();
return;
}
//See whether any updates are required
int numMatches = 0;
int total = 0;
for (PhoneNumber phoneNumber : phoneNumbers){
if (phoneNumber!=null){
total++;
String type = phoneNumber.getType();
if (this.phoneNumbers.containsKey(type)){
if (this.phoneNumbers.get(type).equals(phoneNumber)) numMatches++;
}
}
}
int numPhoneNumbers = 0;
if (this.getPhoneNumbers()!=null) numPhoneNumbers = this.getPhoneNumbers().length;
//If the input array equals the current list of phoneNumbers, do nothing...
if (numMatches==total && numMatches==numPhoneNumbers){
return;
}
else {
this.phoneNumbers.clear();
for (PhoneNumber phoneNumber : phoneNumbers){
addPhoneNumber(phoneNumber);
}
}
}
//**************************************************************************
//** addPhoneNumber
//**************************************************************************
/** Used to associate a phone number with this contact.
*/
public void addPhoneNumber(PhoneNumber phoneNumber){
//Check whether this is a new phone number
boolean update = false;
if (phoneNumbers.containsKey(phoneNumber.getType())){
java.util.Iterator<String> it = phoneNumbers.keySet().iterator();
while (it.hasNext()){
String key = it.next();
if (key.equals(phoneNumber.getType())){
String number = null;
if (phoneNumbers.get(key)!=null){
number = phoneNumbers.get(key).getNumber();
}
if (number==null || !number.equals(phoneNumber.getNumber())){
update = true;
}
break;
}
}
}
else{
update = true;
}
if (update){
phoneNumbers.put(phoneNumber.getType(), phoneNumber);
if (id!=null) updates.put("PhoneNumbers", getPhoneUpdates());
}
}
//**************************************************************************
//** removePhoneNumber
//**************************************************************************
public void removePhoneNumber(PhoneNumber phoneNumber){
//Find any "types" associated with this phone number
java.util.List<String> types = new java.util.ArrayList<String>();
java.util.Iterator<String> it = phoneNumbers.keySet().iterator();
while (it.hasNext()){
String type = it.next();
if (phoneNumbers.get(type).getNumber().equals(phoneNumber.getNumber())){
types.add(type);
}
}
//Update the hashmap of phone numbers
if (!types.isEmpty()){
it = types.iterator();
while (it.hasNext()){
String type = it.next();
if (id==null) phoneNumbers.remove(type);
else phoneNumbers.put(type, null);
}
if (id!=null) updates.put("PhoneNumbers", getPhoneUpdates());
}
}
//**************************************************************************
//** removePhoneNumbers
//**************************************************************************
public void removePhoneNumbers(){
PhoneNumber[] phoneNumbers = getPhoneNumbers();
if (phoneNumbers!=null){
for (PhoneNumber phoneNumber : phoneNumbers){
removePhoneNumber(phoneNumber);
}
}
}
private String getPhoneUpdates(){
if (phoneNumbers.isEmpty()) return "";
else{
StringBuffer xml = new StringBuffer();
java.util.Iterator<String> it = phoneNumbers.keySet().iterator();
while (it.hasNext()){
String type = it.next();
PhoneNumber phoneNumber = phoneNumbers.get(type);
if (phoneNumber==null){
if (id!=null){
xml.append("<t:DeleteItemField>");
xml.append("<t:IndexedFieldURI FieldURI=\"contacts:PhoneNumber\" FieldIndex=\"" + type + "\" /> ");
xml.append("</t:DeleteItemField>");
}
}
else{
String number = phoneNumber.getNumber();
xml.append("<t:SetItemField>");
xml.append("<t:IndexedFieldURI FieldURI=\"contacts:PhoneNumber\" FieldIndex=\"" + type + "\"/>");
xml.append("<t:Contact><t:PhoneNumbers><t:Entry Key=\"" + type + "\">" + number + "</t:Entry></t:PhoneNumbers></t:Contact>");
xml.append("</t:SetItemField>");
}
}
return xml.toString();
}
}
public EmailAddress getPrimaryEmailAddress(){
//TODO: Add primary attribute to email addresses!
EmailAddress[] email = getEmailAddresses();
if (email==null) return null;
else return email[0];
}
//**************************************************************************
//** getEmailAddresses
//**************************************************************************
/** Returns an array of email addresses associated with this contact. Note
* that Exchange only allows 3 email addresses per contact.
*/
public EmailAddress[] getEmailAddresses(){
//Only include non-null values in the array
java.util.ArrayList<EmailAddress> arr = new java.util.ArrayList<EmailAddress>();
java.util.Iterator<EmailAddress> it = emailAddresses.iterator();
while (it.hasNext()){
EmailAddress emailAddress = it.next();
if (emailAddress!=null) arr.add(emailAddress);
if (arr.size()==3) break;
}
if (arr.isEmpty()) return null;
else return arr.toArray(new EmailAddress[arr.size()]);
}
//**************************************************************************
//** setEmailAddresses
//**************************************************************************
/** Used to add email Addresses to a contact.
*/
public void setEmailAddresses(EmailAddress[] emailAddresses) {
if (emailAddresses==null || emailAddresses.length==0){
removeEmailAddresses();
return;
}
//See whether any updates are required
int numMatches = 0;
int total = 0;
for (EmailAddress emailAddress : emailAddresses){
if (emailAddress!=null){
total++;
if (this.emailAddresses.contains(emailAddress)) numMatches++;
}
}
int numEmails = 0;
if (this.getEmailAddresses()!=null) numEmails = this.getEmailAddresses().length;
//If the input array equals the current list of emailAddresses, do nothing...
if (numMatches==total && numMatches==numEmails){
return;
}
else {
this.emailAddresses.clear();
for (EmailAddress emailAddress : emailAddresses){
addEmailAddress(emailAddress);
}
}
}
public void setEmailAddress(EmailAddress emailAddress){
setEmailAddresses(new EmailAddress[]{emailAddress});
}
//**************************************************************************
//** addEmailAddress
//**************************************************************************
/** Used to associate an email address with the contact. Note that Exchange
* only allows 3 email addresses per contact.
*/
public void addEmailAddress(EmailAddress emailAddress) {
if (emailAddresses.size()<3){
if (id!=null && !emailAddresses.contains(emailAddress)){
emailAddresses.add(emailAddress);
updates.put("EmailAddresses", getEmailUpdates());
}
else{
emailAddresses.add(emailAddress);
}
}
}
//**************************************************************************
//** removeEmailAddress
//**************************************************************************
/** Used delete an email address associated with this contact.
*/
public void removeEmailAddress(EmailAddress emailAddress){
//emailAddresses.remove(emailAddress.toLowerCase());
}
//**************************************************************************
//** removeEmailAddresses
//**************************************************************************
/** Used to remove all email addresses associated with this contact.
*/
public void removeEmailAddresses(){
EmailAddress[] addresses = getEmailAddresses();
if (addresses!=null){
for (EmailAddress address : addresses){
removeEmailAddress(address);
}
}
}
//**************************************************************************
//** getEmailUpdates
//**************************************************************************
/** Used to return an xml fragment used to update or delete email addresses.
*/
private String getEmailUpdates(){
if (emailAddresses.isEmpty()) return "";
else{
StringBuffer xml = new StringBuffer();
int x = 1;
java.util.Iterator<EmailAddress> it = emailAddresses.iterator();
while (it.hasNext()){
EmailAddress emailAddress = it.next();
if (emailAddress!=null){
if (x>3) break;
xml.append("<t:SetItemField>");
xml.append("<t:IndexedFieldURI FieldURI=\"contacts:EmailAddress\" FieldIndex=\"EmailAddress" + x + "\"/>");
xml.append("<t:Contact><t:EmailAddresses><t:Entry Key=\"EmailAddress" + x + "\">" + emailAddress + "</t:Entry></t:EmailAddresses></t:Contact>");
xml.append("</t:SetItemField>");
x++;
}
}
for (int i=x; i<4; i++){
if (id!=null){
xml.append("<t:DeleteItemField>");
xml.append("<t:IndexedFieldURI FieldURI=\"contacts:EmailAddress\" FieldIndex=\"EmailAddress" + i + "\" /> ");
xml.append("</t:DeleteItemField>");
//Apparently for emails, you have to delete property sets as well:
//http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/thread/b57736cf-b007-49f6-b8c6-c4ba00b5cc23/
if (i==1){
for (String propertyID : new String[]{"32896", "32898", "32900"}){
xml.append("<t:DeleteItemField>");
xml.append("<t:ExtendedFieldURI PropertySetId=\"00062004-0000-0000-C000-000000000046\" PropertyId=\"" + propertyID + "\" PropertyType=\"String\"/>");
xml.append("</t:DeleteItemField>");
}
xml.append("<t:DeleteItemField>");
xml.append("<t:ExtendedFieldURI PropertySetId=\"00062004-0000-0000-C000-000000000046\" PropertyId=\"32901\" PropertyType=\"Binary\"/>");
xml.append("</t:DeleteItemField>");
}
if (i==2){
for (String propertyID : new String[]{"32912", "32914", "32916"}){
xml.append("<t:DeleteItemField>");
xml.append("<t:ExtendedFieldURI PropertySetId=\"00062004-0000-0000-C000-000000000046\" PropertyId=\"" + propertyID + "\" PropertyType=\"String\"/>");
xml.append("</t:DeleteItemField>");
}
xml.append("<t:DeleteItemField>");
xml.append("<t:ExtendedFieldURI PropertySetId=\"00062004-0000-0000-C000-000000000046\" PropertyId=\"32917\" PropertyType=\"Binary\"/>");
xml.append("</t:DeleteItemField>");
}
if (i==3){
for (String propertyID : new String[]{"32928", "32930", "32932"}){
xml.append("<t:DeleteItemField>");
xml.append("<t:ExtendedFieldURI PropertySetId=\"00062004-0000-0000-C000-000000000046\" PropertyId=\"" + propertyID + "\" PropertyType=\"String\"/>");
xml.append("</t:DeleteItemField>");
}
xml.append("<t:DeleteItemField>");
xml.append("<t:ExtendedFieldURI PropertySetId=\"00062004-0000-0000-C000-000000000046\" PropertyId=\"32933\" PropertyType=\"Binary\"/>");
xml.append("</t:DeleteItemField>");
}
}
}
return xml.toString();
}
}
//**************************************************************************
//** setCompanyName
//**************************************************************************
/** Used to associate the contact with a company or organization.
*/
public void setCompanyName(String company){
company = getValue(company);
if (id!=null) {
if (company==null && this.company!=null) updates.put("CompanyName", null);
if (company!=null && !company.equals(this.company)) updates.put("CompanyName", company);
}
this.company = company;
}
//**************************************************************************
//** getCompanyName
//**************************************************************************
/** Returns the company or organization associated with this contact.
*/
public String getCompanyName(){
company = getValue(company);
return company;
}
//**************************************************************************
//** setBirthDay
//**************************************************************************
/** Used to set the date of birth using a java.util.Date.
*/
public void setBirthDay(java.util.Date birthday){
setBirthDay(new javaxt.utils.Date(birthday));
}
//**************************************************************************
//** setBirthDay
//**************************************************************************
/** Used to set the date of birth using a javaxt.utils.Date.
*/
public void setBirthDay(javaxt.utils.Date birthday){
if (birthday!=null) birthday = birthday.clone();
//Validate the date
if (birthday!=null){
if (birthday.getYear()<1900 || birthday.isAfter(new javaxt.utils.Date())){
birthday = null;
}
}
//Flip the timezone to UTC as needed.
if (birthday!=null && !birthday.hasTimeStamp()){
birthday.setTimeZone("UTC", true);
}
//Update birthday
if (id!=null){
if (birthday==null && this.birthday!=null) updates.put("Birthday", null);
if (birthday!=null && !formatDate(birthday).equals(formatDate(this.birthday))){
updates.put("Birthday", formatDate(birthday));
}
}
this.birthday = birthday;
}
//**************************************************************************
//** setBirthDay
//**************************************************************************
/** Used to set the date of birth using a String. If the method fails to
* parse the string, the new value will be ignored.
*/
public void setBirthDay(String birthday){
javaxt.utils.Date date = null;
try{
date = new javaxt.utils.Date(birthday);
}
catch(java.text.ParseException e){}
setBirthDay(date);
}
//**************************************************************************
//** getBirthDay
//**************************************************************************
/** Returns the date of birth.
*/
public javaxt.utils.Date getBirthDay(){
if (birthday==null) return null;
return birthday.clone();
}
//**************************************************************************
//** getAge
//**************************************************************************
/** Returns the age of the contact, in years.
*/
public Integer getAge(){
if (birthday!=null) return (int) -birthday.compareTo(new java.util.Date(), "YEAR");
return null;
}
//**************************************************************************
//** setTitle
//**************************************************************************
/** Used to set the job title.
*/
public void setTitle(String title){
title = getValue(title);
if (id!=null) {
this.title = getTitle();
if (title==null && this.title!=null) updates.put("JobTitle", null);
if (title!=null && !title.equals(this.title)) updates.put("JobTitle", title);