Refactor generation of property editing links so done in one step rather than two

This commit is contained in:
ryounes 2011-08-05 23:33:08 +00:00
parent b5b44b73c1
commit debc7e15e0
7 changed files with 192 additions and 221 deletions

View file

@ -4,6 +4,8 @@ package edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual;
import java.util.List;
import java.util.HashMap;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -27,9 +29,6 @@ public class DataPropertyStatementTemplateModel extends PropertyStatementTemplat
private static final String EDIT_PATH = "edit/editDatapropStmtRequestDispatch.jsp";
protected String value;
// Used for editing
protected String dataPropHash;
//Extended to include vitro request to check for special parameters
DataPropertyStatementTemplateModel(String subjectUri, String propertyUri,
@ -38,9 +37,8 @@ public class DataPropertyStatementTemplateModel extends PropertyStatementTemplat
//attempt to strip any odd HTML
this.value = cleanTextForDisplay( literal.getLexicalForm() );
setEditAccess(literal, policyHelper, propertyUri);
setEditUrls(literal, policyHelper, propertyUri);
}
/*
@ -51,92 +49,87 @@ public class DataPropertyStatementTemplateModel extends PropertyStatementTemplat
*/
DataPropertyStatementTemplateModel(String subjectUri, String propertyUri, VitroRequest vreq, EditingPolicyHelper policyHelper) {
super(subjectUri, propertyUri, policyHelper, vreq);
DataPropertyStatementDao dpsDao = vreq.getWebappDaoFactory().getDataPropertyStatementDao();
List<Literal> literals = dpsDao.getDataPropertyValuesForIndividualByProperty(subjectUri, propertyUri);
// Make sure the subject has a value for this property
if (literals.size() > 0) {
Literal literal = literals.get(0);
value = literal.getLexicalForm();
setEditAccess(literal, policyHelper, propertyUri);
}
}
protected void setValue(String value) {
this.value = value;
}
protected void setEditAccess(Literal value, EditingPolicyHelper policyHelper, String propertyUri) {
if (policyHelper != null) { // we're editing
protected void setEditUrls(Literal value, EditingPolicyHelper policyHelper, String propertyUri) {
if (policyHelper == null) {
return;
}
DataPropertyStatement dps = new DataPropertyStatementImpl(subjectUri, propertyUri, value.getLexicalForm());
// Language and datatype are needed to get the correct hash value
dps.setLanguage(value.getLanguage());
dps.setDatatypeURI(value.getDatatypeURI());
this.dataPropHash = String.valueOf(RdfLiteralHash.makeRdfLiteralHash(dps));
DataPropertyStatement dps = new DataPropertyStatementImpl(subjectUri, propertyUri, value.getLexicalForm());
// Language and datatype are needed to get the correct hash value
dps.setLanguage(value.getLanguage());
dps.setDatatypeURI(value.getDatatypeURI());
String dataPropHash = String.valueOf(RdfLiteralHash.makeRdfLiteralHash(dps));
// Determine whether the statement can be edited
RequestedAction action = new EditDataPropStmt(dps);
// vitro:moniker is deprecated. We display existing data values so editors can move them to other properties
// and delete, but don't allow editing.
if ( ( ! propertyUri.equals(VitroVocabulary.MONIKER) ) && policyHelper.isAuthorizedAction(action)) {
markEditable();
}
// Determine whether the statement can be deleted
// Hack for rdfs:label - the policy doesn't prevent deletion.
if ( ! propertyUri.equals(VitroVocabulary.LABEL) ) {
action = new DropDataPropStmt(dps);
if (policyHelper.isAuthorizedAction(action)) {
markDeletable();
}
}
}
// Do delete url first, since used in building edit url
setDeleteUrl(policyHelper, propertyUri, dps, dataPropHash);
setEditUrl(policyHelper, propertyUri, dps, dataPropHash);
}
protected void setDeleteUrl(EditingPolicyHelper policyHelper, String propertyUri, DataPropertyStatement dps, String dataPropHash) {
// Hack for rdfs:label - the policy doesn't prevent deletion.
if (propertyUri.equals(VitroVocabulary.LABEL)) {
return;
}
// Determine whether the statement can be deleted
RequestedAction action = new DropDataPropStmt(dps);
if ( ! policyHelper.isAuthorizedAction(action)) {
return;
}
ParamMap params = new ParamMap(
"subjectUri", subjectUri,
"predicateUri", propertyUri,
"datapropKey", dataPropHash,
"cmd", "delete");
params.putAll(UrlBuilder.getModelParams(vreq));
deleteUrl = UrlBuilder.getUrl(EDIT_PATH, params);
}
protected void setEditUrl(EditingPolicyHelper policyHelper, String propertyUri, DataPropertyStatement dps, String dataPropHash) {
// vitro:moniker is deprecated. We display existing data values so editors can
// move them to other properties and delete, but don't allow editing.
if ( propertyUri.equals(VitroVocabulary.MONIKER) ) {
return;
}
// Determine whether the statement can be edited
RequestedAction action = new EditDataPropStmt(dps);
if ( ! policyHelper.isAuthorizedAction(action)) {
return;
}
ParamMap params = new ParamMap(
"subjectUri", subjectUri,
"predicateUri", propertyUri,
"datapropKey", dataPropHash);
if ( deleteUrl.isEmpty() ) {
params.put("deleteProhibited", "prohibited");
}
params.putAll(UrlBuilder.getModelParams(vreq));
editUrl = UrlBuilder.getUrl(EDIT_PATH, params);
}
/* Template properties */
/* Template methods */
public String getValue() {
return value;
}
public String getEditUrl() {
String editUrl = "";
if (isEditable()) {
ParamMap params = new ParamMap(
"subjectUri", subjectUri,
"predicateUri", propertyUri,
"datapropKey", dataPropHash);
if (! isDeletable()) {
params.put("deleteProhibited", "prohibited");
}
params.putAll(UrlBuilder.getModelParams(vreq));
editUrl = UrlBuilder.getUrl(EDIT_PATH, params);
}
return editUrl;
}
public String getDeleteUrl() {
String deleteUrl = "";
if (isDeletable()) {
ParamMap params = new ParamMap(
"subjectUri", subjectUri,
"predicateUri", propertyUri,
"datapropKey", dataPropHash,
"cmd", "delete");
params.putAll(UrlBuilder.getModelParams(vreq));
deleteUrl = UrlBuilder.getUrl(EDIT_PATH, params);
}
return deleteUrl;
}
}

