VIVO-695 VIVO-699 Refactor and optimize the PropertyRestrictionBean

Formerly the PropertyRestrictionPolicyHelper.
This commit is contained in:
Jim Blake 2014-12-10 15:01:51 -05:00
parent da9e244c18
commit 481d3fc1d8
30 changed files with 1321 additions and 1116 deletions

View file

@ -0,0 +1,122 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package stubs.edu.cornell.mannlib.vitro.webapp.auth.policy.bean;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import com.hp.hpl.jena.rdf.model.impl.Util;
import edu.cornell.mannlib.vitro.webapp.auth.policy.bean.PropertyRestrictionBean;
import edu.cornell.mannlib.vitro.webapp.auth.policy.bean.PropertyRestrictionLevels;
import edu.cornell.mannlib.vitro.webapp.beans.BaseResourceBean.RoleLevel;
import edu.cornell.mannlib.vitro.webapp.beans.Property;
/**
* Allow the unit test to specify a variety of restrictions
*/
public class PropertyRestrictionBeanStub extends PropertyRestrictionBean {
// ----------------------------------------------------------------------
// Stub infrastructure
// ----------------------------------------------------------------------
/** Don't prohibit or restrict anything. */
public static PropertyRestrictionBean getInstance() {
return getInstance(null, null);
}
/** Prohibit some namespaces. */
public static PropertyRestrictionBeanStub getInstance(
String[] restrictedNamespaces) {
return getInstance(restrictedNamespaces, null);
}
/**
* Prohibit some namespaces and restrict some properties from modification
* by anybody. They may still be displayed or published.
*
* We can implement more granular control if we need it.
*/
public static PropertyRestrictionBeanStub getInstance(
String[] restrictedNamespaces, String[] restrictedProperties) {
PropertyRestrictionBeanStub stub = new PropertyRestrictionBeanStub(
restrictedNamespaces, restrictedProperties);
PropertyRestrictionBean.instance = stub;
return stub;
}
private final Set<String> restrictedNamespaces;
private final Set<String> restrictedProperties;
private PropertyRestrictionBeanStub(String[] restrictedNamespaces,
String[] restrictedProperties) {
this.restrictedNamespaces = (restrictedNamespaces == null) ? Collections
.<String> emptySet() : new HashSet<>(
Arrays.asList(restrictedNamespaces));
this.restrictedProperties = (restrictedProperties == null) ? Collections
.<String> emptySet() : new HashSet<>(
Arrays.asList(restrictedProperties));
}
private boolean isPermittedNamespace(String uri) {
return !restrictedNamespaces.contains(namespace(uri));
}
private String namespace(String uri) {
return uri.substring(0, Util.splitNamespace(uri));
}
private boolean isPermittedProperty(String uri) {
return !restrictedProperties.contains(uri);
}
// ----------------------------------------------------------------------
// Stub methods
// ----------------------------------------------------------------------
@Override
public boolean canDisplayResource(String resourceUri, RoleLevel userRole) {
return isPermittedNamespace(resourceUri);
}
@Override
public boolean canModifyResource(String resourceUri, RoleLevel userRole) {
return isPermittedNamespace(resourceUri)
&& isPermittedProperty(resourceUri);
}
@Override
public boolean canPublishResource(String resourceUri, RoleLevel userRole) {
return isPermittedNamespace(resourceUri);
}
@Override
public boolean canDisplayPredicate(Property predicate, RoleLevel userRole) {
return isPermittedNamespace(predicate.getURI());
}
@Override
public boolean canModifyPredicate(Property predicate, RoleLevel userRole) {
return isPermittedNamespace(predicate.getURI())
&& isPermittedProperty(predicate.getURI());
}
@Override
public boolean canPublishPredicate(Property predicate, RoleLevel userRole) {
return isPermittedNamespace(predicate.getURI());
}
// ----------------------------------------------------------------------
// Un-implemented methods
// ----------------------------------------------------------------------
@Override
public void updateProperty(PropertyRestrictionLevels levels) {
throw new RuntimeException(
"PropertyRestrictionBeanStub.updateProperty() not implemented.");
}
}

