BrightSide Workbench Full Report + Source Code
FunnelGoalsVM.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.web.funnel.model;
20 
21 import java.util.List;
22 import org.turro.elephant.db.ElephantPU;
23 import org.turro.elephant.entities.web.WebGoal;
24 import org.turro.elephant.util.Messages;
25 import org.turro.elephant.util.Toasts;
26 import org.turro.i18n.I_;
27 import org.turro.jpa.Dao;
28 import org.turro.sql.SqlClause;
29 import org.turro.util.Cached;
30 import org.turro.zkoss.dialog.DialogField;
31 import org.turro.zkoss.dialog.Dialogs;
32 import org.zkoss.bind.BindUtils;
33 import org.zkoss.bind.annotation.NotifyChange;
34 import org.zkoss.zk.ui.Executions;
35 import org.zkoss.zul.Textbox;
36 
41 public class FunnelGoalsVM {
42 
43  public WebGoal getSelected() {
44  return selected;
45  }
46 
47  public void setSelected(WebGoal selected) {
48  this.selected = selected;
49  BindUtils.postNotifyChange(null, null, FunnelGoalsVM.this,
50  "selected", "showForm", "nextsModel");
51  }
52 
53  public boolean isShowForm() {
54  return selected != null;
55  }
56 
57  public void addGoal() {
58  Dialogs.title(I_.get("Add"))
59  .width("400px")
60  .height("250px")
61  .addField(DialogField.field("GoalAction"))
62  .onOk((dialogs) -> {
63  String goalAction = dialogs.<Textbox>getEditor("GoalAction").getValue();
64  if(notInModel(goalAction)) {
65  WebGoal wi = new WebGoal();
66  wi.setGoalAction(goalAction);
67  if(wi.isValid()) {
68  model.add(wi);
69  selected = wi;
70  BindUtils.postNotifyChange(null, null, FunnelGoalsVM.this,
71  "model", "selected", "showForm");
72  } else {
73  Toasts.message("GoalActions should not include spaces or punctuation characters.").show();
74  }
75  } else {
76  Toasts.message("GoalAction already in the model.").show();
77  }
78  })
79  .emptyCancel()
80  .show();
81  }
82 
83  @NotifyChange({"model", "selected", "showForm"})
84  public void save() {
85  model.stream().filter(wi -> wi.isEmpty() && !wi.isNew())
86  .forEach(wi -> dao.get().deleteEntity(wi));
87  model.removeIf(wi -> wi.isEmpty());
88  model.forEach(wi -> wi.compose());
89  dao.get().saveEntities(model);
90  Executions.getCurrent().sendRedirect("");
91  }
92 
93  public void delete() {
94  Messages.confirmDeletion().add(selected.getItemLabel()).show(() -> {
95  dao.get().deleteObject(selected);
96  Executions.getCurrent().sendRedirect("");
97  });
98  }
99 
100  /* Utils */
101 
102  private boolean notInModel(String actionGoal) {
103  return !getModel().stream().anyMatch(wg -> wg.getGoalAction().equals(actionGoal));
104  }
105 
106  /* Models */
107 
108  private WebGoal selected;
109 
110  private List<WebGoal> model;
111 
112  public List<WebGoal> getModel() {
113  if(model == null) {
114  model = SqlClause.select("wg").from("WebGoal wg")
115  .orderBy("ordering, goalAction")
116  .dao(dao.get())
117  .resultList(WebGoal.class);
118  }
119  return model;
120  }
121 
122  /* Dao */
123 
124  private final Cached<Dao> dao = Cached.instance(() -> new ElephantPU());
125 
126 }
void setGoalAction(String goalAction)
Definition: WebGoal.java:68
static Messages confirmDeletion()
Definition: Messages.java:87
Messages add(String word)
Definition: Messages.java:50
static Toasts message(String message)
Definition: Toasts.java:114
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