BrightSide Workbench Full Report + Source Code
RelationsVM.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.relation;
20 
21 import java.util.List;
22 import org.turro.action.queue.ConstraintKeys;
23 import org.turro.string.Strings;
24 import org.turro.contacts.BusinessRelation;
25 import org.turro.contacts.db.ContactsPU;
26 import org.turro.elephant.db.SQLHelper;
27 import org.turro.elephant.db.WhereClause;
28 import org.turro.elephant.util.Messages;
29 import org.turro.jpa.Dao;
30 import org.turro.mail.queue.QueueManager;
31 import org.turro.security.SocialGroups;
32 import org.zkoss.bind.BindUtils;
33 import org.zkoss.bind.annotation.BindingParam;
34 import org.zkoss.bind.annotation.Command;
35 import org.zkoss.bind.annotation.NotifyChange;
36 
41 public class RelationsVM {
42 
43  private String searchValue;
44 
45  public RelationsVM() {
46  }
47 
48  @NotifyChange("model")
49  @Command
50  public void validate(@BindingParam("relation") BusinessRelation relation) {
51  if(relation != null) {
52  relation.setValidated(true);
53  getDao().saveObject(relation);
54  SocialGroups.checkInheritance(List.of(relation.getContact()), getDao());
56  new RelationAcceptedNotification(relation).send();
57  }
58  }
59 
60  @Command
61  public void delete(@BindingParam("relation") BusinessRelation relation) {
62  if(relation != null) {
63  Messages.confirmDeletion().show(() -> {
64  getDao().deleteObject(relation);
65  BindUtils.postNotifyChange(null, null, RelationsVM.this, "model");
66  });
67  }
68  }
69 
70  @NotifyChange("model")
71  @Command("update")
72  public void update() {}
73 
74  public List<BusinessRelation> getModel() {
75  WhereClause wc = new WhereClause();
76  wc.addClause("select r from BusinessRelation r");
77  wc.addClause("where r.validated = FALSE");
78  if(!Strings.isBlank(searchValue)) {
79  wc.addClause("and (r.contact.name like :searchValue");
80  wc.addClause("or r.business.name like :searchValue)");
81  wc.addNamedValue("searchValue", SQLHelper.convertToPartialLike(searchValue));
82  }
83  return getDao().getResultList(wc);
84  }
85 
86  public String getSearchValue() {
87  return searchValue;
88  }
89 
90  public void setSearchValue(String searchValue) {
91  this.searchValue = searchValue;
92  }
93 
94  /* Dao */
95 
96  private Dao _dao;
97 
98  private Dao getDao() {
99  if(_dao == null) {
100  _dao = new ContactsPU();
101  }
102  return _dao;
103  }
104 
105 }
106 
static ConstraintKeys from(IContact contact)
void validate(@BindingParam("relation") BusinessRelation relation)
List< BusinessRelation > getModel()
void setSearchValue(String searchValue)
static String convertToPartialLike(String value)
Definition: SQLHelper.java:38
void addNamedValue(String name, Object value)
static Messages confirmDeletion()
Definition: Messages.java:87
void deleteObject(Object obj)
Definition: Dao.java:162
void subscribeDefaults(ConstraintKeys keys)
static void checkInheritance(List< Contact > contacts, Dao dao)