BrightSide Workbench Full Report + Source Code
UserSummaries.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2016 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.action;
20 
21 import java.util.Map;
22 import java.util.Set;
23 import java.util.TreeSet;
24 import org.turro.string.Strings;
25 import org.turro.annotation.UserSummary;
26 import org.turro.elephant.context.Application;
27 import org.turro.elephant.context.IConstructor;
28 import org.turro.marker.ElephantMarker;
29 import org.turro.reflection.Instances;
30 
35 public class UserSummaries {
36 
37  public static void initSummaries(IConstructor constructor) {
38  for(IUserSummary iSummary : getSortedSummaries()) {
39  iSummary.init(constructor);
40  }
41  }
42 
43  public static String parseSummaries(IConstructor constructor) {
44  initSummaries(constructor);
45  String prevSummary = null, separator = renderSeparator(constructor);
46  StringBuilder sb = new StringBuilder();
47  for(IUserSummary iSummary : getSortedSummaries()) {
48  String summary = iSummary.render(constructor);
49  if(!Strings.isBlank(summary)) {
50  if(!Strings.isBlank(prevSummary)) {
51  sb.append(separator);
52  }
53  sb.append(summary);
54  prevSummary = summary;
55  }
56  }
57  return sb.toString();
58  }
59 
60  /* Attributes */
61 
62  public static boolean isNotZero(String attribute) {
63  Object attr = Application.getApplication().getConstructor().findAttribute("us_" + attribute);
64  if(attr instanceof Number) {
65  return ((Number) attr).doubleValue() != 0.0;
66  }
67  return false;
68  }
69 
70  public static boolean isTrue(String attribute) {
71  Object attr = Application.getApplication().getConstructor().findAttribute("us_" + attribute);
72  if(attr instanceof Boolean) {
73  return ((Boolean) attr).booleanValue();
74  }
75  return false;
76  }
77 
78  public static Object getAttribute(String attribute) {
79  return Application.getApplication().getConstructor().findAttribute("us_" + attribute);
80  }
81 
82  public static Object getAttribute(String attribute, Object defaultValue) {
83  Object value = Application.getApplication().getConstructor().findAttribute("us_" + attribute);
84  return value == null ? defaultValue : value;
85  }
86 
87  public static void setAttribute(String attribute, Object value) {
88  Application.getApplication().getConstructor().setSessionAttribute("us_" + attribute, value);
89  }
90 
91  public static Map<String, Object> getAttributes() {
93  }
94 
95  public static void removeAttributes() {
96  for(String key : getAttributes().keySet()) {
98  }
99  }
100 
101  /* Separator */
102 
103  private static String renderSeparator(IConstructor constructor) {
104  ElephantMarker marker = new ElephantMarker(constructor);
106  return marker.parse("iterator", "separator");
107  }
108 
109  /* User summaries */
110 
111  private static Set<IUserSummary> getSortedSummaries() {
112  return new TreeSet<>(Instances.cached().byAnnotation(UserSummary.class, IUserSummary.class));
113  }
114 
115  private UserSummaries() {
116  }
117 
118 }
static Object getAttribute(String attribute, Object defaultValue)
static boolean isNotZero(String attribute)
static void setAttribute(String attribute, Object value)
static Map< String, Object > getAttributes()
static Object getAttribute(String attribute)
static void initSummaries(IConstructor constructor)
static String parseSummaries(IConstructor constructor)
static boolean isTrue(String attribute)
String parse(String rootTmpl, String tmpl)
Map< String, Object > getSessionAttributeMap(String prefix)
void setSessionAttribute(String key, Object value)