BrightSide Workbench Full Report + Source Code
VCardFiles.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.db;
19 
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.FilenameFilter;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.io.OutputStreamWriter;
28 import java.io.Writer;
29 import java.util.List;
30 import org.turro.csv.CSVList;
31 import org.turro.csv.CSVParser;
32 import org.turro.elephant.context.Application;
33 import org.turro.elephant.context.ElephantContext;
34 import org.turro.vcard.VCard;
35 
40 public class VCardFiles {
41 
42  public static final String
43  VCARD_IMPORT_FOLDER = "/WEB-INF/vCard/in",
44  VCARD_EXPORT_FOLDER = "/WEB-INF/vCard/out";
45 
46  public static File getImportFolder() {
47  File root = new File(ElephantContext.getRealPath(VCARD_IMPORT_FOLDER));
48  if(!root.exists()) root.mkdir();
49  return root;
50  }
51 
52  public static File[] getFiles() {
53  File root = new File(ElephantContext.getRealPath(VCARD_IMPORT_FOLDER));
54  if(!root.exists()) root.mkdir();
55  return root.listFiles(new FilenameFilter() {
56  @Override
57  public boolean accept(File dir, String name) {
58  return name.endsWith(".vcf");
59  }
60  });
61  }
62 
63  public static File[] getCSVs() {
64  File root = new File(ElephantContext.getRealPath(VCARD_IMPORT_FOLDER));
65  if(!root.exists()) root.mkdir();
66  return root.listFiles(new FilenameFilter() {
67  @Override
68  public boolean accept(File dir, String name) {
69  return name.endsWith(".csv");
70  }
71  });
72  }
73 
74  public static File[] getCSVRels() {
75  File root = new File(ElephantContext.getRealPath(VCARD_IMPORT_FOLDER));
76  if(!root.exists()) root.mkdir();
77  return root.listFiles(new FilenameFilter() {
78  @Override
79  public boolean accept(File dir, String name) {
80  return name.endsWith(".csvrel");
81  }
82  });
83  }
84 
85  public static VCardList loadVCards() throws IOException {
86  File files[] = getFiles();
87  VCardList vl = new VCardList();
88  for(File f : files) {
89  BufferedReader in = new BufferedReader(
90  new InputStreamReader(new FileInputStream(f)));
91  List<VCard> l = new VCardParser(in).getVCards();
92  if(l != null) {
93  vl.addAll(l);
94  }
95  in.close();
96  }
97  return vl;
98  }
99 
100  public static CSVList loadCSVs() throws IOException {
101  File files[] = getCSVs();
102  CSVList cl = new CSVList();
103  for(File f : files) {
104  BufferedReader in = new BufferedReader(
105  new InputStreamReader(new FileInputStream(f)));
106  cl = new CSVParser(in).getCSVs();
107  in.close();
108  // Can't read more than a single CVS at time, fields may differ
109  break;
110  }
111  return cl;
112  }
113 
114  public static CSVList loadCSVRels() throws IOException {
115  File files[] = getCSVRels();
116  CSVList cl = new CSVList();
117  for(File f : files) {
118  BufferedReader in = new BufferedReader(
119  new InputStreamReader(new FileInputStream(f)));
120  cl = new CSVParser(in).getCSVs();
121  in.close();
122  // Can't read more than a single CVS at time, fields may differ
123  break;
124  }
125  return cl;
126  }
127 
128  public static File saveVCards(VCardList list) throws IOException {
129  File root = new File(ElephantContext.getRealPath(VCARD_EXPORT_FOLDER));
130  if(!root.exists()) root.mkdirs();
131  File output = new File(ElephantContext.getRealPath(VCARD_EXPORT_FOLDER +
132  "/" + Application.getUser().getId() + ".vcf"));
133  boolean append = false;
134  for(VCard vCard : list) {
135  Writer out = new OutputStreamWriter(new FileOutputStream(output, append));
136  out.write(vCard.getVCard());
137  out.close();
138  append = true;
139  }
140  return output;
141  }
142 
143  private VCardFiles() {
144  }
145 
146 }
static final String VCARD_IMPORT_FOLDER
Definition: VCardFiles.java:43
static File saveVCards(VCardList list)
static CSVList loadCSVRels()
static VCardList loadVCards()
Definition: VCardFiles.java:85