This commit is contained in:
Lucio Lelii 2014-12-04 11:44:44 +00:00
parent dcc51657cb
commit 77c461eeaa
6 changed files with 159 additions and 94 deletions

View File

@ -1,4 +1,7 @@
<ReleaseNotes> <ReleaseNotes>
<Changeset component="org.gcube.data-access.obis-spd-plugin.1-8-1" date="2013-11-28">
<Change> Added field SNAutorship and Dataset to occurrence points</Change>
</Changeset>
<Changeset component="org.gcube.data-access.obis-spd-plugin.1-7-0" date="2013-07-18"> <Changeset component="org.gcube.data-access.obis-spd-plugin.1-7-0" date="2013-07-18">
<Change> Sync with spd-model-library changes</Change> <Change> Sync with spd-model-library changes</Change>
</Changeset> </Changeset>

View File

@ -11,7 +11,7 @@
<groupId>org.gcube.data.spd</groupId> <groupId>org.gcube.data.spd</groupId>
<artifactId>obis-spd-plugin</artifactId> <artifactId>obis-spd-plugin</artifactId>
<version>1.8.0-SNAPSHOT</version> <version>1.8.1-SNAPSHOT</version>
<name>ObisPlugin</name> <name>ObisPlugin</name>
<description>obis plugin for species manager service</description> <description>obis plugin for species manager service</description>

View File

