%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.Transcription" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.Feature" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Arrays" %>
<%@ page import="java.util.Comparator" %>
<%@ page import="org.crosswire.xml.XMLBlock" %>
<%@ page import="org.crosswire.webtools.annotation.*" %>
<%@ page import="org.crosswire.webtools.*" %>
<%!
@Description(value = "Retrieve list of valid feature types.", name = "feature/definition/get")
public static class MyParameters extends Parameters {
@Description(value = "whether or not to include manuscript-scoped feature definitions", defaultValue = "true", example = "false")
public Boolean manuscriptFeatures = true;
@Description(value = "whether or not to include page-scoped feature definitions", defaultValue = "true", example = "false")
public Boolean pageFeatures = true;
@Description(value = "retrieve a specific feature definition by featureCode", example = "Illumination")
public String featureCode;
@Description(value = "whether or not to include surrogate-scoped feature definitions", defaultValue = "true", example = "false")
public Boolean surrogateFeatures = true;
@Description(value = "option either use cache or to skip cache and force update from database", defaultValue = "true", example = "false")
public Boolean useCache = true;
@Description(value = "also include features private to User Name", example = "jsmith")
public String userName = null;
@Description(value = "also include features private to Group Name", example = "ECM Matthew")
public String groupName = null;
@Description(value = "Show usage for this web service", example = "true", defaultValue = "false")
public Boolean help = false;
@Description(value = "internal")
public String userID;
protected void afterLoad() {
userName = Transcription.assureUnicode(userName);
if (userName == null) userName = userID;
groupName = Transcription.assureUnicode(groupName);
}
}
%>
<%
MyParameters params = new MyParameters().loadFromRequest(request, response, false);
if (params.help) {
params.format = "html";
}
else if (params.getErrors().size() == 0) {
Map featureDefs = Feature.getFeatureDefinitions(params.useCache, params.userName, params.groupName);
if (params.featureCode != null) {
Feature.FeatureDefinition fd = featureDefs.get(params.featureCode);
featureDefs = new HashMap();
if (fd != null) featureDefs.put(params.featureCode, fd);
}
Feature.FeatureDefinition features[] = featureDefs.values().toArray(new Feature.FeatureDefinition[0]);
XMLBlock retVal = XMLBlock.createXMLBlock("");
retVal.setAttribute("useCache", params.useCache.toString());
if (params.userName != null) retVal.setAttribute("extraFeaturesForUser", params.userName);
if (params.groupName != null) retVal.setAttribute("extraFeaturesForGroup", params.groupName);
retVal.setAttribute("count", Integer.toString(featureDefs.size()));
Arrays.sort(features, 0, features.length, new Comparator() {
public int compare(Feature.FeatureDefinition o1, Feature.FeatureDefinition o2) {
if (o1.getFeatureCategory() == null) return (o2.getFeatureCategory() == null) ? 0 : -1;
if ("Other".equals(o1.getFeatureCategory())) return "Other".equals(o2.getFeatureCategory()) ? 0 : 1;
if ("Other".equals(o2.getFeatureCategory())) return -1;
if (o1.getFeatureCategory().equals(o2.getFeatureCategory())) return o1.getFeatureCode().compareTo(o2.getFeatureCode());
return o1.getFeatureCategory().compareTo(o2.getFeatureCategory());
}
});
XMLBlock category = null;
for (Feature.FeatureDefinition f : features) {
if ( (params.manuscriptFeatures && f.isManuscriptFeature())
|| (params.pageFeatures && f.isPageFeature())
|| (params.surrogateFeatures && f.isSurrogateFeature())
|| (params.manuscriptFeatures && params.pageFeatures && params.surrogateFeatures)) {
if (f.getFeatureCategory() != null && f.getFeatureCategory().length() > 0 && (category == null || !f.getFeatureCategory().equals(category.getAttribute("name")))) {
category = retVal.createBlock("featureCategory");
category.setAttribute("name", f.getFeatureCategory());
}
XMLBlock feature = XMLBlock.createXMLBlock(f.toFormattedXML());
if (category != null) category.addBlock(feature);
else retVal.addBlock(feature);
}
}
Serializer.output(response, out, params, retVal);
return;
}
else params.format = "html";
Serializer.reportErrors(request, response, out, params, true);
%>