View file

@ -56,37 +56,42 @@ public class DataPropertyTemplateModel extends PropertyTemplateModel {
setAddUrl(policyHelper, dp);
}
// Determine whether a new statement can be added
@Override
protected void setAddUrl(EditingPolicyHelper policyHelper, Property property) {
this.addUrl = "";
if (policyHelper != null) {
DataProperty dp = (DataProperty) property;
// NIHVIVO-2790 vitro:moniker now included in the display, but don't allow new statements
if (dp.getURI().equals(VitroVocabulary.MONIKER)) {
return;
}
// If the display limit has already been reached, we can't add a new statement.
// NB This appears to be a misuse of a value called "display limit". Note that it's
// not used to limit display, either, so should be renamed.
int displayLimit = dp.getDisplayLimit();
// Display limit of -1 (default value for new property) means no display limit
if ( (displayLimit < 0) || (displayLimit > statements.size()) ) {
RequestedAction action = new AddDataPropStmt(subjectUri, propertyUri,RequestActionConstants.SOME_LITERAL, null, null);
if (policyHelper.isAuthorizedAction(action)) {
ParamMap params = new ParamMap(
"subjectUri", subjectUri,
"predicateUri", propertyUri);
params.putAll(UrlBuilder.getModelParams(vreq));
addUrl = UrlBuilder.getUrl(EDIT_PATH, params);
}
}
}
if (policyHelper == null) {
return;
}
DataProperty dp = (DataProperty) property;
// NIHVIVO-2790 vitro:moniker now included in the display, but don't allow new statements
if (dp.getURI().equals(VitroVocabulary.MONIKER)) {
return;
}
// If the display limit has already been reached, we can't add a new statement.
// NB This appears to be a misuse of a value called "display limit". Note that it's
// not used to limit display, either, so should be renamed.
int displayLimit = dp.getDisplayLimit();
// Display limit of -1 (default value for new property) means no display limit
if ( displayLimit >= 0 && statements.size() >= displayLimit ) {
return;
}
// Determine whether a new statement can be added
RequestedAction action = new AddDataPropStmt(subjectUri, propertyUri, RequestActionConstants.SOME_LITERAL, null, null);
if ( ! policyHelper.isAuthorizedAction(action)) {
return;
}
ParamMap params = new ParamMap(
"subjectUri", subjectUri,
"predicateUri", propertyUri);
params.putAll(UrlBuilder.getModelParams(vreq));
addUrl = UrlBuilder.getUrl(EDIT_PATH, params);
}
@Override

View file

