BrightSide Workbench Full Report + Source Code
VCardToContact.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 java.util.List;
21 import org.turro.contacts.*;
22 import org.turro.contacts.util.MostUsed;
23 import org.turro.elephant.context.ElephantProperties;
24 import org.turro.util.PhraseBuilder;
25 import org.turro.vcard.properties.Property;
26 import org.turro.vcard.properties.PropertyTag;
27 
32 public class VCardToContact {
33 
34  private Contact contact;
35  private VCard vCard;
36 
37  public VCardToContact(VCard vCard) {
38  this.vCard = vCard;
39  }
40 
41  public Contact getContact() {
42  if(contact == null) {
43  generateContact();
44  }
45  return contact;
46  }
47 
48  private void generateContact() {
49 
50  contact = new Contact();
51 
52  Property p;
53 
54  p = vCard.getProperty(PropertyTag.VCP_GID);
55  if(p != null) {
56  contact.setGlobalIdentifier(p.getValue(0));
57  }
58 
59  p = vCard.getProperty(PropertyTag.VCP_N);
60  PhraseBuilder pb = new PhraseBuilder();
61  pb.addWord(p.getValue(1));
62  pb.addWord(p.getValue(2));
63  pb.addWord(p.getValue(0));
64  contact.setName(pb.toString());
65  contact.getComplexName().setFull(pb.toString());
66 
67  for(Property prop : vCard.getProperties(PropertyTag.VCP_ADR)) {
68  Address caddress = new Address();
69  caddress.setContact(contact);
70  caddress.setStreet(prop.getValue(2));
71  caddress.setCity(prop.getValue(3));
72  caddress.setProvince(prop.getValue(4));
73  caddress.setZipCode(prop.getValue(5));
74  caddress.setState(prop.getValue(6));
75  caddress.setDescription(translateGroup(prop.getTypes()));
76  contact.getAddresses().add(caddress);
77  }
78 
79  for(Property prop : vCard.getProperties(PropertyTag.VCP_EMAIL)) {
80  Connector connector = new Connector();
81  connector.setContact(contact);
82  connector.setDescription(getMostUsedConnector("Email"));
83  connector.setValue(prop.getValue());
84  contact.getConnectors().add(connector);
85  }
86 
87  for(Property prop : vCard.getProperties(PropertyTag.VCP_TEL)) {
88  Connector connector = new Connector();
89  connector.setContact(contact);
90  if(prop.getTypes().contains("CELL")) {
91  connector.setDescription(getMostUsedConnector("Cell"));
92  } else if(prop.getTypes().contains("FAX")) {
93  connector.setDescription(getMostUsedConnector("Fax"));
94  } else {
95  connector.setDescription(getMostUsedConnector("Telephone"));
96  }
97  connector.setValue(prop.getValue());
98  contact.getConnectors().add(connector);
99  }
100 
101  for(Property prop : vCard.getProperties(PropertyTag.VCP_SOURCE)) {
102  Connector connector = new Connector();
103  connector.setContact(contact);
104  connector.setDescription(getMostUsedConnector("Web"));
105  connector.setValue(prop.getValue());
106  contact.getConnectors().add(connector);
107  }
108 
109  for(Property prop : vCard.getProperties(PropertyTag.VCP_NOTE)) {
110  Comment comment = new Comment();
111  comment.setContact(contact);
112  comment.setComment(prop.getValue());
113  contact.getComments().add(comment);
114  }
115 
116  for(Property prop : vCard.getProperties(PropertyTag.VCP_TITLE)) {
117  String v[] = prop.getValue().split("\\>");
118  BusinessRelation relation = new BusinessRelation();
119  relation.setContact(contact);
120  if(v.length > 0) {
121  relation.setDescription(v[0]);
122  }
123  if(v.length > 1) {
124  relation.setRelated(v[1]);
125  }
126  contact.getBusinessRelations().add(relation);
127  }
128 
129  }
130 
131  public static String translateGroup(List<String> types) {
132  if(matchValue("Work", "Fiscal", types)) {
133  return getMostUsedAddress("Work");
134  } else if(matchValue("Home", "Postal", types)) {
135  return getMostUsedAddress("Home");
136  }
137  return types.isEmpty() ? "***" : types.get(0);
138  }
139 
140  public static boolean matchValue(String key, String defaultValue, List<String> types) {
141  for(String p : types) {
142  if(ElephantProperties.getContextProperty("vCard", key, defaultValue).toLowerCase()
143  .matches(".*(,|^)" + p.toLowerCase() + "(,|$).*")) {
144  return true;
145  }
146  }
147  return false;
148  }
149 
150  public static String getMostUsedConnector(String key) {
151  return MostUsed.getConnector(
152  ElephantProperties.getContextProperty("vCard", key, key),
153  key);
154  }
155 
156  public static String getMostUsedAddress(String key) {
157  return MostUsed.getAddress(
158  ElephantProperties.getContextProperty("vCard", key, key),
159  key);
160  }
161 
162 }
void setProvince(String province)
Definition: Address.java:130
void setState(String state)
Definition: Address.java:138
void setZipCode(String zipCode)
Definition: Address.java:114
void setCity(String city)
Definition: Address.java:122
void setStreet(String street)
Definition: Address.java:106
void setDescription(String description)
Definition: Address.java:146
void setContact(org.turro.contacts.Contact contact)
Definition: Address.java:170
void setDescription(String description)
void setContact(Contact contact)
Definition: Comment.java:93
void setComment(String comment)
Definition: Comment.java:85
void setContact(org.turro.contacts.Contact contact)
Definition: Connector.java:103
void setDescription(String description)
Definition: Connector.java:87
void setValue(String value)
Definition: Connector.java:95
Set< BusinessRelation > getBusinessRelations()
Definition: Contact.java:379
void setName(String name)
Definition: Contact.java:217
Set< Comment > getComments()
Definition: Contact.java:415
ComplexName getComplexName()
Definition: Contact.java:152
void setGlobalIdentifier(String globalIdentifier)
Definition: Contact.java:271
Set< Address > getAddresses()
Definition: Contact.java:355
Set< Connector > getConnectors()
Definition: Contact.java:367
static String getConnector(String choices, String defaultChoice)
Definition: MostUsed.java:31
static String getAddress(String choices, String defaultChoice)
Definition: MostUsed.java:43
static String getContextProperty(String context, String property)
static String translateGroup(List< String > types)
static String getMostUsedConnector(String key)
static String getMostUsedAddress(String key)
static boolean matchValue(String key, String defaultValue, List< String > types)
List< Property > getProperties(PropertyTag tag)
Definition: VCard.java:73
Property getProperty(PropertyTag tag)
Definition: VCard.java:64