1 | /**
|
---|
2 | * Permission.java Copyright (C) 2010 New Zealand Digital Library,
|
---|
3 | * http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify it under
|
---|
6 | * the terms of the GNU General Public License as published by the Free Software
|
---|
7 | * Foundation, either version 3 of the License, or (at your option) any later
|
---|
8 | * version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
12 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
---|
13 | * details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License along with
|
---|
16 | * this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.items;
|
---|
20 |
|
---|
21 | public enum Permission {
|
---|
22 | none, followLinks, copy, createFrames, full;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Converts the given Expeditee permission code into a int corresponding to the
|
---|
26 | * constants defined in Item.
|
---|
27 | *
|
---|
28 | * @param permissionCode
|
---|
29 | * The Expeditee permission code to convert
|
---|
30 | * @return The resulting int corresponding to one of the constants defined in
|
---|
31 | * Item
|
---|
32 | */
|
---|
33 | public static Permission convertString(String permissionCode) {
|
---|
34 | return getPermission(permissionCode, Permission.full);
|
---|
35 | }
|
---|
36 |
|
---|
37 | public static Permission getPermission(String permissionCode, Permission defaultPermission) {
|
---|
38 | if (permissionCode == null)
|
---|
39 | return defaultPermission;
|
---|
40 |
|
---|
41 | permissionCode = permissionCode.trim().toLowerCase();
|
---|
42 | if (permissionCode.length() == 0)
|
---|
43 | return defaultPermission;
|
---|
44 |
|
---|
45 | // if it is a single char just match the first character
|
---|
46 | try {
|
---|
47 | return values()[Integer.parseInt(permissionCode)];
|
---|
48 | // Otherwise match the whole string
|
---|
49 | } catch (Exception ex) {
|
---|
50 | try {
|
---|
51 | return valueOf(permissionCode);
|
---|
52 | } catch (Exception e) {
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | // default permission
|
---|
57 | return defaultPermission;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public int getCode() {
|
---|
61 | return ordinal();
|
---|
62 | }
|
---|
63 |
|
---|
64 | public String toString() {
|
---|
65 | return this.name();
|
---|
66 | }
|
---|
67 |
|
---|
68 | public static Permission min(Permission p1, Permission p2) {
|
---|
69 | return p1.ordinal() < p2.ordinal() ? p1 : p2;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public static Permission max(Permission p1, Permission p2) {
|
---|
73 | return p1.ordinal() > p2.ordinal() ? p1 : p2;
|
---|
74 | }
|
---|
75 | }
|
---|