BrightSide Workbench Full Report + Source Code
AgreementsUtil.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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.agreements;
19 
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Date;
23 import java.util.List;
24 import java.util.stream.Collectors;
25 import org.turro.string.Strings;
26 import org.turro.action.Contacts;
27 import org.turro.elephant.db.ElephantPU;
28 import org.turro.elephant.db.WhereClause;
29 import org.turro.elephant.entities.db.Agreement;
30 import org.turro.elephant.entities.db.AgreementSignature;
31 import org.turro.jpa.Dao;
32 import org.turro.plugin.contacts.IContact;
33 
38 public class AgreementsUtil {
39 
40  public static List<Agreement> getAgreements() {
41  WhereClause wc = new WhereClause();
42  wc.addClause("select a from Agreement a");
43  return new ElephantPU().getResultList(wc);
44  }
45 
46  public static boolean deleteAgreement(Agreement agreement) {
47  if(agreement != null && agreement.getSignatures().isEmpty()) {
48  new ElephantPU().deleteObject(agreement);
49  return true;
50  }
51  return false;
52  }
53 
54  public static List<AgreementSignature> notSigned(IContact contact) {
55  return new AgreementsUtil(contact).getNotSigned();
56  }
57 
58  private final IContact contact;
59 
60  private List<AgreementSignature> signatures;
61 
62  public AgreementsUtil(IContact contact) {
63  this.contact = contact;
64  }
65 
66  public AgreementsUtil(String idContact) {
67  this.contact = Contacts.getContactById(idContact);
68  }
69 
70  public IContact getContact() {
71  return contact;
72  }
73 
74  public boolean isValid() {
75  return Contacts.isValid(contact);
76  }
77 
78  public boolean canNotify() {
79  return getSignatures(null).stream().noneMatch((as) -> (as.getAgreement().isRequiredToNotify() && !as.doesAgree()));
80  }
81 
82  public boolean canAccess() {
83  return getSignatures(null).stream().noneMatch((as) -> (as.getAgreement().isRequiredToAccess() && !as.doesAgree()));
84  }
85 
86  public boolean canAct(String action) {
87  return getSignatures(action).stream().noneMatch((as) -> (as.getAgreement().isRequiredToAction() && action.equals(as.getAgreement().getAction()) && !as.doesAgree()));
88  }
89 
90  public boolean declinedNotifications() {
91  return getSignatures(null).stream().anyMatch((as) -> (as.getAgreement().isRequiredToNotify() && as.isSigned() && !as.isAgreed()));
92  }
93 
94  public boolean declinedAccessing() {
95  return getSignatures(null).stream().anyMatch((as) -> (as.getAgreement().isRequiredToAccess() && as.isSigned() && !as.isAgreed()));
96  }
97 
98  public boolean declinedActing(String action) {
99  return getSignatures(action).stream().anyMatch((as) -> (as.getAgreement().isRequiredToAction() && action.equals(as.getAgreement().getAction()) && as.isSigned() && !as.isAgreed()));
100  }
101 
102  public List<AgreementSignature> getPeremptories() {
103  return getSignatures(null).stream().filter((s) -> s.getAgreement().isPeremptory()).collect(Collectors.toList());
104  }
105 
106  public List<AgreementSignature> getSignatures(String action) {
107  if(signatures == null) {
108  signatures = new ArrayList<>();
109  if(Contacts.isValid(contact)) {
110  signatures.addAll(getAgreementSignatures());
111  notCreated(action).stream().map((a) -> {
113  as.setIdContact(contact.getId());
114  as.setAgreement(a);
115  as.setAgreed(false);
116  as.setSentCount(0);
117  as.setSignedDate(null);
118  return getDao().saveObject(as);
119  }).forEachOrdered((as) -> {
120  signatures.add(as);
121  });
122  }
123  }
124  return signatures;
125  }
126 
127  public List<AgreementSignature> getNotSigned() {
128  return getSignatures(null).stream().filter((s) -> !s.isSigned()).collect(Collectors.toList());
129  }
130 
131  public List<AgreementSignature> getSigned() {
132  return getSignatures(null).stream().filter((s) -> s.isSigned()).collect(Collectors.toList());
133  }
134 
135  public void sign(Agreement agreement, boolean agreed) {
136  if(agreement != null) {
137  WhereClause wc = new WhereClause();
138  wc.addClause("select asig from AgreementSignature asig");
139  wc.addClause("where asig.agreement = :agreement");
140  wc.addNamedValue("agreement", agreement);
141  wc.addClause("and asig.idContact = :contact");
142  wc.addNamedValue("contact", contact.getId());
144  if(as == null) {
145  as = new AgreementSignature();
146  as.setIdContact(contact.getId());
147  as.setAgreement(agreement);
148  as.setSentCount(0);
149  }
150  if(!as.doesAgree()) {
151  as.setAgreed(agreed);
152  as.setSignedDate(new Date());
153  getDao().saveObject(as);
154  }
155  }
156  }
157 
158  public void sign(Long agreementId, boolean agreed) {
159  sign(getDao().find(Agreement.class, agreementId), agreed);
160  }
161 
162  public void markAsSent(AgreementSignature as) {
163  if(as.getId() != null) {
164  WhereClause wc = new WhereClause();
165  wc.addClause("update AgreementSignature");
166  wc.addClause("set sentCount = sentCount + 1");
167  wc.addClause("where agreement = :agreement");
168  wc.addNamedValue("agreement", as.getAgreement());
169  wc.addClause("and idContact = :contact");
170  wc.addNamedValue("contact", as.getIdContact());
171  getDao().executeUpdate(wc);
172  } else {
173  as.setSentCount(1);
174  getDao().saveObject(as);
175  }
176  }
177 
178  private List<Agreement> notCreated(String action) {
179  WhereClause wc = new WhereClause();
180  wc.addClause("select a from Agreement a");
181  wc.addClause("where not exists (");
182  wc.addClause(" select asig from AgreementSignature asig");
183  wc.addClause(" where asig.agreement = a");
184  wc.addClause(" and asig.idContact = :contact");
185  wc.addNamedValue("contact", contact.getId());
186  wc.addClause(")");
187  // Only empty actions or requested action
188  wc.addClause("and (a.action is null or trim(a.action) = '' or a.action = :action)");
189  wc.addNamedValue("action", action);
190 
191  return (List<Agreement>) getDao().getResultList(wc).stream().filter((a) ->
192  fit((Agreement) a)).collect(Collectors.toList());
193  }
194 
195  private boolean fit(Agreement agreement) {
196  String appliesString = agreement.getAppliesTo();
197  if(Strings.isBlank(appliesString) || "*".equals(appliesString)) {
198  return true;
199  }
200  String[] applies = appliesString.split(",");
201  for(String syndication : applies) {
202  if(contact.getSyndications().contains(syndication)) {
203  return true;
204  }
205  }
206  return false;
207  }
208 
209  private Collection<AgreementSignature> getAgreementSignatures() {
210  WhereClause wc = new WhereClause();
211  wc.addClause("select asig from AgreementSignature asig");
212  wc.addClause("where asig.idContact = :contact");
213  wc.addNamedValue("contact", contact.getId());
214  return getDao().getResultList(wc);
215  }
216 
217  /* Dao */
218 
219  private Dao _dao;
220 
221  private Dao getDao() {
222  if (_dao == null) {
223  _dao = new ElephantPU();
224  }
225  return _dao;
226  }
227 
228 }
static boolean isValid(IContact contact)
Definition: Contacts.java:52
static IContact getContactById(String id)
Definition: Contacts.java:72
void sign(Long agreementId, boolean agreed)
List< AgreementSignature > getSignatures(String action)
static List< AgreementSignature > notSigned(IContact contact)
static boolean deleteAgreement(Agreement agreement)
List< AgreementSignature > getPeremptories()
static List< Agreement > getAgreements()
List< AgreementSignature > getSigned()
void sign(Agreement agreement, boolean agreed)
List< AgreementSignature > getNotSigned()
boolean declinedActing(String action)
void markAsSent(AgreementSignature as)
void addNamedValue(String name, Object value)
List< AgreementSignature > getSignatures()
Definition: Agreement.java:152
void deleteObject(Object obj)
Definition: Dao.java:162
int executeUpdate(String query)
Definition: Dao.java:463
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419