BrightSide Workbench Full Report + Source Code
AxContact.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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.alliance.db.entities;
20 
21 import java.io.Serializable;
22 import java.security.NoSuchAlgorithmException;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Objects;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import javax.json.JsonValue;
29 import javax.persistence.EmbeddedId;
30 import javax.persistence.Entity;
31 import org.turro.string.Phrases;
32 import org.turro.string.Strings;
33 import org.hibernate.annotations.Formula;
34 import org.turro.action.Contacts;
35 import org.turro.alliance.contact.AllianceContact;
36 import org.turro.alliance.contact.ContactRelationAdapter;
37 import org.turro.alliance.db.AlliancePU;
38 import org.turro.contacts.BusinessRelation;
39 import org.turro.contacts.Contact;
40 import org.turro.crypto.Digest;
41 import org.turro.elephant.context.ElephantContext;
42 import org.turro.importer.ImporterRegister;
43 import org.turro.jpa.entity.IDaoEntity;
44 import org.turro.json.IJSONizable;
45 import org.turro.plugin.contacts.IContact;
46 import org.turro.plugin.contacts.IContactRelation;
47 import org.turro.sql.SqlClause;
48 
53 @Entity
54 public class AxContact implements Serializable, IDaoEntity, IJSONizable {
55 
56  @EmbeddedId private ProcedenceId contactId;
57 
58  @Formula("concat(entityId,'##',memberId)")
59  private String id; /* Widgets compatibility */
60 
61  private String name, email;
62  private String passdig;
63  private String company, position;
64  private String face, companyFace;
65 
66  private boolean active;
67 
69  return contactId;
70  }
71 
72  public void setContactId(ProcedenceId contactId) {
73  this.contactId = contactId;
74  }
75 
76  public String getId() {
77  return id;
78  }
79 
80  public String getName() {
81  return name;
82  }
83 
84  public void setName(String name) {
85  this.name = name;
86  }
87 
88  public String getEmail() {
89  return email;
90  }
91 
92  public void setEmail(String email) {
93  this.email = email;
94  }
95 
96  public String getPassdig() {
97  return passdig;
98  }
99 
100  public void setPassdig(String passdig) {
101  this.passdig = passdig;
102  }
103 
104  public String getCompany() {
105  return company;
106  }
107 
108  public void setCompany(String company) {
109  this.company = company;
110  }
111 
112  public String getPosition() {
113  return position;
114  }
115 
116  public void setPosition(String position) {
117  this.position = position;
118  }
119 
120  public String getFace() {
121  return face;
122  }
123 
124  public void setFace(String face) {
125  this.face = face;
126  }
127 
128  public String getCompanyFace() {
129  return companyFace;
130  }
131 
132  public void setCompanyFace(String companyFace) {
133  this.companyFace = companyFace;
134  }
135 
136  public boolean isActive() {
137  return active;
138  }
139 
140  public void setActive(boolean active) {
141  this.active = active;
142  }
143 
144  /* Relations */
145 
146  public Iterable<IContactRelation> getBusinessRelations() {
147  return List.of(new ContactRelationAdapter(true, new AllianceContact(this).getBusiness()));
148  }
149 
150  public Iterable<IContactRelation> getWorkerRelations() {
151  return SqlClause.select("c").from("AxContact c")
152  .where().equal("c.company", company)
153  .and().equal("c.contactId.memberId", contactId.getMemberId())
154  .dao(new AlliancePU())
155  .resultList(AxContact.class)
156  .stream().map(c -> (IContactRelation) new ContactRelationAdapter(true, new AllianceContact(c)))
157  .toList();
158  }
159 
160  /* Factory */
161 
162  public static AxContact from(long memberId, String contactId) {
163  if(!Strings.isBlank(contactId)) {
164  IContact contact = Contacts.getContactById(contactId);
165  if(contact.isValid()) {
166  AxContact axc = new AxContact();
167  ProcedenceId id = new ProcedenceId();
168  id.setMemberId(memberId);
169  id.setEntityId(contact.getId());
170  axc.setContactId(id);
171  axc.setName(contact.getName());
172  axc.setEmail(contact.getEmail());
173  String pass = ((Contact) contact.getContact()).getPass1();
174  if(!Strings.isBlank(pass)) try {
175  axc.setPassdig(Digest.sha256(ElephantContext.decrypt(pass)).hexString());
176  } catch (NoSuchAlgorithmException ex) {
177  Logger.getLogger(AxContact.class.getName()).log(Level.INFO, null, ex);
178  }
179  axc.setActive(!contact.isDeactivated());
180  String serverURL = ElephantContext.getServerUrl("http");
181  axc.setFace(Strings.isBlank(contact.getFace()) ? null : serverURL + contact.getFace());
183  if(cr != null) {
184  IContact business = cr.getRelatedIContact();
186  axc.setCompany(business.getName());
187  axc.setPosition(Phrases.start(cr.getDatesString()).add(br.getDescription()).toString());
188  axc.setCompanyFace(Strings.isBlank(business.getFace()) ? null : serverURL + business.getFace());
189  }
190  return axc;
191  }
192  }
193  return null;
194  }
195 
196  public static AxContact from(long memberId, ImporterRegister register) {
197  AxContact axc = new AxContact();
198  ProcedenceId id = new ProcedenceId();
199  id.setMemberId(memberId);
200  id.setEntityId(register.getString("contactId"));
201  axc.setContactId(id);
202  axc.setName(register.getString("name"));
203  axc.setEmail(register.getString("email"));
204  String pass = register.getString("pass");
205  if(!Strings.isBlank(pass)) try {
206  axc.setPassdig(Digest.sha256(ElephantContext.decrypt(pass)).hexString());
207  } catch (NoSuchAlgorithmException ex) {
208  Logger.getLogger(AxContact.class.getName()).log(Level.INFO, null, ex);
209  }
210  axc.setActive(!register.getBoolean("deactivated"));
211  axc.setFace(register.getString("face"));
212  axc.setCompany(register.getString("company"));
213  axc.setPosition(register.getString("position"));
214  axc.setCompanyFace(register.getString("companyFace"));
215  return axc;
216  }
217 
218  /* IDaoEntity */
219 
220  @Override
221  public Object entityId() {
222  return contactId;
223  }
224 
225  @Override
226  public boolean isEmpty() {
227  return contactId.isEmpty() ||
228  Strings.isBlank(name);
229  }
230 
231  /* JSONizable */
232 
233  @Override
234  public String toJson() {
235  return toJson(this);
236  }
237 
238  @Override
239  public String toJson(Map<String, Object> properties) {
240  return toJson(this, properties);
241  }
242 
243  public static AxContact fromJson(JsonValue value) {
244  return IJSONizable.fromJson(value.toString(), AxContact.class);
245  }
246 
247  /* Utils */
248 
249  @Override
250  public int hashCode() {
251  int hash = 7;
252  hash = 83 * hash + Objects.hashCode(this.contactId);
253  return hash;
254  }
255 
256  @Override
257  public boolean equals(Object obj) {
258  if (this == obj) {
259  return true;
260  }
261  if (obj == null) {
262  return false;
263  }
264  if (getClass() != obj.getClass()) {
265  return false;
266  }
267  final AxContact other = (AxContact) obj;
268  return Objects.equals(this.contactId, other.contactId);
269  }
270 
271 }
static IContact getContactById(String id)
Definition: Contacts.java:72
Iterable< IContactRelation > getBusinessRelations()
Definition: AxContact.java:146
static AxContact fromJson(JsonValue value)
Definition: AxContact.java:243
void setContactId(ProcedenceId contactId)
Definition: AxContact.java:72
static AxContact from(long memberId, String contactId)
Definition: AxContact.java:162
void setCompanyFace(String companyFace)
Definition: AxContact.java:132
String toJson(Map< String, Object > properties)
Definition: AxContact.java:239
static AxContact from(long memberId, ImporterRegister register)
Definition: AxContact.java:196
Iterable< IContactRelation > getWorkerRelations()
Definition: AxContact.java:150
static String getServerUrl(String scheme)
IContactRelation getCompanyRelation()