BrightSide Workbench Full Report + Source Code
ExportToVCard.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.util;
19 
20 import a_vcard.android.syncml.pim.vcard.VCardException;
21 import java.awt.image.BufferedImage;
22 import java.io.File;
23 import java.io.IOException;
24 import java.net.URLEncoder;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Properties;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30 import org.turro.barcode.Barcode;
31 import org.turro.contacts.Contact;
32 import org.turro.elephant.context.ElephantContext;
33 import org.turro.elephant.impl.util.FileUtil;
34 import org.turro.vcard.ContactToVCard;
35 import org.turro.vcard.VCard;
36 import org.turro.vcard.db.VCardFiles;
37 import org.turro.vcard.db.VCardList;
38 import org.turro.vcard.properties.Version;
39 
44 public class ExportToVCard {
45 
46  private List<Contact> contacts;
47  private Version version;
48 
49  public ExportToVCard(List<Contact> contacts) {
50  this.contacts = contacts;
51  }
52 
53  public ExportToVCard(Contact contact) {
54  contacts = new ArrayList<Contact>();
55  contacts.add(contact);
56  }
57 
58  public File export() throws IOException {
59  Properties properties = new Properties();
60  properties.load(FileUtil.getBufferedFile(
61  new File(ElephantContext.getRealPath("/WEB-INF/elephant/conf/vCard.properties"))));
62  VCardList list = new VCardList();
63  for(Contact c : contacts) {
64  VCard vCard = new ContactToVCard(c).getVCard(properties);
65  if(vCard == null) continue;
66  list.add(vCard);
67  }
68  return VCardFiles.saveVCards(list);
69  }
70 
71  public String getEncodedString() throws IOException {
72  Properties properties = new Properties();
73  properties.load(FileUtil.getBufferedFile(
74  new File(ElephantContext.getRealPath("/WEB-INF/elephant/conf/vCard.properties"))));
75  String result = "";
76  for(Contact c : contacts) {
77  VCard vCard = new ContactToVCard(c).getVCard(properties);
78  if(vCard == null) continue;
79  result += vCard.getVCard();
80  }
81  return URLEncoder.encode(result, "UTF-8");
82  }
83 
84  public String getAndroidString() throws IOException {
85  Properties properties = new Properties();
86  properties.load(FileUtil.getBufferedFile(
87  new File(ElephantContext.getRealPath("/WEB-INF/elephant/conf/vCard.properties"))));
88  String result = "";
89  for(Contact c : contacts) {
90  try {
91  result += new ContactToVCard(c).generateAndroidVCard(properties);
92  } catch (VCardException ex) {
93  Logger.getLogger(ExportToVCard.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
94  }
95  }
96  return result;
97  }
98 
99  public BufferedImage getQRCode() {
100  try {
101  Barcode qr = new Barcode();
102  return qr.encodeQrCode(getAndroidString(), 300);
103  } catch (IOException ex) {
104  Logger.getLogger(ExportToVCard.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
105  return null;
106  }
107  }
108 
109 }
static BufferedReader getBufferedFile(String folder, String file)
Definition: FileUtil.java:146
String generateAndroidVCard(Properties properties)
VCard getVCard(Properties properties)
String getVCard()
Definition: VCard.java:45
static File saveVCards(VCardList list)
ExportToVCard(List< Contact > contacts)