|
From: Dieter W. <di...@wi...> - 2003-02-18 22:35:15
|
Hi Alex,
> do you have any example on how to use the contact API? For me it is much
> easier to understand if I see some line of codes.
I'll attach you a small application class, see below.
> Let's say I have an empty address book and want to create:
An "address book" in this sense does not exist in the library (yet?).
> - a "group" for private contacts
> - a "group" for bussiness contacts
Well, a group is probably not what you are really looking for. See the
example. I add you the code though.
> - a contact for "Frank Miller" inside of bussiness
> Can you provide the code for this (or any similar example)?
Here you go:
import net.wimpi.pim.Pim;
import net.wimpi.pim.contact.io.ContactMarshaller;
import net.wimpi.pim.contact.io.vcard.vCardMarshaller;
import net.wimpi.pim.contact.model.*;
import net.wimpi.pim.factory.ContactModelFactory;
public class vCardTest {
public static void main(String[] args) {
try {
ContactModelFactory cmf =3D Pim.getContactModelFactory();
Contact contact =3D cmf.createContact();
contact.addCategory("Business");
//a personal identity
PersonalIdentity pid =3D cmf.createPersonalIdentity();
pid.setFormattedName("Frank Miller");
pid.setFirstname("Frank");
pid.setLastname("Miller");
contact.setPersonalIdentity(pid);
//an address
Address addr =3D cmf.createAddress();
addr.setStreet("Somestreet");
addr.setCity("Somecity");
addr.setPostalCode("55555");
addr.setCountry("United States");
addr.setWork(true);
contact.addAddress(addr);
//some communications
Communications comm =3D cmf.createCommunications();
contact.setCommunications(comm);
//a phone number
PhoneNumber number =3D cmf.createPhoneNumber();
number.setNumber("+1(55)555-4575");
number.setWork(true);
comm.addPhoneNumber(number);
//an email address
EmailAddress email =3D new EmailAddressImpl();
email.setAddress("som...@ao...");
comm.addEmailAddress(email);
//and now write the vCard to standard out
ContactMarshaller marshaller =3D new vCardMarshaller();
marshaller.marshallContact(System.out, contact);
} catch (Exception ex) {
ex.printStackTrace();
}
}//main
}//class vCardTest
Here is what you should get when you execute it:
[host:=97/development/java/jpim] wimpi% java -cp build/jpim.jar:. vCardTest
BEGIN:VCARD
VERSION:3.0
N:Miller;Frank;;;
FN:Frank Miller
ADR;TYPE=3DWORK:;;Somestreet;Somecity;;55555;United States
TEL;TYPE=3DWORK:+1(55)555-4575
EMAIL;TYPE=3DINTERNET:som...@ao...
CATEGORIES:Business
END:VCARD
For a group you can use:
ContactGroup group =3D cmf.createContactGroup();
group.setName("Business");
group.addContact(contact);
However, this is rather a container then anything else at the moment.
Note that the factory is there to allow any type of actual implementations.
We just use the interfaces....
Hope that helps,
Dieter
|