Commit 0d6e81d9 authored by Matija Obreza's avatar Matija Obreza
Browse files

Download all core accession data in DWCA for instCode

parent 5d66ca13
......@@ -19,6 +19,7 @@ package org.genesys2.server.persistence.domain;
import org.genesys2.server.model.genesys.Method;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.FaoInstitute;
import org.springframework.jdbc.core.RowCallbackHandler;
public interface GenesysLowlevelRepository {
......@@ -36,4 +37,6 @@ public interface GenesysLowlevelRepository {
void ensureMethodTable(Method method);
void refreshMetadataMethods();
void listAccessions(String instCode, RowCallbackHandler rowCallbackHandler);
}
......@@ -16,7 +16,9 @@
package org.genesys2.server.persistence.domain;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
......@@ -29,6 +31,8 @@ import org.genesys2.server.model.genesys.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
......@@ -198,4 +202,18 @@ public class GenesysLowlevelRepositoryImpl implements GenesysLowlevelRepository
}
}
}
@Override
public void listAccessions(final String instCode, RowCallbackHandler rowCallbackHandler) {
jdbcTemplate.query(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
PreparedStatement stmt = con.prepareStatement("select a.id, a.uuid, a.instCode, a.acceNumb, a.genus, t.species, a.createdBy, a.createdDate, a.lastModifiedBy, a.lastModifiedDate from accession a left outer join taxonomy t on t.id=a.taxonomyId where a.instCode = ?");
stmt.setString(1, instCode);
// Set mysql JConnector to stream results
stmt.setFetchSize(Integer.MIN_VALUE);
return stmt;
}
}, rowCallbackHandler);
}
}
......@@ -16,6 +16,8 @@
package org.genesys2.server.service;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;
......@@ -134,4 +136,6 @@ public interface GenesysService {
long countDatasets(FaoInstitute faoInstitute);
void writeAccessions(FaoInstitute faoInstitute, OutputStream outputStream) throws IOException;
}
......@@ -20,6 +20,8 @@ import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
......@@ -81,6 +83,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.acls.domain.BasePermission;
import org.springframework.stereotype.Service;
......@@ -275,7 +278,7 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
public Page<Metadata> listDatasets(FaoInstitute faoInstitute, Pageable pageable) {
return metadataRepository.findByInstituteCode(faoInstitute.getCode(), pageable);
}
@Override
public long countDatasets(FaoInstitute faoInstitute) {
return metadataRepository.countByInstituteCode(faoInstitute.getCode());
......@@ -334,7 +337,7 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
public Page<Accession> listAccessionsByTaxonomy(Taxonomy taxonomy, Pageable pageable) {
return accessionRepository.findByTaxonomy(taxonomy, pageable);
}
@Override
public Page<Accession> listAccessionsByGenus(String genus, Pageable pageable) {
return accessionRepository.findByGenus(genus, pageable);
......@@ -627,6 +630,73 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
// methodValues.keySet());
}
@Override
public void writeAccessions(final FaoInstitute faoInstitute, OutputStream outputStream) throws IOException {
// UTF8 is used for encoding entry names
ZipOutputStream zos = new ZipOutputStream(outputStream);
zos.setComment("Genesys Accessions instCode=" + faoInstitute.getCode());
ZipEntry coreEntry = new ZipEntry("core.csv");
coreEntry.setComment("Accession information");
coreEntry.setTime(System.currentTimeMillis());
zos.putNextEntry(coreEntry);
// Accessions
@SuppressWarnings("resource")
final CSVWriter csv = new CSVWriter(new BufferedWriter(new OutputStreamWriter(zos)), ',', '"', '\\', "\n");
csv.writeNext(new String[] { "Genesys ID", "UUID", "instCode", "acceNumb", "genus", "species", "createdBy", "createdDate", "lastModifiedBy",
"lastModifiedDate" });
// Write accession information
genesysLowlevelRepository.listAccessions(faoInstitute.getCode(), new RowCallbackHandler() {
int i = 0;
@Override
public void processRow(ResultSet rs) throws SQLException {
String createdBy = rs.getString(7);
createdBy = rs.wasNull() ? "" : createdBy;
String lastModifiedBy = rs.getString(9);
lastModifiedBy = rs.wasNull() ? "" : lastModifiedBy;
csv.writeNext(new String[] { String.valueOf(rs.getLong(1)), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5),
rs.getString(6), createdBy, "" + rs.getTimestamp(8), lastModifiedBy, "" + rs.getTimestamp(10) });
if ((i++ % 5000) == 0) {
LOG.info("Writing accessions DWCA " + faoInstitute.getCode() + " " + i);
}
}
});
csv.flush();
zos.closeEntry();
ZipEntry metaEntry = new ZipEntry("meta.xml");
metaEntry.setComment("Darwin Core Archive metadata");
metaEntry.setTime(System.currentTimeMillis());
zos.putNextEntry(metaEntry);
BufferedWriter osw = new BufferedWriter(new OutputStreamWriter(zos));
osw.write("<?xml version='1.0' encoding='utf-8'?>\n");
osw.write("<archive xmlns=\"http://rs.tdwg.org/dwc/text/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://rs.tdwg.org/dwc/text/ http://rs.tdwg.org/dwc/text/tdwg_dwc_text.xsd\">\n");
osw.write("<core encoding=\"UTF-8\" fieldsTerminatedBy=\",\" linesTerminatedBy=\"\\n\" fieldsEnclosedBy=\"&quot;\" ignoreHeaderLines=\"0\">\n");
osw.write("\t<files><location>core.csv</location></files>\n");
osw.write("\t<id index=\"0\" />\n");
osw.write("\t<field index=\"1\" term=\"UUID\"/>\n");
osw.write("\t<field index=\"2\" term=\"http://purl.org/germplasm/germplasmType#wiewsInstituteID\"/>\n");
osw.write("\t<field index=\"3\" term=\"http://purl.org/germplasm/germplasmTerm#germplasmIdentifier\"/>\n");
osw.write("\t<field index=\"4\" term=\"http://rs.tdwg.org/dwc/terms/genus\"/>\n");
osw.write("\t<field index=\"5\" term=\"http://rs.tdwg.org/dwc/terms/species\"/>\n");
osw.write("\t<field index=\"6\" term=\"createdBy\"/>\n");
osw.write("\t<field index=\"7\" term=\"createdDate\"/>\n");
osw.write("\t<field index=\"8\" term=\"lastModifiedBy\"/>\n");
osw.write("\t<field index=\"9\" term=\"lastModifiedDate\"/>\n");
osw.write("</core>\n");
osw.write("</archive>\n");
osw.flush();
zos.closeEntry();
zos.finish();
}
@Override
public void writeDataset(Metadata metadata, OutputStream outputStream) throws IOException {
// Methods: A CSV is generated for each method
......
......@@ -16,9 +16,13 @@
package org.genesys2.server.servlet.controller;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.Taxonomy;
import org.genesys2.server.model.impl.FaoInstitute;
......@@ -200,4 +204,22 @@ public class WiewsController extends BaseController {
return "/accession/data";
}
@RequestMapping("/{wiewsCode}/dwca")
public void viewData(ModelMap model, @PathVariable(value = "wiewsCode") String wiewsCode, HttpServletResponse response) throws IOException {
_logger.debug("Viewing country " + wiewsCode);
FaoInstitute faoInstitute = instituteService.getInstitute(wiewsCode);
if (faoInstitute == null) {
throw new ResourceNotFoundException();
}
_logger.warn("Searching accessions of: " + faoInstitute);
response.setContentType("application/zip");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-accessions-%1$s.zip\"", faoInstitute.getCode()));
// Write Darwin Core Archive to the stream.
OutputStream outputStream = response.getOutputStream();
genesysService.writeAccessions(faoInstitute, outputStream);
response.flushBuffer();
}
}
......@@ -127,10 +127,16 @@
</c:forEach>
</ul>
</div>
</div>
<div class="row" style="margin-top: 2em;">
<div class="col-sm-4">
<a href="<c:url value="/wiews/${faoInstitute.code.toLowerCase()}/dwca" />"><spring:message code="metadata.download-dwca" /></a>
</div>
</div>
<c:if test="${faoInstitute.latitude ne null}">
<script type="text/javascript">
jQuery(document).ready(function() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment