BrightSide Workbench Full Report + Source Code
ContactToVCard.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.vcard;
19 
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;
32 
33 
38 public class ContactToVCard {
39 
40  private Contact contact;
41  private VCard vCard;
42 
43  public ContactToVCard(Contact contact) {
44  this.contact = contact;
45  }
46 
47  public VCard getVCard(Properties properties) {
48  if(vCard == null) {
49  generateVCard(properties);
50  }
51  return vCard;
52  }
53 
54  public String generateAndroidVCard(Properties properties) throws VCardException, IOException {
55  VCardComposer composer = new VCardComposer();
56 
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));
61 
62  for(Address address : contact.getAddresses()) {
63  if(!address.isEmpty()) {
64  contact1.addContactmethod(Contacts.KIND_POSTAL, Contacts.ContactMethods.TYPE_CUSTOM,
65  getAddressString(address), address.getDescription(),
66  "Fiscal".equals(address.getDescription()));
67  }
68  }
69 
70  for(Connector connector : contact.getConnectors()) {
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());
82  }
83  }
84  }
85 
86  for(BusinessRelation relation : contact.getBusinessSet()) {
87  if(!relation.isEmpty()) {
88  contact1.addOrganization(Contacts.KIND_ORGANIZATION, relation.getBusiness().getName(), relation.getFormattedDescription(), false);
89  }
90  }
91 
92  for(Comment comment : contact.getComments()) {
93  if(!comment.isEmpty()) {
94  contact1.notes.add(comment.getComment());
95  }
96  }
97 
98  StringWriter writer = new StringWriter();
99  String vcardString = composer.createVCard(contact1, VCardComposer.VERSION_VCARD30_INT);
100  writer.write(vcardString);
101  writer.write("\n");
102  writer.close();
103  return writer.toString();
104  }
105 
106  private String getAddressString(Address address) {
107  return ";;" + blankIfNull(address.getStreet()) + ";" +
108  blankIfNull(address.getCity()) + ";" +
109  blankIfNull(address.getProvince()) + ";" +
110  blankIfNull(address.getZipCode()) + ";" +
111  blankIfNull(address.getState());
112  }
113 
114  private void generateVCard(Properties properties) {
115  vCard = new VCard(Version.V21);
116 
117  vCard.addProperty(new Property(PropertyTag.VCP_ID, vCard, contact.getId()));
118  vCard.addProperty(new Property(PropertyTag.VCP_GID, vCard, contact.getGlobalIdentifier()));
119 
120  vCard.addProperty(new Property(PropertyTag.VCP_FN, vCard, contact.getName()));
121  vCard.addProperty(new Property(PropertyTag.VCP_N, vCard, contact.getName()));
122 
123  for(Address address : contact.getAddresses()) {
124  Property p = new Property(PropertyTag.VCP_ADR, vCard);
125  p.setValues(new String[] {
126  "",
127  "",
128  blankIfNull(address.getStreet()),
129  blankIfNull(address.getCity()),
130  blankIfNull(address.getProvince()),
131  blankIfNull(address.getZipCode()),
132  blankIfNull(address.getState())
133  });
134  p.addType(translateGroup(properties, address.getDescription()));
135  vCard.addProperty(p);
136  }
137 
138  for(Connector connector : contact.getConnectors()) {
139  Property p;
140  if(matchValue(properties, "Telephone", "Telephone", connector.getDescription())) {
141  p = new Property(PropertyTag.VCP_TEL, vCard);
142  p.setValue(connector.getValue());
143  vCard.addProperty(p);
144  } else if(matchValue(properties, "Cell", "Cell", connector.getDescription())) {
145  p = new Property(PropertyTag.VCP_TEL, vCard);
146  p.addType("CELL");
147  p.setValue(connector.getValue());
148  vCard.addProperty(p);
149  } else if(matchValue(properties, "Fax", "Fax", connector.getDescription())) {
150  p = new Property(PropertyTag.VCP_TEL, vCard);
151  p.addType("FAX");
152  p.setValue(connector.getValue());
153  vCard.addProperty(p);
154  } else if(matchValue(properties, "Email", "Email", connector.getDescription())) {
155  p = new Property(PropertyTag.VCP_EMAIL, vCard);
156  p.setValue(connector.getValue());
157  vCard.addProperty(p);
158  } else if(matchValue(properties, "Source", "Web", connector.getDescription())) {
159  p = new Property(PropertyTag.VCP_SOURCE, vCard);
160  p.setValue(connector.getValue());
161  vCard.addProperty(p);
162  }
163  }
164 
165  for(Comment comment : contact.getComments()) {
166  vCard.addProperty(new Property(PropertyTag.VCP_NOTE, vCard, comment.getComment()));
167  }
168 
169  for(BusinessRelation relation : contact.getBusinessSet()) {
170  vCard.addProperty(new Property(PropertyTag.VCP_TITLE, vCard,
171  relation.getFormattedDescription() + ">" +
172  relation.getBusiness().getName()));
173  }
174 
175  }
176 
177  private static String translateGroup(Properties properties, String description) {
178  if(matchValue(properties, "Work", "Fiscal", description)) {
179  return "WORK";
180  } else if(matchValue(properties, "Home", "Postal", description)) {
181  return "HOME";
182  }
183  return description;
184  }
185 
186  private static boolean matchValue(Properties properties, String key, String defaultValue, String value) {
187  return properties.getProperty(key, defaultValue).toLowerCase()
188  .matches(".*(,|^)" + value.toLowerCase() + "(,|$).*");
189  }
190 
191  private String blankIfNull(String value) {
192  return value == null ? "" : value;
193  }
194 
195 }
Set< Comment > getComments()
Definition: Contact.java:415
Set< Address > getAddresses()
Definition: Contact.java:355
Set< Connector > getConnectors()
Definition: Contact.java:367
String generateAndroidVCard(Properties properties)
VCard getVCard(Properties properties)
void addProperty(Property property)
Definition: VCard.java:60