Commit 30af09cd authored by Matija Obreza's avatar Matija Obreza
Browse files

Download datasets as Darwin Core Archive

parent 90fddbab
/**
* Copyright 2013 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.model.genesys;
/*
* Looks like {@link AccessionTrait}
*/
public class ExperimentAccessionTrait {
private long experimentId;
private long accessionId;
private Object value;
public ExperimentAccessionTrait(final long experimentId, final long accessionId, final Object value) {
this.experimentId = experimentId;
this.accessionId=accessionId;
this.value = value;
}
public long getExperimentId() {
return experimentId;
}
public void setExperimentId(final long experimentId) {
this.experimentId = experimentId;
}
public long getAccessionId() {
return accessionId;
}
public void setAccessionId(long accessionId) {
this.accessionId = accessionId;
}
public Object getValue() {
return value;
}
public void setValue(final Object value) {
this.value = value;
}
@Override
public String toString() {
return "{" + value + ", " + experimentId + "}";
}
}
......@@ -87,4 +87,12 @@ public class TraitCode {
}
return map;
}
public static Map<String, String> parseCodeMap(final String options) {
final Map<String, String> map = new HashMap<String, String>();
for (final TraitCode code : parseOptions(options)) {
map.put(code.getCode(), code.getValue());
}
return map;
}
}
......@@ -22,6 +22,7 @@ import java.util.Set;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionTrait;
import org.genesys2.server.model.genesys.ExperimentAccessionTrait;
import org.genesys2.server.model.genesys.ExperimentTrait;
import org.genesys2.server.model.genesys.Metadata;
import org.genesys2.server.model.genesys.MetadataMethod;
......@@ -33,6 +34,8 @@ public interface TraitValueRepository {
//
// List<ExperimentTrait> getValues(Accession accession, Method method);
List<ExperimentAccessionTrait> getValues(Metadata metadata, Method method);
Map<Long, List<ExperimentTrait>> getValues(List<AccessionTrait> accessionTraits);
Map<Long, Map<Long, List<Object>>> getValues(Metadata metadata, List<Method> methods, List<Accession> accessions);
......@@ -69,4 +72,5 @@ public interface TraitValueRepository {
*/
void updateMetadataMethods(Metadata metadata, Set<Long> methodIds);
}
\ No newline at end of file
......@@ -16,7 +16,9 @@
package org.genesys2.server.persistence.domain;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -31,6 +33,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionTrait;
import org.genesys2.server.model.genesys.ExperimentAccessionTrait;
import org.genesys2.server.model.genesys.ExperimentTrait;
import org.genesys2.server.model.genesys.Metadata;
import org.genesys2.server.model.genesys.MetadataMethod;
......@@ -38,7 +41,9 @@ import org.genesys2.server.model.genesys.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
......@@ -90,6 +95,25 @@ public class TraitValueRepositoryImpl implements TraitValueRepository {
// return ret;
// }
@Override
public List<ExperimentAccessionTrait> getValues(final Metadata metadata, final Method method) {
final long metadataId = metadata.getId();
return this.jdbcTemplate.query(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
PreparedStatement pst = conn.prepareStatement("select `ALIS_Id` as accessionId, `" + method.getFieldName() + "` as traitvalue from `"
+ method.getId() + "` where `Meta_Id`=?");
pst.setLong(1, metadataId);
return pst;
}
}, new RowMapper<ExperimentAccessionTrait>() {
@Override
public ExperimentAccessionTrait mapRow(ResultSet rs, int pos) throws SQLException {
return new ExperimentAccessionTrait(metadataId, rs.getLong(1), rs.getObject(2));
}
});
}
@Override
public Map<Long, List<ExperimentTrait>> getValues(List<AccessionTrait> accessionTraits) {
if (accessionTraits == null) {
......
......@@ -16,6 +16,8 @@
package org.genesys2.server.service;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
......@@ -38,4 +40,13 @@ public interface DatasetService {
void touch(@Param("metadata") Metadata metadata);
void upsertAccessionData(Metadata metadata, Accession accession, Map<Long, List<Object>> methodValues);
/**
* Writes a Darwin Core Archive to the specified output stream
*
* @param metadata
* @param outputStream
* @throws IOException
*/
void writeDataset(Metadata metadata, OutputStream outputStream) throws IOException;
}
......@@ -16,12 +16,18 @@
package org.genesys2.server.service.impl;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
......@@ -33,12 +39,14 @@ import org.genesys2.server.model.genesys.AllAcqBreeding;
import org.genesys2.server.model.genesys.AllAcqCollect;
import org.genesys2.server.model.genesys.AllAcqExchange;
import org.genesys2.server.model.genesys.AllEnvironment;
import org.genesys2.server.model.genesys.ExperimentAccessionTrait;
import org.genesys2.server.model.genesys.ExperimentTrait;
import org.genesys2.server.model.genesys.Metadata;
import org.genesys2.server.model.genesys.Method;
import org.genesys2.server.model.genesys.Parameter;
import org.genesys2.server.model.genesys.SvalbardData;
import org.genesys2.server.model.genesys.Taxonomy;
import org.genesys2.server.model.genesys.TraitCode;
import org.genesys2.server.model.impl.AccessionIdentifier3;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.Crop;
......@@ -70,6 +78,7 @@ import org.genesys2.server.service.TraitService;
import org.genesys2.spring.SecurityContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
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.security.access.prepost.PreAuthorize;
......@@ -77,6 +86,8 @@ import org.springframework.security.acls.domain.BasePermission;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import au.com.bytecode.opencsv.CSVWriter;
@Service
@Transactional(readOnly = true)
public class GenesysServiceImpl implements GenesysService, TraitService, DatasetService {
......@@ -554,4 +565,101 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
// traitValueRepository.updateMetadataMethods(metadata,
// methodValues.keySet());
}
@Override
public void writeDataset(Metadata metadata, OutputStream outputStream) throws IOException {
// Methods: A CSV is generated for each method
List<Method> metadataMethods = listMethods(metadata);
CSVWriter csv = null;
// UTF8 is used for encoding entry names
ZipOutputStream zos = new ZipOutputStream(outputStream);
zos.setComment("Genesys Dataset " + metadata.getUuid());
ZipEntry coreEntry = new ZipEntry("core.csv");
coreEntry.setComment("Accession information");
coreEntry.setTime(System.currentTimeMillis());
zos.putNextEntry(coreEntry);
// Accessions
csv = new CSVWriter(new BufferedWriter(new OutputStreamWriter(zos)), ',', '"', '\\', "\n");
csv.writeNext(new String[] { "coreId", "instCode", "acceNumb", "genus" });
// Write accession information
Page<Accession> datasetAccessions = null;
int page = 0;
do {
datasetAccessions = listMetadataAccessions(metadata.getId(), new PageRequest(page++, 50));
for (Accession accn : datasetAccessions.getContent()) {
csv.writeNext(new String[] { String.valueOf(accn.getId()), accn.getInstituteCode(), accn.getAccessionName(), accn.getGenus() });
}
} while (datasetAccessions.hasNextPage());
csv.flush();
zos.closeEntry();
for (Method method : metadataMethods) {
ZipEntry methodEntry = new ZipEntry(String.format("%1$s.csv", method.getFieldName().toLowerCase()));
methodEntry.setComment(method.getMethod());
methodEntry.setTime(System.currentTimeMillis());
zos.putNextEntry(methodEntry);
List<ExperimentAccessionTrait> vals = traitValueRepository.getValues(metadata, method);
csv = new CSVWriter(new BufferedWriter(new OutputStreamWriter(zos)), ',', '"', '\\', "\n");
csv.writeNext(new String[] { "coreId", "datasetId", "value" });
Map<String, String> codeMap = null;
if (method.isCoded()) {
codeMap = TraitCode.parseCodeMap(method.getOptions());
}
for (ExperimentAccessionTrait et : vals) {
if (et.getValue() == null)
continue;
if (codeMap == null) {
csv.writeNext(new String[] { String.valueOf(et.getAccessionId()), String.valueOf(et.getExperimentId()), et.getValue().toString() });
} else {
csv.writeNext(new String[] { String.valueOf(et.getAccessionId()), String.valueOf(et.getExperimentId()), codeMap.get(et.getValue()) });
}
}
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=\"http://purl.org/germplasm/germplasmType#wiewsInstituteID\"/>\n");
osw.write("\t<field index=\"2\" term=\"http://purl.org/germplasm/germplasmTerm#germplasmIdentifier\"/>\n");
osw.write("\t<field index=\"3\" term=\"http://rs.tdwg.org/dwc/terms/genus\"/>\n");
osw.write("</core>\n");
for (int i = 0; i < metadataMethods.size(); i++) {
Method method = metadataMethods.get(i);
osw.write("<extension encoding=\"UTF-8\" fieldsTerminatedBy=\",\" linesTerminatedBy=\"\\n\" fieldsEnclosedBy=\"&quot;\" ignoreHeaderLines=\"0\">\n");
osw.write("\t<files><location>");
osw.write(method.getFieldName().toLowerCase());
osw.write(".csv</location></files>\n");
osw.write("\t<coreid index=\"0\" />\n");
osw.write("\t<field index=\"1\" term=\"http://rs.tdwg.org/dwc/terms/datasetID\"/>\n");
osw.write("\t<field index=\"2\" term=\"http://www.genesys-pgr.org/descriptors//");
osw.write(String.valueOf(method.getId()));
osw.write("\"/>\n");
osw.write("</extension>\n");
}
osw.write("</archive>\n");
osw.flush();
zos.closeEntry();
zos.finish();
}
}
......@@ -16,8 +16,14 @@
package org.genesys2.server.servlet.controller;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.Metadata;
import org.genesys2.server.service.DatasetService;
import org.genesys2.server.service.GenesysService;
import org.genesys2.spring.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -29,6 +35,7 @@ import org.springframework.ui.ModelMap;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
......@@ -41,10 +48,13 @@ public class DatasetController extends BaseController {
@Autowired
private SelectionBean selectionBean;
@Autowired
private GenesysService genesysService;
@Autowired
private DatasetService datasetService;
@Autowired
private Validator validator;
......@@ -72,8 +82,25 @@ public class DatasetController extends BaseController {
model.addAttribute("accessionMethods", genesysService.getMetadataTraitValues(metadata, accessions.getContent()));
model.addAttribute("selection", selectionBean);
return "/metadata/view";
}
@RequestMapping(value = "/view/{id}/dwca", method = RequestMethod.GET, produces = "application/zip")
public void downloadDwca(@PathVariable(value = "id") long metadataId, HttpServletResponse response) throws IOException {
_logger.debug("Downloading data for " + metadataId);
Metadata metadata = genesysService.getMetadata(metadataId);
if (metadata == null) {
throw new ResourceNotFoundException();
}
response.setContentType("application/zip");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-dataset-%1$s.zip\"", metadata.getUuid()));
// Write Darwin Core Archive to the stream.
OutputStream outputStream = response.getOutputStream();
datasetService.writeDataset(metadata, outputStream);
response.flushBuffer();
}
}
......@@ -250,6 +250,7 @@ search.button.label=Search
admin.page.title=Genesys 2 Administration
metadata.page.title=Datasets
metadata.page.view.title=Dataset details
metadata.download-dwca=Download dataset (Darwin Core Archive)
page.login=Login
traits.page.title=Descriptors
......
......@@ -24,7 +24,7 @@ db.url=jdbc:mysql://127.0.0.1/genesys2?useUnicode=true&characterEncoding=UTF-8&u
db.driverClassName = com.mysql.jdbc.Driver
db.username = root
db.password =
db.showSql=true
db.showSql=false
db.hbm2ddl=do-nothing
c3p0.acquireIncrement=1
......
......@@ -49,6 +49,7 @@
</div>
<div><a href="<c:url value="/data/view/${metadata.id}/dwca" />"><spring:message code="metadata.download-dwca" /></a></div>
<h3>Observations</h3>
</c:if>
......
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