@ -40,7 +40,7 @@ public class NameStatementTemplateModel extends
if (literal != null) {
value = cleanTextForDisplay( literal.getLexicalForm() );
setEditAccess(literal, policyHelper);
setEditUrls(literal, policyHelper, propertyUri);
} else {
// If the individual has no rdfs:label, use the local name. It will not be editable. (This replicates previous behavior;
// perhaps we would want to allow a label to be added. But such individuals do not usually have their profiles viewed or
@ -49,25 +49,5 @@ public class NameStatementTemplateModel extends
value = uri.getLocalName();
}
}
protected void setEditAccess(EditLiteral value, EditingPolicyHelper policyHelper) {
if (policyHelper != null) { // we're editing
DataPropertyStatement dps = new DataPropertyStatementImpl(subjectUri, propertyUri, value.getLexicalForm());
// Language and datatype are needed to get the correct hash value
dps.setLanguage(value.getLanguage());
dps.setDatatypeURI(value.getDatatypeURI());
this.dataPropHash = String.valueOf(RdfLiteralHash.makeRdfLiteralHash(dps));
// Determine whether the statement can be edited
RequestedAction action = new EditDataPropStmt(dps);
if (policyHelper.isAuthorizedAction(action)) {
markEditable();
}
// The label cannot be deleted, so we don't need to check
// the policy for the delete action.
}
}
}

View file