@ -79,17 +79,40 @@ public class ObisClient {
String credits = generateCredits(); String credits = generateCredits();
//String citation = generateCitation(); //String citation = generateCitation();
DataSet dataset = getDataSetById(session, productKey.getDataSetId());
boolean continueWrite = true; boolean continueWrite = true;
while(rs.next() && continueWrite) { while(rs.next() && continueWrite) {
OccurrencePoint occurrence = generateOccurrencePoint(rs, credits, dataset);
OccurrencePoint occurrence = generateOccurrencePoint(rs, credits);
continueWrite = writer.write(occurrence); continueWrite = writer.write(occurrence);
}; };
rs.close(); rs.close();
} }
protected static OccurrencePoint generateOccurrencePoint(ResultSet rs, String credits) throws SQLException private static DataSet getDataSetById(PluginSession session, int datasetId){
logger.debug("retrieving dataset for id "+datasetId);
DataSet dataset = null;
ResultSet datasetRs = null;
try{
PreparedStatement datasetStatement = session.getDatasetPerDatasetIdPreparedStatement(datasetId);
datasetRs = datasetStatement.executeQuery();
if (!datasetRs.next()) return null;
dataset = retrieveDatasetInformation(datasetRs);
}catch(Exception e){
logger.warn("cannot retrieve dataset for occurrence point with id "+datasetId,e);
}finally{
try{
if (datasetRs!=null)
datasetRs.close();
}catch(Exception e) {}
}
logger.debug("dataset returned "+dataset);
return dataset;
}
protected static OccurrencePoint generateOccurrencePoint(ResultSet rs, String credits, DataSet dataSet) throws SQLException
{ {
int id = rs.getInt("id"); int id = rs.getInt("id");
OccurrencePoint occurrence = new OccurrencePoint(String.valueOf(id)); OccurrencePoint occurrence = new OccurrencePoint(String.valueOf(id));
@ -171,7 +194,7 @@ Species: C. carcharias
if (scientificNameStartIndex>0) { if (scientificNameStartIndex>0) {
String author = (authorStartIndex+1<concatenated.length())?concatenated.substring(authorStartIndex+1):""; String author = (authorStartIndex+1<concatenated.length())?concatenated.substring(authorStartIndex+1):"";
String sn = (scientificNameStartIndex+1<snPart.length())?snPart.substring(scientificNameStartIndex+1):""; String sn = (scientificNameStartIndex+1<snPart.length())?snPart.substring(scientificNameStartIndex+1):"";
occurrence.setScientificName(sn+" ("+author+")"); occurrence.setScientificName(sn/*+" ("+author+")"*/);
} }
} }
/*String[] taxon = concatenated.split("|"); /*String[] taxon = concatenated.split("|");
@ -183,8 +206,13 @@ Species: C. carcharias
}*/ }*/
} }
occurrence.setScientificNameAuthorship(rs.getString("identifiedBy")); occurrence.setScientificNameAuthorship(rs.getString("snAuthor"));
occurrence.setIdentifiedBy(rs.getString("identifiedBy"));
occurrence.setDataSet(dataSet);
//retrieving dataset
return occurrence; return occurrence;
} }
@ -195,7 +223,10 @@ Species: C. carcharias
OccurrencePoint occurrence = null; OccurrencePoint occurrence = null;
String credits = generateCredits(); String credits = generateCredits();
if(rs.next()) occurrence = generateOccurrencePoint(rs, credits); if(rs.next()){
int objId = rs.getInt("valid_id");
occurrence = generateOccurrencePoint(rs, credits, getDataSetById(session, objId));
}
rs.close(); rs.close();
return occurrence; return occurrence;
@ -237,10 +268,13 @@ Species: C. carcharias
//OCCURRENCES //OCCURRENCES
Product occurences = new Product(ProductType.Occurrence, key); Product occurences = new Product(ProductType.Occurrence, key);
int occurencesCount = getOccurrencesCount(session, speciesId, datasetId, filters); int occurencesCount = getOccurrencesCount(session, speciesId, datasetId, filters);
occurences.setCount(occurencesCount); occurences.setCount(occurencesCount);
products.add(occurences); products.add(occurences);
logger.trace("product is "+occurences);
item.setProducts(products); item.setProducts(products);
} }
@ -271,6 +305,7 @@ Species: C. carcharias
fillCommonNames(session, id, baseItem); fillCommonNames(session, id, baseItem);
PreparedStatement datasetStatement = session.getDatasetPreparedStatement(id); PreparedStatement datasetStatement = session.getDatasetPreparedStatement(id);
ResultSet dataSetrs = datasetStatement.executeQuery(); ResultSet dataSetrs = datasetStatement.executeQuery();
@ -278,10 +313,16 @@ Species: C. carcharias
ResultItem item = Util.cloneResultItem(baseItem); ResultItem item = Util.cloneResultItem(baseItem);
int dataSetId = fillDatasetInformation(dataSetrs, item); DataSet dataset = retrieveDatasetInformation(dataSetrs);
item.setDataSet(dataset);
int dataSetId = Integer.parseInt(dataset.getId());
ProductKey key = new ProductKey(id, dataSetId, filters); ProductKey key = new ProductKey(id, dataSetId, filters);
logger.trace("datasetid is "+dataSetId+" and product key created is "+key);
fillProducts(session, id, dataSetId, key.serialize(), filters, item); fillProducts(session, id, dataSetId, key.serialize(), filters, item);
continueWrite = writer.write(item); continueWrite = writer.write(item);
@ -400,8 +441,7 @@ Species: C. carcharias
item.setCommonNames(commonNames); item.setCommonNames(commonNames);
} }
protected static int fillDatasetInformation(ResultSet rs, ResultItem item) throws SQLException private static DataSet retrieveDatasetInformation(ResultSet rs) throws SQLException{
{
int dataSetId = rs.getInt("datasetId"); int dataSetId = rs.getInt("datasetId");
DataSet dataSet = new DataSet(String.valueOf(dataSetId)); DataSet dataSet = new DataSet(String.valueOf(dataSetId));
dataSet.setCitation(rs.getString("datasetCitation")); dataSet.setCitation(rs.getString("datasetCitation"));
@ -410,10 +450,7 @@ Species: C. carcharias
DataProvider dataProvider = new DataProvider(String.valueOf(rs.getInt("providerId"))); DataProvider dataProvider = new DataProvider(String.valueOf(rs.getInt("providerId")));
dataProvider.setName(rs.getString("providerName")); dataProvider.setName(rs.getString("providerName"));
dataSet.setDataProvider(dataProvider); dataSet.setDataProvider(dataProvider);
return dataSet;
item.setDataSet(dataSet);
return dataSetId;
} }
public static Set<String> getCommonNames(PluginSession session, String scientificName) throws SQLException public static Set<String> getCommonNames(PluginSession session, String scientificName) throws SQLException

View File

