BrightSide Workbench Full Report + Source Code
TalentCategoriesVM.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2024 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.alliance.model;
20 
21 import java.util.List;
22 import org.turro.alliance.db.AlliancePU;
23 import org.turro.alliance.db.entities.AxTalentCategory;
24 import org.turro.i18n.I_;
25 import org.turro.jpa.Dao;
26 import org.turro.sql.SqlClause;
27 import org.turro.util.Cached;
28 import org.turro.zkoss.dialog.DialogField;
29 import org.turro.zkoss.dialog.Dialogs;
30 import org.zkoss.bind.BindUtils;
31 import org.zkoss.bind.annotation.Command;
32 import org.zkoss.bind.annotation.NotifyChange;
33 import org.zkoss.zul.Textbox;
34 
39 public class TalentCategoriesVM {
40 
41  public boolean isNeedsSave() {
42  return needsSave;
43  }
44 
45  public void setNeedsSave(boolean needsSave) {
46  this.needsSave = needsSave;
47  }
48 
49  @NotifyChange("model")
50  @Command("add")
51  public void add() {
52  Dialogs.title(I_.get("Add category"))
53  .width("400px")
54  .height("200px")
55  .addField(DialogField.field("Name"))
56  .onOk((dialogs) -> {
57  String domain = dialogs.<Textbox>getEditor("Name").getValue();
58  AxTalentCategory category = new AxTalentCategory();
59  category.setName(domain);
60  model.add(category);
61  needsSave = true;
62  BindUtils.postNotifyChange(null, null, TalentCategoriesVM.this, "model");
63  })
64  .emptyCancel()
65  .show();
66  }
67 
68  @NotifyChange("model")
69  @Command("save")
70  public void save() {
71  model.stream().filter(category -> category.isEmpty() && !category.isNew())
72  .forEach(category -> dao.get().deleteEntity(category));
73  model.removeIf(category -> category.isEmpty());
74  dao.get().saveEntities(model);
75  needsSave = false;
76  }
77 
78  /* Model */
79 
80  private boolean needsSave;
81 
82  private List<AxTalentCategory> model;
83 
84  public List<AxTalentCategory> getModel() {
85  if(model == null) {
86  model = SqlClause.select("c").from("AxTalentCategory c")
87  .orderBy("c.categoryId")
88  .dao(dao.get())
89  .resultList(AxTalentCategory.class);
90  }
91  return model;
92  }
93 
94  /* Dao */
95 
96  private final Cached<Dao> dao = Cached.instance(() -> new AlliancePU());
97 
98 }
static String get(String msg)
Definition: I_.java:41
static DialogField field(String label)
Dialogs width(String width)
Definition: Dialogs.java:70
Dialogs height(String height)
Definition: Dialogs.java:75
Dialogs onOk(Consumer< Dialogs > onOk)
Definition: Dialogs.java:85
static Dialogs title(String title)
Definition: Dialogs.java:161
Dialogs addField(DialogField field)
Definition: Dialogs.java:110