Commit 021db830 authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Maxym Borodenko

Export all passport data in MS Access format

WIP: Access dump

fixes for review
parent beafa94b
......@@ -439,6 +439,11 @@
<version>${hazelcast.version}</version>
</dependency>
<dependency>
<groupId>com.healthmarketscience.jackcess</groupId>
<artifactId>jackcess</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
......
......@@ -258,6 +258,27 @@ public class GenesysLowlevelRepositoryCustomImpl implements GenesysLowlevelRepos
}, rowCallbackHandler);
}
@Override
public ResultSet resultSetOfAccessions(final AppliedFilters filter) throws SQLException {
final DirectMysqlQuery directQuery = new DirectMysqlQuery("accession", "a");
directQuery.innerJoin("acce", "acce", "acce.id=a.id");
directQuery.outerJoin("taxonomy2", "t", "t.id=a.taxonomyId2");
directQuery.jsonFilter(filter, new MethodResolver() {
@Override
public Method getMethod(final long methodId) {
return GenesysLowlevelRepositoryCustomImpl.this.methodRepository.findOne(methodId);
}
});
final Connection con = this.jdbcTemplate.getDataSource().getConnection();
final PreparedStatement stmt = con.prepareStatement(directQuery
.getQuery("a.id, acce.uuid, a.instCode, a.acceNumb, t.genus, t.species, t.taxonName, a.orgCty, a.acqSrc, a.acqDate, a.mlsStat, a.available, a.historic, a.storage, a.sampStat, a.duplSite, acce.createdBy, acce.createdDate, acce.lastModifiedBy, acce.lastModifiedDate, a.doi"));
final ArgumentPreparedStatementSetter apss = new ArgumentPreparedStatementSetter(directQuery.getParameters());
apss.setValues(stmt);
final ResultSet rs = stmt.executeQuery();
return rs;
}
@Override
public void listAccessionIds(final AppliedFilters filter, final Sort sort, final RowCallbackHandler rowCallbackHandler) {
final DirectMysqlQuery directQuery = new DirectMysqlQuery("accession", "a");
......@@ -312,6 +333,25 @@ public class GenesysLowlevelRepositoryCustomImpl implements GenesysLowlevelRepos
}, rowCallbackHandler);
}
@Override
public ResultSet resultSetOfAccessionsGeo(final AppliedFilters filter) throws SQLException {
final DirectMysqlQuery directQuery = new DirectMysqlQuery("accessiongeo", "geo");
directQuery.innerJoin("accession", "a", "a.id=geo.accessionId");
directQuery.jsonFilter(filter, new MethodResolver() {
@Override
public Method getMethod(final long methodId) {
return GenesysLowlevelRepositoryCustomImpl.this.methodRepository.findOne(methodId);
}
});
final Connection con = this.jdbcTemplate.getDataSource().getConnection();
final PreparedStatement stmt = con.prepareStatement(directQuery.getQuery("a.id, geo.latitude, geo.longitude, geo.elevation, geo.datum, geo.uncertainty, geo.method, geo.version"));
final ArgumentPreparedStatementSetter apss = new ArgumentPreparedStatementSetter(directQuery.getParameters());
apss.setValues(stmt);
final ResultSet rs = stmt.executeQuery();
return rs;
}
@Override
public void listAccessionsAlias(final AppliedFilters filter, final RowCallbackHandler rowCallbackHandler) {
// from n inner join accession a on a.id=n.accessionId
......@@ -339,6 +379,25 @@ public class GenesysLowlevelRepositoryCustomImpl implements GenesysLowlevelRepos
}, rowCallbackHandler);
}
@Override
public ResultSet resultSetOfAccessionsAlias(final AppliedFilters filter) throws SQLException {
final DirectMysqlQuery directQuery = new DirectMysqlQuery("accessionalias", "n");
directQuery.innerJoin("accession", "a", "a.id=n.accessionId");
directQuery.jsonFilter(filter, new MethodResolver() {
@Override
public Method getMethod(final long methodId) {
return GenesysLowlevelRepositoryCustomImpl.this.methodRepository.findOne(methodId);
}
});
final Connection con = this.jdbcTemplate.getDataSource().getConnection();
final PreparedStatement stmt = con.prepareStatement(directQuery.getQuery("a.id, n.instCode, n.name, n.aliasType, n.lang, n.version"));
final ArgumentPreparedStatementSetter apss = new ArgumentPreparedStatementSetter(directQuery.getParameters());
apss.setValues(stmt);
final ResultSet rs = stmt.executeQuery();
return rs;
}
@Override
public void listAccessionsAlias(final RowCallbackHandler rowCallbackHandler) {
this.jdbcTemplate.query(new PreparedStatementCreator() {
......@@ -382,6 +441,26 @@ public class GenesysLowlevelRepositoryCustomImpl implements GenesysLowlevelRepos
}, rowCallbackHandler);
}
@Override
public ResultSet resultSetOfAccessionsColl(final AppliedFilters filter) throws SQLException {
final DirectMysqlQuery directQuery = new DirectMysqlQuery("accessioncollect", "coll");
directQuery.innerJoin("accession", "a", "a.id=coll.accessionId");
directQuery.jsonFilter(filter, new MethodResolver() {
@Override
public Method getMethod(final long methodId) {
return GenesysLowlevelRepositoryCustomImpl.this.methodRepository.findOne(methodId);
}
});
final Connection con = this.jdbcTemplate.getDataSource().getConnection();
final PreparedStatement stmt = con.prepareStatement(directQuery
.getQuery("a.id, coll.collMissId, coll.collNumb, coll.collDate, coll.collSrc, coll.collSite, coll.collCode, coll.collName, coll.collInstAddress, coll.version"));
final ArgumentPreparedStatementSetter apss = new ArgumentPreparedStatementSetter(directQuery.getParameters());
apss.setValues(stmt);
final ResultSet rs = stmt.executeQuery();
return rs;
}
@Override
public void listMetadataAccessions(final long id, final RowCallbackHandler rowCallbackHandler) {
this.jdbcTemplate.query(new PreparedStatementCreator() {
......
/**
* Copyright 2017 Global Crop Diversity Trust
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.service.worker;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.DatabaseBuilder;
import com.healthmarketscience.jackcess.util.ImportUtil;
import org.apache.commons.lang3.time.StopWatch;
import org.genesys2.server.persistence.domain.GenesysLowlevelRepository;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
/**
* @author Maxym Borodenko
*/
@Component
public class ExportPassportData {
public static final Logger LOG = LoggerFactory.getLogger(ExportPassportData.class);
@Autowired
@Qualifier("genesysLowlevelRepositoryCustomImpl")
private GenesysLowlevelRepository genesysLowlevelRepository;
public void exportToAccess(final AppliedFilters filters, File targetFile) throws IOException, SQLException {
StopWatch stopWatch = new StopWatch();
try (Database db = DatabaseBuilder.create(Database.FileFormat.V2010, targetFile)) {
stopWatch.start();
writeAccessionsCore(filters, db);
stopWatch.split();
LOG.info("Writing core took {}", stopWatch.getSplitTime());
writeAccessionsGeo(filters, db);
stopWatch.split();
LOG.info("Writing geo took {}", stopWatch.getSplitTime());
writeAccessionsColl(filters, db);
stopWatch.split();
LOG.info("Writing coll took {}", stopWatch.getSplitTime());
writeAccessionsNames(filters, db);
stopWatch.split();
LOG.info("Writing names took {}", stopWatch.getSplitTime());
// TODO Add foreign keys
// TODO Add MCPD view
}
}
private void writeAccessionsCore(final AppliedFilters filters, Database db) throws IOException, SQLException {
LOG.info("Exporting resultset to core table ...");
try {
new ImportUtil.Builder(db, "core").importResultSet(genesysLowlevelRepository.resultSetOfAccessions(filters));
} catch (final IOException | SQLException e) {
LOG.error("Error while exporting resultset to table core: {}", e.getMessage());
throw e;
}
db.flush();
}
private void writeAccessionsGeo(final AppliedFilters filters, Database db) throws IOException, SQLException {
LOG.info("Exporting resultset to geo table ...");
try {
new ImportUtil.Builder(db, "geo").importResultSet(genesysLowlevelRepository.resultSetOfAccessionsGeo(filters));
} catch (IOException | SQLException e) {
LOG.error("Error while exporting resultset to table geo: {}", e.getMessage());
throw e;
}
db.flush();
}
private void writeAccessionsColl(final AppliedFilters filters, Database db) throws IOException, SQLException {
LOG.info("Exporting resultset to coll table ...");
try {
new ImportUtil.Builder(db, "coll").importResultSet(genesysLowlevelRepository.resultSetOfAccessionsColl(filters));
} catch (final IOException | SQLException e) {
LOG.error("Error while exporting resultset to table coll: {}", e.getMessage());
throw e;
}
db.flush();
}
private void writeAccessionsNames(final AppliedFilters filters, Database db) throws IOException, SQLException {
LOG.info("Exporting resultset to names table ...");
try {
new ImportUtil.Builder(db, "names").importResultSet(genesysLowlevelRepository.resultSetOfAccessionsAlias(filters));
} catch (final IOException | SQLException e) {
LOG.error("Error while exporting resultset to table names: {}", e.getMessage());
throw e;
}
db.flush();
}
}
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