BrightSide Workbench Full Report + Source Code
InterestsVM.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.interest;
20 
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24 import org.turro.action.EntityInfoContent;
25 import org.turro.action.EntityInfoType;
26 import org.turro.action.LinkType;
27 import org.turro.auth.Authentication;
28 import org.turro.cache.SessionObjects;
29 import org.turro.elephant.context.Application;
30 import org.turro.elephant.context.ElephantContext;
31 import org.zkoss.bind.annotation.BindingParam;
32 import org.zkoss.bind.annotation.Command;
33 import org.zkoss.bind.annotation.NotifyChange;
34 
39 public class InterestsVM {
40 
41  public InterestsVM() {
42  SessionObjects objects = SessionObjects.instance(Application.getResolverRequest());
43  selectedCategories = objects.sync("interest-categories", HashSet.class, () -> new HashSet<>());
44  selectedLabels = objects.sync("interest-labels", HashSet.class, () -> new HashSet<>());
45  }
46 
47  @NotifyChange({"model", "categories", "labels"})
48  @Command("selectAllCats")
49  public void selectAllCats() {
50  selectedCategories.addAll(InterestCategories.get());
51  interests.clear();
52  }
53 
54  @NotifyChange({"model", "categories", "labels"})
55  @Command("removeAllCats")
56  public void removeAllCats() {
57  selectedCategories.clear();
58  interests.clear();
59  }
60 
61  public boolean isSelected(InterestCategory category) {
62  return selectedCategories.contains(category);
63  }
64 
65  @NotifyChange({"model", "labels"})
66  @Command("toggleCategory")
67  public void toggleCategory(@BindingParam("category") InterestCategory category) {
68  if(selectedCategories.contains(category)) {
69  selectedCategories.remove(category);
70  } else {
71  selectedCategories.add(category);
72  }
73  interests.clear();
74  }
75 
76  @NotifyChange({"model", "labels"})
77  @Command("selectAllLabels")
78  public void selectAllLabels() {
79  selectedLabels.addAll(interests.labels());
80  interests.clear();
81  }
82 
83  @NotifyChange({"model", "labels"})
84  @Command("removeAllLabels")
85  public void removeAllLabels() {
86  selectedLabels.clear();
87  interests.clear();
88  }
89 
90  public boolean isSelected(String label) {
91  return selectedLabels.contains(label);
92  }
93 
94  @NotifyChange("model")
95  @Command("toggleLabel")
96  public void toggleLabel(@BindingParam("label") String label) {
97  if(selectedLabels.contains(label)) {
98  selectedLabels.remove(label);
99  } else {
100  selectedLabels.add(label);
101  }
102  }
103 
104  /* Selection */
105 
106  private final Set<InterestCategory> selectedCategories;
107  private final Set<String> selectedLabels;
108 
109  /* Model */
110 
111  private final Interests interests = Interests.from(Authentication.getIContact());
112 
113  public List<Interest> getModel() {
114  if(interests.isEmpty()) {
115  interests.collect(selectedCategories);
116  }
117  return interests.get().stream()
118  .filter(i -> selectedLabels.contains(i.getLabel()))
119  .toList();
120  }
121 
122  public List<InterestCategory> getCategories() {
123  return InterestCategories.get();
124  }
125 
126  public List<String> getLabels() {
127  return interests.labels();
128  }
129 
130  /* Info */
131 
132  public String getLinkAttribute(Interest interest) {
133  return switch(interest.getEntity().getRoot()) {
134  case "contact", "convocation", "issue", "publication", "service", "practicalwork" ->
135  "onclick=\"" +
138  "\"";
139  default -> "href=\"" +
141  "\"";
142  };
143  }
144 
145 }
static String createURL(EntityInfoType type, LinkType link)
static HttpServletRequest getResolverRequest()
static final List< InterestCategory > get()
IElephantEntity getEntity()
Definition: Interest.java:45
List< InterestCategory > getCategories()
boolean isSelected(InterestCategory category)
void toggleLabel(@BindingParam("label") String label)
void toggleCategory(@BindingParam("category") InterestCategory category)
boolean isSelected(String label)
String getLinkAttribute(Interest interest)
void collect(Set< InterestCategory > categories)
Definition: Interests.java:35
static Interests from(IContact contact)
Definition: Interests.java:80
Set< Interest > get()
Definition: Interests.java:66
List< String > labels()
Definition: Interests.java:62