/**
* PermissionPair.java
* Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.expeditee.items;
import java.util.Arrays;
import java.util.List;
import org.expeditee.settings.UserSettings;
public class PermissionTriple {
private UserAppliedPermission ownerPermission;
private UserAppliedPermission groupPermission;
private UserAppliedPermission otherPermission;
public PermissionTriple(String permissionCode, UserAppliedPermission defaultPermission) {
ownerPermission = defaultPermission;
groupPermission = defaultPermission;
otherPermission = defaultPermission;
if (permissionCode != null && permissionCode.length() > 0) {
permissionCode = permissionCode.trim().toLowerCase();
int numberOfMinuses = permissionCode.chars().reduce(0, (a, b) -> b == '-' ? a + 1 : a);
String[] codes = new String[3];
Arrays.fill(codes, "");
if (permissionCode.length() == 1 + numberOfMinuses) {
codes[0] = permissionCode;
} else {
int i = 0;
for (char c : permissionCode.toCharArray()) {
try {
int asInt = Integer.parseInt(c + "");
codes[i] += asInt + "";
i++;
} catch (NumberFormatException e) {
if (c == '-') {
codes[i] += c + "";
}
}
}
}
this.ownerPermission = UserAppliedPermission.getPermission(codes[0], this.ownerPermission);
if (codes[1].length() == 0) { // If there is only a permission code for owner.
this.groupPermission = UserAppliedPermission.getPermission(codes[0], this.groupPermission);
this.otherPermission = UserAppliedPermission.getPermission(codes[0], this.otherPermission);
} else if (codes[2].length() == 0) { // If there is only two of three permission codes specified.
this.groupPermission = UserAppliedPermission.getPermission(codes[1], this.groupPermission);
this.otherPermission = UserAppliedPermission.getPermission(codes[1], this.otherPermission);
} else { // If all permission codes are specified.
this.groupPermission = UserAppliedPermission.getPermission(codes[1], this.groupPermission);
this.otherPermission = UserAppliedPermission.getPermission(codes[2], this.otherPermission);
}
}
}
public PermissionTriple(UserAppliedPermission ownerPermission, UserAppliedPermission groupPermission, UserAppliedPermission otherPermission) {
this.ownerPermission = ownerPermission;
this.groupPermission = groupPermission;
this.otherPermission = otherPermission;
}
public PermissionTriple(UserAppliedPermission permissionForAll) {
this(permissionForAll, permissionForAll, permissionForAll);
}
public PermissionTriple(PermissionTriple pt) {
this.ownerPermission = pt.ownerPermission;
this.groupPermission = pt.groupPermission;
this.otherPermission = pt.otherPermission;
}
public UserAppliedPermission getPermission(String ownerOfItem, List group) {
if (UserSettings.UserName.get().equals(ownerOfItem)) {
return ownerPermission;
} else if (group.contains(UserSettings.UserName.get())) {
return groupPermission;
} else {
return otherPermission;
}
}
/**
* Converts the given Expeditee permission code into a PermissionPair
* corresponding to the constants defined in Item.
*
* @param permissionCode
* The Expeditee permission code to convert
* @return The resulting PermissionPair corresponding to a pair of constants as
* defined in Item
*/
public static PermissionTriple convertString(String permissionCode) {
PermissionTriple pt = new PermissionTriple(permissionCode, UserAppliedPermission.full);
return pt;
}
public String getCode() {
return Integer.toString(ownerPermission.getCode()) + Integer.toString(groupPermission.getCode()) + Integer.toString(otherPermission.getCode());
}
@Override
public String toString() {
return ownerPermission + ":" + groupPermission + ":" + otherPermission;
}
}