BrightSide Workbench Full Report + Source Code
NamesVM.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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.name;
20 
21 import java.util.List;
22 import java.util.Set;
23 import org.turro.contacts.Contact;
24 import org.turro.contacts.ContactType;
25 import org.turro.contacts.db.ContactsPU;
26 import org.turro.elephant.util.Messages;
27 import org.turro.jpa.Dao;
28 import org.turro.sql.SqlClause;
29 import org.turro.util.Cached;
30 import org.zkoss.bind.BindUtils;
31 import org.zkoss.bind.annotation.Command;
32 
37 public class NamesVM {
38 
39  private Set<NameEntry> selected;
40 
41  public Set<NameEntry> getSelected() {
42  return selected;
43  }
44 
45  public void setSelected(Set<NameEntry> selected) {
46  this.selected = selected;
47  }
48 
49  @Command
50  public void processSelected() {
51  if(selected != null && !selected.isEmpty()) {
52  Messages.confirmProcess().add(selected.size() + " items").show(() -> {
53  selected.forEach(item -> {
54  SqlClause.update("Contact")
55  .set("complexName.full", item.getFull())
56  .andSet("complexName.informal", item.getInformal())
57  .andSet("complexName.formal", item.getFormal())
58  .where().equal("id", item.getContactId())
59  .dao(dao.get()).execute();
60  });
61  BindUtils.postNotifyChange(null, null, NamesVM.this, "model");
62  });
63  }
64  }
65 
66  public List<NameEntry> getModel() {
67  return SqlClause.select("c").from("Contact c")
68  .where().group().isNull("complexName.formal").or().isNull("complexName.informal").endGroup()
69  .and().in("type", List.of(ContactType.CONTACT_USER, ContactType.CONTACT_INTERNAL))
70  .orderBy("complexName.full")
71  .dao(dao.get()).resultList(Contact.class).stream()
72  .map(c -> NameEntry.from(c.getId(), c.getComplexName().getFull(), false))
73  .filter(e -> !e.isEmpty())
74  .toList();
75  }
76 
77  private final Cached<Dao> dao = Cached.instance(() -> new ContactsPU());
78 
79 }
static NameEntry from(String contactId, String name, boolean entity)
void setSelected(Set< NameEntry > selected)
Definition: NamesVM.java:45
List< NameEntry > getModel()
Definition: NamesVM.java:66
Set< NameEntry > getSelected()
Definition: NamesVM.java:41
static Messages confirmProcess()
Definition: Messages.java:95
Messages add(String word)
Definition: Messages.java:50