BrightSide Workbench Full Report + Source Code
UserMenus.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 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.elephant.user.menu;
20 
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.TreeSet;
26 import java.util.concurrent.locks.Lock;
27 import java.util.concurrent.locks.ReentrantLock;
28 import org.turro.string.Strings;
29 import org.turro.action.UserSummaries;
30 import org.turro.elephant.context.Application;
31 import org.turro.elephant.context.IConstructor;
32 import org.turro.path.Path;
33 import org.turro.reflection.ClassNames;
34 import org.turro.reflection.Reflections;
35 
40 public class UserMenus {
41 
42  public static Set<IUserMenu> getUserMenu(IConstructor constructor) {
43  if(constructor.getUser() != null) {
44  setActive();
45  UserSummaries.initSummaries(constructor);
46  return userMenus();
47  } else {
48  return Collections.EMPTY_SET;
49  }
50  }
51 
52  public static Object executeMenu(String idMenu) {
53  return doExecuteMenu(userMenus(), idMenu);
54  }
55 
56  public static boolean isInRole(String path) {
57  if(!Strings.isBlank(path) && path.startsWith("/user/")) {
58  IUserMenu userMenu = getUserMenuByPath(userMenus(), Path.reduce(path, 2));
59  return userMenu != null ? userMenu.isInRole() : true;
60  }
61  return true;
62  }
63 
64  private static IUserMenu getUserMenuByPath(Set<IUserMenu> menus, String path) {
65  IUserMenu result = null;
66  for(IUserMenu userMenu : menus) {
67  if(path.equals(userMenu.getLink())) {
68  return userMenu;
69  }
70  if(userMenu.getChildren() != null) {
71  result = getUserMenuByPath(userMenu.getChildren(), path);
72  }
73  if(result != null) break;
74  }
75  return result;
76  }
77 
78  private static Object doExecuteMenu(Set<IUserMenu> menus, String idMenu) {
79  for(IUserMenu userMenu : menus) {
80  if(idMenu.equals(userMenu.getId())) {
81  return userMenu.execute();
82  }
83  if(userMenu.getChildren() != null) doExecuteMenu(userMenu.getChildren(), idMenu);
84  }
85  return null;
86  }
87 
88  public static IUserMenu getSessionMenu() {
89  for(IUserMenu um : userMenus()) {
90  if(um instanceof SessionUserMenu) {
91  return um;
92  }
93  }
94  return null;
95  }
96 
97  public static IUserMenu getSettingsMenu() {
98  for(IUserMenu um : userMenus()) {
99  if(um instanceof SettingsUserMenu) {
100  return um;
101  }
102  }
103  return null;
104  }
105 
106  public static void setActive() {
109  }
110 
111  public static String getActive() {
112  return (String) Application.getApplication().getConstructor().getSessionAttribute("currPath_usrmx");
113  }
114 
115  /* Attributes */
116 
117  public static Object getAttribute(String attribute) {
118  return Application.getApplication().getConstructor().findAttribute("um_" + attribute);
119  }
120 
121  public static void setAttribute(String attribute, Object value) {
122  Application.getApplication().getConstructor().setSessionAttribute("um_" + attribute, value);
123  }
124 
125  public static Map<String, Object> getAttributes() {
127  }
128 
129  public static void removeAttributes() {
130  for(String key : getAttributes().keySet()) {
132  }
133  }
134 
135  /* User menus */
136 
137  private static TreeSet<IUserMenu> _userMenus;
138  private static final Lock lock = new ReentrantLock();
139 
140  private static Set<IUserMenu> userMenus() {
141  if(_userMenus == null) {
142  lock.lock();
143  try {
144  if(_userMenus == null) {
145  _userMenus = new TreeSet<>();
146  List<String> menus = ClassNames.cached().byAnnotation(UserMenu.class.getName());
147  if(menus != null) {
148  for(String menu : menus) {
149  Object obj = Reflections.of(menu).instance();
150  if(obj instanceof IUserMenu) {
151  if(((IUserMenu) obj).isValid()) {
152  _userMenus.add((IUserMenu) obj);
153  }
154  }
155  }
156  }
157  for(IUserMenu um : _userMenus) {
158  um.initMenu();
159  }
160  }
161  } finally {
162  lock.unlock();
163  }
164  }
165  return _userMenus;
166  }
167 
168  private UserMenus() {
169  }
170 
171 }
static void initSummaries(IConstructor constructor)
static void setAttribute(String attribute, Object value)
Definition: UserMenus.java:121
static boolean isInRole(String path)
Definition: UserMenus.java:56
static Set< IUserMenu > getUserMenu(IConstructor constructor)
Definition: UserMenus.java:42
static Object getAttribute(String attribute)
Definition: UserMenus.java:117
static Object executeMenu(String idMenu)
Definition: UserMenus.java:52
static Map< String, Object > getAttributes()
Definition: UserMenus.java:125
Map< String, Object > getSessionAttributeMap(String prefix)
void setSessionAttribute(String key, Object value)