18 package org.turro.agreements;
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;
42 wc.
addClause(
"select a from Agreement a");
47 if(agreement !=
null && agreement.
getSignatures().isEmpty()) {
60 private List<AgreementSignature> signatures;
63 this.contact = contact;
79 return getSignatures(
null).stream().noneMatch((as) -> (as.getAgreement().isRequiredToNotify() && !as.doesAgree()));
83 return getSignatures(
null).stream().noneMatch((as) -> (as.getAgreement().isRequiredToAccess() && !as.doesAgree()));
86 public boolean canAct(String action) {
87 return getSignatures(action).stream().noneMatch((as) -> (as.getAgreement().isRequiredToAction() && action.equals(as.getAgreement().getAction()) && !as.doesAgree()));
91 return getSignatures(
null).stream().anyMatch((as) -> (as.getAgreement().isRequiredToNotify() && as.isSigned() && !as.isAgreed()));
95 return getSignatures(
null).stream().anyMatch((as) -> (as.getAgreement().isRequiredToAccess() && as.isSigned() && !as.isAgreed()));
99 return getSignatures(action).stream().anyMatch((as) -> (as.getAgreement().isRequiredToAction() && action.equals(as.getAgreement().getAction()) && as.isSigned() && !as.isAgreed()));
103 return getSignatures(
null).stream().filter((s) -> s.getAgreement().isPeremptory()).collect(Collectors.toList());
107 if(signatures ==
null) {
108 signatures =
new ArrayList<>();
110 signatures.addAll(getAgreementSignatures());
111 notCreated(action).stream().map((a) -> {
118 return getDao().saveObject(as);
119 }).forEachOrdered((as) -> {
128 return getSignatures(
null).stream().filter((s) -> !s.isSigned()).collect(Collectors.toList());
132 return getSignatures(
null).stream().filter((s) -> s.isSigned()).collect(Collectors.toList());
136 if(agreement !=
null) {
138 wc.
addClause(
"select asig from AgreementSignature asig");
139 wc.
addClause(
"where asig.agreement = :agreement");
141 wc.
addClause(
"and asig.idContact = :contact");
153 getDao().saveObject(as);
158 public void sign(Long agreementId,
boolean agreed) {
163 if(as.
getId() !=
null) {
165 wc.
addClause(
"update AgreementSignature");
166 wc.
addClause(
"set sentCount = sentCount + 1");
167 wc.
addClause(
"where agreement = :agreement");
169 wc.
addClause(
"and idContact = :contact");
174 getDao().saveObject(as);
178 private List<Agreement> notCreated(String action) {
180 wc.
addClause(
"select a from Agreement a");
182 wc.
addClause(
" select asig from AgreementSignature asig");
183 wc.
addClause(
" where asig.agreement = a");
184 wc.
addClause(
" and asig.idContact = :contact");
188 wc.
addClause(
"and (a.action is null or trim(a.action) = '' or a.action = :action)");
191 return (List<Agreement>) getDao().getResultList(wc).stream().filter((a) ->
192 fit((
Agreement) a)).collect(Collectors.toList());
195 private boolean fit(Agreement agreement) {
196 String appliesString = agreement.getAppliesTo();
197 if(Strings.isBlank(appliesString) ||
"*".equals(appliesString)) {
200 String[] applies = appliesString.split(
",");
201 for(String syndication : applies) {
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);
221 private Dao getDao() {
223 _dao =
new ElephantPU();
boolean declinedAccessing()
void sign(Long agreementId, boolean agreed)
List< AgreementSignature > getSignatures(String action)
static List< AgreementSignature > notSigned(IContact contact)
static boolean deleteAgreement(Agreement agreement)
AgreementsUtil(String idContact)
boolean declinedNotifications()
List< AgreementSignature > getPeremptories()
static List< Agreement > getAgreements()
AgreementsUtil(IContact contact)
boolean canAct(String action)
List< AgreementSignature > getSigned()
void sign(Agreement agreement, boolean agreed)
List< AgreementSignature > getNotSigned()
boolean declinedActing(String action)
void markAsSent(AgreementSignature as)
void addClause(String clause)
void addNamedValue(String name, Object value)
void setIdContact(String idContact)
void setAgreed(boolean agreed)
void setAgreement(Agreement agreement)
void setSentCount(int sentCount)
void setSignedDate(Date signedDate)
List< AgreementSignature > getSignatures()
void deleteObject(Object obj)
int executeUpdate(String query)
Object getSingleResultOrNull(SqlClause sc)