BrightSide Workbench Full Report + Source Code
ValidationStatus.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.relation;
20 
21 import java.net.URISyntaxException;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import org.turro.url.URIs;
25 import org.turro.contacts.BusinessRelation;
26 import org.turro.contacts.Connector;
27 import org.turro.contacts.RelationValidation;
28 import org.turro.contacts.db.ContactsPU;
29 import org.turro.contacts.organigram.RelationType;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.elephant.security.IUser;
32 import org.turro.jpa.DaoTransaction;
33 
38 public class ValidationStatus {
39 
40  private final BusinessRelation relation;
41  private final RelationValidation validation;
42  private final Workers workers;
43 
45  this.relation = relation;
46  this.workers = relation.getBusiness().getWorkerSet();
47  this.validation = RelationValidation.getFrom(relation);
48  updateRule();
49  }
50 
52  return validation.getRule();
53  }
54 
55  public boolean isValidated() {
56  return relation.isValidated();
57  }
58 
59  public boolean isPending() {
60  return validation != null && !validation.isValidated();
61  }
62 
63  public boolean hasHHRR() {
64  return workers.stream().anyMatch(r -> RelationType.REL_HHRR.equals(r.getType()));
65  }
66 
67  private void updateRule() {
68  if(relation.isValidated() || hasSameDomain()) {
70  } else if(hasHHRR()) {
72  } else if(isInternal()) {
73  validation.setRule(ValidationRule.VALIDATION_INTERNAL);
74  } else {
75  validation.setRule(ValidationRule.VALIDATION_DUBIOUS);
76  }
77  }
78 
79  private boolean isInternal() {
80  Connector email = relation.getContact().getConnectorMap().get(IUser.CONNECTOR_EMAIL);
81  if(email != null) {
82  try {
83  return URIs.sameBaseDomain(ElephantContext.getServerBase("http"), email.getValue());
84  } catch (URISyntaxException ex) {
85  Logger.getLogger(ValidationStatus.class.getName()).log(Level.INFO, null, ex);
86  }
87  }
88  return false;
89  }
90 
91  private BusinessRelation getHHRR() {
92  return workers.stream().filter(r -> RelationType.REL_HHRR.equals(r.getType())).findFirst().orElse(null);
93  }
94 
95  private boolean hasSameDomain() {
96  try {
97  Connector email = relation.getContact().getConnectorMap().get(IUser.CONNECTOR_EMAIL);
98  if(email != null) {
99  Connector web = relation.getBusiness().getConnectorMap().get("Web");
100  if(web != null && URIs.sameBaseDomain(web.getValue(), email.getValue())) {
101  return true;
102  }
103  Connector domain = relation.getBusiness().getConnectorMap().get("Domain");
104  if(domain != null) {
105  String[] domains = domain.getValue().split("\\s*,\\s*");
106  for(String d : domains) {
107  if(URIs.sameBaseDomain(d, email.getValue())) {
108  return true;
109  }
110  }
111  }
112  }
113  } catch (URISyntaxException ex) {
114  Logger.getLogger(ValidationStatus.class.getName()).log(Level.INFO, null, ex);
115  }
116  return false;
117  }
118 
119  private void saveStatus() {
120  try(DaoTransaction transaction = new DaoTransaction(new ContactsPU())) {
121  validation.setValidated(true);
122  transaction.saveObject(validation);
123  relation.setValidated(true);
124  transaction.saveObject(relation);
125  }
126  }
127 
128 }
Map< String, Connector > getConnectorMap()
Definition: Contact.java:499
static RelationValidation getFrom(BusinessRelation relation)