View file

@ -1,63 +0,0 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package stubs.edu.cornell.mannlib.vitro.webapp.auth.policy.bean;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import edu.cornell.mannlib.vitro.webapp.auth.policy.bean.PropertyRestrictionPolicyHelper;
import edu.cornell.mannlib.vitro.webapp.beans.BaseResourceBean.RoleLevel;
import edu.cornell.mannlib.vitro.webapp.dao.PropertyDao.FullPropertyKey;
/**
* Allow the unit test to specify a variety of restrictions
*/
public class PropertyRestrictionPolicyHelperStub extends
PropertyRestrictionPolicyHelper {
/** Don't prohibit or restrict anything. */
public static PropertyRestrictionPolicyHelper getInstance() {
return getInstance(null, null);
}
/** Prohibit some namespaces. */
public static PropertyRestrictionPolicyHelperStub getInstance(
String[] restrictedNamespaces) {
return getInstance(restrictedNamespaces, null);
}
/**
* Prohibit some namespaces and restrict some properties from modification
* by anybody.
*/
public static PropertyRestrictionPolicyHelperStub getInstance(
String[] restrictedNamespaces, String[] restrictedProperties) {
Set<String> namespaceSet = new HashSet<String>();
if (restrictedNamespaces != null) {
namespaceSet.addAll(Arrays.asList(restrictedNamespaces));
}
Map<FullPropertyKey, RoleLevel> thresholdMap = new HashMap<>();
if (restrictedProperties != null) {
for (String prop : restrictedProperties) {
thresholdMap.put(new FullPropertyKey(prop), RoleLevel.NOBODY);
}
}
return new PropertyRestrictionPolicyHelperStub(namespaceSet, null,
null, thresholdMap, null);
}
private PropertyRestrictionPolicyHelperStub(
Set<String> modifyRestrictedNamespaces,
Set<String> modifyPermittedExceptions,
Map<FullPropertyKey, RoleLevel> displayThresholds,
Map<FullPropertyKey, RoleLevel> modifyThresholds,
Map<FullPropertyKey, RoleLevel> publishThresholds) {
super(modifyRestrictedNamespaces, modifyPermittedExceptions,
displayThresholds, modifyThresholds, publishThresholds);
}
}

View file

