BrightSide Workbench Full Report + Source Code
CSVToRelation.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 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 
19 package org.turro.vcard;
20 
21 import java.util.List;
22 import org.turro.contacts.BusinessRelation;
23 import org.turro.contacts.Contact;
24 import org.turro.contacts.db.ContactsPU;
25 import org.turro.csv.CSVEntry;
26 import org.turro.jpa.Dao;
27 
32 public class CSVToRelation {
33 
34  private CSVEntry entry;
35 
36  public CSVToRelation(CSVEntry v) {
37  entry = v;
38  }
39 
40  public void create() {
41  Dao dao = new ContactsPU();
42  Contact main = null, related = null;
43  String header = entry.getList().getHeader().getHeader("GID-MAIN");
44  if(header != null) {
45  String value = entry.getField(header);
46  main = contactByGID(dao, value);
47  }
48  if(main == null) {
49  header = entry.getList().getHeader().getPrefixHeader("ID-MAIN");
50  if(header != null) {
51  String prefixValue = null;
52  if(header.length() > "ID-MAIN".length()) {
53  prefixValue = header.substring("ID-MAIN".length() + 1);
54  }
55  String value = prefixValue == null ? entry.getField(header) :
56  entry.getField(header).substring(prefixValue.length());
57  main = contactByConnector(dao, "ID", value);
58  }
59  }
60  header = entry.getList().getHeader().getPrefixHeader("CONNECTOR-REL");
61  if(header != null) {
62  String connector = null;
63  if(header.length() > "CONNECTOR-REL".length()) {
64  connector = header.substring("CONNECTOR-REL".length() + 1);
65  }
66  String value = entry.getField(header);
67  related = contactByConnector(dao, connector, value);
68  }
69  if(main != null && related != null) {
70  header = entry.getList().getHeader().getHeader("RELATION");
71  if(header != null) {
72  String value = entry.getField(header);
73  BusinessRelation relation = new BusinessRelation();
74  relation.setBusiness(main);
75  relation.setContact(related);
76  relation.setDescription(value == null ? "Contacto" : value);
77  dao.saveObject(relation);
78  }
79  }
80  }
81 
82  private Contact contactByGID(Dao dao, String value) {
83  List l = dao.getResultList(
84  "select c from Contact c where c.globalIdentifier = ?",
85  new Object[] { value });
86  if(l != null && l.size() > 0) {
87  return (Contact) l.iterator().next();
88  }
89  return null;
90  }
91 
92  private Contact contactByConnector(Dao dao, String connector, String value) {
93  List l = dao.getResultList(
94  "select distinct c.contact from Connector c where c.description = ? and c.value = ?",
95  new Object[] { connector, value });
96  if(l != null && l.size() > 0) {
97  return (Contact) l.iterator().next();
98  }
99  return null;
100  }
101 
102 }
void setDescription(String description)