@ -37,6 +37,7 @@ public class PluginSession {
protected PreparedStatement commonNamePreparedStatement; protected PreparedStatement commonNamePreparedStatement;
protected PreparedStatement childrenTaxonPreparedStatement; protected PreparedStatement childrenTaxonPreparedStatement;
protected PreparedStatement occurrenceByIdPreparedStatement; protected PreparedStatement occurrenceByIdPreparedStatement;
protected PreparedStatement datasetPerIdPreparedStatement;
/** /**
* @param connection * @param connection
@ -100,6 +101,7 @@ public class PluginSession {
createCommonNamePreparedStatement(); createCommonNamePreparedStatement();
createChildrenTaxonPreparedStatement(); createChildrenTaxonPreparedStatement();
createOccurrenceByIdPreparedStatement(); createOccurrenceByIdPreparedStatement();
createDatasetPerDatasetIdPreparedStatement();
} }
@ -141,16 +143,12 @@ public class PluginSession {
return datasetPreparedStatement; return datasetPreparedStatement;
} }
protected void createDatasetPreparedStatement() throws SQLException public PreparedStatement getDatasetPerDatasetIdPreparedStatement(int dataSetId) throws SQLException
{ {
/*String query = "SELECT r.id as datasetId, r.resname as datasetName, r.citation as datasetCitation, p.id as providerId, p.providername as providerName " + if (datasetPerIdPreparedStatement==null) createDatasetPerDatasetIdPreparedStatement();
"FROM obis.resources r, obis.providers p WHERE " + datasetPerIdPreparedStatement.clearParameters();
"exists (SELECT r.id FROM obis.drs d WHERE d.valid_id = ? AND d.resource_id = r.id) AND r.provider_id = p.id";*/ datasetPerIdPreparedStatement.setInt(1, dataSetId);
return datasetPerIdPreparedStatement;
String query = "SELECT r.id as datasetId, r.resname as datasetName, r.citation as datasetCitation, p.id as providerId, p.providername as providerName " +
"FROM obis.resources r, obis.providers p WHERE " +
"r.id in (SELECT resource_id from portal.species_per_resource where valid_id = ?) AND r.provider_id = p.id";
datasetPreparedStatement = connection.prepareStatement(query);
} }
public PreparedStatement getCommonNameFromScientificNamePreparedStatement(String scientificaName) throws SQLException public PreparedStatement getCommonNameFromScientificNamePreparedStatement(String scientificaName) throws SQLException
@ -314,7 +312,7 @@ public class PluginSession {
protected PreparedStatement getOccurrencesPreparedStatement(int taxonId, int datasetId, SearchFilters filters) throws SQLException protected PreparedStatement getOccurrencesPreparedStatement(int taxonId, int datasetId, SearchFilters filters) throws SQLException
{ {
//We don't cache it because in this case a PS is less performant //We don't cache it because in this case a PS is less performant
StringBuilder query = new StringBuilder("SELECT drs.id, drs.latitude, drs.longitude, drs.datecollected, drs.basisofrecord, dxs.citation, dxs.institutioncode, dxs.collectioncode, dxs.catalognumber, dxs.collector, dxs.datelastmodified, dxs.country, dxs.locality, dxs.minimumdepth, dxs.maximumdepth, dxs.coordinateprecision, dxs.concatenated, dxs.identifiedBy, dxs.yearcollected, dxs.monthcollected, dxs.daycollected FROM obis.drs drs, obis.dxs dxs WHERE drs.valid_id = ? AND drs.resource_id = ? AND drs.id = dxs.dr_id"); StringBuilder query = new StringBuilder("SELECT drs.id, drs.valid_id, drs.latitude, drs.longitude, drs.datecollected, drs.basisofrecord, dxs.citation, dxs.institutioncode, dxs.collectioncode, dxs.catalognumber, dxs.collector, dxs.datelastmodified, dxs.country, dxs.locality, dxs.minimumdepth, dxs.maximumdepth, dxs.coordinateprecision, dxs.concatenated, dxs.identifiedBy, dxs.yearcollected, dxs.monthcollected, dxs.daycollected, tn.tauthor as snAuthor FROM obis.drs drs, obis.dxs dxs, obis.tnames tn WHERE drs.valid_id = ? AND drs.resource_id = ? AND drs.id = dxs.dr_id AND tn.id = drs.valid_id ");
buildConditions(query, "drs.", filters.getConditions()); buildConditions(query, "drs.", filters.getConditions());
@ -400,9 +398,32 @@ public class PluginSession {
protected void createOccurrenceByIdPreparedStatement() throws SQLException protected void createOccurrenceByIdPreparedStatement() throws SQLException
{ {
String query = "SELECT drs.id, drs.latitude, drs.longitude, drs.datecollected, drs.basisofrecord, dxs.citation, dxs.institutioncode, dxs.collectioncode, dxs.catalognumber, dxs.collector, dxs.datelastmodified, dxs.country, dxs.locality, dxs.minimumdepth, dxs.maximumdepth, dxs.coordinateprecision, dxs.concatenated, dxs.yearcollected, dxs.monthcollected, dxs.daycollected FROM obis.drs drs, obis.dxs dxs WHERE drs.id = ? AND drs.id = dxs.dr_id"; String query = "SELECT drs.id, drs.latitude, drs.longitude, drs.datecollected, drs.basisofrecord, dxs.citation, dxs.institutioncode, dxs.collectioncode, dxs.catalognumber, dxs.collector, dxs.datelastmodified, dxs.country, dxs.locality, dxs.minimumdepth, dxs.maximumdepth, dxs.coordinateprecision, dxs.concatenated, dxs.yearcollected, dxs.monthcollected, dxs.daycollected, tn.tauthor as snAuthor FROM obis.drs drs, obis.dxs dxs, obis.tnames as tn WHERE drs.id = ? AND drs.id = dxs.dr_id AND drs.valid_id = tn.id";
occurrenceByIdPreparedStatement = connection.prepareStatement(query); occurrenceByIdPreparedStatement = connection.prepareStatement(query);
} }
protected void createDatasetPreparedStatement() throws SQLException
{
/*String query = "SELECT r.id as datasetId, r.resname as datasetName, r.citation as datasetCitation, p.id as providerId, p.providername as providerName " +
"FROM obis.resources r, obis.providers p WHERE " +
"exists (SELECT r.id FROM obis.drs d WHERE d.valid_id = ? AND d.resource_id = r.id) AND r.provider_id = p.id";*/
String query = "SELECT r.id as datasetId, r.resname as datasetName, r.citation as datasetCitation, p.id as providerId, p.providername as providerName " +
"FROM obis.resources r, obis.providers p WHERE " +
"r.id in (SELECT resource_id from portal.species_per_resource where valid_id = ?) AND r.provider_id = p.id";
datasetPreparedStatement = connection.prepareStatement(query);
}
protected void createDatasetPerDatasetIdPreparedStatement() throws SQLException
{
/*String query = "SELECT r.id as datasetId, r.resname as datasetName, r.citation as datasetCitation, p.id as providerId, p.providername as providerName " +
"FROM obis.resources r, obis.providers p WHERE " +
"exists (SELECT r.id FROM obis.drs d WHERE d.valid_id = ? AND d.resource_id = r.id) AND r.provider_id = p.id";*/
String query = "SELECT r.id as datasetId, r.resname as datasetName, r.citation as datasetCitation, p.id as providerId, p.providername as providerName " +
"FROM obis.resources r, obis.providers p WHERE " +
"r.id = ? AND r.provider_id = p.id";
datasetPerIdPreparedStatement = connection.prepareStatement(query);
}
} }

View File

@ -29,6 +29,8 @@ public class ProductKey {
this.filters = filters; this.filters = filters;
} }
protected ProductKey() {}
/** /**
* @return the taxonId * @return the taxonId
*/ */

View File

@ -17,6 +17,8 @@ public class SearchFilters {
protected List<Condition> conditions; protected List<Condition> conditions;
protected SearchFilters(){}
public SearchFilters(Condition ... conditions){ public SearchFilters(Condition ... conditions){
this.conditions = new ArrayList<Condition>(Arrays.asList(conditions)); this.conditions = new ArrayList<Condition>(Arrays.asList(conditions));
} }