BrightSide Workbench Full Report + Source Code
SynonymsVM.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.synonyms;
20 
21 import java.util.List;
22 import java.util.Set;
23 import org.turro.string.Strings;
24 import org.turro.command.Context;
25 import org.turro.elephant.db.ElephantPU;
26 import org.turro.elephant.entities.db.Synonyms;
27 import org.turro.i18n.I_;
28 import org.turro.jpa.Dao;
29 import org.turro.zkoss.dialog.InputDialog;
30 import org.turro.zul.frame.Framework;
31 import org.zkoss.bind.BindUtils;
32 import org.zkoss.bind.annotation.BindingParam;
33 import org.zkoss.bind.annotation.Command;
34 import org.zkoss.bind.annotation.NotifyChange;
35 
40 public class SynonymsVM {
41 
42  private Synonyms selected;
43  private String searchValue;
44 
45  public SynonymsVM() {
46  }
47 
48  public Synonyms getSelected() {
49  return selected;
50  }
51 
52  public void setSelected(Synonyms selected) {
53  this.selected = selected;
54  }
55 
56  public String getSearchValue() {
57  return searchValue;
58  }
59 
60  public void setSearchValue(String searchValue) {
61  this.searchValue = searchValue;
62  }
63 
64  public boolean isSelected(Synonyms synonyms) {
65  return selected != null && synonyms != null &&
66  selected.getId() != null && synonyms.getId() != null &&
67  selected.getId().equals(synonyms.getId());
68  }
69 
70  @NotifyChange("model")
71  @Command("update")
72  public void update() {}
73 
74  @Command
75  public void addGroup() {
76  selected = new Synonyms();
77  addWord();
78  }
79 
80  @Command
81  @NotifyChange({"wordModel", "model"})
82  public void deleteGroup() {
83  if(selected != null) {
84  getDao().deleteObject(selected);
85  selected = null;
86  }
87  }
88 
89  @Command
90  public void addWord() {
91  if(selected != null) {
93  I_.get("Synonyms"), "Word",
94  "", null, 0, (Context context) -> {
95  String value = (String) context.get("value");
96  if(!Strings.isBlank(value)) {
97  selected.getWords().add(value);
98  selected = getDao().saveObject(selected);
99  BindUtils.postNotifyChange(null, null, SynonymsVM.this, "model");
100  BindUtils.postNotifyChange(null, null, SynonymsVM.this, "wordModel");
101  }
102  return null;
103  });
104  }
105  }
106 
107  @Command
108  @NotifyChange({"wordModel", "model"})
109  public void deleteWord(@BindingParam("word") String word) {
110  if(selected != null) {
111  if(selected.getWords().remove(word)) {
112  if(selected.isEmpty()) {
113  getDao().deleteObject(selected);
114  selected = null;
115  } else {
116  selected = getDao().saveObject(selected);
117  }
118  }
119  }
120  }
121 
122  @Command
123  @NotifyChange({"wordModel", "model"})
124  public void selectSynonyms(@BindingParam("selected") Synonyms selected) {
125  this.selected = selected;
126  }
127 
128  public Set<String> getWordModel() {
129  if(selected == null) return null;
130  return selected.getWords();
131  }
132 
133  public List<Synonyms> getModel() {
134  if(!Strings.isBlank(searchValue)) {
135  return Synonyms.getSynonyms(searchValue, 0);
136  } else {
137  return getDao().getResultList("select s from Synonyms s");
138  }
139  }
140 
141  /* Dao */
142 
143  private Dao _dao;
144 
145  private Dao getDao() {
146  if(_dao == null) {
147  _dao = new ElephantPU();
148  }
149  return _dao;
150  }
151 
152 }
static List< Synonyms > getSynonyms(String word)
Definition: Synonyms.java:92
static String get(String msg)
Definition: I_.java:41
void deleteObject(Object obj)
Definition: Dao.java:162
void selectSynonyms(@BindingParam("selected") Synonyms selected)
void setSelected(Synonyms selected)
Definition: SynonymsVM.java:52
List< Synonyms > getModel()
boolean isSelected(Synonyms synonyms)
Definition: SynonymsVM.java:64
void setSearchValue(String searchValue)
Definition: SynonymsVM.java:60
void deleteWord(@BindingParam("word") String word)
static void getInput(Page page, String title, String label, Object value, String format, int scale, final Command onOk)
static Framework getCurrent()
Definition: Framework.java:203