<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page trimDirectiveWhitespaces="true" %> <%@ page import="org.crosswire.community.projects.ntmss.data.ProjectManagement" %> <%@ page import="org.crosswire.community.projects.ntmss.data.ProjectManagement.Project" %> <%@ page import="org.crosswire.community.projects.ntmss.data.Apparatus.Segment" %> <%@ page import="org.crosswire.community.projects.ntmss.data.Transcription" %> <%@ page import="org.crosswire.data.DataObject" %> <%@ page import="org.crosswire.sword.keys.VerseKey" %> <%@ page import="org.crosswire.sword.keys.ListKey" %> <%@ page import="java.util.Map" %> <%@ page import="java.util.HashMap" %> <%@ page import="java.util.Set" %> <%@ page import="java.util.HashSet" %> <%@ page import="java.util.List" %> <%@ page import="java.util.ArrayList" %> <%@ page import="java.util.Collections" %> <%@ page import="org.apache.log4j.Logger" %> <%@ page import="org.crosswire.webtools.annotation.*" %> <%@ page import="org.crosswire.webtools.*" %> <%@ page import="javax.validation.constraints.NotNull" %> <%! @Description(value = "Retrieve variant apparatus data", name = "variant/apparatus/get") public static class MyParameters extends Parameters { protected ProjectManagement.Project project = null; @NotNull @Description(value = "which range of apparatus to obtain", example = "Acts.4.5") public String indexContent = null; @Description(value = "limit results to a specific segmentGroupID; -1 = all", defaultValue = "-1", example = "3") public Integer segmentGroupID = null; @Description(value = "Project Name from which data should be retrieved (i.e., lookup the project segmentGroupID by project name)", example = "ECM Matthew") public String projectName; @Description(value = "include only witnesses from a project specified by projectID", example = "1") public Integer includeOnlyProjectWitnesses = null; @Override protected void customValidation() { projectName = Transcription.assureUnicode(projectName); project = segmentGroupID != null ? ProjectManagement.getProject(segmentGroupID) : projectName != null ? ProjectManagement.getProject(projectName) : null; if (segmentGroupID == null && project != null) segmentGroupID = project.getProjectID(); } } %> <% MyParameters params = new MyParameters().loadFromRequest(request, response, false); if (params.getErrors().size() == 0) { String v11n = null; // fix this soon VerseKey vk = new VerseKey(); vk.setIntros(true); if (v11n != null) vk.setVersificationSystem(v11n); ListKey verses = vk.ParseVerseList(params.indexContent, "Mat.1.1", true); Object outputToSerialize = null; boolean firstVerse = true; int verseCount = 0; DataObject query = new DataObject(); /* String sql = "select DOCUMENTID, GROUP_CONCAT(CONCAT(VERSE, '/', CONTEXTDESCRIPTION) order by VERSE, CONTEXTDESCRIPTION SEPARATOR ', ') as LACUNA from SEGMENTREADINGWITNESS SRW left join SEGMENTREADING SR on SR.SEGMENTREADINGID = SRW.SEGMENTREADINGID left join SEGMENT S on S.SEGMENTID = SR.SEGMENTID left join SEGMENTGROUPSEGMENT SGS on SGS.SEGMENTID = S.SEGMENTID where SGS.SEGMENTGROUPID={SEGMENTGROUPID} and SR.READINGNAME = 'zz'"; query.setIntValue("SEGMENTGROUPID", params.segmentGroupID); if (verses.getCount() > 0) sql += " AND ("; for (int i = 0; i < verses.getCount(); ++i) { if (i > 0) sql += " or "; sql += "VERSE BETWEEN {VERSEMIN" + i + "} and {VERSEMAX" + i + "}"; query.setIntValue("VERSEMIN" + i, ((VerseKey)verses.getElement(i)).getLowerBound().getHashNumber()); query.setIntValue("VERSEMAX" + i, ((VerseKey)verses.getElement(i)).getUpperBound().getHashNumber()); } if (verses.getCount() > 0) sql += ")"; sql += " GROUP BY DOCUMENTID ORDER BY DOCUMENTID, VERSE"; query.executeSQL("SET SESSION group_concat_max_len = 4000000"); List rows = query.getDataSet(sql); */ Integer docGroupID = params.project.getTertiaryDocumentGroupID(); Segment fullAppQuery = new Segment(); String fullAppSQL = "select S.VERSE, S.SEGMENTID, S.CONTEXTDESCRIPTION, count(1) WITNESSCOUNT from SEGMENTREADINGWITNESS SRW left join SEGMENTREADING SR on SR.SEGMENTREADINGID = SRW.SEGMENTREADINGID left join SEGMENT S on S.SEGMENTID = SR.SEGMENTID left join SEGMENTGROUPSEGMENT SGS on SGS.SEGMENTID = S.SEGMENTID where SGS.SEGMENTGROUPID={SEGMENTGROUPID}"; fullAppQuery.setIntValue("SEGMENTGROUPID", params.segmentGroupID); if (verses.getCount() > 0) fullAppSQL += " AND ("; for (int i = 0; i < verses.getCount(); ++i) { if (i > 0) fullAppSQL += " or "; fullAppSQL += "VERSE BETWEEN {VERSEMIN" + i + "} and {VERSEMAX" + i + "}"; fullAppQuery.setIntValue("VERSEMIN" + i, ((VerseKey)verses.getElement(i)).getLowerBound().getHashNumber()); fullAppQuery.setIntValue("VERSEMAX" + i, ((VerseKey)verses.getElement(i)).getUpperBound().getHashNumber()); } if (verses.getCount() > 0) fullAppSQL += ")"; fullAppSQL += " GROUP BY VERSE, SEGMENTID ORDER BY VERSE, SEGMENTID"; params.getLogger().info("fullAppSQL: " + fullAppSQL); params.getLogger().info("fullAppSQL params: " + fullAppQuery); List segments = fullAppQuery.getDataSet(fullAppSQL); Collections.sort(segments); // "docID-VerseInstance", Set Map> lacSegs = new HashMap>(); Map> unsureMajorSegs = new HashMap>(); Map providedLacWords = new HashMap(); String sql = "select SRW.DOCUMENTID, SRW.VERSEINSTANCE, SRW.SEGMENTID, SR.READINGNAME, SRW.UNSUREMAJOR, SRW.LACWORDS from SEGMENTREADINGWITNESS SRW left join SEGMENTREADING SR on SR.SEGMENTREADINGID = SRW.SEGMENTREADINGID left join SEGMENT S on S.SEGMENTID = SR.SEGMENTID left join SEGMENTGROUPSEGMENT SGS on SGS.SEGMENTID = S.SEGMENTID where SGS.SEGMENTGROUPID={SEGMENTGROUPID} and (SR.READINGNAME = 'zz'"; sql += " or SRW.UNSUREMAJOR=1"; sql += ")"; query.setIntValue("SEGMENTGROUPID", params.segmentGroupID); if (verses.getCount() > 0) sql += " AND ("; for (int i = 0; i < verses.getCount(); ++i) { if (i > 0) sql += " or "; sql += "VERSE BETWEEN {VERSEMIN" + i + "} and {VERSEMAX" + i + "}"; query.setIntValue("VERSEMIN" + i, ((VerseKey)verses.getElement(i)).getLowerBound().getHashNumber()); query.setIntValue("VERSEMAX" + i, ((VerseKey)verses.getElement(i)).getUpperBound().getHashNumber()); } if (verses.getCount() > 0) sql += ")"; for (DataObject r : (List)query.getDataSet(sql)) { int docID = r.getIntValue("DOCUMENTID"); String verseInstance = r.getStringValue("VERSEINSTANCE"); String readingName = r.getStringValue("READINGNAME"); if ("zz".equals(readingName)) { Set docLacSegs = lacSegs.get("" + docID + "-" + verseInstance); if (docLacSegs == null) { docLacSegs = new HashSet(); lacSegs.put("" + docID + "-" + verseInstance, docLacSegs); } docLacSegs.add(r.getIntValue("SEGMENTID")); } else { if (r.getBoolValue("UNSUREMAJOR")) { Set docUMSegs = unsureMajorSegs.get("" + docID + "-" + verseInstance); if (docUMSegs == null) { docUMSegs = new HashSet(); unsureMajorSegs.put("" + docID + "-" + verseInstance, docUMSegs); } docUMSegs.add(r.getIntValue("SEGMENTID")); if (r.getValue("LACWORDS") != null) { providedLacWords.put("" + docID + "-" + verseInstance + ":" + r.getIntValue("SEGMENTID"), r.getStringValue("LACWORDS")); } } } } List docs = new ArrayList(lacSegs.keySet()); Collections.sort(docs); params.getLogger().info("docs.size: " + docs.size()); params.getLogger().info("unsureMajorSegs.size: " + unsureMajorSegs.keySet().size()); %> <% //out.print("segments.size(): " + segments.size() + "
"); %> <% for (String d : docs) { StringBuffer lacList = new StringBuffer(); StringBuffer umList = new StringBuffer(); int i = 0; for (; i < 2; ++i) { boolean inLac = false; Set docLacSegs = i == 0 ? lacSegs.get(d) : unsureMajorSegs.get(d); if (docLacSegs == null) continue; StringBuffer docLacList = new StringBuffer(); Segment firstLacSegment = null; Segment lastLacSegment = null; VerseKey dvk = new VerseKey(); dvk.setIntros(true); VerseKey dvk2 = new VerseKey(); dvk2.setIntros(true); for (Segment s : segments) { //params.getLogger().info("i: " + i + "; document: " + d + "; segment: " + s.getContextDescription()); // skip if we are an overlapping segment if (s.getIntValue("WITNESSCOUNT") < 100) continue; if (docLacSegs.contains(s.getSegmentID())) { if (!inLac) { if (docLacList.length() > 0) docLacList.append(". "); dvk.setHashNumber(s.getVerseHash()); docLacList.append(dvk.getChapter()).append(",").append(dvk.getVerse()).append("/").append(s.getWordStart()); firstLacSegment = s; inLac = true; } lastLacSegment = s; } else if (inLac && !docLacSegs.contains(s.getSegmentID())) { dvk.setHashNumber(lastLacSegment.getVerseHash()); dvk2.setHashNumber(firstLacSegment.getVerseHash()); Segment.MutableInteger verseEnd = new Segment.MutableInteger(); int wordEnd = lastLacSegment.getWordEnd(null, verseEnd); int wordStart = firstLacSegment.getWordStart(); if (verseEnd.toInteger() == null) verseEnd.set(dvk.getVerse()); boolean dashed = false; if (dvk.getChapter() != dvk2.getChapter()) { docLacList.append("-"); dashed = true; docLacList.append(dvk.getChapter()).append(","); } if (dvk.getChapter() != dvk2.getChapter() || verseEnd.toInteger() != dvk2.getVerse()) { if (!dashed) { docLacList.append("-"); dashed = true; } docLacList.append(verseEnd.toInteger()).append("/"); } if (dvk.getChapter() != dvk2.getChapter() || verseEnd.toInteger() != dvk2.getVerse() || wordEnd != wordStart) { if (!dashed) { docLacList.append("-"); dashed = true; } docLacList.append(wordEnd); } String lacWords = providedLacWords.get(d + ":" + firstLacSegment.getSegmentID()); if (lacWords != null) docLacList.append(" [ " + lacWords + " ]"); inLac = false; } } if (inLac) { dvk.setHashNumber(lastLacSegment.getVerseHash()); dvk2.setHashNumber(firstLacSegment.getVerseHash()); Segment.MutableInteger verseEnd = new Segment.MutableInteger(); int wordEnd = lastLacSegment.getWordEnd(null, verseEnd); int wordStart = firstLacSegment.getWordStart(); if (verseEnd.toInteger() == null) verseEnd.set(dvk.getVerse()); boolean dashed = false; if (dvk.getChapter() != dvk2.getChapter()) { docLacList.append("-"); dashed = true; docLacList.append(dvk.getChapter()).append(","); } if (dvk.getChapter() != dvk2.getChapter() || verseEnd.toInteger() != dvk2.getVerse()) { if (!dashed) { docLacList.append("-"); dashed = true; } docLacList.append(verseEnd.toInteger()).append("/"); } if (dvk.getChapter() != dvk2.getChapter() || verseEnd.toInteger() != dvk2.getVerse() || wordEnd != wordStart) { if (!dashed) { docLacList.append("-"); dashed = true; } docLacList.append(wordEnd); } String lacWords = providedLacWords.get(d + ":" + firstLacSegment.getSegmentID()); if (lacWords != null) docLacList.append(" [ " + lacWords + " ]"); } docLacList = new StringBuffer(docLacList.toString().replaceAll("\\. ", "
")); if (i == 0) lacList = docLacList; else umList = docLacList; } if (d.endsWith("-")) d = d.substring(0,d.length()-1); %>
<% } %>
Document IDLacuna
<%=d%> <%=lacList.toString() + (umList.length() > 0 ? ("

UnsureMajors:

" + umList.toString()) : "")%>
<% return; } params.format = "html"; Serializer.reportErrors(request, response, out, params, true); %>