BrightSide Workbench Full Report + Source Code
TagCloud.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.tags;
20 
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.stream.Collectors;
24 import org.turro.collections.KeyValueMap;
25 import org.turro.elephant.context.IConstructor;
26 import org.turro.marker.MarkerHelper;
27 
32 public class TagCloud {
33 
34  private static final String TAGS_CONTEXT = "tag-cloud-ctx";
35 
36  public static TagSet getTags(IConstructor constructor, String root) {
37  TagSet tags = (TagSet) constructor.getSessionAttribute(TAGS_CONTEXT + "-" + root);
38  if(tags == null) {
39  tags = Tags.getAvailables(root);
40  constructor.setSessionAttribute(TAGS_CONTEXT + "-" + root, tags);
41  }
42  return tags;
43  }
44 
45  public static void toggleTags(IConstructor constructor, String root) {
46  KeyValueMap kvm = MarkerHelper.getObfuscatedParameters();
47  if(kvm != null && kvm.containsKey("tag")) {
48  TagSet set = (TagSet) constructor.getSessionAttribute(TAGS_CONTEXT + "-" + root);
49  if(set != null) {
50  set.toggle(kvm.get("tag"));
51  set.markSiblingsFromSelection(root);
52  }
53  }
54  }
55 
56  public static void resetContext(IConstructor constructor, String root) {
57  constructor.removeSessionAttribute(TAGS_CONTEXT + "-" + root);
58  }
59 
60  public static List<String> getIdentifiers(IConstructor constructor, String root) {
61  TagSet set = (TagSet) constructor.getSessionAttribute(TAGS_CONTEXT + "-" + root);
62  if(set != null) {
63  return set.getIdentifiers(root);
64  }
65  return Collections.EMPTY_LIST;
66  }
67 
68  public static List<Long> getIdentifiersAsLong(IConstructor constructor, String root) {
69  return getIdentifiers(constructor, root).stream().map(s -> Long.valueOf(s)).collect(Collectors.toList());
70  }
71 
72  public static boolean hasSelected(IConstructor constructor, String root) {
73  TagSet set = (TagSet) constructor.getSessionAttribute(TAGS_CONTEXT + "-" + root);
74  if(set != null) {
75  return !(set.getSelected().isEmpty());
76  }
77  return false;
78  }
79 
80 }
static KeyValueMap getObfuscatedParameters()
static void toggleTags(IConstructor constructor, String root)
Definition: TagCloud.java:45
static boolean hasSelected(IConstructor constructor, String root)
Definition: TagCloud.java:72
static TagSet getTags(IConstructor constructor, String root)
Definition: TagCloud.java:36
static void resetContext(IConstructor constructor, String root)
Definition: TagCloud.java:56
static List< Long > getIdentifiersAsLong(IConstructor constructor, String root)
Definition: TagCloud.java:68
static List< String > getIdentifiers(IConstructor constructor, String root)
Definition: TagCloud.java:60
static TagSet getAvailables()
Definition: Tags.java:65
void setSessionAttribute(String key, Object value)