BrightSide Workbench Full Report + Source Code
ImporterEntry.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2023 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.contacts.importer;
20 
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Objects;
24 import org.turro.contacts.Contact;
25 import org.turro.contacts.ContactType;
26 import org.turro.contacts.db.ContactsPU;
27 import org.turro.contacts.organigram.RelationType;
28 import org.turro.jpa.Dao;
29 import org.turro.plugin.contacts.IContact;
30 import org.turro.sql.SqlClause;
31 import org.turro.util.Cached;
32 
37 public class ImporterEntry {
38 
39  private final String email, name, relatedTo;
40  private String position;
41  private RelationType type;
42  private Contact contact, possibleRelated;
43  private boolean duplicated;
44 
45  public ImporterEntry(String email, String name, String relatedTo, String position) {
46  this.email = email;
47  this.name = name;
48  this.relatedTo = relatedTo;
49  this.position = position;
50  type = RelationType.REL_STAFF;
51  solve();
52  }
53 
54  public String getEmail() {
55  return email;
56  }
57 
58  public String getName() {
59  return name;
60  }
61 
62  public String getRelatedTo() {
63  return relatedTo;
64  }
65 
66  public String getPosition() {
67  return position;
68  }
69 
70  public void setPosition(String position) {
71  this.position = position;
72  }
73 
74  public boolean isDuplicated() {
75  return duplicated;
76  }
77 
78  public RelationType getType() {
79  return type;
80  }
81 
82  public void setType(RelationType type) {
83  this.type = type;
84  }
85 
86  public Contact getContact() {
87  return contact;
88  }
89 
90  public void setContact(Contact contact) {
91  this.contact = contact;
92  }
93 
95  return possibleRelated;
96  }
97 
98  public void setPossibleRelated(Contact possibleRelated) {
99  this.possibleRelated = possibleRelated;
100  }
101 
102  public String getFinalName() {
103  return contact == null ? name : contact.getName();
104  }
105 
106  /* Init */
107 
108  public void rebuild() {
109  solve();
110  }
111 
112  private void solve() {
113  List<Contact> found = findEmail();
114  if(found.size() > 1) {
115  duplicated = true;
116  return;
117  } else if(found.size() == 1) {
118  contact = found.get(0);
119  }
120  List<Contact> related = findRelated();
121  if(!related.isEmpty()) {
122  possibleRelated = related.get(0);
123  }
124  }
125 
126  private List<Contact> findEmail() {
127  return SqlClause.select("c.contact").from("Connector c")
128  .where().equal("c.value", email)
129  .and().equal("c.description", IContact.CONNECTOR_EMAIL)
130  .dao(dao.get())
131  .resultList(Contact.class);
132  }
133 
134  private List<Contact> findRelated() {
135  return SqlClause.select("c").from("Contact c")
136  .where().equal("c.name", relatedTo)
137  .and().in("c.type", List.of(
138  ContactType.CONTACT_COMPANY,
139  ContactType.CONTACT_LEARNINGCENTER))
140  .dao(dao.get())
141  .resultList(Contact.class);
142  }
143 
144  /* Dao */
145 
146  private final Cached<Dao> dao = Cached.instance(() -> new ContactsPU());
147 
148  /* Factory */
149 
150  public static ImporterEntry from(Map<String, String> values) {
151  return new ImporterEntry(values.get("email"), values.get("name"),
152  values.get("relatedTo"), values.get("position"));
153  }
154 
155  /* Utils */
156 
157  @Override
158  public int hashCode() {
159  int hash = 5;
160  hash = 53 * hash + Objects.hashCode(this.email);
161  return hash;
162  }
163 
164  @Override
165  public boolean equals(Object obj) {
166  if (this == obj) {
167  return true;
168  }
169  if (obj == null) {
170  return false;
171  }
172  if (getClass() != obj.getClass()) {
173  return false;
174  }
175  final ImporterEntry other = (ImporterEntry) obj;
176  return Objects.equals(this.email, other.email);
177  }
178 
179 }
void setPossibleRelated(Contact possibleRelated)
static ImporterEntry from(Map< String, String > values)
ImporterEntry(String email, String name, String relatedTo, String position)