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.List;
import java.util.HashMap; import java.util.HashMap;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -27,9 +29,6 @@ public class DataPropertyStatementTemplateModel extends PropertyStatementTemplat
private static final String EDIT_PATH = "edit/editDatapropStmtRequestDispatch.jsp"; private static final String EDIT_PATH = "edit/editDatapropStmtRequestDispatch.jsp";
protected String value; protected String value;
// Used for editing
protected String dataPropHash;
//Extended to include vitro request to check for special parameters //Extended to include vitro request to check for special parameters
DataPropertyStatementTemplateModel(String subjectUri, String propertyUri, DataPropertyStatementTemplateModel(String subjectUri, String propertyUri,
@ -38,9 +37,8 @@ public class DataPropertyStatementTemplateModel extends PropertyStatementTemplat
//attempt to strip any odd HTML //attempt to strip any odd HTML
this.value = cleanTextForDisplay( literal.getLexicalForm() ); 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) { DataPropertyStatementTemplateModel(String subjectUri, String propertyUri, VitroRequest vreq, EditingPolicyHelper policyHelper) {
super(subjectUri, propertyUri, policyHelper, vreq); 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) { protected void setValue(String value) {
this.value = value; this.value = value;
} }
protected void setEditAccess(Literal value, EditingPolicyHelper policyHelper, String propertyUri) { protected void setEditUrls(Literal value, EditingPolicyHelper policyHelper, String propertyUri) {
if (policyHelper != null) { // we're editing if (policyHelper == null) {
return;
}
DataPropertyStatement dps = new DataPropertyStatementImpl(subjectUri, propertyUri, value.getLexicalForm()); DataPropertyStatement dps = new DataPropertyStatementImpl(subjectUri, propertyUri, value.getLexicalForm());
// Language and datatype are needed to get the correct hash value // Language and datatype are needed to get the correct hash value
dps.setLanguage(value.getLanguage()); dps.setLanguage(value.getLanguage());
dps.setDatatypeURI(value.getDatatypeURI()); dps.setDatatypeURI(value.getDatatypeURI());
this.dataPropHash = String.valueOf(RdfLiteralHash.makeRdfLiteralHash(dps)); String dataPropHash = String.valueOf(RdfLiteralHash.makeRdfLiteralHash(dps));
// Determine whether the statement can be edited // Do delete url first, since used in building edit url
RequestedAction action = new EditDataPropStmt(dps); setDeleteUrl(policyHelper, propertyUri, dps, dataPropHash);
// vitro:moniker is deprecated. We display existing data values so editors can move them to other properties setEditUrl(policyHelper, propertyUri, dps, dataPropHash);
// 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();
}
}
}
} }
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 properties */
/* Template methods */
public String getValue() { public String getValue() {
return value; 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); setAddUrl(policyHelper, dp);
} }
// Determine whether a new statement can be added
@Override @Override
protected void setAddUrl(EditingPolicyHelper policyHelper, Property property) { 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( if (policyHelper == null) {
"subjectUri", subjectUri, return;
"predicateUri", propertyUri); }
params.putAll(UrlBuilder.getModelParams(vreq)); DataProperty dp = (DataProperty) property;
// NIHVIVO-2790 vitro:moniker now included in the display, but don't allow new statements
addUrl = UrlBuilder.getUrl(EDIT_PATH, params); 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 @Override

View file

@ -40,7 +40,7 @@ public class NameStatementTemplateModel extends
if (literal != null) { if (literal != null) {
value = cleanTextForDisplay( literal.getLexicalForm() ); value = cleanTextForDisplay( literal.getLexicalForm() );
setEditAccess(literal, policyHelper); setEditUrls(literal, policyHelper, propertyUri);
} else { } else {
// If the individual has no rdfs:label, use the local name. It will not be editable. (This replicates previous behavior; // 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 // 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(); 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.HashMap;
import java.util.Map; import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -38,64 +39,33 @@ public class ObjectPropertyStatementTemplateModel extends PropertyStatementTempl
this.data = data; this.data = data;
this.objectUri = data.get(objectKey); this.objectUri = data.get(objectKey);
this.templateName = templateName; 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. // 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 // We do this now rather than in getEditUrl() and getDeleteUrl(), because getEditUrl() also needs to know
// whether a delete is allowed. // whether a delete is allowed.
if (policyHelper != null) { if (policyHelper != null) {
ObjectPropertyStatement objectPropertyStatement = new ObjectPropertyStatementImpl(subjectUri, propertyUri, objectUri); ObjectPropertyStatement ops = new ObjectPropertyStatementImpl(subjectUri, propertyUri, objectUri);
// Determine whether the statement can be edited // Do delete url first, since used in building edit url
RequestedAction action = new EditObjPropStmt(objectPropertyStatement); setDeleteUrl(policyHelper, ops);
if (policyHelper.isAuthorizedAction(action)) { setEditUrl(policyHelper, ops);
markEditable();
}
// Determine whether the statement can be deleted
action = new DropObjectPropStmt(subjectUri, propertyUri, objectUri);
if (policyHelper.isAuthorizedAction(action)) {
markDeletable();
}
} }
} }
/* Access methods for templates */ protected void setDeleteUrl(EditingPolicyHelper policyHelper, ObjectPropertyStatement ops) {
public Object get(String key) { // Determine whether the statement can be deleted
return cleanTextForDisplay( data.get(key) ); RequestedAction action = new DropObjectPropStmt(subjectUri, propertyUri, objectUri);
} if (policyHelper.isAuthorizedAction(action)) {
return;
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);
} }
return editUrl; if (propertyUri.equals(VitroVocabulary.IND_MAIN_IMAGE)) {
} deleteUrl = ObjectPropertyTemplateModel.getImageUploadUrl(subjectUri, "delete");
} else {
public String getDeleteUrl() {
String deleteUrl = "";
if (isDeletable()) {
if (propertyUri.equals(VitroVocabulary.IND_MAIN_IMAGE)) {
return ObjectPropertyTemplateModel.getImageUploadUrl(subjectUri, "delete");
}
ParamMap params = new ParamMap( ParamMap params = new ParamMap(
"subjectUri", subjectUri, "subjectUri", subjectUri,
"predicateUri", propertyUri, "predicateUri", propertyUri,
@ -119,8 +89,40 @@ public class ObjectPropertyStatementTemplateModel extends PropertyStatementTempl
params.putAll(UrlBuilder.getModelParams(vreq)); params.putAll(UrlBuilder.getModelParams(vreq));
deleteUrl = UrlBuilder.getUrl(EDIT_PATH, params); 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 PropertyListConfig config;
private String objectKey; private String objectKey;
// Used for editing
private boolean addAccess; // defaults to false
ObjectPropertyTemplateModel(ObjectProperty op, Individual subject, VitroRequest vreq, ObjectPropertyTemplateModel(ObjectProperty op, Individual subject, VitroRequest vreq,
EditingPolicyHelper policyHelper) EditingPolicyHelper policyHelper)
throws InvalidConfigurationException { throws InvalidConfigurationException {
@ -112,30 +109,32 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
objectKey = getQueryObjectVariableName(); objectKey = getQueryObjectVariableName();
setAddUrl(policyHelper, op); setAddUrl(policyHelper, op);
} }
// Determine whether a new statement can be added
@Override @Override
protected void setAddUrl(EditingPolicyHelper policyHelper, Property property) { protected void setAddUrl(EditingPolicyHelper policyHelper, Property property) {
addUrl = "";
if (policyHelper != null) { if (policyHelper == null) {
RequestedAction action = new AddObjectPropStmt(subjectUri, propertyUri, RequestActionConstants.SOME_URI); return;
if (policyHelper.isAuthorizedAction(action)) { }
if (propertyUri.equals(VitroVocabulary.IND_MAIN_IMAGE)) { // Determine whether a new statement can be added
addUrl = getImageUploadUrl(subjectUri, "add"); RequestedAction action = new AddObjectPropStmt(subjectUri, propertyUri, RequestActionConstants.SOME_URI);
} else { if ( ! policyHelper.isAuthorizedAction(action) ) {
ParamMap params = new ParamMap( return;
"subjectUri", subjectUri, }
"predicateUri", propertyUri);
if (propertyUri.equals(VitroVocabulary.IND_MAIN_IMAGE)) {
params.putAll(UrlBuilder.getModelParams(vreq)); addUrl = getImageUploadUrl(subjectUri, "add");
} else {
addUrl = UrlBuilder.getUrl(EDIT_PATH, params); 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() { 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 final Log log = LogFactory.getLog(PropertyStatementTemplateModel.class);
private static enum EditAccess {
EDIT, DELETE;
}
protected final VitroRequest vreq; protected final VitroRequest vreq;
// Used for editing // Used for editing
protected final String subjectUri; protected final String subjectUri;
protected final String propertyUri; protected final String propertyUri;
private final List<EditAccess> editAccessList; protected String editUrl;
protected String deleteUrl;
PropertyStatementTemplateModel(String subjectUri, String propertyUri, EditingPolicyHelper policyHelper, VitroRequest vreq) { PropertyStatementTemplateModel(String subjectUri, String propertyUri, EditingPolicyHelper policyHelper, VitroRequest vreq) {
this.vreq = 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.subjectUri = subjectUri; this.subjectUri = subjectUri;
this.propertyUri = propertyUri; this.propertyUri = propertyUri;
editUrl = "";
deleteUrl = "";
} }
protected void markEditable() {
editAccessList.add(EditAccess.EDIT);
/* Template properties */
public String getEditUrl() {
return editUrl;
} }
protected void markDeletable() { public String getDeleteUrl() {
editAccessList.add(EditAccess.DELETE); 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(); localName = property.getLocalName();
log.debug("Local name for property " + propertyUri + ": " + localName); log.debug("Local name for property " + propertyUri + ": " + localName);
setVerboseDisplayValues(property); setVerboseDisplayValues(property);
addUrl = "";
// Do in subclass constructor. The label has not been set on the property, and the // 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. // means of getting the label differs between object and data properties.