1 | /**
|
---|
2 | * PermissionPair.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 | import org.expeditee.settings.UserSettings;
|
---|
22 |
|
---|
23 | public class PermissionPair {
|
---|
24 |
|
---|
25 | private UserAppliedPermission ownerPermission;
|
---|
26 | private UserAppliedPermission notOwnerPermission;
|
---|
27 |
|
---|
28 | public PermissionPair(String permissionCode, UserAppliedPermission defaultPermission) {
|
---|
29 | ownerPermission = defaultPermission;
|
---|
30 | notOwnerPermission = defaultPermission;
|
---|
31 |
|
---|
32 | if (permissionCode != null) {
|
---|
33 |
|
---|
34 | permissionCode = permissionCode.trim().toLowerCase();
|
---|
35 | if (permissionCode.length() != 0) {
|
---|
36 |
|
---|
37 | if (permissionCode.length() == 1 || (permissionCode.length() == 2 && permissionCode.startsWith("-"))) {
|
---|
38 | // replicate it to cover ifOwner/ifNotOwner
|
---|
39 | permissionCode += permissionCode;
|
---|
40 | }
|
---|
41 |
|
---|
42 | final int ownerPermissionIndexStart = 0;
|
---|
43 | final int ownerPermissionIndexEnd = permissionCode.charAt(ownerPermissionIndexStart) == '-' ?
|
---|
44 | ownerPermissionIndexStart + 2 : ownerPermissionIndexStart + 1;
|
---|
45 | final int notOwnerPermissionIndexStart = ownerPermissionIndexEnd;
|
---|
46 | final int notOwnerPermissionIndexEnd = permissionCode.charAt(notOwnerPermissionIndexStart) == '-' ?
|
---|
47 | notOwnerPermissionIndexStart + 2 : notOwnerPermissionIndexStart + 1;
|
---|
48 | final String ownerPermissionCode = permissionCode.substring(ownerPermissionIndexStart, ownerPermissionIndexEnd);
|
---|
49 | final String notOwnerPermissionCode = permissionCode.substring(notOwnerPermissionIndexStart, notOwnerPermissionIndexEnd);
|
---|
50 | ownerPermission = UserAppliedPermission.getPermission(ownerPermissionCode, defaultPermission);
|
---|
51 | notOwnerPermission = UserAppliedPermission.getPermission(notOwnerPermissionCode, defaultPermission);
|
---|
52 | }
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public PermissionPair(UserAppliedPermission ownerPermission, UserAppliedPermission notOwnerPermission) {
|
---|
57 | this.ownerPermission = ownerPermission;
|
---|
58 | this.notOwnerPermission = notOwnerPermission;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public PermissionPair(UserAppliedPermission ownerPermissionForBoth) {
|
---|
62 | this(ownerPermissionForBoth, ownerPermissionForBoth);
|
---|
63 | }
|
---|
64 |
|
---|
65 | public PermissionPair(PermissionPair pp) {
|
---|
66 | ownerPermission = pp.ownerPermission;
|
---|
67 | notOwnerPermission = pp.notOwnerPermission;
|
---|
68 | }
|
---|
69 |
|
---|
70 | public UserAppliedPermission getPermission(String ownerOfItem) {
|
---|
71 |
|
---|
72 | if (UserSettings.UserName.get().equals(ownerOfItem)) {
|
---|
73 | return ownerPermission;
|
---|
74 | } else {
|
---|
75 | return notOwnerPermission;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Converts the given Expeditee permission code into a PermissionPair
|
---|
81 | * corresponding to the constants defined in Item.
|
---|
82 | *
|
---|
83 | * @param permissionCode
|
---|
84 | * The Expeditee permission code to convert
|
---|
85 | * @return The resulting PermissionPair corresponding to a pair of constants as
|
---|
86 | * defined in Item
|
---|
87 | */
|
---|
88 | public static PermissionPair convertString(String permissionCode) {
|
---|
89 | PermissionPair pp = new PermissionPair(permissionCode, UserAppliedPermission.full);
|
---|
90 |
|
---|
91 | return pp;
|
---|
92 | }
|
---|
93 |
|
---|
94 | public String getCode() {
|
---|
95 | return Integer.toString(ownerPermission.getCode()) + Integer.toString(notOwnerPermission.getCode());
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | public String toString() {
|
---|
100 | return ownerPermission.toString() + ":" + notOwnerPermission.toString();
|
---|
101 | }
|
---|
102 | }
|
---|