BrightSide Workbench Full Report + Source Code
RelationUtil.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 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.util;
20 
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;
30 
35 @Deprecated
36 public class RelationUtil {
37 
38  public static void addRelation(Contact business, Contact contact, String relation) {
39  if(business != null && contact != null && !Strings.isBlank(relation)) {
40  Dao dao = new ContactsPU();
41  WhereClause wc = new WhereClause();
42  wc.addClause("select r from BusinessRelation as r");
43  wc.addClause("where r.contact = :c1");
44  wc.addNamedValue("c1", contact);
45  wc.addClause("and r.business = :c2");
46  wc.addNamedValue("c2", business);
47  wc.addClause("and r.relationType = :pr");
48  wc.addNamedValue("pr", relation);
49  List<BusinessRelation> relations = dao.getResultList(wc);
50  if(relations.isEmpty()) {
52  r.setContact(contact);
53  r.setBusiness(business);
54  r.setRelationType(relation);
55  dao.saveObject(r);
56  }
57  }
58  }
59 
60  public static void addRelation(String business, String contact, String relation) {
61  if(business != null && contact != null && !Strings.isBlank(relation)) {
62  Dao dao = new ContactsPU();
63  WhereClause wc = new WhereClause();
64  wc.addClause("select r from BusinessRelation as r");
65  wc.addClause("where r.contact.id = :c1");
66  wc.addNamedValue("c1", contact);
67  wc.addClause("and r.business.id = :c2");
68  wc.addNamedValue("c2", business);
69  wc.addClause("and r.relationType = :pr");
70  wc.addNamedValue("pr", relation);
71  List<BusinessRelation> relations = dao.getResultList(wc);
72  if(relations.isEmpty()) {
74  r.setContact(dao.find(Contact.class, contact));
75  r.setBusiness(dao.find(Contact.class, business));
76  r.setRelationType(relation);
77  if(r.getContact() != null && r.getBusiness()!= null) {
78  dao.saveObject(r);
79  }
80  }
81  }
82  }
83 
84  public static void addRelations(Collection<BusinessRelation> relations) {
85  Dao dao = new ContactsPU();
86  dao.saveCollection(relations);
87  }
88 
89  public static void killRelation(Contact business, Contact contact, String relation) {
90  if(business != null && contact != null && !Strings.isBlank(relation)) {
91  Dao dao = new ContactsPU();
92  WhereClause wc = new WhereClause();
93  wc.addClause("delete from BusinessRelation as r");
94  wc.addClause("where r.contact = :c1");
95  wc.addNamedValue("c1", contact);
96  wc.addClause("and r.business = :c2");
97  wc.addNamedValue("c2", business);
98  wc.addClause("and r.relationType = :pr");
99  wc.addNamedValue("pr", relation);
100  dao.executeUpdate(wc);
101  }
102  }
103 
104  public static void killRelation(String business, String contact, String relation) {
105  if(business != null && contact != null && !Strings.isBlank(relation)) {
106  Dao dao = new ContactsPU();
107  WhereClause wc = new WhereClause();
108  wc.addClause("delete from BusinessRelation as r");
109  wc.addClause("where r.contact.id = :c1");
110  wc.addNamedValue("c1", contact);
111  wc.addClause("and r.business.id = :c2");
112  wc.addNamedValue("c2", business);
113  wc.addClause("and r.relationType = :pr");
114  wc.addNamedValue("pr", relation);
115  dao.executeUpdate(wc);
116  }
117  }
118 
119  public static void killRelations(String like) {
120  if(!Strings.isBlank(like)) {
121  Dao dao = new ContactsPU();
122  WhereClause wc = new WhereClause();
123  wc.addClause("delete from BusinessRelation as r");
124  wc.addClause("where r.relationType like :pr");
125  wc.addNamedValue("pr", like);
126  dao.executeUpdate(wc);
127  }
128  }
129 
130  public static void setPreferentialRelation() {
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() }
139  );
140  break;
141  }
142  }
143  }
144  });
145  }
146  }
147 
148  private RelationUtil() {
149  }
150 
151 }
void setRelationType(String relationType)
static void addRelation(Contact business, Contact contact, String relation)
static void killRelation(String business, String contact, String relation)
static void killRelations(String like)
static void addRelations(Collection< BusinessRelation > relations)
static void addRelation(String business, String contact, String relation)
static void killRelation(Contact business, Contact contact, String relation)
void addNamedValue(String name, Object value)
int executeUpdate(String query)
Definition: Dao.java:463
void saveCollection(Collection objs)
Definition: Dao.java:144