BrightSide Workbench Full Report + Source Code
SocialGroupVM.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.social;
20 
21 import java.util.ArrayList;
22 import java.util.Date;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.TreeSet;
26 import org.turro.contacts.BusinessRelation;
27 import org.turro.contacts.db.ContactsPU;
28 import org.turro.elephant.db.WhereClause;
29 import org.turro.plugin.contacts.IContact;
30 import org.turro.security.SocialGroups;
31 import org.zkoss.bind.annotation.BindingParam;
32 import org.zkoss.bind.annotation.Command;
33 import org.zkoss.bind.annotation.NotifyChange;
34 
39 public class SocialGroupVM {
40 
41  private SocialGroupValue socialGroup;
42  private boolean active = true, students = false;
43 
44  public SocialGroupVM() {
45  }
46 
47  @NotifyChange("model")
48  @Command
49  public void checkBusiness(@BindingParam("relation") SocialRelation relation) {
50  IContact contact = relation.getBusiness().getIContact();
51  contact.syndicate(socialGroup.getKey(), !contact.getSyndications().contains(socialGroup.getKey()));
52  }
53 
54  @NotifyChange("model")
55  @Command
56  public void checkContact(@BindingParam("relation") SocialRelation relation) {
57  IContact contact = relation.getContact().getIContact();
58  contact.syndicate(socialGroup.getKey(), !contact.getSyndications().contains(socialGroup.getKey()));
59  }
60 
62  return socialGroup;
63  }
64 
65  public void setSocialGroup(SocialGroupValue socialGroup) {
66  this.socialGroup = socialGroup;
67  }
68 
69  public boolean isActive() {
70  return active;
71  }
72 
73  public void setActive(boolean active) {
74  this.active = active;
75  }
76 
77  public boolean isStudents() {
78  return students;
79  }
80 
81  public void setStudents(boolean students) {
82  this.students = students;
83  }
84 
85  public Set<SocialGroupValue> getSocialGroupModel() {
86  TreeSet<SocialGroupValue> set = new TreeSet<>();
87  SocialGroups.allowedSocialGroups().forEach(social -> {
88  set.add(new SocialGroupValue(social.getName(), social.getId(), social.getType()));
89  });
90  return set;
91  }
92 
93  @NotifyChange("model")
94  @Command("update")
95  public void update() {}
96 
97  public List<SocialRelation> getModel() {
98  ArrayList<SocialRelation> list = new ArrayList<>();
99  if(socialGroup != null) {
100  WhereClause wc = new WhereClause();
101  wc.addClause("select br from BusinessRelation br");
102  wc.addClause("where br.validated = TRUE");
103  if(active) {
104  wc.addInRange("and", "br.startDate", "br.endDate", "date", new Date());
105  }
106 
107  wc.addClause("and ( exists (");
108  wc.addClause("select s from Syndication s");
109  wc.addClause("where s.name = :name");
110  wc.addClause("and s.contact = br.business");
111  wc.addClause(") and not exists (");
112  wc.addClause("select s from Syndication s");
113  wc.addClause("where s.name = :name");
114  wc.addClause("and s.contact = br.contact");
115  wc.addClause(")) or ( not exists (");
116  wc.addClause("select s from Syndication s");
117  wc.addClause("where s.name = :name");
118  wc.addClause("and s.contact = br.business");
119  wc.addClause(") and exists (");
120  wc.addClause("select s from Syndication s");
121  wc.addClause("where s.name = :name");
122  wc.addClause("and s.contact = br.contact");
123  wc.addClause("))");
124 
125  wc.addClause("and not exists (");
126  wc.addClause("select b2 from BusinessRelation b2");
127  wc.addClause("where b2.validated = TRUE");
128  wc.addInRange("and", "b2.startDate", "b2.endDate", "date", new Date());
129  wc.addClause("and b2.contact = br.contact");
130  wc.addClause("and b2.business <> br.business");
131  wc.addClause("and exists (");
132  wc.addClause("select s from Syndication s");
133  wc.addClause("where s.name = :name");
134  wc.addClause("and s.contact = b2.business");
135  wc.addClause("))");
136 
137  wc.addNamedValue("name", socialGroup.getKey());
138 
139  wc.addClause("order by br.business.name, br.contact.name");
140 
141  for(BusinessRelation br : new ContactsPU().getResultList(BusinessRelation.class, wc)) {
142  list.add(new SocialRelation(br.getBusiness(), br.getContact(), socialGroup.getKey()));
143  }
144  }
145  return list;
146  }
147 
148 }
void checkContact(@BindingParam("relation") SocialRelation relation)
void checkBusiness(@BindingParam("relation") SocialRelation relation)
void setSocialGroup(SocialGroupValue socialGroup)
Set< SocialGroupValue > getSocialGroupModel()
void addInRange(String operator, String startField, String endField, String attribute, Date date)
void addNamedValue(String name, Object value)
static Collection< SecurityGroup > allowedSocialGroups()
void syndicate(String syndicationName, boolean syndicate)