BrightSide Workbench Full Report + Source Code
ProfilePolicies.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.profile;
20 
21 import java.util.Arrays;
22 import java.util.HashSet;
23 import java.util.Set;
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;
31 
36 public class ProfilePolicies {
37 
38  private final Contact contact;
39  private final Set<ProfilePolicy> policies;
40 
41  public ProfilePolicies(Contact contact) {
42  this.contact = contact;
43  policies = new HashSet<>();
44  load();
45  }
46 
47  public void setPolicy(Object obj, String element, PublishPolicy publish) {
48  ProfilePolicy pp = new ProfilePolicy();
49  pp.setProfilePath(createPathFrom(obj, element));
50  pp.setPublish(publish);
51  if(policies.contains(pp)) policies.remove(pp);
52  policies.add(pp);
53  }
54 
55  public PublishPolicy getPolicy(Object obj, String element) {
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));
60  }
61 
62  public void save() {
63  Dao dao = new ContactsPU();
64  removeFromContact(dao);
65  dao.saveEntities(policies);
66  }
67 
68  private void removeFromContact(Dao dao) {
69  WhereClause wc = new WhereClause();
70  wc.addClause("delete from ProfilePolicy");
71  wc.addClause("where profilePath like concat(:path, '/%')");
72  wc.addNamedValue("path", contactPath());
73  dao.executeUpdate(wc);
74  }
75 
76  private PublishPolicy getDefaultFor(String element) {
77  return Arrays.asList("position", "dates").contains(element) ?
79  }
80 
81  /* Path helpers */
82 
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();
89  }
90  return path + "/" + Strings.isBlank(element, "");
91  }
92 
93  private String contactPath() {
94  return "/contact/" + contact.getId();
95  }
96 
97  /* Init */
98 
99  private void load() {
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)) {
106  policies.add(pp);
107  }
108  }
109 
110 }
void setPublish(PublishPolicy publish)
void setProfilePath(String profilePath)
PublishPolicy getPolicy(Object obj, String element)
void setPolicy(Object obj, String element, PublishPolicy publish)
void addNamedValue(String name, Object value)
int executeUpdate(String query)
Definition: Dao.java:463
void saveEntities(Collection<? extends IDaoEntity > entities)
Definition: Dao.java:133