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