BrightSide Workbench Full Report + Source Code
menu/UserMenu.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.elephant.menu;
20 
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Set;
25 import java.util.concurrent.locks.Lock;
26 import java.util.concurrent.locks.ReentrantLock;
27 import org.turro.action.Contacts;
28 import org.turro.annotation.ProfileAction;
29 import org.turro.annotation.UserAction;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.elephant.context.IConstructor;
32 import org.turro.i18n.I_;
33 import org.turro.plugin.contacts.IContact;
34 import org.turro.reflection.ReflectionUtil;
35 
40 @Deprecated
41 public class UserMenu {
42 
43  private final IConstructor constructor;
44  private IContact contact;
45 
46  public UserMenu(IConstructor constructor) {
47  this.constructor = constructor;
48  }
49 
50  public Collection<IUserAction> getActions() {
51  if(constructor.isInRole("webmenu:out")) {
52  return noActions(constructor);
53  }
54  return userActions(constructor);
55  }
56 
57  public Collection<IUserAction> getProfileActions() {
58  return profileActions(constructor);
59  }
60 
61  public boolean isAllowed(IUserAction action) {
62  if("/app/frame".equals(action.getLink())) {
63  if(!getUser().isWebapp()) {
64  return false;
65  }
66  }
67  return true;
68  }
69 
70  public IContact getUser() {
71  if(contact == null) {
72  contact = Contacts.getBehavingAs();
73  }
74  return contact;
75  }
76 
77  public String getAvatar() {
78  IContact contact = getUser();
79  if(contact != null) {
80  File favatar = new File(ElephantContext.getRealPath("/_internal/files/contact/" + contact.getId() + "/profile/face.png"));
81  if(favatar.exists()) {
82  return ElephantContext.getRootWebPath() + "/_internal/files/contact/" + contact.getId() + "/profile/face.png";
83  }
84  }
85  return null;
86  }
87 
88  /* IUserActions Statics */
89 
90  private static Collection<IUserAction> _userActions, _profileActions, _noActions;
91  private static final Lock lock = new ReentrantLock();
92 
93  public static void executeAction(String idAction) {
94  if(_userActions != null) {
95  for(IUserAction userAction : _userActions) {
96  if(idAction.equals(userAction.getId())) {
97  userAction.execute();
98  return;
99  }
100  }
101  }
102  if(_profileActions != null) {
103  for(IUserAction userAction : _profileActions) {
104  if(idAction.equals(userAction.getId())) {
105  userAction.execute();
106  return;
107  }
108  }
109  }
110  }
111 
112  private static Collection<IUserAction> userActions(IConstructor constructor) {
113  if(_userActions == null) {
114  lock.lock();
115  try {
116  if(_userActions == null) {
117  _userActions = new ArrayList<>();
118  elephantActions(constructor);
119  Set<String> menus = ElephantContext.getClassesByAnnotation(UserAction.class.getName());
120  if(menus != null) {
121  for(String menu : menus) {
122  Object obj = ReflectionUtil.instance(ReflectionUtil.classCheck(menu));
123  if(obj instanceof IUserAction) {
124  if(((IUserAction) obj).isValid()) {
125  _userActions.add((IUserAction) obj);
126  }
127  }
128  }
129  }
130  }
131  } finally {
132  lock.unlock();
133  }
134  }
135  return _userActions;
136  }
137 
138  private static Collection<IUserAction> noActions(IConstructor constructor) {
139  if(_noActions == null) {
140  lock.lock();
141  try {
142  if(_noActions == null) {
143  String root = ElephantContext.getRootWebPath();
144  _noActions = new ArrayList<>();
145  _noActions.add(new DefaultUserAction("home", I_.get("Home"), null, root + "/user"));
146  _noActions.add(new DefaultUserAction("agreements", "Agreements", null, root + "/user/myagreements"));
147  }
148  } finally {
149  lock.unlock();
150  }
151  }
152  return _noActions;
153  }
154 
155  private static void elephantActions(IConstructor constructor) {
156  String root = ElephantContext.getRootWebPath();
157 
158  boolean contracts = ReflectionUtil.classCheckSilent("org.turro.financials.entity.Contract") != null,
159  dossiers = ReflectionUtil.classCheckSilent("org.turro.dossier.entity.Dossier") != null,
160  publications = ReflectionUtil.classCheckSilent("org.turro.publication.entity.Publication") != null;
161 
162  _userActions.add(new DefaultUserAction("home", "Home", null, root + "/user"));
163  _userActions.add(new DefaultUserAction("session", "Session", null));
164  _userActions.add(new DefaultUserAction("acceptances", "Acceptances",
165  root + "/_zul/images/64/acceptance.png", root + "/user/acceptances"));
166  _userActions.add(new DefaultUserAction("agreements", "Agreements",
167  root + "/_zul/images/64/acceptance.png", root + "/user/myagreements"));
168  _userActions.add(new DefaultUserAction("polls", "Polls",
169  root + "/_zul/images/64/poll.png", root + "/user/mypolls"));
170  if(publications) {
171  _userActions.add(new DefaultUserAction("myblogs", "My blogs",
172  null, root + "/user/myblogs"));
173  _userActions.add(new DefaultUserAction("subscriptions", "Subscriptions",
174  root + "/_zul/images/64/subscription.png", root + "/user/subscriptions"));
175  }
176  _userActions.add(new DefaultUserAction("mycalendar", "Calendar",
177  root + "/_zul/images/64/calendar.png", root + "/user/mycalendar"));
178  _userActions.add(new DefaultUserAction("end", "end", null));
179  _userActions.add(new DefaultUserAction("mycontacts", "Contacts",
180  root + "/_zul/images/64/contacts.png", root + "/user/mycontacts"));
181  if(contracts) {
182  _userActions.add(new DefaultUserAction("financials", "Billing", null));
183  _userActions.add(new DefaultUserAction("contracts", "Contracts",
184  root + "/_zul/images/64/contract.png", root + "/user/contracts"));
185  _userActions.add(new DefaultUserAction("documents", "Documents",
186  root + "/_zul/images/64/documents.png", root + "/user/documents"));
187  _userActions.add(new DefaultUserAction("logistic", "Logistic",
188  root + "/_zul/images/64/shopping-cart.png", root + "/user/logistic"));
189  _userActions.add(new DefaultUserAction("end", "end", null));
190  }
191  if(dossiers) {
192  _userActions.add(new DefaultUserAction("dossiers", "Dossiers", null));
193  _userActions.add(new DefaultUserAction("mydossiers", "My Dossiers",
194  root + "/_zul/images/64/dossier.png", root + "/user/mydossiers"));
195  _userActions.add(new DefaultUserAction("myprojects", "My Projects",
196  root + "/_zul/images/64/dossier.png", root + "/user/myprojects"));
197  _userActions.add(new DefaultUserAction("kbase", "Knowledge base",
198  root + "/_zul/images/64/kbase.png", root + "/user/kbase"));
199  _userActions.add(new DefaultUserAction("myissues", "My Issues",
200  root + "/_zul/images/64/issue.png", root + "/user/myissues"));
201  _userActions.add(new DefaultUserAction("pendingrevision", "Pending revision",
202  root + "/_zul/images/64/issue.png", root + "/user/pendingrevision"));
203  _userActions.add(new DefaultUserAction("newissue", "Report Issue",
204  root + "/_zul/images/64/issue_new.png", root + "/user/newissue"));
205  _userActions.add(new DefaultUserAction("end", "end", null));
206  }
207 
208  _userActions.add(new DefaultUserAction("baas", "BaaS",
209  root + "/_zul/images/64/desktop.png", root + "/app/frame"));
210  }
211 
212  private static Collection<IUserAction> profileActions(IConstructor constructor) {
213  if(_profileActions == null) {
214  lock.lock();
215  try {
216  if(_profileActions == null) {
217  _profileActions = new ArrayList<>();
218  elephantProfileActions(constructor);
219  Set<String> menus = ElephantContext.getClassesByAnnotation(ProfileAction.class.getName());
220  if(menus != null) {
221  for(String menu : menus) {
222  Object obj = ReflectionUtil.instance(ReflectionUtil.classCheck(menu));
223  if(obj instanceof IUserAction) {
224  if(((IUserAction) obj).isValid()) {
225  _profileActions.add((IUserAction) obj);
226  }
227  }
228  }
229  }
230  }
231  } finally {
232  lock.unlock();
233  }
234  }
235  return _profileActions;
236  }
237 
238  private static void elephantProfileActions(IConstructor constructor) {
239  String root = ElephantContext.getRootWebPath();
240 
241  File file = new File(ElephantContext.getRealPath("/user/my-data"));
242  if(file.exists()) {
243  _profileActions.add(new DefaultUserAction("mydata", "UAMyData",
244  root + "/_zul/images/64/userMenu.png", root + "/user/my-data"));
245  }
246 
247  file = new File(ElephantContext.getRealPath("/user/tellsomeone"));
248  if(file.exists()) {
249  _profileActions.add(new DefaultUserAction("tellsome", "UATellSomeone",
250  root + "/_zul/images/64/favorite.png", root + "/user/tellsomeone"));
251  }
252  }
253 
254 }
static IContact getBehavingAs()
Definition: Contacts.java:141
static Set< String > getClassesByAnnotation(Class annotationClass)
Collection< IUserAction > getProfileActions()
Collection< IUserAction > getActions()
boolean isAllowed(IUserAction action)
UserMenu(IConstructor constructor)
static void executeAction(String idAction)