BrightSide Workbench Full Report + Source Code
UserContents.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.user.content;
20 
21 import java.util.Collections;
22 import java.util.Set;
23 import java.util.TreeSet;
24 import org.turro.string.Strings;
25 import org.turro.elephant.context.Application;
26 import org.turro.elephant.context.IConstructor;
27 import org.turro.marker.ElephantMarker;
28 import org.turro.plugin.contacts.IContact;
29 import org.turro.reflection.ClassNames;
30 import org.turro.reflection.Reflections;
31 
36 public class UserContents {
37 
38  private static final String USER_CONTENT_CONTEXT = "elusrctx_";
39 
40  public static String parseContents(IConstructor constructor) {
41  ElephantMarker marker = new ElephantMarker(constructor);
42  return marker.parse("widgets/user", "userContent");
43  }
44 
45  public static Set<IElephantUserContent> getContents(IConstructor constructor, IContact contact) {
46  return getContents(constructor, contact, false);
47  }
48 
49  public static Set<IElephantUserContent> getContents(IConstructor constructor, IContact contact, boolean fresh) {
50  if(contact == null || !contact.isValid()) return Collections.EMPTY_SET;
51  Set<IElephantUserContent> set = (Set<IElephantUserContent>) constructor.getSessionAttribute(USER_CONTENT_CONTEXT + contact.getId());
52  if(fresh || (set == null)) {
53  set = loadContents(contact);
54  constructor.setSessionAttribute(USER_CONTENT_CONTEXT + contact.getId(), set);
55  }
56  return set;
57  }
58 
59  public static Set<String> getTags(IConstructor constructor, IContact contact) {
60  Set<String> set = new TreeSet<>();
61  for(IElephantUserContent euc : getContents(constructor, contact)) {
62  if(!Strings.isBlank(euc.render(constructor))) {
63  set.addAll(euc.getTags());
64  }
65  }
66  return set;
67  }
68 
69  public static void reset() {
71  }
72 
73  public static void reset(IConstructor constructor, IContact contact) {
74  for(IElephantUserContent euc : getContents(constructor, contact)) {
75  if(euc instanceof AbstractUserContent) {
76  ((AbstractUserContent) euc).reset();
77  }
78  }
79  }
80 
81  private static Set<IElephantUserContent> loadContents(IContact contact) {
82  Set<IElephantUserContent> set = new TreeSet<>();
83  for(String className : ClassNames.cached().byAnnotation(ElephantUserContent.class.getName())) {
84  IElephantUserContent instance = (IElephantUserContent) Reflections.of(className)
85  .is(IElephantUserContent.class)
86  .cast(className)
87  .classes(IContact.class)
88  .objects(contact)
89  .instance();
90  if(instance != null) {
91  set.add(instance);
92  }
93  }
94  return set;
95  }
96 
97  private UserContents() {
98  }
99 
100 }
String parse(String rootTmpl, String tmpl)
static void reset(IConstructor constructor, IContact contact)
static String parseContents(IConstructor constructor)
static Set< IElephantUserContent > getContents(IConstructor constructor, IContact contact, boolean fresh)
static Set< IElephantUserContent > getContents(IConstructor constructor, IContact contact)
static Set< String > getTags(IConstructor constructor, IContact contact)
void setSessionAttribute(String key, Object value)