%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Collections" %>
<%@ page import="javax.validation.constraints.Pattern" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.Apparatus" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.Apparatus.Segment" %>
<%@ page import="org.crosswire.utils.Utils" %>
<%@ page import="org.crosswire.utils.HTTPUtils" %>
<%@ page import="org.crosswire.webtools.annotation.*" %>
<%@ page import="org.crosswire.webtools.*" %>
<%!
@Description(value = "Retrieve segments", name = "variant/segment/get")
public static class MyParameters extends Parameters {
protected int minDocID = -1;
protected int maxDocID = -1;
@Description(value = "Segment id to retrieve", example = "1")
public Integer segmentID;
@Description(value = "Retrieve all segments in a segment group", example = "1")
public Integer segmentGroupID;
@Description(value = "Limit segements to indexContent", example = "John.3")
public String indexContent;
@Description(value = "Limit segements to where docID has data. Can also be a range", example = "91035-91037")
@Pattern(regexp = "^[0-9]*(-[0-9]*)?$", message = "Please give a docID or docID range with min and max values separated by a - (dash).")
public String docID = null;
@Description(value = "whether or not to break out sublabel readings into their own readings", defaultValue = "true", example = "false")
public Boolean breakoutSublabelReadings = true;
@Description(value = "add extra detail if desired", defaultValue = "complete", example = "extra")
public String detail = "complete";
@Override
protected void customValidation() {
if (docID != null) {
String range[] = docID.split("-");
minDocID = Integer.parseInt(range[0]);
maxDocID = Integer.parseInt(range[range.length-1]);
}
}
@Override
protected boolean permissionValidation(HttpServletRequest request, HttpServletResponse response) { return true; }
@Override
protected String[] getExamples() {
return new String[] {
"ECM for Acts.1.1: segmentGroupID=-1&indexContent=Acts.1.1",
"d-ECM for Acts.1.1: segmentGroupID=69&indexContent=Acts.1.1"
};
}
}
%>
<%
MyParameters params = new MyParameters().loadFromRequest(request, response);
if (params.getErrors().size() == 0 && (params.segmentID != null || params.segmentGroupID != null)) {
int detail = Segment.parseDetail(params.detail, Segment.DETAIL_COMPLETE);
response.setContentType("text/xml");
List segments = new ArrayList();
if (params.segmentID != null) {
Segment s = Segment.getSegment(params.segmentID);
if (s != null) {
segments.add(s);
}
}
else segments = Apparatus.getSegments(params.indexContent, params.segmentGroupID, params.minDocID, params.maxDocID);
Collections.sort(segments);
StringBuffer retVal = new StringBuffer();
retVal.append("");
for (Segment s : segments) {
if (params.minDocID != -1) {
for (int d = params.minDocID; d <= params.maxDocID; ++d) {
for (Apparatus.SegmentReadingWitness srw : Apparatus.getSegmentReadingWitnessesForDocumentID(s.getSegmentID(), d)) {
s.addSegmentReadingWitness(srw);
}
}
}
s.setBreakoutReadingSublabels(params.breakoutSublabelReadings);
retVal.append(s.toFormattedXML(detail));
}
retVal.append("");
if ("json".equals(params.format) || "csv".equals(params.format)) {
try {
Map result = Serializer.fromXML(retVal.toString());
Serializer.output(response, out, params, "csv".equals(params.format) ? ((Map)result.get("segments")).get("segment") : result, null);
}
catch (Exception e) { params.getLogger().error("ERROR! " + e); e.printStackTrace(); }
return;
}
else out.print(retVal);
}
else {
params.format = "html";
if (params.getErrors().size() == 0) params.addError(0, "usage");
Serializer.reportErrors(request, response, out, params);
}
%>