18 package org.turro.vcard;
20 import a_vcard.android.provider.Contacts;
21 import a_vcard.android.syncml.pim.PropertyNode;
22 import a_vcard.android.syncml.pim.vcard.ContactStruct;
23 import a_vcard.android.syncml.pim.vcard.VCardComposer;
24 import a_vcard.android.syncml.pim.vcard.VCardException;
25 import java.io.IOException;
26 import java.io.StringWriter;
27 import java.util.Properties;
28 import org.turro.contacts.*;
29 import org.turro.vcard.properties.Property;
30 import org.turro.vcard.properties.PropertyTag;
31 import org.turro.vcard.properties.Version;
44 this.contact = contact;
49 generateVCard(properties);
55 VCardComposer composer =
new VCardComposer();
57 ContactStruct contact1 =
new ContactStruct();
58 contact1.name = contact.
getName();
59 contact1.addExtension(
new PropertyNode(
"ID", contact.
getId(),
null,
null,
null,
null,
null));
60 contact1.addExtension(
new PropertyNode(
"GID", contact.
getGlobalIdentifier(),
null,
null,
null,
null,
null));
63 if(!address.isEmpty()) {
64 contact1.addContactmethod(Contacts.KIND_POSTAL, Contacts.ContactMethods.TYPE_CUSTOM,
65 getAddressString(address), address.getDescription(),
66 "Fiscal".equals(address.getDescription()));
71 if(!connector.isEmpty()) {
72 if(matchValue(properties,
"Telephone",
"Telephone", connector.getDescription())) {
73 contact1.addPhone(Contacts.Phones.TYPE_WORK, connector.getValue(),
null,
true);
74 }
else if(matchValue(properties,
"Cell",
"Cell", connector.getDescription())) {
75 contact1.addPhone(Contacts.Phones.TYPE_MOBILE, connector.getValue(),
null,
false);
76 }
else if(matchValue(properties,
"Fax",
"Fax", connector.getDescription())) {
77 contact1.addPhone(Contacts.Phones.TYPE_FAX_WORK, connector.getValue(),
null,
false);
78 }
else if(matchValue(properties,
"Email",
"Email", connector.getDescription())) {
79 contact1.addContactmethod(Contacts.KIND_EMAIL, Contacts.ContactMethods.TYPE_WORK, connector.getValue(),
null,
true);
80 }
else if(matchValue(properties,
"Source",
"Web", connector.getDescription())) {
81 contact1.addWeb(connector.getValue());
87 if(!relation.isEmpty()) {
88 contact1.addOrganization(Contacts.KIND_ORGANIZATION, relation.getBusiness().getName(), relation.getFormattedDescription(),
false);
93 if(!comment.isEmpty()) {
94 contact1.notes.add(comment.getComment());
98 StringWriter writer =
new StringWriter();
99 String vcardString = composer.createVCard(contact1, VCardComposer.VERSION_VCARD30_INT);
100 writer.write(vcardString);
103 return writer.toString();
106 private String getAddressString(
Address address) {
107 return ";;" + blankIfNull(address.
getStreet()) +
";" +
108 blankIfNull(address.
getCity()) +
";" +
114 private void generateVCard(Properties properties) {
115 vCard =
new VCard(Version.V21);
117 vCard.
addProperty(
new Property(PropertyTag.VCP_ID, vCard, contact.
getId()));
124 Property p =
new Property(PropertyTag.VCP_ADR, vCard);
125 p.setValues(
new String[] {
128 blankIfNull(address.getStreet()),
129 blankIfNull(address.getCity()),
130 blankIfNull(address.getProvince()),
131 blankIfNull(address.getZipCode()),
132 blankIfNull(address.getState())
134 p.addType(translateGroup(properties, address.getDescription()));
140 if(matchValue(properties,
"Telephone",
"Telephone", connector.getDescription())) {
141 p =
new Property(PropertyTag.VCP_TEL, vCard);
142 p.setValue(connector.getValue());
144 }
else if(matchValue(properties,
"Cell",
"Cell", connector.getDescription())) {
145 p =
new Property(PropertyTag.VCP_TEL, vCard);
147 p.setValue(connector.getValue());
149 }
else if(matchValue(properties,
"Fax",
"Fax", connector.getDescription())) {
150 p =
new Property(PropertyTag.VCP_TEL, vCard);
152 p.setValue(connector.getValue());
154 }
else if(matchValue(properties,
"Email",
"Email", connector.getDescription())) {
155 p =
new Property(PropertyTag.VCP_EMAIL, vCard);
156 p.setValue(connector.getValue());
158 }
else if(matchValue(properties,
"Source",
"Web", connector.getDescription())) {
159 p =
new Property(PropertyTag.VCP_SOURCE, vCard);
160 p.setValue(connector.getValue());
166 vCard.
addProperty(
new Property(PropertyTag.VCP_NOTE, vCard, comment.getComment()));
170 vCard.
addProperty(
new Property(PropertyTag.VCP_TITLE, vCard,
171 relation.getFormattedDescription() +
">" +
172 relation.getBusiness().getName()));
177 private static String translateGroup(Properties properties, String description) {
178 if(matchValue(properties,
"Work",
"Fiscal", description)) {
180 }
else if(matchValue(properties,
"Home",
"Postal", description)) {
186 private static boolean matchValue(Properties properties, String key, String defaultValue, String value) {
187 return properties.getProperty(key, defaultValue).toLowerCase()
188 .matches(
".*(,|^)" + value.toLowerCase() +
"(,|$).*");
191 private String blankIfNull(String value) {
192 return value ==
null ?
"" : value;
void addProperty(Property property)