KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencrx > kernel > layer > application > VCard


1 /*
2  * ====================================================================
3  * Project: opencrx, http://www.opencrx.org/
4  * Name: $Id: VCard.java,v 1.10 2006/01/03 00:49:17 wfro Exp $
5  * Description: VCard
6  * Revision: $Revision: 1.10 $
7  * Owner: CRIXP AG, Switzerland, http://www.crixp.com
8  * Date: $Date: 2006/01/03 00:49:17 $
9  * ====================================================================
10  *
11  * This software is published under the BSD license
12  * as listed below.
13  *
14  * Copyright (c) 2004-2005, CRIXP Corp., Switzerland
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  *
21  * * Redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer.
23  *
24  * * Redistributions in binary form must reproduce the above copyright
25  * notice, this list of conditions and the following disclaimer in
26  * the documentation and/or other materials provided with the
27  * distribution.
28  *
29  * * Neither the name of CRIXP Corp. nor the names of the contributors
30  * to openCRX may be used to endorse or promote products derived
31  * from this software without specific prior written permission
32  *
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
35  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
36  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
39  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
41  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
43  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46  * POSSIBILITY OF SUCH DAMAGE.
47  *
48  * ------------------
49  *
50  * This product includes software developed by the Apache Software
51  * Foundation (http://www.apache.org/).
52  *
53  * This product includes software developed by contributors to
54  * openMDX (http://www.openmdx.org/)
55  */

