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