BrightSide Workbench Full Report + Source Code
CSVToContact.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.Arrays;
21 import org.turro.contacts.*;
22 import org.turro.csv.CSVEntry;
23 import org.turro.util.PhraseBuilder;
24 
29 public class CSVToContact {
30 
31  private Contact contact;
32  private CSVEntry entry;
33 
34  public CSVToContact(CSVEntry v) {
35  entry = v;
36  }
37 
38  public Contact getContact() {
39  if(contact == null) {
40  generateContact();
41  }
42  return contact;
43  }
44 
45  private void generateContact() {
46 
47  contact = new Contact();
48 
49  if(entry.getList().getHeader().hasPrefix("X-GID")) {
50  contact.setGlobalIdentifier(entry.getField("X-GID"));
51  }
52  if(entry.getList().getHeader().hasPrefix("FN")) {
53  contact.setName(entry.getField("FN"));
54  contact.getComplexName().setFull(entry.getField("FN"));
55  }
56  if(entry.getList().getHeader().hasPrefix("N:")) {
57  PhraseBuilder pb = new PhraseBuilder();
58  pb.addWord(entry.getField("N:First"));
59  pb.addWord(entry.getField("N:Middle"));
60  pb.addWord(entry.getField("N:Last"));
61  contact.setName(pb.toString());
62  contact.getComplexName().setFull(pb.toString());
63  }
64  if(entry.getList().getHeader().hasPrefix("ADR:WORK:")) {
65  Address caddress = new Address();
66  caddress.setContact(contact);
67  caddress.setStreet(entry.getField("ADR:WORK:Street"));
68  caddress.setCity(entry.getField("ADR:WORK:City"));
69  caddress.setProvince(entry.getField("ADR:WORK:Province"));
70  caddress.setZipCode(entry.getField("ADR:WORK:ZipCode"));
71  caddress.setState(entry.getField("ADR:WORK:State"));
72  caddress.setDescription(VCardToContact.translateGroup(Arrays.asList(new String[] {"WORK"})));
73  contact.getAddresses().add(caddress);
74  }
75  if(entry.getList().getHeader().hasPrefix("ADR:HOME:")) {
76  Address caddress = new Address();
77  caddress.setContact(contact);
78  caddress.setStreet(entry.getField("ADR:HOME:Street"));
79  caddress.setCity(entry.getField("ADR:HOME:City"));
80  caddress.setProvince(entry.getField("ADR:HOME:Province"));
81  caddress.setZipCode(entry.getField("ADR:HOME:ZipCode"));
82  caddress.setState(entry.getField("ADR:HOME:State"));
83  caddress.setDescription(VCardToContact.translateGroup(Arrays.asList(new String[] {"HOME"})));
84  contact.getAddresses().add(caddress);
85  }
86  if(entry.getList().getHeader().hasPrefix("EMAIL")) {
87  Connector connector = new Connector();
88  connector.setContact(contact);
89  connector.setDescription(VCardToContact.getMostUsedConnector("Email"));
90  connector.setValue(entry.getField("EMAIL"));
91  contact.getConnectors().add(connector);
92  }
93  if(entry.getList().getHeader().hasPrefix("TEL")) {
94  Connector connector = new Connector();
95  connector.setContact(contact);
96  connector.setDescription(VCardToContact.getMostUsedConnector("Telephone"));
97  connector.setValue(entry.getField("TEL"));
98  contact.getConnectors().add(connector);
99  }
100  if(entry.getList().getHeader().hasPrefix("CELL")) {
101  Connector connector = new Connector();
102  connector.setContact(contact);
103  connector.setDescription(VCardToContact.getMostUsedConnector("Cell"));
104  connector.setValue(entry.getField("CELL"));
105  contact.getConnectors().add(connector);
106  }
107  if(entry.getList().getHeader().hasPrefix("FAX")) {
108  Connector connector = new Connector();
109  connector.setContact(contact);
110  connector.setDescription(VCardToContact.getMostUsedConnector("Fax"));
111  connector.setValue(entry.getField("FAX"));
112  contact.getConnectors().add(connector);
113  }
114  if(entry.getList().getHeader().hasPrefix("WEB")) {
115  Connector connector = new Connector();
116  connector.setContact(contact);
117  connector.setDescription(VCardToContact.getMostUsedConnector("Web"));
118  connector.setValue(entry.getField("WEB"));
119  contact.getConnectors().add(connector);
120  }
121  if(entry.getList().getHeader().hasPrefix("NOTE")) {
122  Comment comment = new Comment();
123  comment.setContact(contact);
124  comment.setComment(entry.getField("NOTE"));
125  contact.getComments().add(comment);
126  }
127  if(entry.getList().getHeader().hasPrefix("TITLE")) {
128  String v[] = entry.getField("TITLE").split("\\>");
129  BusinessRelation relation = new BusinessRelation();
130  relation.setContact(contact);
131  if(v.length > 0) {
132  relation.setDescription(v[0]);
133  }
134  if(v.length > 1) {
135  relation.setRelated(v[1]);
136  }
137  contact.getBusinessRelations().add(relation);
138  }
139  if(entry.getList().getHeader().hasPrefix("CON:ID")) {
140  Connector connector = new Connector();
141  connector.setContact(contact);
142  connector.setDescription("ID");
143  connector.setValue(entry.getField("CON:ID"));
144  contact.getConnectors().add(connector);
145  }
146 
147  }
148 
149 }
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