NIHVIVO-1332 Refactored common code from DataPropertyStatementTemplateModel and ObjectPropertyStatementTemplateModel into a common superclass PropertyStatementTemplateModel

This commit is contained in:
rjy7 2011-01-12 16:19:38 +00:00
parent 3ba7346ac4
commit ff7ccf573c
3 changed files with 67 additions and 35 deletions

View file

@ -20,50 +20,39 @@ import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.ParamMa
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.RdfLiteralHash; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.RdfLiteralHash;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel;
public class DataPropertyStatementTemplateModel extends BaseTemplateModel { public class DataPropertyStatementTemplateModel extends PropertyStatementTemplateModel {
private static final Log log = LogFactory.getLog(DataPropertyStatementTemplateModel.class); private static final Log log = LogFactory.getLog(DataPropertyStatementTemplateModel.class);
private static final String EDIT_PATH = "edit/editDatapropStmtRequestDispatch.jsp"; private static final String EDIT_PATH = "edit/editDatapropStmtRequestDispatch.jsp";
private static enum EditAccess {
EDIT, DELETE;
}
private Literal value; private Literal value;
// Used for editing // Used for editing
private String subjectUri = null;
private String propertyUri = null;
private List<EditAccess> editAccessList = null;
private String dataPropHash = null; private String dataPropHash = null;
DataPropertyStatementTemplateModel(String subjectUri, String propertyUri, DataPropertyStatementTemplateModel(String subjectUri, String propertyUri,
Literal value, EditingPolicyHelper policyHelper) { Literal value, EditingPolicyHelper policyHelper) {
super(subjectUri, propertyUri, policyHelper);
this.value = value; this.value = value;
if (policyHelper != null) { if (policyHelper != null) { // we're editing
this.subjectUri = subjectUri;
this.propertyUri = propertyUri;
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)); this.dataPropHash = String.valueOf(RdfLiteralHash.makeRdfLiteralHash(dps));
editAccessList = new ArrayList<EditAccess>();
// Determine whether the statement can be edited // Determine whether the statement can be edited
RequestedAction action = new EditDataPropStmt(dps); RequestedAction action = new EditDataPropStmt(dps);
if (policyHelper.isAuthorizedAction(action)) { if (policyHelper.isAuthorizedAction(action)) {
editAccessList.add(EditAccess.EDIT); markEditable();
} }
// Determine whether the statement can be deleted // Determine whether the statement can be deleted
action = new DropDataPropStmt(dps); action = new DropDataPropStmt(dps);
if (policyHelper.isAuthorizedAction(action)) { if (policyHelper.isAuthorizedAction(action)) {
editAccessList.add(EditAccess.DELETE); markDeletable();
} }
} }
} }
@ -76,12 +65,12 @@ public class DataPropertyStatementTemplateModel extends BaseTemplateModel {
public String getEditUrl() { public String getEditUrl() {
String editUrl = ""; String editUrl = "";
if (editAccessList.contains(EditAccess.EDIT)) { if (isEditable()) {
ParamMap params = new ParamMap( ParamMap params = new ParamMap(
"subjectUri", subjectUri, "subjectUri", subjectUri,
"predicateUri", propertyUri, "predicateUri", propertyUri,
"datapropKey", dataPropHash); "datapropKey", dataPropHash);
if (! editAccessList.contains(EditAccess.DELETE)) { if (! isDeletable()) {
params.put("deleteProhibited", "prohibited"); params.put("deleteProhibited", "prohibited");
} }
editUrl = UrlBuilder.getUrl(EDIT_PATH, params); editUrl = UrlBuilder.getUrl(EDIT_PATH, params);
@ -91,7 +80,7 @@ public class DataPropertyStatementTemplateModel extends BaseTemplateModel {
public String getDeleteUrl() { public String getDeleteUrl() {
String deleteUrl = ""; String deleteUrl = "";
if (editAccessList.contains(EditAccess.DELETE)) { if (isDeletable()) {
ParamMap params = new ParamMap( ParamMap params = new ParamMap(
"subjectUri", subjectUri, "subjectUri", subjectUri,
"predicateUri", propertyUri, "predicateUri", propertyUri,

View file

@ -18,49 +18,42 @@ import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.ParamMap; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.ParamMap;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel;
public class ObjectPropertyStatementTemplateModel extends BaseTemplateModel { public class ObjectPropertyStatementTemplateModel extends PropertyStatementTemplateModel {
private static final Log log = LogFactory.getLog(ObjectPropertyStatementTemplateModel.class); private static final Log log = LogFactory.getLog(ObjectPropertyStatementTemplateModel.class);
private static final String EDIT_PATH = "edit/editRequestDispatch.jsp"; private static final String EDIT_PATH = "edit/editRequestDispatch.jsp";
private static enum EditAccess {
EDIT, DELETE;
}
private Map<String, String> data; private Map<String, String> data;
// Used for editing // Used for editing
private String subjectUri = null;
private String propertyUri = null;
private String objectUri = null; private String objectUri = null;
private List<EditAccess> editAccessList;
ObjectPropertyStatementTemplateModel(String subjectUri, String propertyUri, ObjectPropertyStatementTemplateModel(String subjectUri, String propertyUri,
String objectKey, Map<String, String> data, EditingPolicyHelper policyHelper) { String objectKey, Map<String, String> data, EditingPolicyHelper policyHelper) {
super(subjectUri, propertyUri, policyHelper);
this.data = data; this.data = data;
// 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) {
this.subjectUri = subjectUri;
this.propertyUri = propertyUri;
objectUri = data.get(objectKey); objectUri = data.get(objectKey);
editAccessList = new ArrayList<EditAccess>();
ObjectPropertyStatement objectPropertyStatement = new ObjectPropertyStatementImpl(subjectUri, propertyUri, objectUri); ObjectPropertyStatement objectPropertyStatement = new ObjectPropertyStatementImpl(subjectUri, propertyUri, objectUri);
// Determine whether the statement can be edited // Determine whether the statement can be edited
RequestedAction action = new EditObjPropStmt(objectPropertyStatement); RequestedAction action = new EditObjPropStmt(objectPropertyStatement);
if (policyHelper.isAuthorizedAction(action)) { if (policyHelper.isAuthorizedAction(action)) {
editAccessList.add(EditAccess.EDIT); markEditable();
} }
// Determine whether the statement can be deleted // Determine whether the statement can be deleted
action = new DropObjectPropStmt(subjectUri, propertyUri, objectUri); action = new DropObjectPropStmt(subjectUri, propertyUri, objectUri);
if (policyHelper.isAuthorizedAction(action)) { if (policyHelper.isAuthorizedAction(action)) {
editAccessList.add(EditAccess.DELETE); markDeletable();
} }
} }
} }
@ -74,12 +67,12 @@ public class ObjectPropertyStatementTemplateModel extends BaseTemplateModel {
public String getEditUrl() { public String getEditUrl() {
String editUrl = ""; String editUrl = "";
if (editAccessList.contains(EditAccess.EDIT)) { if (isEditable()) {
ParamMap params = new ParamMap( ParamMap params = new ParamMap(
"subjectUri", subjectUri, "subjectUri", subjectUri,
"predicateUri", propertyUri, "predicateUri", propertyUri,
"objectUri", objectUri); "objectUri", objectUri);
if (! editAccessList.contains(EditAccess.DELETE)) { if (! isDeletable()) {
params.put("deleteProhibited", "prohibited"); params.put("deleteProhibited", "prohibited");
} }
editUrl = UrlBuilder.getUrl(EDIT_PATH, params); editUrl = UrlBuilder.getUrl(EDIT_PATH, params);
@ -90,7 +83,7 @@ public class ObjectPropertyStatementTemplateModel extends BaseTemplateModel {
public String getDeleteUrl() { public String getDeleteUrl() {
String deleteUrl = ""; String deleteUrl = "";
if (editAccessList.contains(EditAccess.DELETE)) { if (isDeletable()) {
ParamMap params = new ParamMap( ParamMap params = new ParamMap(
"subjectUri", subjectUri, "subjectUri", subjectUri,
"predicateUri", propertyUri, "predicateUri", propertyUri,

View file

@ -0,0 +1,50 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel;
public abstract class PropertyStatementTemplateModel extends BaseTemplateModel {
private static final Log log = LogFactory.getLog(PropertyStatementTemplateModel.class);
private static enum EditAccess {
EDIT, DELETE;
}
// Used for editing
protected String subjectUri = null;
protected String propertyUri = null;
private List<EditAccess> editAccessList = null;
PropertyStatementTemplateModel(String subjectUri, String propertyUri, EditingPolicyHelper policyHelper) {
if (policyHelper != null) { // we're editing
this.subjectUri = subjectUri;
this.propertyUri = propertyUri;
editAccessList = new ArrayList<EditAccess>();
}
}
protected void markEditable() {
editAccessList.add(EditAccess.EDIT);
}
protected void markDeletable() {
editAccessList.add(EditAccess.DELETE);
}
protected boolean isEditable() {
return editAccessList.contains(EditAccess.EDIT);
}
protected boolean isDeletable() {
return editAccessList.contains(EditAccess.DELETE);
}
}