first pass at creating a csv download option. TODO icons on page, debug failing load

This commit is contained in:
Stephen V. Williams 2013-05-14 14:40:17 -06:00
parent 1b890a3767
commit 96bed7ce03
4 changed files with 52 additions and 3 deletions

View file

@ -70,6 +70,7 @@ public class PagedSearchController extends FreemarkerHttpServlet {
protected static final int DEFAULT_MAX_HIT_COUNT = 1000; protected static final int DEFAULT_MAX_HIT_COUNT = 1000;
private static final String PARAM_XML_REQUEST = "xml"; private static final String PARAM_XML_REQUEST = "xml";
private static final String PARAM_CSV_REQUEST = "csv";
private static final String PARAM_START_INDEX = "startIndex"; private static final String PARAM_START_INDEX = "startIndex";
private static final String PARAM_HITS_PER_PAGE = "hitsPerPage"; private static final String PARAM_HITS_PER_PAGE = "hitsPerPage";
private static final String PARAM_CLASSGROUP = "classgroup"; private static final String PARAM_CLASSGROUP = "classgroup";
@ -79,7 +80,7 @@ public class PagedSearchController extends FreemarkerHttpServlet {
protected static final Map<Format,Map<Result,String>> templateTable; protected static final Map<Format,Map<Result,String>> templateTable;
protected enum Format { protected enum Format {
HTML, XML; HTML, XML, CSV;
} }
protected enum Result { protected enum Result {
@ -101,9 +102,10 @@ public class PagedSearchController extends FreemarkerHttpServlet {
throws IOException, ServletException { throws IOException, ServletException {
VitroRequest vreq = new VitroRequest(request); VitroRequest vreq = new VitroRequest(request);
boolean wasXmlRequested = isRequestedFormatXml(vreq); boolean wasXmlRequested = isRequestedFormatXml(vreq);
if( ! wasXmlRequested ){ boolean wasCSVRequested = isRequestedFormatCSV(vreq);
if( !wasXmlRequested && !wasCSVRequested){
super.doGet(vreq,response); super.doGet(vreq,response);
}else{ }else if (wasXMLRequested){
try { try {
ResponseValues rvalues = processRequest(vreq); ResponseValues rvalues = processRequest(vreq);
@ -113,6 +115,16 @@ public class PagedSearchController extends FreemarkerHttpServlet {
} catch (Exception e) { } catch (Exception e) {
log.error(e, e); log.error(e, e);
} }
}else if (wasCSVRequested){
try {
ResponseValues rvalues = processRequest(vreq);
response.setCharacterEncoding("UTF-8");
response.setContentType("text/csv;charset=UTF-8");
writeTemplate(rvalues.getTemplateName(), rvalues.getMap(), request, response);
} catch (Exception e) {
log.error(e, e);
}
} }
} }
@ -672,10 +684,25 @@ public class PagedSearchController extends FreemarkerHttpServlet {
return false; return false;
} }
} }
protected boolean isRequestedFormatCSV(VitroRequest req){
if( req != null ){
String param = req.getParameter(PARAM_CSV_REQUEST);
if( param != null && "1".equals(param)){
return true;
}else{
return false;
}
}else{
return false;
}
}
protected Format getFormat(VitroRequest req){ protected Format getFormat(VitroRequest req){
if( req != null && req.getParameter("xml") != null && "1".equals(req.getParameter("xml"))) if( req != null && req.getParameter("xml") != null && "1".equals(req.getParameter("xml")))
return Format.XML; return Format.XML;
else if ( req != null && req.getParameter("csv") != null && "1".equals(req.getParameter("csv")))
return Format.CSV;
else else
return Format.HTML; return Format.HTML;
} }
@ -705,9 +732,20 @@ public class PagedSearchController extends FreemarkerHttpServlet {
resultsToTemplates = new HashMap<Result,String>(); resultsToTemplates = new HashMap<Result,String>();
resultsToTemplates.put(Result.PAGED, "search-xmlResults.ftl"); resultsToTemplates.put(Result.PAGED, "search-xmlResults.ftl");
resultsToTemplates.put(Result.ERROR, "search-xmlError.ftl"); resultsToTemplates.put(Result.ERROR, "search-xmlError.ftl");
// resultsToTemplates.put(Result.BAD_QUERY, "search-xmlBadQuery.ftl"); // resultsToTemplates.put(Result.BAD_QUERY, "search-xmlBadQuery.ftl");
templateTable.put(Format.XML, Collections.unmodifiableMap(resultsToTemplates)); templateTable.put(Format.XML, Collections.unmodifiableMap(resultsToTemplates));
// set up CSV format
resultsToTemplates = new HashMap<Result,String>();
resultsToTemplates.put(Result.PAGED, "search-csvResults.ftl");
resultsToTemplates.put(Result.ERROR, "search-csvError.ftl");
// resultsToTemplates.put(Result.BAD_QUERY, "search-xmlBadQuery.ftl");
templateTable.put(Format.CSV, Collections.unmodifiableMap(resultsToTemplates));
return Collections.unmodifiableMap(templateTable); return Collections.unmodifiableMap(templateTable);
} }
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View file

@ -0,0 +1,5 @@
<#-- $This file is distributed under the terms of the license in /doc/license.txt$ -->
URI,Name
<#list individuals as individual>
${individual.uri?xml},${individual.name?xml}
</#list>

View file

@ -8,8 +8,14 @@
<#if classGroupName?has_content>limited to type '${classGroupName}'</#if> <#if classGroupName?has_content>limited to type '${classGroupName}'</#if>
<#if typeName?has_content>limited to type '${typeName}'</#if> <#if typeName?has_content>limited to type '${typeName}'</#if>
</#escape> </#escape>
<span id="downloadResults">
<img src="images/share-uri-icon.png" alt="XML/RDF Results" />
</span>
</h2> </h2>
<span id="searchHelp"><a href="${urls.base}/searchHelp" title="search help">Not the results you expected?</a></span> <span id="searchHelp"><a href="${urls.base}/searchHelp" title="search help">Not the results you expected?</a></span>
<div class="contentsBrowseGroup"> <div class="contentsBrowseGroup">
<#-- Refinement links --> <#-- Refinement links -->