Commit fdc67c31 authored by Matija Obreza's avatar Matija Obreza
Browse files

Download PDCI for faoInstitute

parent ac3bd24b
......@@ -16,8 +16,8 @@
package org.genesys2.server.persistence.domain;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import org.genesys2.server.model.genesys.PDCI;
import org.genesys2.server.model.impl.FaoInstitute;
......@@ -30,7 +30,7 @@ public interface PDCIRepository extends JpaRepository<PDCI, Long> {
PDCI findByAccessionId(Long accessionId);
@Query("select pdci from PDCI pdci where pdci.accession.id in (?1)")
List<PDCI> findByAccessionId(Set<Long> accessionIds);
List<PDCI> findByAccessionId(Collection<Long> accessionIds);
/**
* Gets [ min(pdci.score), max(pdci.score), avg(pdci.score), count(pdci) ]
......
......@@ -31,4 +31,6 @@ public interface DownloadService {
void writeXlsxMCPD(AppliedFilters filters, OutputStream outputStream) throws IOException;
void writeXlsxPDCI(AppliedFilters appliedFilters, OutputStream outputStream) throws IOException;
}
......@@ -237,4 +237,6 @@ public interface GenesysService {
void updatePDCI(Set<AccessionDetails> ads);
List<PDCI> loadPDCI(List<Long> batch);
}
......@@ -18,10 +18,13 @@ package org.genesys2.server.service.impl;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.persistence.EntityManager;
......@@ -44,7 +47,9 @@ import org.genesys2.server.model.genesys.AccessionCollect;
import org.genesys2.server.model.genesys.AccessionData;
import org.genesys2.server.model.genesys.AccessionExchange;
import org.genesys2.server.model.genesys.AccessionGeo;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.genesys.AccessionRemark;
import org.genesys2.server.model.genesys.PDCI;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.persistence.domain.GenesysLowlevelRepository;
......@@ -77,9 +82,14 @@ public class DownloadServiceImpl implements DownloadService {
@Autowired
private MethodRepository methodRepository;
@Autowired(required = false)
private PDCICalculator pdciCalculator;
@Value("${base.url}")
private String baseUrl;
private HashMap<String, Method> pdciMethods;
@Override
@Transactional(readOnly = true)
public void writeXlsxMCPD(final AppliedFilters filters, final OutputStream outputStream) throws IOException {
......@@ -156,10 +166,26 @@ public class DownloadServiceImpl implements DownloadService {
Row row = sheet.createRow(i + 1);
writeMCPDRow(sheet, row, all.accession, all.geo, all.collect, all.bred, all.names, all.exch, all.remarks);
i++;
}
LOG.info("Writing XLSX row " + i);
try {
((SXSSFSheet) sheet).flushRows();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
private void processPDCIBatch(Sheet sheet, List<Long> batch) {
List<PDCI> alls = genesysService.loadPDCI(batch);
LOG.info("Writing MCPD row " + i);
for (PDCI pdci : alls) {
Row row = sheet.createRow(i + 1);
writePDCIRow(sheet, row, pdci);
i++;
}
LOG.info("Writing XLSX row " + i);
try {
((SXSSFSheet) sheet).flushRows();
} catch (IOException e) {
......@@ -295,6 +321,126 @@ public class DownloadServiceImpl implements DownloadService {
}
}
@Override
@Transactional(readOnly = true)
public void writeXlsxPDCI(final AppliedFilters filters, final OutputStream outputStream) throws IOException {
XSSFWorkbook template = new XSSFWorkbook(getClass().getResourceAsStream("/template/download/PDCI.xlsx"));
// keep 1000 rows in memory, exceeding rows will be flushed to disk
SXSSFWorkbook wb = new SXSSFWorkbook(template, -1);
CellStyle dateStyle = wb.createCellStyle();
dateStyle.setDataFormat(wb.createDataFormat().getFormat("dd/mm/yyyy"));
dateStyle.setAlignment(CellStyle.ALIGN_LEFT);
Sheet legal = wb.getSheet("Legal information");
Row r;
Cell c;
r = legal.createRow(0);
r.createCell(0).setCellValue("Server URL");
r.createCell(1).setCellValue(baseUrl);
r = legal.createRow(1);
r.createCell(0).setCellValue("Filters");
r.createCell(1).setCellValue(filters.toString());
r = legal.createRow(2);
r.createCell(0).setCellValue("Data source");
c = r.createCell(1);
c.setCellValue(baseUrl + "/explore?filter=" + filters.toString());
r = legal.createRow(3);
r.createCell(0).setCellValue("Date");
c = r.createCell(1);
c.setCellStyle(dateStyle);
c.setCellValue(new Date());
r = legal.createRow(4);
r.createCell(0).setCellValue("Attribution");
r.createCell(1).setCellValue(baseUrl + "/content/terms");
((SXSSFSheet) legal).flushRows();
outputStream.flush();
final Sheet sheet = wb.getSheet("PDCI");
final ArrayList<Long> batch = new ArrayList<Long>(100);
final Writer writer = new Writer();
// Write accession information
genesysLowlevelRepository.listAccessionIds(filters, new Sort("acceNumb"), new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
long accessionId = rs.getLong(1);
if (batch.size() == 500) {
writer.processPDCIBatch(sheet, batch);
batch.clear();
}
batch.add(accessionId);
}
});
writer.processPDCIBatch(sheet, batch);
((SXSSFSheet) sheet).flushRows();
LOG.info("Writing to output stream");
wb.write(outputStream);
wb.dispose();
outputStream.flush();
LOG.info("Done");
}
private void writePDCIRow(Sheet sheet, Row row, PDCI pdci) {
// Process and write result
AccessionId accession = pdci.getAccession();
if (accession.getUuid() != null) {
XSSFHyperlink hypr = (XSSFHyperlink) sheet.getWorkbook().getCreationHelper().createHyperlink(Hyperlink.LINK_URL);
hypr.setAddress("http://purl.org/germplasm/id/" + accession.getUuid());
Cell cell = createCell(row, 0, accession.getUuid().toString());
cell.setHyperlink(hypr);
}
createCell(row, 1, pdci.getScore());
int col = 2;
for (String indicator : pdci.getIndependentItems()) {
createCell(row, col++, getPDCIScore(pdci, indicator));
}
for (String indicator : pdci.getDependentItems()) {
createCell(row, col++, getPDCIScore(pdci, indicator));
}
}
private Number getPDCIScore(PDCI pdci, String indicator) {
synchronized (this) {
if (pdciMethods == null) {
pdciMethods = new HashMap<String, Method>();
try {
for (String name : PDCI.independentItems) {
Method m = PDCI.class.getMethod("get" + StringUtils.capitalize(name));
if (m.getReturnType() == int.class) {
pdciMethods.put(name, m);
}
}
for (String name : PDCI.dependentItems) {
Method m = PDCI.class.getMethod("get" + StringUtils.capitalize(name));
if (m.getReturnType() == int.class) {
pdciMethods.put(name, m);
}
}
} catch (Throwable e) {
LOG.warn(e.getMessage(), e);
}
}
}
try {
Method m = pdciMethods.get(indicator);
if (m != null) {
return (int) m.invoke(pdci);
} else {
return -1;
}
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
LOG.warn(e.getMessage());
return -1;
}
}
private Cell createCell(Row row, int column, Number integer) {
if (integer == null)
return null;
......
......@@ -1555,6 +1555,11 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
public PDCI loadPDCI(Long accessionId) {
return repoPdci.findByAccessionId(accessionId);
}
@Override
public List<PDCI> loadPDCI(List<Long> accessionIds) {
return repoPdci.findByAccessionId(accessionIds);
}
@Override
@Transactional
......
......@@ -323,4 +323,33 @@ public class WiewsController extends BaseController {
_logger.warn("Download was aborted", e);
}
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{wiewsCode}/download", method = RequestMethod.POST, params = { "pdci" })
public void downloadPdci(ModelMap model, @PathVariable(value = "wiewsCode") String wiewsCode, HttpServletResponse response) throws IOException {
final FaoInstitute faoInstitute = instituteService.getInstitute(wiewsCode);
if (faoInstitute == null) {
throw new ResourceNotFoundException();
}
_logger.warn("Searching accessions of: " + faoInstitute);
// Create JSON filter
final AppliedFilters appliedFilters = new AppliedFilters();
final AppliedFilter arr = new AppliedFilter().setFilterName(FilterConstants.INSTCODE);
arr.addFilterValue(new FilterHandler.LiteralValueFilter(faoInstitute.getCode()));
appliedFilters.add(arr);
// Write MCPD to the stream.
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-pdci-%1$s.xlsx\"", faoInstitute.getCode()));
response.flushBuffer();
final OutputStream outputStream = response.getOutputStream();
try {
downloadService.writeXlsxPDCI(appliedFilters, outputStream);
response.flushBuffer();
} catch (EOFException e) {
_logger.warn("Download was aborted", e);
}
}
}
......@@ -634,3 +634,5 @@ accession.acqDate=Acquisition date
accession.svalbard-data.url=Svalbard database URL
accession.svalbard-data.url-title=Deposit information in SGSV database
accession.svalbard-data.url-text=View SGSV deposit information for {0}
filter.download-pdci=Download PDCI data
......@@ -234,11 +234,14 @@
<form class="form-horizontal" method="post" action="/download/wiews/${faoInstitute.code}/download">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<div class="row" style="margin-top: 2em;">
<div class="col-sm-4">
<div class="col-sm-12">
<security:authorize access="isAuthenticated()">
<button name="mcpd" class="btn btn-primary" type="submit">
<spring:message code="filter.download-mcpd" />
</button>
<button name="pdci" class="btn btn-default" type="submit">
<spring:message code="filter.download-pdci" />
</button>
</security:authorize>
<button name="dwca" class="btn btn-default" type="submit">
<spring:message code="metadata.download-dwca" />
......
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