@ -5,6 +5,7 @@ package edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -38,64 +39,33 @@ public class ObjectPropertyStatementTemplateModel extends PropertyStatementTempl
this.data = data;
this.objectUri = data.get(objectKey);
this.templateName = templateName;
setEditAccess(policyHelper);
setEditUrls(policyHelper);
}
private void setEditAccess(EditingPolicyHelper policyHelper) {
protected void setEditUrls(EditingPolicyHelper policyHelper) {
// If the policyHelper is non-null, we are in edit mode, so create the list of editing permissions.
// We do this now rather than in getEditUrl() and getDeleteUrl(), because getEditUrl() also needs to know
// whether a delete is allowed.
if (policyHelper != null) {
ObjectPropertyStatement objectPropertyStatement = new ObjectPropertyStatementImpl(subjectUri, propertyUri, objectUri);
ObjectPropertyStatement ops = new ObjectPropertyStatementImpl(subjectUri, propertyUri, objectUri);
// Determine whether the statement can be edited
RequestedAction action = new EditObjPropStmt(objectPropertyStatement);
if (policyHelper.isAuthorizedAction(action)) {
markEditable();
}
// Determine whether the statement can be deleted
action = new DropObjectPropStmt(subjectUri, propertyUri, objectUri);
if (policyHelper.isAuthorizedAction(action)) {
markDeletable();
}
// Do delete url first, since used in building edit url
setDeleteUrl(policyHelper, ops);
setEditUrl(policyHelper, ops);
}
}
/* Access methods for templates */
public Object get(String key) {
return cleanTextForDisplay( data.get(key) );
}
public String getEditUrl() {
String editUrl = "";
if (isEditable()) {
if (propertyUri.equals(VitroVocabulary.IND_MAIN_IMAGE)) {
return ObjectPropertyTemplateModel.getImageUploadUrl(subjectUri, "edit");
}
ParamMap params = new ParamMap(
"subjectUri", subjectUri,
"predicateUri", propertyUri,
"objectUri", objectUri);
if (! isDeletable()) {
params.put("deleteProhibited", "prohibited");
}
params.putAll(UrlBuilder.getModelParams(vreq));
editUrl = UrlBuilder.getUrl(EDIT_PATH, params);
protected void setDeleteUrl(EditingPolicyHelper policyHelper, ObjectPropertyStatement ops) {
// Determine whether the statement can be deleted
RequestedAction action = new DropObjectPropStmt(subjectUri, propertyUri, objectUri);
if (policyHelper.isAuthorizedAction(action)) {
return;
}
return editUrl;
}
public String getDeleteUrl() {
String deleteUrl = "";
if (isDeletable()) {
if (propertyUri.equals(VitroVocabulary.IND_MAIN_IMAGE)) {
return ObjectPropertyTemplateModel.getImageUploadUrl(subjectUri, "delete");
}
if (propertyUri.equals(VitroVocabulary.IND_MAIN_IMAGE)) {
deleteUrl = ObjectPropertyTemplateModel.getImageUploadUrl(subjectUri, "delete");
} else {
ParamMap params = new ParamMap(
"subjectUri", subjectUri,
"predicateUri", propertyUri,
@ -119,8 +89,40 @@ public class ObjectPropertyStatementTemplateModel extends PropertyStatementTempl
params.putAll(UrlBuilder.getModelParams(vreq));
deleteUrl = UrlBuilder.getUrl(EDIT_PATH, params);
}
return deleteUrl;
}
}
protected void setEditUrl(EditingPolicyHelper policyHelper, ObjectPropertyStatement ops) {
// Determine whether the statement can be edited
RequestedAction action = new EditObjPropStmt(ops);
if ( ! policyHelper.isAuthorizedAction(action)) {
return;
}
if (propertyUri.equals(VitroVocabulary.IND_MAIN_IMAGE)) {
editUrl = ObjectPropertyTemplateModel.getImageUploadUrl(subjectUri, "edit");
} else {
ParamMap params = new ParamMap(
"subjectUri", subjectUri,
"predicateUri", propertyUri,
"objectUri", objectUri);
if ( deleteUrl.isEmpty() ) {
params.put("deleteProhibited", "prohibited");
}
params.putAll(UrlBuilder.getModelParams(vreq));
editUrl = UrlBuilder.getUrl(EDIT_PATH, params);
}
}
/* Template methods */
public Object get(String key) {
return cleanTextForDisplay( data.get(key) );
}
}

View file

@ -90,9 +90,6 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
private PropertyListConfig config;
private String objectKey;
// Used for editing
private boolean addAccess; // defaults to false
ObjectPropertyTemplateModel(ObjectProperty op, Individual subject, VitroRequest vreq,
EditingPolicyHelper policyHelper)
throws InvalidConfigurationException {
@ -112,30 +109,32 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
objectKey = getQueryObjectVariableName();
setAddUrl(policyHelper, op);
}
// Determine whether a new statement can be added
@Override
protected void setAddUrl(EditingPolicyHelper policyHelper, Property property) {
addUrl = "";
if (policyHelper != null) {
RequestedAction action = new AddObjectPropStmt(subjectUri, propertyUri, RequestActionConstants.SOME_URI);
if (policyHelper.isAuthorizedAction(action)) {
if (propertyUri.equals(VitroVocabulary.IND_MAIN_IMAGE)) {
addUrl = getImageUploadUrl(subjectUri, "add");
} else {
ParamMap params = new ParamMap(
"subjectUri", subjectUri,
"predicateUri", propertyUri);
params.putAll(UrlBuilder.getModelParams(vreq));
addUrl = UrlBuilder.getUrl(EDIT_PATH, params);
}
}
}
if (policyHelper == null) {
return;
}
// Determine whether a new statement can be added
RequestedAction action = new AddObjectPropStmt(subjectUri, propertyUri, RequestActionConstants.SOME_URI);
if ( ! policyHelper.isAuthorizedAction(action) ) {
return;
}
if (propertyUri.equals(VitroVocabulary.IND_MAIN_IMAGE)) {
addUrl = getImageUploadUrl(subjectUri, "add");
} else {
ParamMap params = new ParamMap(
"subjectUri", subjectUri,
"predicateUri", propertyUri);
params.putAll(UrlBuilder.getModelParams(vreq));
addUrl = UrlBuilder.getUrl(EDIT_PATH, params);
}
}
protected List<Map<String, String>> getStatementData() {

View file

@ -16,41 +16,32 @@ public abstract class PropertyStatementTemplateModel extends BaseTemplateModel {
private static final Log log = LogFactory.getLog(PropertyStatementTemplateModel.class);
private static enum EditAccess {
EDIT, DELETE;
}
protected final VitroRequest vreq;
// Used for editing
protected final String subjectUri;
protected final String propertyUri;
private final List<EditAccess> editAccessList;
protected String editUrl;
protected String deleteUrl;
PropertyStatementTemplateModel(String subjectUri, String propertyUri, EditingPolicyHelper policyHelper, VitroRequest vreq) {
this.vreq = vreq;
// Instantiate the list even if not editing, so calls to getEditUrl() and getDeleteUrl() from
// dump methods don't generate an error when they call isEditable() and isDeletable().
editAccessList = new ArrayList<EditAccess>();
this.vreq = vreq;
this.subjectUri = subjectUri;
this.propertyUri = propertyUri;
editUrl = "";
deleteUrl = "";
}
protected void markEditable() {
editAccessList.add(EditAccess.EDIT);
/* Template properties */
public String getEditUrl() {
return editUrl;
}
protected void markDeletable() {
editAccessList.add(EditAccess.DELETE);
public String getDeleteUrl() {
return deleteUrl;
}
protected boolean isEditable() {
return editAccessList.contains(EditAccess.EDIT);
}
protected boolean isDeletable() {
return editAccessList.contains(EditAccess.DELETE);
}
}

View file

@ -43,6 +43,7 @@ public abstract class PropertyTemplateModel extends BaseTemplateModel {
localName = property.getLocalName();
log.debug("Local name for property " + propertyUri + ": " + localName);
setVerboseDisplayValues(property);
addUrl = "";
// Do in subclass constructor. The label has not been set on the property, and the
// means of getting the label differs between object and data properties.