BrightSide Workbench Full Report + Source Code
ProjectGrantFilter.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.client;
20 
21 import java.time.Instant;
22 import java.util.Date;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.TreeSet;
26 import org.turro.alliance.db.entities.AxProjectGrant;
27 import org.turro.string.Strings;
28 import org.turro.time.Time;
29 import org.zkoss.bind.BindUtils;
30 import org.zkoss.zul.Checkbox;
31 
36 public class ProjectGrantFilter {
37 
38  private transient ProjectGrantVM vm;
39 
40  private String search, template;
41 
42  private final Set<String> sources = new TreeSet<>();
43  private final Set<String> tags = new TreeSet<>();
44 
45  private Boolean minimis,
46  subvention, loan, inKind,
47  collaboration,
48  micro, small, medium, big, otherOrgs,
49  hiring, idi, investment, startUp, formation,
50  active, endOfBudget, recurrent;
51 
53  this.vm = vm;
54  }
55 
56  public void setVm(ProjectGrantVM vm) {
57  this.vm = vm;
58  }
59 
60  public String getTemplate() {
61  return Strings.isBlank(template, "items");
62  }
63 
64  public void setTemplate(String template) {
65  this.template = template;
66  BindUtils.postNotifyChange(vm, "grants");
67  }
68 
69  public String getSearch() {
70  return search;
71  }
72 
73  public void setSearch(String search) {
74  this.search = search;
75  BindUtils.postNotifyChange(vm, "grants");
76  }
77 
78  public Checkbox.State getMinimis() {
79  return getTristate(minimis);
80  }
81 
82  public void setMinimis(Checkbox.State minimis) {
83  this.minimis = setTristate(minimis);
84  BindUtils.postNotifyChange(vm, "grants");
85  }
86 
87  public Checkbox.State getSubvention() {
88  return getTristate(subvention);
89  }
90 
91  public void setSubvention(Checkbox.State subvention) {
92  this.subvention = setTristate(subvention);
93  BindUtils.postNotifyChange(vm, "grants");
94  }
95 
96  public Checkbox.State getLoan() {
97  return getTristate(loan);
98  }
99 
100  public void setLoan(Checkbox.State loan) {
101  this.loan = setTristate(loan);
102  BindUtils.postNotifyChange(vm, "grants");
103  }
104 
105  public Checkbox.State getInKind() {
106  return getTristate(inKind);
107  }
108 
109  public void setInKind(Checkbox.State inKind) {
110  this.inKind = setTristate(inKind);
111  BindUtils.postNotifyChange(vm, "grants");
112  }
113 
114  public Checkbox.State getCollaboration() {
115  return getTristate(collaboration);
116  }
117 
118  public void setCollaboration(Checkbox.State collaboration) {
119  this.collaboration = setTristate(collaboration);
120  BindUtils.postNotifyChange(vm, "grants");
121  }
122 
123  public Checkbox.State getMicro() {
124  return getTristate(micro);
125  }
126 
127  public void setMicro(Checkbox.State micro) {
128  this.micro = setTristate(micro);
129  BindUtils.postNotifyChange(vm, "grants");
130  }
131 
132  public Checkbox.State getSmall() {
133  return getTristate(small);
134  }
135 
136  public void setSmall(Checkbox.State small) {
137  this.small = setTristate(small);
138  BindUtils.postNotifyChange(vm, "grants");
139  }
140 
141  public Checkbox.State getMedium() {
142  return getTristate(medium);
143  }
144 
145  public void setMedium(Checkbox.State medium) {
146  this.medium = setTristate(medium);
147  BindUtils.postNotifyChange(vm, "grants");
148  }
149 
150  public Checkbox.State getBig() {
151  return getTristate(big);
152  }
153 
154  public void setBig(Checkbox.State big) {
155  this.big = setTristate(big);
156  BindUtils.postNotifyChange(vm, "grants");
157  }
158 
159  public Checkbox.State getOtherOrgs() {
160  return getTristate(otherOrgs);
161  }
162 
163  public void setOtherOrgs(Checkbox.State otherOrgs) {
164  this.otherOrgs = setTristate(otherOrgs);
165  BindUtils.postNotifyChange(vm, "grants");
166  }
167 
168  public Checkbox.State getHiring() {
169  return getTristate(hiring);
170  }
171 
172  public void setHiring(Checkbox.State hiring) {
173  this.hiring = setTristate(hiring);
174  BindUtils.postNotifyChange(vm, "grants");
175  }
176 
177  public Checkbox.State getIdi() {
178  return getTristate(idi);
179  }
180 
181  public void setIdi(Checkbox.State idi) {
182  this.idi = setTristate(idi);
183  BindUtils.postNotifyChange(vm, "grants");
184  }
185 
186  public Checkbox.State getInvestment() {
187  return getTristate(investment);
188  }
189 
190  public void setInvestment(Checkbox.State investment) {
191  this.investment = setTristate(investment);
192  BindUtils.postNotifyChange(vm, "grants");
193  }
194 
195  public Checkbox.State getStartUp() {
196  return getTristate(startUp);
197  }
198 
199  public void setStartUp(Checkbox.State startUp) {
200  this.startUp = setTristate(startUp);
201  BindUtils.postNotifyChange(vm, "grants");
202  }
203 
204  public Checkbox.State getFormation() {
205  return getTristate(formation);
206  }
207 
208  public void setFormation(Checkbox.State formation) {
209  this.formation = setTristate(formation);
210  BindUtils.postNotifyChange(vm, "grants");
211  }
212 
213  public Checkbox.State getActive() {
214  return getTristate(active);
215  }
216 
217  public void setActive(Checkbox.State active) {
218  this.active = setTristate(active);
219  BindUtils.postNotifyChange(vm, "grants");
220  }
221 
222  public Checkbox.State getEndOfBudget() {
223  return getTristate(endOfBudget);
224  }
225 
226  public void setEndOfBudget(Checkbox.State endOfBudget) {
227  this.endOfBudget = setTristate(endOfBudget);
228  BindUtils.postNotifyChange(vm, "grants");
229  }
230 
231  public Checkbox.State getRecurrent() {
232  return getTristate(recurrent);
233  }
234 
235  public void setRecurrent(Checkbox.State recurrent) {
236  this.recurrent = setTristate(recurrent);
237  BindUtils.postNotifyChange(vm, "grants");
238  }
239 
240  public Set<String> getSources() {
241  return sources;
242  }
243 
244  public void addSource(String value) {
245  sources.add(value);
246  }
247 
248  public void removeSource(String value) {
249  sources.remove(value);
250  }
251 
252  public boolean containsSource(String value) {
253  return sources.contains(value);
254  }
255 
256  public void clearSources() {
257  sources.clear();
258  }
259 
260  public Set<String> getTags() {
261  return tags;
262  }
263 
264  public void addTag(String value) {
265  tags.add(value);
266  }
267 
268  public void removeTag(String value) {
269  tags.remove(value);
270  }
271 
272  public boolean containsTag(String value) {
273  return tags.contains(value);
274  }
275 
276  public void clearTags() {
277  tags.clear();
278  }
279 
280  public void clear() {
281  search = null;
282  minimis = null;
283  subvention = null;
284  loan = null;
285  inKind = null;
286  collaboration = null;
287  micro = null;
288  small = null;
289  medium = null;
290  big = null;
291  otherOrgs = null;
292  hiring = null;
293  idi = null;
294  investment = null;
295  startUp = null;
296  formation = null;
297  endOfBudget = null;
298  recurrent = null;
299  active = null;
300  clearSources();
301  clearTags();
302  }
303 
304  public List<AxProjectGrant> process(List<AxProjectGrant> grants, List<TagItem> itags) {
305  return grants.stream()
306  .filter(g -> fits(g, itags))
307  .toList();
308  }
309 
310  private boolean fits(AxProjectGrant projectGrant, List<TagItem> itags) {
311  return (Strings.isBlank(search) || Strings.findsIgnoreCase(projectGrant.getTitle(), search)) &&
312  (minimis == null || (minimis.equals(projectGrant.isMinimis()))) &&
313  (subvention == null || subvention.equals(projectGrant.isSubvention())) &&
314  (loan == null || loan.equals(projectGrant.isLoan())) &&
315  (inKind == null || inKind.equals(projectGrant.isInKind())) &&
316  (collaboration == null || collaboration.equals(projectGrant.isCollaboration())) &&
317  (micro == null || micro.equals(projectGrant.isMicro())) &&
318  (small == null || small.equals(projectGrant.isSmall())) &&
319  (medium == null || medium.equals(projectGrant.isMedium())) &&
320  (big == null || big.equals(projectGrant.isBig())) &&
321  (otherOrgs == null || otherOrgs.equals(projectGrant.isOtherOrgs())) &&
322  (hiring == null || hiring.equals(projectGrant.isHiring())) &&
323  (idi == null || idi.equals(projectGrant.isIdi())) &&
324  (investment == null || investment.equals(projectGrant.isInvestment())) &&
325  (startUp == null || startUp.equals(projectGrant.isStartUp())) &&
326  (formation == null || formation.equals(projectGrant.isFormation())) &&
327  (endOfBudget == null || endOfBudget.equals(projectGrant.isEndOfBudget())) &&
328  (recurrent == null || recurrent.equals(projectGrant.isRecurrent())) &&
329  (active == null || active.equals(isActive(projectGrant.getEndDate()))) &&
330  (sources.isEmpty() || sources.contains(projectGrant.getSource())) &&
331  (tags.isEmpty() || itags.stream()
332  .filter(t -> t.id().equals(projectGrant.getId()))
333  .anyMatch(t -> tags.contains(t.tag())));
334 
335  }
336 
337  private boolean isActive(Date value) {
338  return value == null || Time.from(value).isAfter(Instant.now());
339  }
340 
341  private Checkbox.State getTristate(Boolean value) {
342  return value == null ? Checkbox.State.INDETERMINATE :
343  (value ? Checkbox.State.CHECKED : Checkbox.State.UNCHECKED);
344  }
345 
346  private Boolean setTristate(Checkbox.State value) {
347  return switch(value) {
348  case CHECKED -> true;
349  case UNCHECKED -> false;
350  default -> null;
351  };
352  }
353 
354  public record TagItem(String tag, String id) {};
355 
356 }
void setEndOfBudget(Checkbox.State endOfBudget)
void setSubvention(Checkbox.State subvention)
void setCollaboration(Checkbox.State collaboration)
void setInvestment(Checkbox.State investment)
List< AxProjectGrant > process(List< AxProjectGrant > grants, List< TagItem > itags)