56 package org.opencrx.kernel.layer.application;
57
58 import java.io.BufferedReader JavaDoc;
59 import java.io.ByteArrayInputStream JavaDoc;
60 import java.io.ByteArrayOutputStream JavaDoc;
61 import java.io.IOException JavaDoc;
62 import java.io.InputStream JavaDoc;
63 import java.io.InputStreamReader JavaDoc;
64 import java.io.PrintWriter JavaDoc;
65 import java.util.ArrayList JavaDoc;
66 import java.util.Date JavaDoc;
67 import java.util.HashMap JavaDoc;
68 import java.util.Iterator JavaDoc;
69 import java.util.List JavaDoc;
70 import java.util.Map JavaDoc;
71 import java.util.SortedMap JavaDoc;
72 import java.util.StringTokenizer JavaDoc;
73 import java.util.Map.Entry;
74
75 import org.openmdx.application.log.AppLog;
76 import org.openmdx.base.exception.ServiceException;
77 import org.openmdx.base.text.format.DateFormat;
78 import org.openmdx.compatibility.base.dataprovider.cci.AttributeSelectors;
79 import org.openmdx.compatibility.base.dataprovider.cci.DataproviderObject;
80 import org.openmdx.compatibility.base.dataprovider.cci.DataproviderObject_1_0;
81 import org.openmdx.compatibility.base.dataprovider.cci.Directions;
82 import org.openmdx.compatibility.base.dataprovider.cci.RequestCollection;
83 import org.openmdx.compatibility.base.dataprovider.cci.SystemAttributes;
84 import org.openmdx.compatibility.base.naming.Path;
85 import org.openmdx.compatibility.base.query.FilterOperators;
86 import org.openmdx.compatibility.base.query.FilterProperty;
87 import org.openmdx.compatibility.base.query.Quantors;
88 import org.openmdx.kernel.id.UUIDs;
89
90 public class VCard {
91
92   //-------------------------------------------------------------------------
93
public VCard(
94     OpenCrxKernel_1 plugin,
95     RequestCollection delegation,
96     Codes codes
97   ) {
98     this.plugin = plugin;
99     this.delegation = delegation;
100     this.codes = codes;
101   }
102
103   //-------------------------------------------------------------------------
104
private Short JavaDoc numberAsShort(
105     Object JavaDoc number
106   ) {
107     return new Short JavaDoc(((Number JavaDoc)number).shortValue());
108   }
109     
110   //-------------------------------------------------------------------------
111
public byte[] exportItem(
112     DataproviderObject_1_0 contact,
113     short locale
114   ) throws ServiceException {
115     AppLog.trace("inspecting contact", contact);
116
117     String JavaDoc lastName = contact.values("lastName").size() == 0 ? "" : (String JavaDoc)contact.values("lastName").get(0);
118     String JavaDoc firstName = contact.values("firstName").size() == 0 ? "" : (String JavaDoc)contact.values("firstName").get(0);
119     String JavaDoc middleName = contact.values("middleName").size() == 0 ? "" : (String JavaDoc)contact.values("middleName").get(0);
120     String JavaDoc salutation = contact.values("salutation").size() == 0 ? "" : (String JavaDoc)contact.values("salutation").get(0);
121     String JavaDoc suffix = contact.values("suffix").size() == 0 ? "" : (String JavaDoc)contact.values("suffix").get(0);
122     String JavaDoc jobTitle = contact.values("jobTitle").size() == 0 ? "" : (String JavaDoc)contact.values("jobTitle").get(0);
123     String JavaDoc organization = contact.values("organization").size() == 0 ? "" : (String JavaDoc)contact.values("organization").get(0);
124     List JavaDoc addresses = this.delegation.addFindRequest(
125       contact.path().getChild("address"),
126       null,
127       AttributeSelectors.ALL_ATTRIBUTES,
128       0,
129       Integer.MAX_VALUE,
130       Directions.ASCENDING
131     );
132     String JavaDoc telWorkVoice = "";
133     String JavaDoc telHomeVoice = "";
134     String JavaDoc telCellVoice = "";
135     String JavaDoc telWorkFax = "";
136     String JavaDoc telHomeFax = "";
137     String JavaDoc telPrimary = "";
138     String JavaDoc adrWork = "";
139     String JavaDoc adrHome = "";
140     String JavaDoc urlHome = "";
141     String JavaDoc urlWork = "";
142     String JavaDoc emailPrefInternet = "";
143     
144     for(Iterator JavaDoc i = addresses.iterator(); i.hasNext(); ) {
145       
146       DataproviderObject_1_0 address = (DataproviderObject_1_0)i.next();
147       AppLog.trace("inspecting address", address);
148       String JavaDoc addressClass = (String JavaDoc)address.values(SystemAttributes.OBJECT_CLASS).get(0);
149       List JavaDoc usage = new ArrayList JavaDoc();
150       for(Iterator JavaDoc j = address.values("usage").iterator(); j.hasNext(); ) {
151         usage.add(
152           this.numberAsShort(j.next())
153         );
154       }
155       if("org:opencrx:kernel:account1:PostalAddress".equals(addressClass)) {
156         String JavaDoc adr = "";
157         // postalAddressLine
158
List JavaDoc addressLines = address.values("postalAddressLine");
159         for(int j = 0; j < addressLines.size(); j++) {
160           adr += adr.length() == 0 ? "" : "=0D=0A";
161           adr += addressLines.get(j);
162         }
163         // postalStreet
164
List JavaDoc postalStreet = address.values("postalStreet");
165         for(int j = 0; j < postalStreet.size(); j++) {
166           adr += adr.length() == 0 ? "" : "=0D=0A";
167           adr += postalStreet.get(j);
168         }
169         // postalCity
170
adr += address.values("postalCity").size() == 0
171           ? ";"
172           : ";" + (String JavaDoc)address.values("postalCity").get(0);
173         // postalCode
174
adr += ";";
175         adr += address.values("postalCode").size() == 0
176           ? ";"
177           : ";" + (String JavaDoc)address.values("postalCode").get(0);
178         // postalCountry
179
Map JavaDoc postalCountries = this.codes.getLongText("org:opencrx:kernel:address1:PostalAddressable:postalCountry", locale, true);
180         adr += address.values("postalCountry").size() == 0
181           ? ";"
182           : ";" + (String JavaDoc)postalCountries.get(this.numberAsShort(address.values("postalCountry").get(0)));
183         
184         // home
185
if(usage.contains(USAGE_POSTAL_HOME)) {
186           adrHome = adr;
187         }
188         // work
189
else if(usage.contains(USAGE_POSTAL_WORK)) {
190           adrWork = adr;
191         }
192       }
193       else if("org:opencrx:kernel:account1:EMailAddress".equals(addressClass)) {
194         // primary
195
if(usage.contains(USAGE_EMAIL_PRIMARY)) {
196           emailPrefInternet = address.values("emailAddress").size() == 0
197             ? ""
198             : (String JavaDoc)address.values("emailAddress").get(0);
199         }
200       }
201       else if("org:opencrx:kernel:account1:PhoneNumber".equals(addressClass)) {
202         String JavaDoc phoneNumberFull =
203           address.values("phoneNumberFull").size() == 0
204             ? ""
205             : (String JavaDoc)address.values("phoneNumberFull").get(0);
206         // home voice
207
if(usage.contains(USAGE_PHONE_HOME)) {
208           telHomeVoice = phoneNumberFull;
209         }
210         // work voice
211
else if(usage.contains(USAGE_PHONE_WORK)) {
212           telWorkVoice = phoneNumberFull;
213         }
214         // home fax
215
else if(usage.contains(USAGE_FAX_HOME)) {
216           telHomeFax = phoneNumberFull;
217         }
218         // work fax
219
else if(usage.contains(USAGE_FAX_WORK)) {
220           telWorkFax = phoneNumberFull;
221         }
222         // cell voice
223
else if(usage.contains(USAGE_MOBILE)) {
224           telCellVoice = phoneNumberFull;
225         }
226         // primary
227
else if(usage.contains(USAGE_PHONE_PRIMARY)) {
228           telPrimary = phoneNumberFull;
229         }
230       }
231       else if("org:opencrx:kernel:account1:WebAddress".equals(addressClass)) {
232         String JavaDoc webUrl =
233           address.values("webUrl").size() == 0
234             ? ""
235             : (String JavaDoc)address.values("webUrl").get(0);
236         // home url
237
if(usage.contains(USAGE_WEB_HOME)) {
238           urlHome = webUrl;
239         }
240         // work url
241
else if(usage.contains(USAGE_WEB_WORK)) {
242           urlWork = webUrl;
243         }
244       }
245     }
246     
247     ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
248     PrintWriter JavaDoc pw = new PrintWriter JavaDoc(os);
249     pw.println("BEGIN:VCARD");
250     pw.println("VERSION:2.1");
251     pw.println("N:" + lastName + ";" + firstName + ";"+ middleName + ";" + salutation + ";" + suffix);
252     pw.println("FN:" + firstName + (middleName.length() == 0 ? "" : " " + middleName) + (lastName.length() == 0 ? "" : " " + lastName) + (suffix.length() == 0 ? "" : " " + suffix));
253     pw.println("ORG:" + organization);
254     pw.println("TITLE:" + jobTitle);
255     pw.println("TEL;WORK;VOICE:" + telWorkVoice);
256     pw.println("TEL;HOME;VOICE:" + telHomeVoice);
257     pw.println("TEL;CELL;VOICE:" + telCellVoice);
258     pw.println("TEL;WORK;FAX:" + telWorkFax);
259     pw.println("TEL;HOME;FAX:" + telHomeFax);
260     pw.println("TEL;PREF:" + telPrimary);
261     pw.println("ADR;WORK;ENCODING=QUOTED-PRINTABLE:;;" + adrWork);
262     pw.println("ADR;HOME;ENCODING=QUOTED-PRINTABLE:;;" + adrHome);
263     pw.println("URL;HOME:" + urlHome);
264     pw.println("URL;WORK:" + urlWork);
265     pw.println("EMAIL;PREF;INTERNET:" + emailPrefInternet);
266     pw.println("REV:" + DateFormat.getInstance().format(new Date JavaDoc()));
267     pw.println("END:VCARD");
268     try {
269       pw.flush();
270       os.close();
271     } catch(Exception JavaDoc e) {}
272     return os.toByteArray();
273   }
274
275   //-------------------------------------------------------------------------
276
private boolean updatePostalAddress(
277     DataproviderObject address,
278     String JavaDoc newValue,
279     short locale
280   ) throws ServiceException {
281     if((newValue != null) && (newValue.length() > 0)) {
282       String JavaDoc[] tokens = new String JavaDoc[]{"", "", "", "", "", "", ""};
283       StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(newValue, ";", true);
284       int ii = 0;
285       boolean hasTokens = false;
286       while(tokenizer.hasMoreTokens() && (ii < tokens.length)) {
287         String JavaDoc t = tokenizer.nextToken();
288         if(";".equals(t)) {
289           ii++;
290         }
291         else {
292           tokens[ii] = t;
293           hasTokens = true;
294         }
295       }
296       if(!hasTokens) {
297           return false;
298       }
299       // parse street (=0D=0A separated tokens)
300
List JavaDoc street = new ArrayList JavaDoc();
301       String JavaDoc temp = tokens[2];
302       int pos = 0;
303       while((pos = temp.indexOf("=0D=0A")) >= 0) {
304         street.add(temp.substring(0, pos));
305         temp = temp.substring(pos + 6);
306       }
307       street.add(temp);
308       address.clearValues("postalAddressLine");
309       for(int i = 0; i < street.size()-1; i++) {
310         address.values("postalAddressLine").add(street.get(i));
311       }
312       address.clearValues("postalStreet").add(street.get(street.size()-1));
313       address.clearValues("postalCity").add(tokens[3]);
314       address.clearValues("postalState").add(tokens[4]);
315       address.clearValues("postalCode").add(tokens[5]);
316       address.clearValues("postalCountry").add(new Short JavaDoc((short)0));
317       
318       // lookup country
319
AppLog.trace("lookup country", tokens[6]);
320       SortedMap JavaDoc countries = this.codes.getLongText("org:opencrx:kernel:address1:PostalAddressable:postalCountry", locale, true);
321       if(countries != null) {
322         for(Iterator JavaDoc i = countries.entrySet().iterator(); i.hasNext(); ) {
323           Entry entry = (Entry)i.next();
324           if(((String JavaDoc)entry.getValue()).indexOf(tokens[6]) >= 0) {
325             address.clearValues("postalCountry").add(
326               entry.getKey()
327             );
328             break;
329           }
330         }
331       }
332       AppLog.trace("updated address", address);
333       return true;
334     }
335     return false;
336   }
337   
338   //-------------------------------------------------------------------------
339
private boolean updatePhoneNumber(
340     DataproviderObject address,
341     String JavaDoc newValue
342   ) throws ServiceException {
343     if((newValue != null) && (newValue.length() > 0)) {
344       address.clearValues("phoneNumberFull").add(newValue);
345       address.clearValues("automaticParsing").add(Boolean.TRUE);
346       this.plugin.parsePhoneNumber(address, null);
347       AppLog.trace("updated address", address);
348       return true;
349     }
350     return false;
351   }
352   
353   //-------------------------------------------------------------------------
354
private boolean updateWebAddress(
355     DataproviderObject address,
356     String JavaDoc newValue
357   ) {
358     if((newValue != null) && (newValue.length() > 0)) {
359       address.clearValues("webUrl").add(newValue);
360       AppLog.trace("updated address", address);
361       return true;
362     }
363     return false;
364   }
365   
366   //-------------------------------------------------------------------------
367
private boolean updateEMailAddress(
368     DataproviderObject address,
369     String JavaDoc newValue
370   ) {
371     if((newValue != null) && (newValue.length() > 0)) {
372       address.clearValues("emailAddress").add(newValue);
373       AppLog.trace("updated address", address);
374       return true;
375     }
376     return false;
377   }
378   
379   //-------------------------------------------------------------------------
380
public DataproviderObject importItem(
381     byte[] item,
382     Path segmentPath,
383     short locale,
384     List JavaDoc report
385   ) throws ServiceException {
386     try {
387       // parse vcard
388
InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(item);
389       BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
390       Map JavaDoc data = new HashMap JavaDoc();
391       String JavaDoc line = null;
392       boolean lineCont = false;
393       String JavaDoc currentName = null;
394       while((line = reader.readLine()) != null) {
395         if(lineCont) {
396           data.put(
397             currentName,
398             data.get(currentName) + line
399           );
400           lineCont = false;
401         }
402         else if(line.indexOf(":") >= 0) {
403           int pos = line.indexOf(":");
404           currentName = line.substring(0, pos).toUpperCase();
405           lineCont = (currentName.indexOf("ENCODING=QUOTED-PRINTABLE") >= 0) && line.endsWith("=");
406           data.put(
407             currentName,
408             line.substring(pos + 1, lineCont ? line.length() - 1 : line.length())
409           );
410         }
411       }
412       AppLog.trace("parsed vcard", data);
413       return this.importItem(
414         data,
415         segmentPath,
416         locale,
417         false,
418         report
419       );
420     }
421     catch(IOException JavaDoc e) {
422       AppLog.warning("can not read item", e.getMessage());
423     }
424     return null;
425   }
426   
427   //-------------------------------------------------------------------------
428
/**
429    * Imports the vcard. If a contact with email address EMAIL;PREF;INTERNET
430    * is found then the contact and addresses are updated with the new vcard
431    * info. If no contact is found then the contact and all its addresses are
432    * created. If createOnly=true then no existing data is updated. However,
433    * if the contact does not exist it is created.
434    */

435   public DataproviderObject importItem(
436     Map JavaDoc vcard,
437     Path segmentPath,
438     short locale,
439     boolean createOnly,
440     List JavaDoc report
441   ) throws ServiceException {
442     String JavaDoc emailPrefInternet = (String JavaDoc)vcard.get("EMAIL;PREF;INTERNET");
443     DataproviderObject contact = null;
444     if((emailPrefInternet != null) && (emailPrefInternet.length() > 0)) {
445       AppLog.trace("looking up", emailPrefInternet);
446       List JavaDoc addresses = this.delegation.addFindRequest(
447         segmentPath.getChild("extent"),
448         new FilterProperty[]{
449           new FilterProperty(
450             Quantors.THERE_EXISTS,
451             "identity",
452             FilterOperators.IS_LIKE,
453             new String JavaDoc[]{
454               segmentPath.getDescendant(new String JavaDoc[]{"account", ":*", "address", ":*"}).toXri()
455             }
456           ),
457           new FilterProperty(
458             Quantors.THERE_EXISTS,
459             SystemAttributes.OBJECT_CLASS,
460             FilterOperators.IS_IN,
461             new String JavaDoc[]{
462               "org:opencrx:kernel:account1:EMailAddress"
463             }
464           ),
465           new FilterProperty(
466             Quantors.THERE_EXISTS,
467             "emailAddress",
468             FilterOperators.IS_IN,
469             new String JavaDoc[]{
470               emailPrefInternet
471             }
472           )
473         }
474       );
475       if(addresses.iterator().hasNext()) {
476           AppLog.trace("address found");
477           DataproviderObject_1_0 address = (DataproviderObject_1_0)addresses.iterator().next();
478           contact = this.plugin.retrieveObjectForModification(
479               address.path().getParent().getParent()
480           );
481       }
482     }
483     AppLog.trace("account", contact);
484
485     boolean isNew = contact == null;
486     if(isNew) {
487       contact = new DataproviderObject(
488         segmentPath.getDescendant(new String JavaDoc[]{"account", UUIDs.getGenerator().next().toString()})
489       );
490       contact.values(SystemAttributes.OBJECT_CLASS).add(
491         "org:opencrx:kernel:account1:Contact"
492       );
493     }
494     if("org:opencrx:kernel:account1:Contact".equals(contact.values(SystemAttributes.OBJECT_CLASS).get(0))) {
495
496       /**
497        * Contact
498        */

499       // name
500
String JavaDoc name = (String JavaDoc)vcard.get("N");
501       if((name != null) && (name.indexOf(";") >= 0)) {
502         String JavaDoc[] tokens = new String JavaDoc[]{"", "", "", "", ""};
503         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(name, ";", true);
504         int ii = 0;
505         while(tokenizer.hasMoreTokens() && (ii < tokens.length)) {
506           String JavaDoc t = tokenizer.nextToken();
507           if(";".equals(t)) {
508             ii++;
509           }
510           else {
511             tokens[ii] = t;
512           }
513         }
514         // lastName
515
if(tokens[0].length() > 0) {
516           contact.clearValues("lastName").add(tokens[0]);
517         }
518         else if(contact.values("lastName").size() == 0) {
519           contact.values("lastName").add("N/A");
520         }
521         // firstName
522
if(tokens[1].length() > 0) {
523           contact.clearValues("firstName").add(tokens[1]);
524         }
525         // middleName
526
if(tokens[2].length() > 0) {
527           contact.clearValues("middleName").add(tokens[2]);
528         }
529         // saluation
530
if(tokens[3].length() > 0) {
531           contact.clearValues("salutation").add(tokens[3]);
532         }
533         // suffix
534
if(tokens[4].length() > 0) {
535           contact.clearValues("suffix").add(tokens[4]);
536         }
537       }
538       // jobTitle
539
String JavaDoc jobTitle = (String JavaDoc)vcard.get("TITLE");
540       if((jobTitle != null) && (jobTitle.length() > 0)) {
541           contact.clearValues("jobTitle").add(jobTitle);
542       }
543       // organization
544
String JavaDoc organization = (String JavaDoc)vcard.get("ORG");
545       if((organization != null) && (organization.length() > 0)) {
546           contact.clearValues("organization").add(organization);
547       }
548       this.plugin.accounts.setAccountFullName(contact, null);
549       if(isNew) {
550           AppLog.trace("adding", contact);
551           this.delegation.addCreateRequest(
552               contact
553           );
554           report.add("Create contact");
555       }
556       else if(!createOnly) {
557           AppLog.trace("updating", contact);
558           report.add("Update contact");
559       }
560     }
561     // note
562
String JavaDoc s = vcard.get("NOTE") != null ? (String JavaDoc)vcard.get("NOTE") : (String JavaDoc)vcard.get("NOTE;ENCODING=QUOTED-PRINTABLE");
563     if(s != null) {
564       DataproviderObject note = null;
565       isNew = false;
566       try {
567           note = this.plugin.retrieveObjectForModification(
568               contact.path().getDescendant(new String JavaDoc[]{"note", "VCARD"})
569           );
570       }
571       catch(Exception JavaDoc e) {}
572       if(note == null) {
573           note = new DataproviderObject(contact.path().getDescendant(new String JavaDoc[]{"note", "VCARD"}));
574           note.values(SystemAttributes.OBJECT_CLASS).add("org:opencrx:kernel:generic:Note");
575           isNew = true;
576       }
577       note.clearValues("title").add("vCard note");
578       String JavaDoc text = "";
579       int pos = 0;
580       while((pos = s.indexOf("=0D=0A")) >= 0) {
581           text += s.substring(0, pos) + "\n";
582           s = s.substring(pos + 6);
583       }
584       note.clearValues("text").add(text);
585       if(isNew) {
586           this.delegation.addCreateRequest(
587               note
588           );
589           report.add("Create note");
590       }
591       else if(!createOnly) {
592           report.add("Update note");
593       }
594     }
595     
596     // addresses
597
List JavaDoc addresses = this.delegation.addFindRequest(
598         contact.path().getChild("address"),
599         null,
600         AttributeSelectors.ALL_ATTRIBUTES,
601         0,
602         Integer.MAX_VALUE,
603         Directions.ASCENDING
604     );
605     
606     DataproviderObject adrHome = null;
607     DataproviderObject adrWork = null;
608     DataproviderObject telPrimary = null;
609     DataproviderObject telHomeVoice = null;
610     DataproviderObject telWorkVoice = null;
611     DataproviderObject telHomeFax = null;
612     DataproviderObject telWorkFax = null;
613     DataproviderObject urlHome = null;
614     DataproviderObject urlWork = null;
615     DataproviderObject telCellVoice = null;
616     DataproviderObject emailPrimary = null;
617     
618     // get addresses
619
for(Iterator JavaDoc i = addresses.iterator(); i.hasNext(); ) {
620       DataproviderObject address = new DataproviderObject((DataproviderObject_1_0)i.next());
621       String JavaDoc addressClass = (String JavaDoc)address.values(SystemAttributes.OBJECT_CLASS).get(0);
622       List JavaDoc usage = new ArrayList JavaDoc();
623       for(Iterator JavaDoc j = address.values("usage").iterator(); j.hasNext(); ) {
624           usage.add(
625               this.numberAsShort(j.next())
626           );
627       }
628       if("org:opencrx:kernel:account1:PostalAddress".equals(addressClass)) {
629         if(usage.contains(USAGE_POSTAL_HOME)) {
630             adrHome = this.plugin.retrieveObjectForModification(address.path());
631         }
632         else if(usage.contains(USAGE_POSTAL_WORK)) {
633             adrWork = this.plugin.retrieveObjectForModification(address.path());
634         }
635       }
636       else if("org:opencrx:kernel:account1:EMailAddress".equals(addressClass)) {
637         if(usage.contains(USAGE_EMAIL_PRIMARY)) {
638             emailPrimary = this.plugin.retrieveObjectForModification(address.path());
639         }
640       }
641       else if("org:opencrx:kernel:account1:PhoneNumber".equals(addressClass)) {
642         if(usage.contains(USAGE_PHONE_HOME)) {
643             telHomeVoice = this.plugin.retrieveObjectForModification(address.path());
644         }
645         // work voice
646
else if(usage.contains(USAGE_PHONE_WORK)) {
647             telWorkVoice = this.plugin.retrieveObjectForModification(address.path());
648         }
649         // home fax
650
else if(usage.contains(USAGE_FAX_HOME)) {
651             telHomeFax = this.plugin.retrieveObjectForModification(address.path());
652         }
653         // work fax
654
else if(usage.contains(USAGE_FAX_WORK)) {
655             telWorkFax = this.plugin.retrieveObjectForModification(address.path());
656         }
657         // cell voice
658
else if(usage.contains(USAGE_MOBILE)) {
659             telCellVoice = this.plugin.retrieveObjectForModification(address.path());
660         }
661         // primary
662
else if(usage.contains(USAGE_PHONE_PRIMARY)) {
663             telPrimary = this.plugin.retrieveObjectForModification(address.path());
664         }
665       }
666       else if("org:opencrx:kernel:account1:WebAddress".equals(addressClass)) {
667           if(usage.contains(USAGE_WEB_HOME)) {
668               urlHome = this.plugin.retrieveObjectForModification(address.path());
669           }
670           // work url
671
else if(usage.contains(USAGE_WEB_WORK)) {
672               urlWork = this.plugin.retrieveObjectForModification(address.path());
673           }
674       }
675     }
676     // update adrHome
677
isNew = false;
678     if(adrHome == null) {
679         adrHome = new DataproviderObject(contact.path().getDescendant(new String JavaDoc[]{"address", UUIDs.getGenerator().next().toString()}));
680         adrHome.values(SystemAttributes.OBJECT_CLASS).add("org:opencrx:kernel:account1:PostalAddress");
681         adrHome.values("usage").add(USAGE_POSTAL_HOME);
682         isNew = true;
683     }
684     s = vcard.get("ADR;HOME") != null ? (String JavaDoc)vcard.get("ADR;HOME") : (String JavaDoc)vcard.get("ADR;HOME;ENCODING=QUOTED-PRINTABLE");
685     if(this.updatePostalAddress(adrHome, s, locale)) {
686         if(isNew) {
687             this.delegation.addCreateRequest(adrHome);
688             report.add("Create postal address");
689         }
690         else if(!createOnly) {
691             report.add("Update postal address");
692         }
693     }
694     // update adrWork
695
isNew = false;
696     if(adrWork == null) {
697         adrWork = new DataproviderObject(contact.path().getDescendant(new String JavaDoc[]{"address", UUIDs.getGenerator().next().toString()}));
698         adrWork.values(SystemAttributes.OBJECT_CLASS).add("org:opencrx:kernel:account1:PostalAddress");
699         adrWork.values("usage").add(USAGE_POSTAL_WORK);
700         isNew = true;
701     }
702     s = vcard.get("ADR;WORK") != null ? (String JavaDoc)vcard.get("ADR;WORK") : (String JavaDoc)vcard.get("ADR;WORK;ENCODING=QUOTED-PRINTABLE");
703     if(this.updatePostalAddress(adrWork, s, locale)) {
704         if(isNew) {
705             this.delegation.addCreateRequest(adrWork);
706             report.add("Create postal address");
707         }
708         else if(!createOnly) {
709             report.add("Update postal address");
710         }
711     }
712     // update telPrimary
713
isNew = false;
714     if(telPrimary == null) {
715         telPrimary = new DataproviderObject(contact.path().getDescendant(new String JavaDoc[]{"address", UUIDs.getGenerator().next().toString()}));
716         telPrimary.values(SystemAttributes.OBJECT_CLASS).add("org:opencrx:kernel:account1:PhoneNumber");
717         telPrimary.values("usage").add(USAGE_PHONE_PRIMARY);
718         isNew = true;
719     }
720     if(this.updatePhoneNumber(telPrimary, (String JavaDoc)vcard.get("TEL;PREF"))) {
721         if(isNew) {
722             this.delegation.addCreateRequest(telPrimary);
723             report.add("Create phone number");
724         }
725         else if(!createOnly ) {
726             report.add("Update phone number");
727         }
728     }
729     // update telHomeVoice
730
isNew = false;
731     if(telHomeVoice == null) {
732         telHomeVoice = new DataproviderObject(contact.path().getDescendant(new String JavaDoc[]{"address", UUIDs.getGenerator().next().toString()}));
733         telHomeVoice.values(SystemAttributes.OBJECT_CLASS).add("org:opencrx:kernel:account1:PhoneNumber");
734         telHomeVoice.values("usage").add(USAGE_PHONE_HOME);
735         isNew = true;
736     }
737     if(this.updatePhoneNumber(telHomeVoice, (String JavaDoc)vcard.get("TEL;HOME;VOICE"))) {
738         if(isNew) {
739             this.delegation.addCreateRequest(telHomeVoice);
740             report.add("Create phone number");
741         }
742         else if(!createOnly) {
743             report.add("Update phone number");
744         }
745     }
746     // update telWorkVoice
747
isNew = false;
748     if(telWorkVoice == null) {
749         telWorkVoice = new DataproviderObject(contact.path().getDescendant(new String JavaDoc[]{"address", UUIDs.getGenerator().next().toString()}));
750         telWorkVoice.values(SystemAttributes.OBJECT_CLASS).add("org:opencrx:kernel:account1:PhoneNumber");
751         telWorkVoice.values("usage").add(USAGE_PHONE_WORK);
752         isNew = true;
753     }
754     if(this.updatePhoneNumber(telWorkVoice, (String JavaDoc)vcard.get("TEL;WORK;VOICE"))) {
755         if(isNew) {
756             this.delegation.addCreateRequest(telWorkVoice);
757             report.add("Create phone number");
758         }
759         else if(!createOnly) {
760             report.add("Update phone number");
761         }
762     }
763     // update telHomeFax
764
isNew = false;
765     if(telHomeFax == null) {
766         telHomeFax = new DataproviderObject(contact.path().getDescendant(new String JavaDoc[]{"address", UUIDs.getGenerator().next().toString()}));
767         telHomeFax.values(SystemAttributes.OBJECT_CLASS).add("org:opencrx:kernel:account1:PhoneNumber");
768         telHomeFax.values("usage").add(USAGE_FAX_HOME);
769         isNew = true;
770     }
771     if(this.updatePhoneNumber(telHomeFax, (String JavaDoc)vcard.get("TEL;HOME;FAX"))) {
772         if(isNew) {
773             this.delegation.addCreateRequest(telHomeFax);
774             report.add("Create phone number");
775         }
776         else if(!createOnly) {
777             report.add("Update phone number");
778         }
779     }
780     // update telWorkFax
781
isNew = false;
782     if(telWorkFax == null) {
783         telWorkFax = new DataproviderObject(contact.path().getDescendant(new String JavaDoc[]{"address", UUIDs.getGenerator().next().toString()}));
784         telWorkFax.values(SystemAttributes.OBJECT_CLASS).add("org:opencrx:kernel:account1:PhoneNumber");
785         telWorkFax.values("usage").add(USAGE_FAX_WORK);
786         isNew = true;
787     }
788     if(this.updatePhoneNumber(telWorkFax, (String JavaDoc)vcard.get("TEL;WORK;FAX"))) {
789         if(isNew) {
790             this.delegation.addCreateRequest(telWorkFax);
791             report.add("Create phone number");
792         }
793         else if(!createOnly) {
794             report.add("Update phone number");
795         }
796     }
797     // update telCellVoice
798
isNew = false;
799     if(telCellVoice == null) {
800         telCellVoice = new DataproviderObject(contact.path().getDescendant(new String JavaDoc[]{"address", UUIDs.getGenerator().next().toString()}));
801         telCellVoice.values(SystemAttributes.OBJECT_CLASS).add("org:opencrx:kernel:account1:PhoneNumber");
802         telCellVoice.values("usage").add(USAGE_MOBILE);
803         isNew = true;
804     }
805     if(this.updatePhoneNumber(telCellVoice, (String JavaDoc)vcard.get("TEL;CELL;VOICE"))) {
806         if(isNew) {
807             this.delegation.addCreateRequest(telCellVoice);
808             report.add("Create phone number");
809         }
810         else if(!createOnly) {
811             report.add("Update phone number");
812         }
813     }
814     // update urlHome
815
isNew = false;
816     if(urlHome == null) {
817         urlHome = new DataproviderObject(contact.path().getDescendant(new String JavaDoc[]{"address", UUIDs.getGenerator().next().toString()}));
818         urlHome.values(SystemAttributes.OBJECT_CLASS).add("org:opencrx:kernel:account1:WebAddress");
819         urlHome.values("usage").add(USAGE_WEB_HOME);
820         isNew = true;
821     }
822     if(this.updateWebAddress(urlHome, (String JavaDoc)vcard.get("URL;HOME"))) {
823         if(isNew) {
824             this.delegation.addCreateRequest(urlHome);
825             report.add("Create web address");
826         }
827         else if(!createOnly) {
828             report.add("Update web address");
829         }
830     }
831     // update urlWork
832
isNew = false;
833     if(urlWork == null) {
834         urlWork = new DataproviderObject(contact.path().getDescendant(new String JavaDoc[]{"address", UUIDs.getGenerator().next().toString()}));
835         urlWork.values(SystemAttributes.OBJECT_CLASS).add("org:opencrx:kernel:account1:WebAddress");
836         urlWork.values("usage").add(USAGE_WEB_WORK);
837         isNew = true;
838     }
839     if(this.updateWebAddress(urlWork, (String JavaDoc)vcard.get("URL;WORK"))) {
840         if(isNew) {
841             this.delegation.addCreateRequest(urlWork);
842             report.add("Create web address");
843         }
844         else if(!createOnly) {
845             report.add("Update web address");
846         }
847     }
848     // update emailPrimary
849
isNew = false;
850     if(emailPrimary == null) {
851         emailPrimary = new DataproviderObject(contact.path().getDescendant(new String JavaDoc[]{"address", UUIDs.getGenerator().next().toString()}));
852         emailPrimary.values(SystemAttributes.OBJECT_CLASS).add("org:opencrx:kernel:account1:EMailAddress");
853         emailPrimary.values("usage").add(USAGE_EMAIL_PRIMARY);
854         isNew = true;
855     }
856     s = vcard.get("EMAIL;PREF;INTERNET") != null ? (String JavaDoc)vcard.get("EMAIL;PREF;INTERNET") : (String JavaDoc)vcard.get("EMAIL;INTERNET");
857     if(this.updateEMailAddress(emailPrimary, s)) {
858       if(isNew) {
859           this.delegation.addCreateRequest(emailPrimary);
860           report.add("Create email address");
861       }
862       else if(!createOnly) {
863           report.add("Update email address");
864       }
865     }
866     return contact;
867   }
868   
869   //-------------------------------------------------------------------------
870
// Members
871
//-------------------------------------------------------------------------
872
public static final String JavaDoc MIME_TYPE = "text/x-vcard";
873   public static final int MIME_TYPE_CODE = 3;
874   
875   static private final Short JavaDoc USAGE_EMAIL_PRIMARY = new Short JavaDoc((short)300);
876   static private final Short JavaDoc USAGE_POSTAL_HOME = new Short JavaDoc((short)400);
877   static private final Short JavaDoc USAGE_POSTAL_WORK = new Short JavaDoc((short)500);
878   static private final Short JavaDoc USAGE_PHONE_PRIMARY = new Short JavaDoc((short)300);
879   static private final Short JavaDoc USAGE_PHONE_HOME =new Short JavaDoc((short)400);
880   static private final Short JavaDoc USAGE_PHONE_WORK = new Short JavaDoc((short)500);
881   static private final Short JavaDoc USAGE_FAX_HOME = new Short JavaDoc((short)430);
882   static private final Short JavaDoc USAGE_FAX_WORK = new Short JavaDoc((short)530);
883   static private final Short JavaDoc USAGE_MOBILE = new Short JavaDoc((short)200);
884   static private final Short JavaDoc USAGE_WEB_HOME = new Short JavaDoc((short)400);
885   static private final Short JavaDoc USAGE_WEB_WORK = new Short JavaDoc((short)500);
886     
887   private final OpenCrxKernel_1 plugin;
888   private final RequestCollection delegation;
889   private final Codes codes;
890     
891 }
892
893 //--- End of File -----------------------------------------------------------
894
Popular Tags