19 package org.turro.contacts.util;
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.stream.Stream;
24 import org.turro.string.Strings;
25 import org.turro.contacts.BusinessRelation;
26 import org.turro.contacts.Contact;
27 import org.turro.contacts.db.ContactsPU;
28 import org.turro.elephant.db.WhereClause;
29 import org.turro.jpa.Dao;
39 if(business !=
null && contact !=
null && !Strings.isBlank(relation)) {
42 wc.
addClause(
"select r from BusinessRelation as r");
49 List<BusinessRelation> relations = dao.getResultList(wc);
50 if(relations.isEmpty()) {
60 public static void addRelation(String business, String contact, String relation) {
61 if(business !=
null && contact !=
null && !Strings.isBlank(relation)) {
64 wc.
addClause(
"select r from BusinessRelation as r");
71 List<BusinessRelation> relations = dao.getResultList(wc);
72 if(relations.isEmpty()) {
84 public static void addRelations(Collection<BusinessRelation> relations) {
90 if(business !=
null && contact !=
null && !Strings.isBlank(relation)) {
93 wc.
addClause(
"delete from BusinessRelation as r");
104 public static void killRelation(String business, String contact, String relation) {
105 if(business !=
null && contact !=
null && !Strings.isBlank(relation)) {
108 wc.
addClause(
"delete from BusinessRelation as r");
109 wc.
addClause(
"where r.contact.id = :c1");
113 wc.
addClause(
"and r.relationType = :pr");
120 if(!Strings.isBlank(like)) {
123 wc.
addClause(
"delete from BusinessRelation as r");
124 wc.
addClause(
"where r.relationType like :pr");
131 try(Stream<Contact> contacts =
new ContactsPU().stream(
Contact.class,
"select c from Contact c")) {
132 contacts.forEach((contact) -> {
133 if(contact.getBusiness() ==
null) {
134 for(BusinessRelation relation : contact.getBusinessRelations()) {
135 if(!Strings.isBlank(relation.getRelationType()) && relation.getRelationType().startsWith(
"$REL_")) {
136 new ContactsPU().executeUpdate(
137 "update BusinessRelation set preferential = TRUE where identifier = ?",
138 new Object[] { relation.getId() }
148 private RelationUtil() {
void addClause(String clause)
void addNamedValue(String name, Object value)
int executeUpdate(String query)
void saveCollection(Collection objs)