1 | /**
|
---|
2 | * Permission.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU 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 General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with 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
|
---|
26 | * the 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
|
---|
31 | * in Item
|
---|
32 | */
|
---|
33 | public static Permission convertString(String permissionCode) {
|
---|
34 | return getPermission(permissionCode, Permission.full);
|
---|
35 | }
|
---|
36 |
|
---|
37 | public static Permission getPermission(String permissionCode,
|
---|
38 | Permission defaultPermission) {
|
---|
39 | if (permissionCode == null)
|
---|
40 | return defaultPermission;
|
---|
41 |
|
---|
42 | permissionCode = permissionCode.trim().toLowerCase();
|
---|
43 | if (permissionCode.length() == 0)
|
---|
44 | return defaultPermission;
|
---|
45 |
|
---|
46 | // if it is a single char just match the first character
|
---|
47 | try {
|
---|
48 | return values()[Integer.parseInt(permissionCode)];
|
---|
49 | // Otherwise match the whole string
|
---|
50 | } catch (Exception ex) {
|
---|
51 | try {
|
---|
52 | return valueOf(permissionCode);
|
---|
53 | } catch (Exception e) {
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | // default permission
|
---|
58 | return defaultPermission;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public int getCode() {
|
---|
62 | return ordinal();
|
---|
63 | }
|
---|
64 |
|
---|
65 | public String toString() {
|
---|
66 | return this.name();
|
---|
67 | }
|
---|
68 |
|
---|
69 | public static Permission min(Permission p1, Permission p2) {
|
---|
70 | return p1.ordinal() < p2.ordinal() ? p1 : p2;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public static Permission max(Permission p1, Permission p2) {
|
---|
74 | return p1.ordinal() > p2.ordinal() ? p1 : p2;
|
---|
75 | }
|
---|
76 | }
|
---|