%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.Feature" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.Feature.FeatureDefinition" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.Transcription" %>
<%@ page import="org.crosswire.xml.XMLBlock" %>
<%@ page import="org.crosswire.webtools.annotation.*" %>
<%@ page import="org.crosswire.webtools.*" %>
<%@ page import="javax.validation.constraints.NotNull" %>
<%!
@Description(value = "Submit a new or modified feature definition.", name = "feature/definition/put")
public static class MyParameters extends Parameters {
@NotNull
@Description(value = "feature code to modify or add", example = "MyCoolFeature")
public String featureCode;
@Description(value = "category for feature", example = "Cool Features")
public String category;
@Description(value = "brief description of feature", example = "My Cool Feature")
public String description;
@Description(value = "labels for value fields of feature. ';' to separate", example = "Repository Name; URL")
public String labels;
@Description(value = "constraints for value fields of feature. possible values: bool, stringVal[1,2], intVal[1,2], dateVal[1,2]. With stringVal fields, a valid choice list can be provided in brackets. Separate multiple fields with ';'.", example = "stringVal1[choice 1, choice 2]; stringVal2")
public String constraints;
@Description(value = "Any notes for this feature", example = "This feature is useful for marking cool stuff in manuscripts.")
public String notes;
@Description(value = "JavaScript which should be applied during input dialog display", example = "")
public String dialogJavaScript;
@Description(value = "if this feature can be applied to a whole manuscript", defaultValue = "false", example = "true")
public Boolean isManuscriptFeature = null;
@Description(value = "if this feature can be applied a manuscript page", defaultValue = "false", example = "true")
public Boolean isPageFeature = null;
@Description(value = "if this feature can be applied to a surrogate of a manuscript page", defaultValue = "false", example = "true")
public Boolean isSurrogateFeature = null;
@Description(value = "if this feature should be scoped local to the creator of the feature", example = "false", defaultValue = "true")
public Boolean isUserFeature = false;
@Description(value = "feature is scoped to this group", example = "ECM Matthew")
public String groupName = null;
@Description(value = "feature is owned by this user", example = "joe")
public String userName = null;
@Description(value = "internal")
public String groupID;
@Override
protected void afterLoad() {
if (groupName == null && groupID != null) groupName = groupID;
groupName = Transcription.assureUnicode(groupName);
featureCode = Transcription.assureUnicode(featureCode);
category = Transcription.assureUnicode(category);
description = Transcription.assureUnicode(description);
labels = Transcription.assureUnicode(labels);
constraints = Transcription.assureUnicode(constraints);
notes = Transcription.assureUnicode(notes);
dialogJavaScript = Transcription.assureUnicode(dialogJavaScript);
// right now, only VMR Administrators can change features owned by other users
// TODO: allow site admins to change site member group features
if (userName != null && (getUser() == null || !getUser().hasRole("VMR Administrator"))) {
userName = getUser() == null ? null : getUser().getUserName();
}
if (userName == null) userName = getUser() != null ? getUser().getUserName() : null;
if (isUserFeature && featureCode!= null && userName != null && !featureCode.startsWith(userName + "-")) {
featureCode = userName + "-" + featureCode;
}
else userName = null;
}
@Override
protected void customValidation() {
if (getUser() == null) {
addError(-5, "Must be logged in to edit feature definitions.");
return;
}
}
}
%>
<%
MyParameters params = new MyParameters().loadFromRequest(request, response, false);
if (params.getErrors().size() == 0) {
boolean privileged = params.getUser().hasRole("VMR Administrator");
FeatureDefinition featureDefinition = null;
featureDefinition = Feature.getFeatureDefinitions(params.userName).get(params.featureCode);
params.getLogger().info("featureDefinition: " + featureDefinition);
if (!params.isUserFeature && !privileged) {
params.addError(-5, "No permission to edit global features. Must be a VMR Administrator.");
}
else if (!privileged && featureDefinition != null && (params.userName == null || !params.userName.equals(featureDefinition.userID))) {
params.addError(-8, "No permission to edit feature definitions of other users. Must be a VMR Administrator.");
}
else {
if (featureDefinition == null) featureDefinition = new FeatureDefinition();
featureDefinition.featureCode = params.featureCode;
if (params.category != null) featureDefinition.featureCategory = params.category;
if (params.description != null) featureDefinition.featureDescription = params.description;
if (params.labels != null) featureDefinition.featureLabels = params.labels;
if (params.constraints != null) featureDefinition.featureConstraints = params.constraints;
if (params.isPageFeature != null) featureDefinition.pageFeature = params.isPageFeature;
if (params.isManuscriptFeature != null) featureDefinition.manuscriptFeature = params.isManuscriptFeature;
if (params.isSurrogateFeature != null) featureDefinition.surrogateFeature = params.isSurrogateFeature;
if (params.notes != null) featureDefinition.notes = params.notes;
if (params.dialogJavaScript != null) featureDefinition.dialogJavaScript = params.dialogJavaScript;
if (params.isUserFeature) featureDefinition.userID = params.userName;
if (params.groupName != null) featureDefinition.groupID = params.groupName;
params.getLogger().info("modified featureDefinition: " + featureDefinition);
Feature.saveFeatureDefinition(featureDefinition);
Serializer.output(response, out, params, XMLBlock.createXMLBlock(featureDefinition.toFormattedXML()));
return;
}
}
else params.format = "html";
Serializer.reportErrors(request, response, out, params, true);
%>