19 package org.turro.contacts.profile;
21 import java.util.Arrays;
22 import java.util.HashSet;
24 import org.turro.string.Strings;
25 import org.turro.contacts.BusinessRelation;
26 import org.turro.contacts.Contact;
27 import org.turro.contacts.ProfilePolicy;
28 import org.turro.contacts.db.ContactsPU;
29 import org.turro.elephant.db.WhereClause;
30 import org.turro.jpa.Dao;
39 private final Set<ProfilePolicy> policies;
42 this.contact = contact;
43 policies =
new HashSet<>();
51 if(policies.contains(pp)) policies.remove(pp);
56 String entityPath = createPathFrom(obj, element);
57 return policies.stream().filter(pp -> pp.getProfilePath().equals(entityPath))
58 .map(pp -> pp.getPublish())
59 .findFirst().orElse(getDefaultFor(element));
64 removeFromContact(dao);
68 private void removeFromContact(
Dao dao) {
70 wc.
addClause(
"delete from ProfilePolicy");
71 wc.
addClause(
"where profilePath like concat(:path, '/%')");
77 return Arrays.asList(
"position",
"dates").contains(element) ?
83 private String createPathFrom(Object obj, String element) {
84 String path = contactPath();
85 if(obj instanceof BusinessRelation) {
86 path +=
"/relation/" + ((BusinessRelation) obj).getId();
87 }
else if(obj instanceof ProfileRelation) {
88 path +=
"/relation/" + ((ProfileRelation) obj).getRelation().getId();
90 return path +
"/" + Strings.isBlank(element,
"");
93 private String contactPath() {
94 return "/contact/" + contact.
getId();
100 Dao dao =
new ContactsPU();
101 WhereClause wc =
new WhereClause();
102 wc.addClause(
"select p from ProfilePolicy p");
103 wc.addClause(
"where p.profilePath like concat(:path, '/%')");
104 wc.addNamedValue(
"path", contactPath());
105 for(ProfilePolicy pp : dao.getResultList(ProfilePolicy.class, wc)) {
void addClause(String clause)
void addNamedValue(String name, Object value)
int executeUpdate(String query)
void saveEntities(Collection<? extends IDaoEntity > entities)