6107 |
22 Jan 21 |
nicklas |
1 |
package net.sf.basedb.varsearch; |
6107 |
22 Jan 21 |
nicklas |
2 |
|
6160 |
04 Mar 21 |
nicklas |
3 |
import javax.servlet.ServletRequest; |
6160 |
04 Mar 21 |
nicklas |
4 |
|
6160 |
04 Mar 21 |
nicklas |
5 |
import net.sf.basedb.clients.web.Base; |
6160 |
04 Mar 21 |
nicklas |
6 |
import net.sf.basedb.core.Application; |
6160 |
04 Mar 21 |
nicklas |
7 |
import net.sf.basedb.core.Permission; |
6160 |
04 Mar 21 |
nicklas |
8 |
import net.sf.basedb.core.PermissionDeniedException; |
6160 |
04 Mar 21 |
nicklas |
9 |
import net.sf.basedb.core.Role; |
6160 |
04 Mar 21 |
nicklas |
10 |
import net.sf.basedb.core.SessionControl; |
6160 |
04 Mar 21 |
nicklas |
11 |
import net.sf.basedb.core.SystemItems; |
6160 |
04 Mar 21 |
nicklas |
12 |
import net.sf.basedb.core.User; |
6241 |
21 May 21 |
nicklas |
13 |
import net.sf.basedb.util.Values; |
6160 |
04 Mar 21 |
nicklas |
14 |
|
6107 |
22 Jan 21 |
nicklas |
15 |
/** |
6107 |
22 Jan 21 |
nicklas |
Global constants and utility functions for the VarSearch package. |
6107 |
22 Jan 21 |
nicklas |
17 |
|
6107 |
22 Jan 21 |
nicklas |
@since 1.0 |
6107 |
22 Jan 21 |
nicklas |
19 |
*/ |
6107 |
22 Jan 21 |
nicklas |
20 |
public class VarSearch |
6107 |
22 Jan 21 |
nicklas |
21 |
{ |
6107 |
22 Jan 21 |
nicklas |
22 |
/** |
6107 |
22 Jan 21 |
nicklas |
The current version of this package. |
6107 |
22 Jan 21 |
nicklas |
24 |
*/ |
7307 |
28 Aug 23 |
nicklas |
25 |
public static final String VERSION = "1.10-dev"; |
6107 |
22 Jan 21 |
nicklas |
26 |
|
6160 |
04 Mar 21 |
nicklas |
27 |
/** |
6160 |
04 Mar 21 |
nicklas |
Get an existing session control and check that a user is logged in. |
6160 |
04 Mar 21 |
nicklas |
29 |
*/ |
6160 |
04 Mar 21 |
nicklas |
30 |
public static SessionControl getSessionControl(ServletRequest req) |
6160 |
04 Mar 21 |
nicklas |
31 |
{ |
6160 |
04 Mar 21 |
nicklas |
32 |
return Application.getSessionControl(req.getParameter("ID"), |
6160 |
04 Mar 21 |
nicklas |
33 |
Base.WEBCLIENT_ID, |
6160 |
04 Mar 21 |
nicklas |
34 |
req.getRemoteAddr(), true); |
6160 |
04 Mar 21 |
nicklas |
35 |
} |
6160 |
04 Mar 21 |
nicklas |
36 |
|
6160 |
04 Mar 21 |
nicklas |
37 |
/** |
6160 |
04 Mar 21 |
nicklas |
Check if the logged in user is an administrator. |
6160 |
04 Mar 21 |
nicklas |
39 |
*/ |
6160 |
04 Mar 21 |
nicklas |
40 |
public static boolean isAdmin(SessionControl sc) |
6160 |
04 Mar 21 |
nicklas |
41 |
{ |
6160 |
04 Mar 21 |
nicklas |
42 |
boolean isAdmin = sc.getLoggedInUserId() == SystemItems.getId(User.ROOT) |
6160 |
04 Mar 21 |
nicklas |
43 |
|| sc.getActiveRoles().contains(SystemItems.getId(Role.ADMINISTRATOR)); |
6160 |
04 Mar 21 |
nicklas |
44 |
return isAdmin; |
6160 |
04 Mar 21 |
nicklas |
45 |
} |
6160 |
04 Mar 21 |
nicklas |
46 |
|
6160 |
04 Mar 21 |
nicklas |
47 |
/** |
6160 |
04 Mar 21 |
nicklas |
Throws a PermissionDeniedException if the logged in user is not an administrator |
6160 |
04 Mar 21 |
nicklas |
49 |
*/ |
6160 |
04 Mar 21 |
nicklas |
50 |
public static void checkIsAdmin(SessionControl sc, String message) |
6160 |
04 Mar 21 |
nicklas |
51 |
{ |
6160 |
04 Mar 21 |
nicklas |
52 |
if (!isAdmin(sc)) throw new PermissionDeniedException(Permission.USE, message); |
6160 |
04 Mar 21 |
nicklas |
53 |
} |
6241 |
21 May 21 |
nicklas |
54 |
|
6528 |
20 Dec 21 |
nicklas |
55 |
/** |
6528 |
20 Dec 21 |
nicklas |
Checks if Reggie is installed on this server. |
6528 |
20 Dec 21 |
nicklas |
@since 1.4 |
6528 |
20 Dec 21 |
nicklas |
58 |
*/ |
6528 |
20 Dec 21 |
nicklas |
59 |
public static boolean isReggieInstalled() |
6528 |
20 Dec 21 |
nicklas |
60 |
{ |
6528 |
20 Dec 21 |
nicklas |
61 |
return Application.getExtensionsManager().getRegistry().getExtension("net.sf.basedb.reggie") != null; |
6528 |
20 Dec 21 |
nicklas |
62 |
} |
6528 |
20 Dec 21 |
nicklas |
63 |
|
6252 |
25 May 21 |
nicklas |
64 |
public static String formatCount(Number count, String one, String many) |
6241 |
21 May 21 |
nicklas |
65 |
{ |
6241 |
21 May 21 |
nicklas |
66 |
if (count == null) return ""; |
6241 |
21 May 21 |
nicklas |
67 |
long c = count.longValue(); |
6252 |
25 May 21 |
nicklas |
68 |
String suffix = count.intValue()==1?one:many; |
6241 |
21 May 21 |
nicklas |
69 |
if (c > 1000000) |
6241 |
21 May 21 |
nicklas |
70 |
{ |
6241 |
21 May 21 |
nicklas |
71 |
int numDecimals = c < 100000000 ? 1 : 0; |
6252 |
25 May 21 |
nicklas |
72 |
return Values.formatNumber(c / 1000000f, numDecimals, "M")+suffix; |
6241 |
21 May 21 |
nicklas |
73 |
} |
6241 |
21 May 21 |
nicklas |
74 |
else if (c > 1000) |
6241 |
21 May 21 |
nicklas |
75 |
{ |
6241 |
21 May 21 |
nicklas |
76 |
int numDecimals = c < 100000 ? 1 : 0; |
6252 |
25 May 21 |
nicklas |
77 |
return Values.formatNumber(c / 1000f, numDecimals, "k")+suffix; |
6241 |
21 May 21 |
nicklas |
78 |
} |
6252 |
25 May 21 |
nicklas |
79 |
return Long.toString(c)+suffix; |
6241 |
21 May 21 |
nicklas |
80 |
} |
6160 |
04 Mar 21 |
nicklas |
81 |
|
6248 |
25 May 21 |
nicklas |
82 |
/** |
6248 |
25 May 21 |
nicklas |
Format count with thousands separator. |
6248 |
25 May 21 |
nicklas |
84 |
*/ |
6252 |
25 May 21 |
nicklas |
85 |
public static String niceCount(Number count, String one, String many) |
6248 |
25 May 21 |
nicklas |
86 |
{ |
6248 |
25 May 21 |
nicklas |
87 |
if (count == null) return ""; |
6248 |
25 May 21 |
nicklas |
88 |
String s = count.toString(); |
6252 |
25 May 21 |
nicklas |
89 |
return s.replaceAll("(\\d)(?=(\\d{3})+(?!\\d))", "$1 ")+(count.intValue()==1?one:many); //  == NARROW NO-BREAK SPACE |
6248 |
25 May 21 |
nicklas |
90 |
} |
6241 |
21 May 21 |
nicklas |
91 |
|
6848 |
17 Oct 22 |
nicklas |
92 |
/** |
6848 |
17 Oct 22 |
nicklas |
93 |
|
6848 |
17 Oct 22 |
nicklas |
* @param t |
6848 |
17 Oct 22 |
nicklas |
95 |
*/ |
6848 |
17 Oct 22 |
nicklas |
96 |
public static void throwRuntimException(Throwable t) |
6848 |
17 Oct 22 |
nicklas |
97 |
{ |
6848 |
17 Oct 22 |
nicklas |
98 |
RuntimeException r = null; |
6848 |
17 Oct 22 |
nicklas |
99 |
if (t instanceof RuntimeException) |
6848 |
17 Oct 22 |
nicklas |
100 |
{ |
6848 |
17 Oct 22 |
nicklas |
101 |
r = (RuntimeException)t; |
6848 |
17 Oct 22 |
nicklas |
102 |
} |
6848 |
17 Oct 22 |
nicklas |
103 |
else |
6848 |
17 Oct 22 |
nicklas |
104 |
{ |
6848 |
17 Oct 22 |
nicklas |
105 |
r = t.getMessage() == null ? new RuntimeException(t) : new RuntimeException(t.getMessage(), t); |
6848 |
17 Oct 22 |
nicklas |
106 |
} |
6848 |
17 Oct 22 |
nicklas |
107 |
throw r; |
6848 |
17 Oct 22 |
nicklas |
108 |
} |
6107 |
22 Jan 21 |
nicklas |
109 |
} |