@ -2,6 +2,7 @@
package stubs.edu.cornell.mannlib.vitro.webapp.dao;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
@ -57,6 +58,11 @@ public class DataPropertyDaoStub implements DataPropertyDao {
// Stub methods
// ----------------------------------------------------------------------
@Override
public List<DataProperty> getAllDataProperties() {
return new ArrayList<>(dpMap.values());
}
@Override
public DataProperty getDataPropertyByURI(String dataPropertyURI) {
return dpMap.get(dataPropertyURI);
@ -189,12 +195,6 @@ public class DataPropertyDaoStub implements DataPropertyDao {
"PropertyDao.getClassesWithRestrictionOnProperty() not implemented.");
}
@Override
public List getAllDataProperties() {
throw new RuntimeException(
"DataPropertyDao.getAllDataProperties() not implemented.");
}
@Override
public List getAllExternalIdDataProperties() {
throw new RuntimeException(

View file

@ -2,9 +2,11 @@
package stubs.edu.cornell.mannlib.vitro.webapp.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import edu.cornell.mannlib.vitro.webapp.beans.FauxProperty;
import edu.cornell.mannlib.vitro.webapp.dao.FauxPropertyDao;
@ -19,7 +21,7 @@ public class FauxPropertyDaoStub implements FauxPropertyDao {
// ----------------------------------------------------------------------
private final Map<FullPropertyKey, FauxProperty> props = new HashMap<>();
// ----------------------------------------------------------------------
// Stub methods
// ----------------------------------------------------------------------
@ -30,28 +32,32 @@ public class FauxPropertyDaoStub implements FauxPropertyDao {
return props.get(new FullPropertyKey(domainUri, baseUri, rangeUri));
}
@Override
public List<FauxProperty> getFauxPropertiesForBaseUri(String uri) {
List<FauxProperty> list = new ArrayList<>();
for (FauxProperty fp : props.values()) {
if (Objects.equals(fp.getBaseURI(), uri)) {
list.add(fp);
}
}
return list;
}
@Override
public void insertFauxProperty(FauxProperty fp) {
props.put(new FullPropertyKey(fp), fp);
}
// ----------------------------------------------------------------------
// Un-implemented methods
// ----------------------------------------------------------------------
@Override
public List<FauxProperty> getFauxPropertiesForBaseUri(String uri) {
throw new RuntimeException(
"FauxPropertyDaoStub.getFauxPropertiesForBaseUri() not implemented.");
}
@Override
public FauxProperty getFauxPropertyFromContextUri(String contextUri) {
throw new RuntimeException(
"FauxPropertyDaoStub.getFauxPropertyFromContextUri() not implemented.");
}
@Override
public void insertFauxProperty(FauxProperty fp) {
throw new RuntimeException(
"FauxPropertyDaoStub.insertFauxProperty() not implemented.");
}
@Override
public void updateFauxProperty(FauxProperty fp) {
throw new RuntimeException(

View file

@ -2,6 +2,7 @@
package stubs.edu.cornell.mannlib.vitro.webapp.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -40,17 +41,18 @@ public class ObjectPropertyDaoStub implements ObjectPropertyDao {
opMap.put(uri, property);
}
public void setCustomListViewConfigFileName(ObjectProperty property, String filename) {
public void setCustomListViewConfigFileName(ObjectProperty property,
String filename) {
if (property == null) {
throw new NullPointerException("property may not be null.");
}
String uri = property.getURI();
if (uri == null) {
throw new NullPointerException("uri may not be null.");
}
configFilesMap.put(uri, filename);
}
@ -58,6 +60,11 @@ public class ObjectPropertyDaoStub implements ObjectPropertyDao {
// Stub methods
// ----------------------------------------------------------------------
@Override
public List<ObjectProperty> getAllObjectProperties() {
return new ArrayList<>(opMap.values());
}
@Override
public ObjectProperty getObjectPropertyByURI(String objectPropertyURI) {
if (objectPropertyURI == null) {
@ -65,18 +72,18 @@ public class ObjectPropertyDaoStub implements ObjectPropertyDao {
}
return opMap.get(objectPropertyURI);
}
@Override
public ObjectProperty getObjectPropertyByURIs(String objectPropertyURI,
String domainURI, String rangeURI) {
return getObjectPropertyByURI(objectPropertyURI);
}
@Override
public ObjectProperty getObjectPropertyByURIs(String objectPropertyURI,
String domainURI, String rangeURI, ObjectProperty base) {
return getObjectPropertyByURI(objectPropertyURI);
}
@Override
public ObjectProperty getObjectPropertyByURIs(String objectPropertyURI,
String domainURI, String rangeURI) {
return getObjectPropertyByURI(objectPropertyURI);
}
@Override
public ObjectProperty getObjectPropertyByURIs(String objectPropertyURI,
String domainURI, String rangeURI, ObjectProperty base) {
return getObjectPropertyByURI(objectPropertyURI);
}
@Override
public String getCustomListViewConfigFileName(ObjectProperty objectProperty) {
@ -194,12 +201,6 @@ public class ObjectPropertyDaoStub implements ObjectPropertyDao {
"ObjectPropertyDaoStub.getClassesWithRestrictionOnProperty() not implemented.");
}
@Override
public List<ObjectProperty> getAllObjectProperties() {
throw new RuntimeException(
"ObjectPropertyDaoStub.getAllObjectProperties() not implemented.");
}
@Override
public List<ObjectProperty> getObjectPropertiesForObjectPropertyStatements(
List objectPropertyStatements) {