Valentina Marioli 7 years ago
commit 1f825b9aec

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="con" path="DERBY_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>brazilian-flora-spd-plugin</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.apache.derby.ui.derbyEngine</nature>
</natures>
</projectDescription>

@ -0,0 +1,4 @@
eclipse.preferences.version=1
filesCopiedToWebInfLib=
googleCloudSqlEnabled=false
localDevMySqlEnabled=true

@ -0,0 +1,6 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8

@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.7

@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

@ -0,0 +1,6 @@
gCube System - License
------------------------------------------------------------
The gCube/gCore software is licensed as Free Open Source software conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
The software and documentation is provided by its authors/distributors "as is" and no expressed or
implied warranty is given for its use, quality or fitness for a particular case.

@ -0,0 +1,61 @@
The gCube System - ${name}
--------------------------------------------------
${description}
${gcube.description}
${gcube.funding}
Version
--------------------------------------------------
${version} (${buildDate})
Please see the file named "changelog.xml" in this directory for the release notes.
Authors
--------------------------------------------------
* Valentina Marioli (valentina.marioli-AT-isti.cnr.it), CNR, Italy
MAINTAINERS
--------------------------------------------------
* Valentina Marioli (valentina.marioli-AT-isti.cnr.it), CNR, Italy
Download information
--------------------------------------------------
Source code is available from SVN:
${scm.url}
Binaries can be downloaded from the gCube website:
${gcube.website}
Installation
--------------------------------------------------
Installation documentation is available on-line in the gCube Wiki:
${gcube.wikiRoot}
Documentation
--------------------------------------------------
Documentation is available on-line in the gCube Wiki:
${gcube.wikiRoot}
Support
--------------------------------------------------
Bugs and support requests can be reported in the gCube issue tracking tool:
${gcube.issueTracking}
Licensing
--------------------------------------------------
This software is licensed under the terms you may find in the file named "LICENSE" in this directory.

@ -0,0 +1,40 @@
<ReleaseNotes>
<Changeset component="org.gcube.data.spd.brazilian-flora-spd-plugin.1-0-0"
date="2012-05-04">
<Change>First release</Change>
</Changeset>
<Changeset component="org.gcube.data.spd.brazilian-flora-spd-plugin.1-1-0"
date="2012-09-14">
<Change>Bugs fixed</Change>
</Changeset>
<Changeset component="org.gcube.data.spd.brazilian-flora-spd-plugin.1-2-0"
date="2012-10-18">
<Change>Added plugin info</Change>
</Changeset>
<Changeset component="org.gcube.data.spd.brazilian-flora-spd-plugin.1-3-0"
date="2012-11-30">
<Change>Added db connection pool</Change>
</Changeset>
<Changeset component="org.gcube.data.spd.brazilian-flora-spd-plugin.1-4-0"
date="2013-01-17">
<Change>updated to spd-plugin-framework 2.0</Change>
</Changeset>
<Changeset component="org.gcube.data.spd.brazilian-flora-spd-plugin.1-5-0"
date="2013-04-30">
<Change>added ExpansionCapability</Change>
</Changeset>
<Changeset component="org.gcube.data.spd.brazilian-flora-spd-plugin.1-6-0"
date="2013-07-19">
<Change>changed SynonymsCapability method to getExpansionInterface
method</Change>
</Changeset>
<Changeset component="org.gcube.data.spd.brazilian-flora-spd-plugin.1-7-0"
date="2014-04-28">
<Change>Added citation</Change>
</Changeset>
<Changeset component="org.gcube.data.spd.brazilian-flora-spd-plugin.1-8-0"
date="2017-02-08">
<Change>Gcore removed</Change>
</Changeset>
</ReleaseNotes>

@ -0,0 +1,31 @@
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>servicearchive</id>
<formats>
<format>tar.gz</format>
</formats>
<baseDirectory>/</baseDirectory>
<fileSets>
<fileSet>
<directory>${distroDirectory}</directory>
<outputDirectory>/</outputDirectory>
<useDefaultExcludes>true</useDefaultExcludes>
<includes>
<include>README</include>
<include>LICENSE</include>
<include>profile.xml</include>
<include>changelog.xml</include>
</includes>
<fileMode>755</fileMode>
<filtered>true</filtered>
</fileSet>
</fileSets>
<files>
<file>
<source>target/${build.finalName}.jar</source>
<outputDirectory>/${artifactId}</outputDirectory>
</file>
</files>
</assembly>

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<Resource>
<ID></ID>
<Type>Service</Type>
<Profile>
<Description>${description}</Description>
<Class>DataAccess</Class>
<Name>${artifactId}</Name>
<Version>1.0.0</Version>
<Packages>
<Software>
<Description>${description}</Description>
<Name>${artifactId}</Name>
<Version>${version}</Version>
<MavenCoordinates>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
</MavenCoordinates>
<Type>library</Type>
<Files>
<File>${build.finalName}.jar</File>
</Files>
</Software>
</Packages>
</Profile>
</Resource>

@ -0,0 +1,130 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>maven-parent</artifactId>
<groupId>org.gcube.tools</groupId>
<version>1.0.0</version>
<relativePath />
</parent>
<groupId>org.gcube.data.spd</groupId>
<artifactId>brazilian-flora-spd-plugin</artifactId>
<version>1.8.0-SNAPSHOT</version>
<name>brazilian-flora-spd-plugin</name>
<description>Brazilian Flora Plugin</description>
<scm>
<connection>scm:svn:http://svn.d4science.research-infrastructures.eu/gcube/trunk/data-access/${project.artifactId}</connection>
<developerConnection>scm:svn:https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-access/${project.artifactId}</developerConnection>
<url>http://svn.d4science.research-infrastructures.eu/gcube/trunk/data-access/${project.artifactId}</url>
</scm>
<properties>
<distroDirectory>distro</distroDirectory>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-bom</artifactId>
<version>LATEST</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.gcube.resources</groupId>
<artifactId>common-gcore-resources</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.resources.discovery</groupId>
<artifactId>ic-client</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.data.spd</groupId>
<artifactId>spd-model</artifactId>
<version>[3.0.0-SNAPSHOT, 4.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.gcube.data.spd</groupId>
<artifactId>spd-plugin-framework</artifactId>
<version>[3.0.0-SNAPSHOT, 4.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>8.4-702.jdbc4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-encryption</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-profile</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target</outputDirectory>
<resources>
<resource>
<directory>${distroDirectory}</directory>
<filtering>true</filtering>
<includes>
<include>profile.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2</version>
<configuration>
<descriptors>
<descriptor>${distroDirectory}/descriptor.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>servicearchive</id>
<phase>install</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,287 @@
package org.gcube.data.spd.flora;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
import org.gcube.common.encryption.StringEncrypter;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.data.spd.flora.capabilities.ClassificationCapabilityImpl;
import org.gcube.data.spd.flora.capabilities.ExpansionCapabilityImpl;
import org.gcube.data.spd.flora.dbconnection.ConnectionPool;
import org.gcube.data.spd.flora.dbconnection.ConnectionPoolException;
import org.gcube.data.spd.model.Condition;
import org.gcube.data.spd.model.RepositoryInfo;
import org.gcube.data.spd.model.products.DataProvider;
import org.gcube.data.spd.model.products.DataSet;
import org.gcube.data.spd.model.products.ResultItem;
import org.gcube.data.spd.model.products.Taxon;
import org.gcube.data.spd.model.util.Capabilities;
import org.gcube.data.spd.plugin.fwk.AbstractPlugin;
import org.gcube.data.spd.plugin.fwk.capabilities.ClassificationCapability;
import org.gcube.data.spd.plugin.fwk.capabilities.ExpansionCapability;
import org.gcube.data.spd.plugin.fwk.writers.ObjectWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FloraPlugin extends AbstractPlugin{
public static String jdbc;
public static String username ;
public static String password;
public static String baseurl;
public static final String credits = "This information object has been generated via the Species Product Discovery service on XDATEX by interfacing with Lista de Espécies da Flora do Brasil (http://floradobrasil.jbrj.gov.br/)";
public static final String citation = "Accessed through: Lista de Espécies da Flora do Brasil - Jardim Botânico do Rio de Janeiro at http://floradobrasil.jbrj.gov.br/2012/ on ";
public static final String dumpDb = "/createFloraDb.sql";
public static final String tableName = "flora";
public static final String driver = "org.postgresql.Driver";
static Logger logger = LoggerFactory.getLogger(FloraPlugin.class);
@Override
public void initialize(ServiceEndpoint res) throws Exception {
for (AccessPoint ap:res.profile().accessPoints()) {
if (ap.name().equals("rest")) {
baseurl = ap.address();
}
else if (ap.name().equals("jdbc")) {
jdbc = ap.address();
username = ap.username();
password = StringEncrypter.getEncrypter().decrypt(ap.password());
}
}
// if ((!Utils.SQLTableExists(FloraPlugin.tableName) || (!Utils.SQLTableExists("updates")))){
// System.out.println("Create tables");
// if (Utils.createDB())
// logger.trace("Completed.");
// }
//
// long update = Utils.lastupdate();
// new UpdateThread(update);
}
@Override
public void update(ServiceEndpoint res) throws Exception {
for (AccessPoint ap:res.profile().accessPoints()) {
if (ap.name().equals("rest")) {
baseurl = ap.address();
}
else if (ap.name().equals("jdbc")) {
jdbc = ap.address();
username = ap.username();
password = ap.password();
}
}
super.update(res);
}
@Override
public String getDescription() {
return ("Brazilian Flora Plugin");
}
@Override
public String getRepositoryName() {
return ("BrazilianFlora");
}
@Override
public void shutdown() throws Exception {
// Discover.shutdown();
}
@SuppressWarnings("serial")
@Override
public Set<Capabilities> getSupportedCapabilities() {
return new HashSet<Capabilities>(){{add(Capabilities.Classification);add(Capabilities.Expansion);}};
}
@Override
public ClassificationCapability getClassificationInterface() {
return new ClassificationCapabilityImpl();
}
@Override
public ExpansionCapability getExpansionInterface() {
return new ExpansionCapabilityImpl();
}
@Override
public void searchByScientificName(String word,
ObjectWriter<ResultItem> writer, Condition... properties) {
ResultSet results = null;
try{
results = createRSNames(word);
if (results!=null){
while(results.next()) {
String id = results.getString(1);
String scientific_name = results.getString(2);
String rank = results.getString(3);
String idParent = results.getString(4);
String author = results.getString(5);
ResultItem rs = null;
try{
rs = createResultItem(id, scientific_name, rank, idParent, author);
}catch (Exception e) {
logger.error("Error retrieving information ", e);
}
if (writer.isAlive()){
if (rs!=null)
writer.write(rs);
}
else
break;
}
}
} catch (SQLException e) {
logger.error("sql Error", e);
}finally
{
try {
if (results != null)
results.close();
} catch (SQLException ex) {
logger.error("sql Error", ex);
}
}
}
/**
* get ResultSet by scientificName
*/
private ResultSet createRSNames(String scientificName) {
ResultSet results = null;
ConnectionPool pool = null;
try {
pool = ConnectionPool.getConnectionPool();
String term = "%" + scientificName + "%";
String query = "select id, scientific_name, rank, id_parent, citation from "+ tableName + " where UPPER(scientific_name) like UPPER(?)";
results = pool.selectPrestatement(query, term);
} catch (ConnectionPoolException e) {
logger.error("ConnectionPoolException",e);
}
return results;
}
private ResultItem createResultItem(String id, String ScName, String rank, String idParent, String author) {
ResultItem item = new ResultItem(id, ScName);
item.setCitation(Utils.createCitation());
item.setCredits(Utils.createCredits());
item.setRank(rank);
item.setScientificNameAuthorship(author);
// rs.setProvider("Brazilian Flora");
DataSet dataSet = new DataSet("floraid");
dataSet.setName("Brazilian Flora");
DataProvider dp = new DataProvider("floraid");
dp.setName("Brazilian Flora");
dataSet.setDataProvider(dp);
item.setDataSet(dataSet);
if ((rank != null) && (idParent!= null)) {
item.setParent(findTaxonById(idParent));
// if (parentRS!=null){
// Taxon parent = createTaxonByRSItem(parentRS);
// item.setParent(parent);
// }
}
return item;
}
// public Taxon createTaxonByRSItem(RSItem rs){
// Taxon parent = null;
// RSItem parentRs = null;
// Taxon t = new Taxon(rs.getId());
// t.setRank(rs.getRank());
// t.setScientificName(rs.getScientific_name());
// t.setAuthor(rs.getAuthor());
// if (rs.getParentId() != null) {
// parentRs = findTaxonById(rs.getParentId());
// parent = createTaxonByRSItem(parentRs);
// }
//
// t.setParent(parent);
// return t;
// }
/**
* Find taxonomy
*/
public Taxon findTaxonById(String id) {
ResultSet results = null;
Taxon item = null;
ConnectionPool pool = null;
try {
pool = ConnectionPool.getConnectionPool();
String query = "select id_parent, rank, scientific_name, citation from "+ FloraPlugin.tableName + " where id = ?";
results = pool.selectPrestatement(query, id);
if(results.next()) {
item = new Taxon(id);
if (results.getString(1) != null)
item.setParent(findTaxonById(results.getString(1)));
item.setRank(results.getString(2));
item.setScientificName(results.getString(3));
item.setScientificNameAuthorship(results.getString(4));
}
}
catch (SQLException sqlExcept) {
logger.error("sql Error",sqlExcept);
} catch (ConnectionPoolException e) {
logger.error("ConnectionPoolException",e);
} finally{
try {
results.close();
} catch (SQLException e) {
logger.error("sql Error",e);
}
}
// logger.trace("returning taxon");
return item;
}
@Override
public RepositoryInfo getRepositoryInfo() {
RepositoryInfo info = new RepositoryInfo(
"http://floradobrasil.jbrj.gov.br/2012/imgs/logo_fdb_2012.gif",
"http://floradobrasil.jbrj.gov.br/",
"The List of Species of Brazilian Flora is a project coordinated by the Botanical Garden of Rio de Janeiro in partnership with the CRIA. CRIA is responsible for the development and maintenance of the information system. The whole process of validation and inclusion of new data is done online by a network of over 400 taxonomists from Brazil and abroad. The online public version was completed in May 2010." );
return info;
}
}

@ -0,0 +1,848 @@
package org.gcube.data.spd.flora;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;
import org.gcube.data.spd.flora.dbconnection.ConnectionPool;
import org.gcube.data.spd.flora.dbconnection.ConnectionPoolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Utils {
//DERBY CONNECTION
// public static String dbURL = "jdbc:derby:classpath:FloraDB;create=true";
//public static String dbURL = "jdbc:derby:FloraDB;create=true";
static Logger logger = LoggerFactory.getLogger(Utils.class);
/**
* Return true if a url exists
*/
public static boolean urlExists(String pathUrl) {
HttpURLConnection con = null;
try {
HttpURLConnection.setFollowRedirects(false);
con = (HttpURLConnection) new URL(pathUrl).openConnection();
// Set timeouts in milliseconds
con.setConnectTimeout(30000);
con.setReadTimeout(30000);
con.setRequestMethod("HEAD");
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
}
catch (Exception e) {
logger.error("Exception",e);
return false;
}finally{
if( con != null ){
con.disconnect();
}
}
}
/**
* Metod called by CreateDBThread
*/
public static boolean createDB() throws SQLException, IOException {
BufferedReader br = null;
ConnectionPool pool = null;
try {
pool = ConnectionPool.getConnectionPool();
logger.trace("Create tables...");
boolean updateQuery;
br = new BufferedReader(new InputStreamReader(FloraPlugin.class.getResourceAsStream(FloraPlugin.dumpDb)));
if (br!=null){
String line;
while ((line = br.readLine()) != null) {
updateQuery = pool.insertPreStatement(line);
if (!updateQuery)
logger.trace(line);
}
}
} catch (ConnectionPoolException e) {
// logger.error("ConnectionPoolException", e);
return false;
}
return true;
}
//format date
public static String createDate() {
Calendar now = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String date = format.format(now.getTime());
return date;
}
public static String createCitation() {
StringBuilder cit = new StringBuilder();
cit.append(FloraPlugin.citation);
cit.append(createDate());
return cit.toString();
}
public static String createCredits() {
String cred = FloraPlugin.credits;
cred = cred.replace("XDATEX",createDate());
return cred;
}
/**
* getTagValue
*/
public static String getTagValue(String sTag, Element eElement) {
// NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
String a = "";
NodeList nlList = eElement.getElementsByTagName(sTag);
if (nlList!= null){
Element xmlNode = (Element)nlList.item(0);
if (xmlNode != null){
NodeList textFNList = xmlNode.getChildNodes();
if (textFNList != null){
Node nValue = (Node) textFNList.item(0);
if (nValue != null){
a = nValue.getNodeValue();
}
}
}
}
// System.out.println(a);
return a;
}
/**
* Find info about a specific id
*/
public static Map<String, String> findTags(String id) throws SQLException, ConnectException {
String pathUrl = FloraPlugin.baseurl + "/FULLRECORD/VERSION/2012/FORMAT/xml/LANG/en/ID/" + id;
// logger.trace("Find info " + tag);
// logger.trace(pathUrl);
Map<String, String> values = new HashMap<String, String>();
if (urlExists(pathUrl)){
InputStream is = null;
XMLInputFactory ifactory;
XMLEventReader eventReader = null;
try{
// logger.trace("open 1");
is = URI.create(pathUrl).toURL().openStream();
ifactory = XMLInputFactory.newInstance();
eventReader = ifactory.createXMLEventReader(is, "utf-8");
while (eventReader.hasNext()){
XMLEvent event = eventReader.nextEvent();
if (checkStartElement(event, "plain_name")){
// logger.trace(readCharacters(eventReader));
values.put("plain_name", readCharacters(eventReader));
continue;
}else if (checkStartElement(event, "author")){
// logger.trace(readCharacters(eventReader));
values.put("author", readCharacters(eventReader));
continue;
}else if (checkStartElement(event, "rank")){
// logger.trace(readCharacters(eventReader));
values.put("rank", readCharacters(eventReader));
continue;
}else if (checkStartElement(event, "status")){
// logger.trace(readCharacters(eventReader));
values.put("status", readCharacters(eventReader));
continue;
}else if (checkStartElement(event, "qualifier")){
// logger.trace(readCharacters(eventReader));
values.put("qualifier", readCharacters(eventReader));
continue;
}else if (checkEndElement(event, "record")){
break;
}
}
} catch (Exception e) {
logger.trace("Error reading " + pathUrl );
// logger.error("ExceptionPrintStackTrace",e);
}
finally{
// logger.trace("close 1");
try {
if (eventReader != null)
eventReader.close();
if (is != null)
is.close();
} catch (XMLStreamException e) {
logger.error("XMLStreamException",e);
} catch (IOException e) {
logger.error("IOException",e);
}
}
}
return values;
}
protected static String readCharacters(XMLEventReader eventReader) throws Exception{
String characters="";
XMLEvent event = eventReader.nextEvent();
while (eventReader.hasNext() && event.isCharacters() ){
characters+= event.asCharacters().getData();
event = eventReader.nextEvent();
}
return characters.trim();
}
protected static boolean checkStartElement(XMLEvent event, String value){
return event.getEventType() == XMLStreamConstants.START_ELEMENT && event.asStartElement().getName().getLocalPart().equals(value);
}
protected static boolean checkEndElement(XMLEvent event, String value){
return event.getEventType() == XMLStreamConstants.END_ELEMENT && event.asEndElement().getName().getLocalPart().equals(value);
}
/**
* Parse every path and populate db
*/
public static ArrayList<ArrayList<String>> discoverPath(String pathUrl, String id_parent, String name_parent, Integer idThread, Boolean flag) throws SQLException, MalformedURLException, IOException {
ArrayList<ArrayList<String>> listUrls = new ArrayList<ArrayList<String>>();
if (Utils.urlExists(pathUrl)){
String id = "";
String path = "";
String rank = "";
String name = "";
// String status = "";
// logger.trace(pathUrl);
InputStream is = null;
XMLInputFactory ifactory;
XMLEventReader eventReader = null;
try {
// logger.trace("open 3");
is =URI.create(pathUrl).toURL().openStream();
ifactory = XMLInputFactory.newInstance();
eventReader = ifactory.createXMLEventReader(is, "utf-8");
while (eventReader.hasNext()){
XMLEvent event = eventReader.nextEvent();
if (Utils.checkStartElement(event, "id")){
id = readCharacters(eventReader);
continue;
} else if (Utils.checkStartElement(event, "path")){
path = readCharacters(eventReader);
continue;
} else if (Utils.checkStartElement(event, "rank")){
rank = readCharacters(eventReader);
continue;
} else if (Utils.checkStartElement(event, "name")){
name = readCharacters(eventReader);
continue;
} else if (Utils.checkEndElement(event, "record")){
// logger.trace(path);
createOrUpdate(id_parent, id, name, path, rank);
// child Threads check other urls
if (flag) {
discoverPath("http://checklist.florabrasil.net/service/TREE/VERSION/2012/FORMAT/xml/LANG/en/PATH/" + path, id, name, idThread, flag);
}
// // the main thread gets the urls
else {
ArrayList<String> elem = new ArrayList<String> ();
elem.add(path);
elem.add(id);
elem.add(name);
listUrls.add(elem);
}
continue;
} else if (Utils.checkEndElement(event, "result")){
// logger.trace("break");
break;
}
}
} catch (Exception e) {
logger.error("ExceptionPrintStackTrace",e);
}finally{
try {
// logger.trace("close 3");
if (eventReader != null)
eventReader.close();
if (is != null)
is.close();
} catch (XMLStreamException e) {
logger.error("XMLStreamException",e);
}
}
}
return listUrls;
}
protected static void createOrUpdate(String id_parent, String id, String name, String path, String rank) {
// logger.trace(path);
ResultSet res = null;
// Database database = null;
String stmt = "";
ArrayList<String> terms = new ArrayList<String>();
Map<String, String> values = null;
ConnectionPool pool = null;
Connection con = null;
try {
pool = ConnectionPool.getConnectionPool();
res = pool.selectPrestatement("select count(*) from "+ FloraPlugin.tableName + " where id = ? ", id);
if(res.next()) {
values = findTags(id);
if (values.size()>0){
terms.add(id);
terms.add(rank);
terms.add(name);
terms.add(values.get("plain_name"));
terms.add(id_parent);
terms.add(values.get("author"));
terms.add(values.get("status"));
terms.add(path);
terms.add(values.get("qualifier"));
if (res.getInt(1) == 0) {
//Set values
stmt = ("insert into "+ FloraPlugin.tableName + " (id, rank, name, scientific_name, id_parent, citation, status, path, qualifier) values (?, ?, ?, ?, ?, ?, ?, ?, ?)");
}
else{
// logger.trace("update "+ FloraPlugin.tableName + " set id = ?, rank = ?, scientific_name = ?, id_parent = ?, citation = ?, status = ? where id = '" + id + "'");
stmt = ("update "+ FloraPlugin.tableName + " set id = ?, rank = ?, name = ?, scientific_name = ?, id_parent = ?, citation = ?, status = ?, path = ?, qualifier = ? where id = '" + id + "'");
}
if (!pool.preStatement(stmt, terms)){
logger.trace("error");
}
terms.clear();
values.clear();
}
}
}
catch (SQLException e) {
logger.error("SQL Exception",e);
} catch (ConnectException e) {
logger.error("Connection timed out",e);
} catch (ConnectionPoolException e) {
logger.error("ConnectionPoolException",e);
}finally{
try {
if (res != null) {
res.close();
}
if ((pool!=null) && (con!=null)){
pool.releaseConnection(con);
}
}catch (Exception e) {
logger.error("Exception",e);
}
}
}
/**
* Return true if a table exists
*/
public static boolean SQLTableExists(String tableName) {
boolean exists = false;
ConnectionPool pool = null;
Connection con = null;
ResultSet results = null;
try {
pool = ConnectionPool.getConnectionPool();
if (pool!=null){
con = pool.getConnection();
if (con!=null){
Statement statement = con.createStatement();
String sqlText = "SELECT tables.table_name FROM information_schema.tables WHERE table_name = '" + tableName + "'";
results = statement.executeQuery(sqlText);
if (results.next()){
logger.trace(tableName + " already exists");
exists = true;
}
else{
logger.trace(tableName + " does not exists");
exists = false;
}
}
}
} catch (SQLException e) {
logger.error("SQLException", e);
} catch (ConnectionPoolException e) {
logger.error("ConnectionPoolException", e);
}finally {
if ((pool!=null) && (con!=null)){
pool.releaseConnection(con);
}
try {
if (results != null) {
results.close();
}
} catch (SQLException ex) {
logger.error("sql Error", ex);
}
}
return exists;
}
/**
* get next update date
*/
public static long lastupdate() {
ConnectionPool pool = null;
Connection con = null;
ResultSet results = null;
long nextUpdate = 0;
try {
pool = ConnectionPool.getConnectionPool();
con = pool.getConnection();
if (con!=null){
Statement statement = con.createStatement();
String query = "select date from updates where id = (select max(id) from updates)";
logger.trace(query);
results = statement.executeQuery(query);
Date lastUpdate = null;
if (results.next()){
lastUpdate = results.getDate(1);
Date date = new Date();
long days = date.getTime() - lastUpdate.getTime();
if (days < 2592000000L){
return (2592000000L - days);
}
}
}
} catch (SQLException e) {
logger.error("SQLException", e);
} catch (ConnectionPoolException e) {
logger.error("ConnectionPoolException", e);
}finally {
if ((pool!=null) && (con!=null)){
pool.releaseConnection(con);
}
try {
if (results != null) {
results.close();
}
} catch (SQLException ex) {
logger.error("sql Error", ex);
}
}
return nextUpdate;
}
/**
* Convert long in date
*/
public static String nextUpdateDate(long input){
Date date = new Date(input);
Calendar cal = new GregorianCalendar();
cal.setTime(date);
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
return(dateFormat.format(date));
}
}
/**
* Update floraDB thread
*/
class UpdateThread extends Thread {
long nextup;
static Logger logger = LoggerFactory.getLogger(UpdateThread.class);
UpdateThread(long update) {
super("Thread");
this.nextup = update;
start(); // Start the thread
}
// update db every month (30 days)
public void run() {
while (true) {
logger.trace(createLabel(nextup));
try {
sleep(nextup);
//Utils util = new Utils();
if (upInfo()){
setlastupdate();
this.nextup = 2592000000L;
}
} catch (SQLException e) {
logger.error("sql Error", e);
} catch (Throwable e) {
logger.error("General Error", e);
}
}
}
/**
* Set next update date
*/
private String createLabel(long nextup) {
Date date = new Date();
String label = "Flora DB will be updated on " + Utils.nextUpdateDate(nextup + date.getTime());
return label;
}
/**
* Set next update date
*/
private void setlastupdate() {
ConnectionPool pool = null;
Connection con = null;
boolean result;
try {
pool = ConnectionPool.getConnectionPool();
con = pool.getConnection();
Date date = new Date();
long dt = date.getTime();
java.sql.Date today = new java.sql.Date (dt);
String query = "insert into updates (date) values ('" + today + "')";
result = pool.insertPreStatement(query);
if (!result){
logger.trace("problem setting last update date");
}
} catch (ConnectionPoolException e) {
logger.error("ConnectionPoolException", e);
}finally {
if ((pool!=null) && (con!=null)){
pool.releaseConnection(con);
}
}
}
/**
* Update db by new threads
*/
public static boolean upInfo() throws SQLException, MalformedURLException, IOException {
logger.trace("Update DB in BRAZILIAN-FLORA-PLUGIN");
try{
ArrayList<ArrayList<String>> list = Utils.discoverPath("http://checklist.florabrasil.net/service/TREE/VERSION/2012/FORMAT/xml/LANG/en/", null, "", null, false);
int sizeList = list.size();
logger.trace(list.toString());
Thread[] threads = new Thread[sizeList];
for (int i = 0; i < sizeList; i++) {
logger.trace(list.get(i).toString());
threads[i] = new NewThread(list.get(i), i); // creo un nuovo thread
}
for (int i = 0; i < sizeList; i++) {
try {
logger.trace("join");
threads[i].join();
} catch (InterruptedException e) {
logger.trace("interrupted join");
return false;
}
}
}catch (Exception e) {
logger.error("exc", e);
logger.trace("Exception");
return false;
}
return true;
}
}
/**
* Update Synonyms table
*/
class UpdateSynonymsThread extends Thread {
static Logger logger = LoggerFactory.getLogger(UpdateThread.class);
long nextup;
UpdateSynonymsThread(long update) {
super("Thread");
this.nextup = update;
start(); // Start the thread
}
// update db every month (30 days)
public void run() {
Date date = new Date();
logger.trace("Synonyms table in Flora DB will be updated on " + Utils.nextUpdateDate(nextup + date.getTime() ));
while (true) {
try {
sleep(nextup);
//Utils util = new Utils();
if (synonyms()){
this.nextup = 2764800000L;
}
} catch (SQLException e) {
logger.error("sql Error", e);
} catch (Throwable e) {
logger.error("General Error", e);
}
}
}
/**
* Parse every path looking for synonyms
* @return
*/
protected static boolean synonyms() throws SQLException {
ResultSet res = null;
ConnectionPool pool = null;
try {
pool = ConnectionPool.getConnectionPool();
logger.trace("select a.id, a.name, a.rank, b.name, c.name, a.id_parent from " + FloraPlugin.tableName + " as a join flora as b on (a.rank = 'SubSpecies' or a.rank = 'Species' or a.rank = 'Genus') and a.id_parent = b.id join flora as c on b.id_parent=c.id where a.status = 'Accepted name'");
res = pool.selectPrestatement("select a.id, a.name, a.rank, b.name, c.name, a.id_parent from " + FloraPlugin.tableName + " as a join flora as b on (a.rank = 'SubSpecies' or a.rank = 'Species' or a.rank = 'Genus') and a.id_parent = b.id join flora as c on b.id_parent=c.id where a.status = 'Accepted name'", null);
// res = database.get("select a.id, a.name, a.rank, b.name, c.name, a.id_parent from " + FloraPlugin.tableName + " as a join flora as b on (a.rank = 'SubSpecies' or a.rank = 'Species' or a.rank = 'Genus') and a.id_parent = b.id join flora as c on b.id_parent=c.id where a.status = 'Accepted name' ORDER BY a.id");
while(res.next()) {
String id = res.getString(1);
String name = res.getString(2);
String rank = res.getString(3);
String parent = res.getString(4);
String grandparent = res.getString(5);
String id_parent = res.getString(6);
String pathUrl = "";
if (rank.equals("Genus")) {
pathUrl = "http://www.checklist.florabrasil.net/service/VERSION/2012/FORMAT/xml/LANG/en/SYNONYMS/GENUS/" + name.replace(" ", "%20");
// rankParent = "family";
}
else if (rank.equals("Species")) {
pathUrl = "http://www.checklist.florabrasil.net/service/VERSION/2012/FORMAT/xml/LANG/en/SYNONYMS/GENUS/" + parent + "/SPECIES/" + name.replace(" ", "%20");
// rankParent = "genus";
}
else if (rank.equals("SubSpecies")) {
pathUrl = "http://www.checklist.florabrasil.net/service/VERSION/2012/FORMAT/xml/LANG/en/SYNONYMS/GENUS/" + grandparent + "/SPECIES/" + parent + "/SUBSPECIES/" + name.replace(" ", "%20");
// rankParent = "species";
}
// logger.trace(pathUrl);
insertSynonym(id, id_parent, pathUrl);
}
} catch (SQLException sqlExcept) {
logger.error("SQL Exception", sqlExcept);
return false;
} catch (ConnectionPoolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (res != null)
res.close();
}
return true;
}
/**
* Populate table synonyms
*/
protected static void insertSynonym(String id, String id_parent, String pathUrl) {
try {
// logger.trace(" pathUrl: " +pathUrl);
String idSyn = "";
String stmt = "";
Map<String, String> values = null;
if (Utils.urlExists(pathUrl)){
InputStream is = null;
XMLInputFactory ifactory;
XMLEventReader eventReader = null;
try{
// logger.trace("open 2");
is = URI.create(pathUrl.replace(" ", "")).toURL().openStream();
ifactory = XMLInputFactory.newInstance();
eventReader = ifactory.createXMLEventReader(is, "utf-8");
while (eventReader.hasNext()){
XMLEvent event = eventReader.nextEvent();
if (Utils.checkStartElement(event, "id")){
idSyn = Utils.readCharacters(eventReader);
continue;
} else if (Utils.checkEndElement(event, "result")){
break;
} else if (Utils.checkEndElement(event, "record")){
if (!idSyn.equals(id)){
// logger.trace("found synonym");
ConnectionPool pool = null;
Connection con = null;
ResultSet res = null;
try {
pool = ConnectionPool.getConnectionPool();
con = pool.getConnection();
ArrayList<String> terms = new ArrayList<String>();
res = pool.selectPrestatement("select count(*) from " + FloraPlugin.tableName + " where id = ? ", idSyn);
if(res.next()) {
if (res.getInt(1) == 0) {
values = Utils.findTags(idSyn);
terms.add(idSyn);
terms.add(values.get("rank"));
terms.add(values.get("plain_name"));
terms.add(id_parent);
terms.add(values.get("author"));
terms.add(id);
terms.add(values.get("status"));
terms.add(pathUrl);
terms.add(values.get("qualifier"));
stmt = ("insert into flora (id, rank, scientific_name, id_parent, citation, acceptednameusageid, status, path, qualifier) values (?, ?, ?, ?, ?, ?, ?, ?, ?)");
// else{
// stmt = ("update flora set id = ?, rank = ?, scientific_name = ?, id_parent = ?, citation = ?, acceptednameusageid = ?, status = ?, path = ?, qualifier = ? where id = '" + idSyn + "'");
// }
if (!pool.preStatement(stmt, terms)){
logger.trace("error");
}
terms.clear();
values.clear();
}
}
}catch (Exception e) {
logger.error("Error reading " + pathUrl);
// logger.error("General Error", e);
}finally{
if (res != null)
res.close();
if ((pool!=null) && (con!=null)){
pool.releaseConnection(con);
}
}
}
}
}
} catch (Exception e) {
logger.error("ExceptionPrintStackTrace",e);
}finally{
// logger.trace("close 2");
try {
if (eventReader != null)
eventReader.close();
if (is != null)
is.close();
} catch (XMLStreamException e) {
logger.error("XMLStreamException",e);
}
}
}
}catch (Exception e) {
logger.error("General Error", e);
}
}
}
class NewThread extends Thread {
static Logger logger = LoggerFactory.getLogger(NewThread.class);
ArrayList<String> elem;
String pathUrls;
String id_parent;
Integer idThread;
String sName;
NewThread(ArrayList<String> arrayList, Integer idThread) {
super("Thread");
this.elem = arrayList;
this.pathUrls = (elem.get(0));
this.id_parent = (elem.get(1));
this.sName = (elem.get(2));
this.idThread = idThread;
start(); // Start the thread
}
// This is the entry point for the child threads
public void run() {
try {
// logger.trace("http://checklist.florabrasil.net/service/TREE/VERSION/2012/FORMAT/xml/LANG/en/PATH/" + pathUrls);
Utils.discoverPath("http://checklist.florabrasil.net/service/TREE/VERSION/2012/FORMAT/xml/LANG/en/PATH/" + pathUrls, id_parent, sName, idThread, true);
// Discover.shutdown();
}catch (SQLException e) {
logger.error("SQL Exception", e);
} catch (MalformedURLException e) {
logger.error("MalformedURL Exception", e);
} catch (IOException e) {
logger.error("IOException", e);
}
}
}

@ -0,0 +1,366 @@
package org.gcube.data.spd.flora.capabilities;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.gcube.data.spd.flora.FloraPlugin;
import org.gcube.data.spd.flora.Utils;
import org.gcube.data.spd.flora.dbconnection.ConnectionPool;
import org.gcube.data.spd.flora.dbconnection.ConnectionPoolException;
import org.gcube.data.spd.model.Condition;
import org.gcube.data.spd.model.Conditions;
import org.gcube.data.spd.model.exceptions.ExternalRepositoryException;
import org.gcube.data.spd.model.exceptions.IdNotValidException;
import org.gcube.data.spd.model.exceptions.MethodNotSupportedException;
import org.gcube.data.spd.model.exceptions.StreamBlockingException;
import org.gcube.data.spd.model.products.TaxonomyItem;
import org.gcube.data.spd.model.products.TaxonomyStatus;
import org.gcube.data.spd.model.products.TaxonomyStatus.Status;
import org.gcube.data.spd.plugin.fwk.capabilities.ClassificationCapability;
import org.gcube.data.spd.plugin.fwk.writers.ClosableWriter;
import org.gcube.data.spd.plugin.fwk.writers.ObjectWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ClassificationCapabilityImpl extends ClassificationCapability {
static Logger logger = LoggerFactory.getLogger(ClassificationCapabilityImpl.class);
@Override
public Set<Conditions> getSupportedProperties() {
return Collections.emptySet();
}
/* (non-Javadoc)
* @see org.gcube.data.spd.plugin.fwk.capabilities.ClassificationCapability#getSynonymnsById(org.gcube.data.spd.plugin.fwk.writers.ObjectWriter, java.lang.String)
*/
@Override
public void getSynonymnsById(ObjectWriter<TaxonomyItem> writer, String id)
throws IdNotValidException, MethodNotSupportedException,
ExternalRepositoryException {
logger.trace("getSynonimnsByIds");
TaxonomyItem rs = null;
try{
rs = getSynbyId(id);
if ((rs!=null) && (writer.isAlive()))
writer.write(rs);
}catch (Exception e) {
writer.write(new StreamBlockingException("BrazilianFlora",id));
}
}
/**
* get RSItem by id
*/
private TaxonomyItem getSynbyId(String id) {
TaxonomyItem item = null;
ConnectionPool pool = null;
ResultSet results = null;
try {
pool = ConnectionPool.getConnectionPool();
String query ="select scientific_name, rank, status, id_parent, citation, acceptednameusageid, qualifier from "+ FloraPlugin.tableName + " where acceptednameusageid = ?";
results = pool.selectPrestatement(query, id);
if(results!=null) {
if(results.next()) {
String ScName = results.getString(1);
String rank = results.getString(2);
String status = results.getString(3);
String idParent = results.getString(4);
String author = results.getString(5);
String acceptednameusageid = results.getString(6);
String qualifier = results.getString(7);
item = createTaxonomyItem(id, ScName, rank, status, idParent,
author, acceptednameusageid, qualifier, true);
}
}
}
catch (SQLException sqlExcept) {
logger.error("sql Error", sqlExcept);
}catch (Throwable e) {
logger.error("general Error", e);
}
finally {
try {
if (results != null) {
results.close();
}
} catch (SQLException ex) {
logger.error("sql Error", ex);
}
}
return item;
}
/**
* Create Taxonomy Item
*/
private TaxonomyItem createTaxonomyItem(String id, String ScName, String rank, String status, String idParent,
String author, String acceptednameusageid, String qualifier, boolean flag) throws SQLException {
TaxonomyItem item = null;
try{
item = new TaxonomyItem(id);
item.setScientificName(ScName);
item.setRank(rank);
item.setScientificNameAuthorship(author);
item.setStatus(setTaxonomicStatus(status, acceptednameusageid, qualifier));
item.setCitation(Utils.createCitation());
item.setCredits(Utils.createCredits());
if ((flag) && (idParent!=null)){
try {
item.setParent(retrieveTaxonById(idParent));
} catch (IdNotValidException e) {
logger.error("Id Not Valid", e);
}
}
else
item.setParent(null);
}catch (Exception e) {
logger.error("ID not valid Exception", e);
}
return item;
}
/**
* Set Taxonomic status using "status", "acceptednameusageid", "qualifier"
*/
public static TaxonomyStatus setTaxonomicStatus(String status, String id_syn, String qualifier) {
TaxonomyStatus t;
if (status==null)
t = new TaxonomyStatus(qualifier, Status.UNKNOWN);
else if (status.equals("Accepted name"))
t = new TaxonomyStatus(qualifier, Status.ACCEPTED);
else if (status.equals("Synonym"))
t = new TaxonomyStatus(Status.SYNONYM, id_syn, qualifier);
else t = new TaxonomyStatus(qualifier, Status.UNKNOWN);
return t;
}
/**
* Return a ResultSet of Children
*/
private ResultSet createResultItemChilds(String id) {
ConnectionPool pool = null;
ResultSet results = null;
try {
pool = ConnectionPool.getConnectionPool();
String query = "select id, scientific_name, rank, status, citation, acceptednameusageid, qualifier from "+ FloraPlugin.tableName + " where id_parent = ?";
results = pool.selectPrestatement(query, id);
}
catch (Throwable e) {
logger.error("general Error", e);
}
return results;
}
@Override
public void searchByScientificName(String name,
ObjectWriter<TaxonomyItem> writer, Condition... properties) {
logger.trace("Retrive taxa by name " + name);
ResultSet results = null;
try {
results = createResultSet(name);
if (results!=null){
while(results.next()) {
String id = results.getString(1);
String scientificName = results.getString(2);
String rank = results.getString(3);
String status = results.getString(4);
String acceptednameusageid = results.getString(7);
String qualifier = results.getString(8);
String parent = results.getString(5);
String author = results.getString(6);
TaxonomyItem tax = createTaxonomyItem(id, scientificName, rank, status, parent, author, acceptednameusageid, qualifier, true);
if (writer.isAlive() && (tax!=null))
writer.write(tax);
else
break;
}
}
}catch (Exception e) {
writer.write(new StreamBlockingException("BrazilianFlora",""));
}finally{
if (results!=null){
try {
results.close();
} catch (SQLException e) {
logger.error("general Error", e);
}
}
}
}
/**
* Return a ResultSet of scientific names
*/
private ResultSet createResultSet(String scientificName) {
ConnectionPool pool = null;
ResultSet results = null;
try {
pool = ConnectionPool.getConnectionPool();
String term = "%" + scientificName + "%";
String query = "select id, scientific_name, rank, status, id_parent, citation, acceptednameusageid, qualifier from "+ FloraPlugin.tableName + " where UPPER(scientific_name) like UPPER(?)";
results = pool.selectPrestatement(query, term);
}
catch (ConnectionPoolException e) {
logger.error("ConnectionPoolException", e);
}finally{
}
return results;
}
@Override
public void retrieveTaxonByIds(Iterator<String> ids,
ClosableWriter<TaxonomyItem> writer) {
try{
while(ids.hasNext()) {
String id = ids.next();
TaxonomyItem item = null;
try {
item = retrieveTaxonById(id);
if (item!=null){
if (writer.isAlive() && (item!=null))
writer.write(item);
else
break;
}
} catch (IdNotValidException e) {
logger.error("Id Not Valid", e);
}
}
}catch (Exception e) {
writer.write(new StreamBlockingException("BrazilianFlora", ""));
}finally{
writer.close();
}
}
@Override
public TaxonomyItem retrieveTaxonById(String id)
throws IdNotValidException {
ConnectionPool pool = null;
ResultSet results = null;
TaxonomyItem tax = null;
try {
pool = ConnectionPool.getConnectionPool();
String query ="select scientific_name, rank, status, id_parent, citation, acceptednameusageid, qualifier from "+ FloraPlugin.tableName + " where id = ?";
results = pool.selectPrestatement(query, id);
if(results.next()) {
String scientificName = results.getString(1);
String rank = results.getString(2);
String status = results.getString(3);
String parent = results.getString(4);
String author = results.getString(5);
String acceptednameusageid = results.getString(6);
String qualifier = results.getString(7);
tax = createTaxonomyItem(id, scientificName, rank, status, parent, author, acceptednameusageid, qualifier, true);
}
}
catch (Throwable e) {
logger.error("Id not valid exception", e);
}finally{
if (results!=null){
try {
results.close();
} catch (SQLException e) {
logger.error("general Error", e);
}
}
}
return tax;
}
@Override
public List<TaxonomyItem> retrieveTaxonChildrenByTaxonId(String idParent)
throws IdNotValidException, ExternalRepositoryException {
List<TaxonomyItem> list = null;
ResultSet results = null;
try {
results = createResultItemChilds(idParent);
if (results!=null){
list = new ArrayList<TaxonomyItem>();
while(results.next()) {
String id = results.getString(1);
String scientific_name = results.getString(2);
String rank = results.getString(3);
String status = results.getString(4);
String author = results.getString(5);
String acceptednameusageid = results.getString(6);
String qualifier = results.getString(7);
TaxonomyItem tax = createTaxonomyItem(id, scientific_name, rank, status, idParent, author, acceptednameusageid, qualifier, false);
if (tax!=null)
list.add(tax);
}
}
} catch (SQLException e) {
logger.error("sql Error", e);
}finally{
if (results!=null){
try {
results.close();
} catch (SQLException e) {
logger.error("general Error", e);
}
}
}
return list;
}
}

@ -0,0 +1,109 @@
package org.gcube.data.spd.flora.capabilities;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.gcube.data.spd.flora.FloraPlugin;
import org.gcube.data.spd.flora.dbconnection.ConnectionPool;
import org.gcube.data.spd.model.exceptions.StreamBlockingException;
import org.gcube.data.spd.plugin.fwk.capabilities.ExpansionCapability;
import org.gcube.data.spd.plugin.fwk.writers.ObjectWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ExpansionCapabilityImpl implements ExpansionCapability {
Logger logger = LoggerFactory.getLogger(ExpansionCapabilityImpl.class);
@Override
public void getSynonyms(ObjectWriter<String> writer, String scientifcName) {
List<String> ids = null;
try{
ids = getSynonymsBySName(scientifcName);
for (String id : ids){
if (!writer.isAlive()) return;
getSynonymsById(id, writer);
}
}catch (Exception e) {
writer.write(new StreamBlockingException("BrazilianFlora",""));
}
}
//get a list of ids by Scientific name
private List<String> getSynonymsBySName(String scientifcName) {
ConnectionPool pool = null;
ResultSet results = null;
List<String> ids = new ArrayList<String>();
try {
String term = "%" + scientifcName + "%";
pool = ConnectionPool.getConnectionPool();
String query ="select id from "+ FloraPlugin.tableName + " where UPPER(scientific_name) like UPPER(?)";
results = pool.selectPrestatement(query, term);
if(results!=null) {
while(results.next()) {
ids.add(results.getString(1));
}
}
}
catch (SQLException sqlExcept) {
logger.error("sql Error", sqlExcept);
}catch (Throwable e) {
logger.error("general Error", e);
}
finally {
try {
if (results != null) {
results.close();
}
} catch (SQLException ex) {
logger.error("sql Error", ex);
}
}
return ids;
}
private void getSynonymsById(String id, ObjectWriter<String> writer) {
ConnectionPool pool = null;
ResultSet results1 = null;
try {
pool = ConnectionPool.getConnectionPool();
String query ="select scientific_name from "+ FloraPlugin.tableName + " where acceptednameusageid = ?";
results1 = pool.selectPrestatement(query, id);
if(results1!=null) {
while(results1.next()) {
if (writer.isAlive())
writer.write(results1.getString(1));
}
// System.out.println("synonyms.size() " + synonyms.size());
}
}
catch (SQLException sqlExcept) {
logger.error("sql Error", sqlExcept);
}catch (Throwable e) {
logger.error("general Error", e);
}
finally {
try {
if (results1 != null) {
results1.close();
}
} catch (SQLException ex) {
logger.error("sql Error", ex);
}
}
}
}

@ -0,0 +1,209 @@
package org.gcube.data.spd.flora.dbconnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import org.gcube.data.spd.flora.FloraPlugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ConnectionPool {
static Logger logger = LoggerFactory.getLogger(ConnectionPool.class);
private static ConnectionPool connectionPool = null;
private Vector<Connection> freeConnections;
private String dbUrl;
private String dbDriver;
private String dbLogin;
private String dbPassword;
private ConnectionPool() throws ConnectionPoolException {
freeConnections = new Vector<Connection>();
loadParameters();
loadDriver();
}
/**
* load parameters
**/
private void loadParameters() {
dbUrl = FloraPlugin.jdbc;
dbDriver = FloraPlugin.driver;
dbLogin = FloraPlugin.username;
dbPassword = FloraPlugin.password;
}
/**
* load driver
**/
private void loadDriver() throws ConnectionPoolException {
try {
Class.forName(dbDriver).newInstance();
}
catch (Exception e) {
throw new ConnectionPoolException();
}
}
/**
* get connection pool
**/
public static synchronized ConnectionPool getConnectionPool() throws ConnectionPoolException {
if(connectionPool == null) {
connectionPool = new ConnectionPool();
}
return connectionPool;
}
/**
* get a free connection or create a new one
**/
public synchronized Connection getConnection() throws ConnectionPoolException {
Connection con = null;
if(freeConnections.size() > 0) {
con = (Connection)freeConnections.firstElement();
freeConnections.removeElementAt(0);
try {
if(con.isClosed()) {
con = getConnection();
}
}
catch(SQLException e) {
con = getConnection();
}
}
else {
con = newConnection();
}
return con;
}
/**
* Create a new connection
**/
private Connection newConnection() throws ConnectionPoolException {
Connection con = null;
try {
con = DriverManager.getConnection(dbUrl, dbLogin, dbPassword);
}
catch(SQLException e) {
throw new ConnectionPoolException();
}
return con;
}
/**
* Release the connection and put it in the queue
**/
public synchronized void releaseConnection(Connection con) {
freeConnections.add(con);
}
/**
* Update
*/
public boolean preStatement( String query, ArrayList<String> terms){
PreparedStatement stmt = null;
ConnectionPool pool = null;
Connection con = null;
try {
pool = ConnectionPool.getConnectionPool();
con = pool.getConnection();
stmt = con.prepareStatement(query);
for(int i = 0; i < terms.size(); i++){
stmt.setString(i+1, terms.get(i));
}
stmt.executeUpdate();
return true;
}
catch ( SQLException sqle ) {
logger.trace("Error executing: " + stmt);
return false;
} catch (ConnectionPoolException e) {
return false;
}finally{
if ((pool!=null) && (con!=null)){
pool.releaseConnection(con);
}
}
}
/**
* Select using preStatement
*/
public ResultSet selectPrestatement( String query, String term){
ResultSet result = null;
PreparedStatement stmt = null;
ConnectionPool pool = null;
Connection con = null;
try {
pool = ConnectionPool.getConnectionPool();
con = pool.getConnection();
stmt = con.prepareStatement(query);
if (term!=null)
stmt.setString(1, term);
result = stmt.executeQuery();
// System.out.println("stmt: " + stmt);
}
catch ( SQLException e ) {
logger.error("SQLException", e);
} catch (ConnectionPoolException e) {
logger.error("ConnectionPoolException", e);
}finally{
if ((pool!=null) && (con!=null)){
pool.releaseConnection(con);
}
}
return result;
}
/* Insert
*/
public boolean insertPreStatement( String query){
PreparedStatement stmt = null;
ConnectionPool pool = null;
Connection con = null;
try {
pool = ConnectionPool.getConnectionPool();
con = pool.getConnection();
if (con!=null){
stmt = con.prepareStatement(query);
stmt.executeUpdate();
}
}
catch ( SQLException sqle ) {
logger.trace("Error executing: " + stmt);
return false;
} catch (ConnectionPoolException e) {
return false;
}finally{
if ((pool!=null) && (con!=null)){
pool.releaseConnection(con);
}
}
return true;
}
}

@ -0,0 +1,13 @@
package org.gcube.data.spd.flora.dbconnection;
public class ConnectionPoolException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
public ConnectionPoolException() {
}
}

@ -0,0 +1,2 @@
create table updates (id serial NOT NULL PRIMARY KEY, date date);
create table flora(id varchar(15) NOT NULL PRIMARY KEY, name varchar(100), scientific_name varchar(100), rank varchar(100), status character varying(50), id_parent varchar(15), citation varchar(100), acceptednameusageid varchar(15), path varchar(200), qualifier varchar(100));

@ -0,0 +1,141 @@
package org.gcube.data.spd.flora;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.util.List;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.data.spd.flora.capabilities.ClassificationCapabilityImpl;
import org.gcube.data.spd.model.exceptions.StreamException;
import org.gcube.data.spd.model.products.TaxonomyItem;
import org.gcube.data.spd.plugin.fwk.writers.ObjectWriter;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestClassification {
static Logger logger = LoggerFactory.getLogger(TestClassification.class);
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// Test retrieveTaxaByName
FloraPlugin b = new FloraPlugin();
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Category/text() eq 'BiodiversityRepository' and $resource/Profile/Name eq 'BrazilianFlora' ");
ScopeProvider.instance.set("/gcube/devsec");
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> resources = client.submit(query);
System.out.println(resources.size());
if(resources.size() != 0) {
try {
b.initialize(resources.get(0));
} catch (Exception e) {
e.printStackTrace();
}
}
ClassificationCapabilityImpl a = new ClassificationCapabilityImpl();
a.searchByScientificName("rosa", new ObjectWriter<TaxonomyItem>() {
@Override
public boolean isAlive() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean write(TaxonomyItem t) {
System.out.println(t.toString());
return false;
}
@Override
public boolean write(StreamException arg0) {
// TODO Auto-generated method stub
return false;
}
});
a.getSynonymnsById(new ObjectWriter<TaxonomyItem>() {
@Override
public boolean isAlive() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean write(TaxonomyItem t) {
System.out.println(t.toString());
return false;
}
@Override
public boolean write(StreamException arg0) {
// TODO Auto-generated method stub
return false;
}
},
"33");
TaxonomyItem tax = a.retrieveTaxonById("4127");
logger.trace(tax.toString());
// Test retrieveTaxonChildsByTaxonId
// logger.trace(a.retrieveTaxonChildrenByTaxonId(4137+""));
//
// LocalWrapper<String> wrap = new LocalWrapper<String>();
// wrap.add("100");
// wrap.add("32");
// wrap.add("172");
// LocalReader<String> list = new LocalReader<String>(wrap);
// a.retrieveTaxonByIds(list, new ClosableWriter<TaxonomyItem>() {
//
// @Override
// public boolean write(TaxonomyItem t) {
// System.out.println(t.toString());
// return false;
// }
//
// @Override
// public boolean write(StreamException error) {
// // TODO Auto-generated method stub
// return false;
// }
//
// @Override
// public boolean isAlive() {
// // TODO Auto-generated method stub
// return true;
// }
//
// @Override
// public void close() {
// // TODO Auto-generated method stub
//
// }
// });
}
}

@ -0,0 +1,62 @@
package org.gcube.data.spd.flora;
import org.gcube.data.spd.flora.capabilities.ExpansionCapabilityImpl;
import org.gcube.data.spd.model.exceptions.StreamException;
import org.gcube.data.spd.plugin.fwk.writers.ObjectWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestExpansionCapability {
static Logger logger = LoggerFactory.getLogger(TestExpansionCapability.class);
/**
* @param args
*/
public static void main(String[] args) {
// FloraPlugin a = new FloraPlugin();
//
//
// SimpleQuery query = queryFor(ServiceEndpoint.class);
//
// query.addCondition("$resource/Profile/Category/text() eq 'BiodiversityRepository' and $resource/Profile/Name eq 'BrazilianFlora' ");
// ScopeProvider.instance.set("/gcube/devsec");
//
// DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
//
// List<ServiceEndpoint> resources = client.submit(query);
//
// System.out.println(resources.size());
//
// if(resources.size() != 0) {
// try {
// a.initialize(resources.get(0));
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
ExpansionCapabilityImpl b = new ExpansionCapabilityImpl();
b.getSynonyms(new ObjectWriter<String>() {
@Override
public boolean write(StreamException error) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean write(String t) {
System.out.println(t);
return false;
}
@Override
public boolean isAlive() {
// System.out.println("isAlive");
return true;
}
}, "rosa");
}
}

@ -0,0 +1,78 @@
package org.gcube.data.spd.flora;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.util.List;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
public class TestUpdate {
/**
* @param args
* @throws Exception
*/
@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception {
//
// UpdateThread t = new UpdateThread();
// t.upInfo();
FloraPlugin b = new FloraPlugin();
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Category/text() eq 'BiodiversityRepository' and $resource/Profile/Name eq 'BrazilianFlora' ");
ScopeProvider.instance.set("/gcube/devsec");
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> resources = client.submit(query);
System.out.println(resources.size());
if(resources.size() != 0) {
try {
b.initialize(resources.get(0));
} catch (Exception e) {
e.printStackTrace();
}
}
// if (!(Utils.SQLTableExists(FloraPlugin.tableName)))
// new CreateDBThread();
//
// new UpdateThread();
//
// UpdateThread t = new UpdateThread();
// t.upInfo();
//
// UpdateSynonymsThread a = new UpdateSynonymsThread();
// a.synonyms();
// new UpdateThread();
// new UpdateSynonymsThread();
if ((!Utils.SQLTableExists(FloraPlugin.tableName) || (!Utils.SQLTableExists("updates")))){
System.out.println("Create tables");
Utils.createDB();
}
long update = Utils.lastupdate();
new UpdateThread(update);
// new UpdateSynonymsThread(update);
}
}

@ -0,0 +1,192 @@
//package org.gcube.data.spd.flora;
//
//import java.io.IOException;
//import java.io.InputStream;
//import java.net.MalformedURLException;
//import java.net.URI;
//import java.sql.ResultSet;
//import java.sql.SQLException;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.Map;
//
//import javax.xml.stream.XMLEventReader;
//import javax.xml.stream.XMLInputFactory;
//import javax.xml.stream.XMLStreamException;
//import javax.xml.stream.events.XMLEvent;
//
//import org.gcube.common.core.utils.logging.GCUBELog;
//
//
//public class check {
// static GCUBELog logger = new GCUBELog(check.class);
// /**
// * @param args
// */
// public static void main(String[] args) {
// // TODO Auto-generated method stub
// try {
// fill();
// } catch (MalformedURLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
//
//
// /**
// * Parse every path looking for synonyms
// * @throws IOException
// * @throws MalformedURLException
// */
// protected static void fill() throws SQLException, MalformedURLException, IOException {
//
//
// ResultSet res = null;
// Database database = null;
//
// try {
// database = new Database();
// database.connect();
//
// logger.trace("select id, id_parent from " + FloraPlugin.tableName + " where path is null and status='Accepted name'" );
// res = database.get("select id, id_parent from " + FloraPlugin.tableName + " where path is null and status='Accepted name'" );
//
// while(res.next()) {
// String id = res.getString(1);
// String id_parent = res.getString(2);
//
// disco("http://checklist.florabrasil.net/service/FULLRECORD/VERSION/2012/FORMAT/xml/LANG/en/ID/" + id, id, id_parent);
//
// }
// } catch (SQLException sqlExcept) {
//
// logger.error("SQL Exception", sqlExcept);
// }finally{
// if (res != null)
// res.close();
//
// database.shutDown();
// }
//
// }
//
// /**
// * Parse every path and populate db
// */
// public static void disco(String pathUrl, String id, String id_parent) throws SQLException, MalformedURLException, IOException {
//
//
// if (Utils.urlExists(pathUrl)){
// // String status = "";
//
// // logger.trace(pathUrl);
// InputStream is = null;
// XMLInputFactory ifactory;
// XMLEventReader eventReader = null;
// try {
// // logger.trace("open 3");
// is =URI.create(pathUrl).toURL().openStream();
// ifactory = XMLInputFactory.newInstance();
// eventReader = ifactory.createXMLEventReader(is, "utf-8");
// Map<String, String> values = new HashMap<String, String>();
// while (eventReader.hasNext()){
// XMLEvent event = eventReader.nextEvent();
//
// if (Utils.checkStartElement(event, "plain_name")){
// // logger.trace(readCharacters(eventReader));
// values.put("plain_name", Utils.readCharacters(eventReader));
// continue;
// }else if (Utils.checkStartElement(event, "author")){
// // logger.trace(readCharacters(eventReader));
// values.put("author", Utils.readCharacters(eventReader));
// continue;
// }else if (Utils.checkStartElement(event, "rank")){
// // logger.trace(readCharacters(eventReader));
// values.put("rank", Utils.readCharacters(eventReader));
// continue;
// }else if (Utils.checkStartElement(event, "status")){
// // logger.trace(readCharacters(eventReader));
// values.put("status", Utils.readCharacters(eventReader));
// continue;
// }else if (Utils.checkStartElement(event, "qualifier")){
// // logger.trace(readCharacters(eventReader));
// values.put("qualifier", Utils.readCharacters(eventReader));
// continue;
//
//
// } else if (Utils.checkEndElement(event, "record")){
// // logger.trace(path);
// ArrayList<String> terms = new ArrayList<String>();
// ResultSet res = null;
// Database database = null;
// try {
// database = new Database();
// database.connect();
// // logger.trace("select count(*) from "+ FloraPlugin.tableName + " where id = '" + id + "'");
// res = database.get("select count(*) from "+ FloraPlugin.tableName + " where id = '" + id + "'");
//
// if(res.next()) {
//
// String stmt = "";
// terms.add(id);
// terms.add(values.get("rank"));
// terms.add(values.get("plain_name"));
// terms.add(id_parent);
// terms.add(values.get("author"));
// terms.add(values.get("status"));
// terms.add(values.get("qualifier"));
//
// if (res.getInt(1) == 0) {
// //Set values
// stmt = ("insert into "+ FloraPlugin.tableName + " (id, rank, scientific_name, id_parent, citation, status, qualifier) values (?, ?, ?, ?, ?, ?, ?)");
// }
// else{
// // logger.trace("update "+ FloraPlugin.tableName + " set id = ?, rank = ?, scientific_name = ?, id_parent = ?, citation = ?, status = ? where id = '" + id + "'");
// stmt = ("update "+ FloraPlugin.tableName + " set id = ?, rank = ?, scientific_name = ?, id_parent = ?, citation = ?, status = ?, qualifier = ? where id = '" + id + "'");
// }
//
// if (!database.preStatement(stmt, terms)){
// logger.trace("error");
// }
// terms.clear();
//
// }
//
// }
// catch (SQLException e) {
// logger.error("SQL Exception",e);
// }
//
// continue;
//
// } else if (Utils.checkEndElement(event, "result")){
// // logger.trace("break");
// break;
// }
// }
// } catch (Exception e) {
// logger.error("ExceptionPrintStackTrace",e);
// }finally{
// try {
// // logger.trace("close 3");
// if (eventReader != null)
// eventReader.close();
// if (is != null)
// is.close();
// } catch (XMLStreamException e) {
// logger.error("XMLStreamException",e);
// }
// }
// }
// }
//
//
//
//}

@ -0,0 +1,18 @@
package org.gcube.data.spd.flora;
import java.io.IOException;
import java.sql.SQLException;
public class insert {
/**
* @param args
* @throws IOException
* @throws SQLException
*/
public static void main(String[] args) throws SQLException, IOException {
Utils.createDB();
}
}

@ -0,0 +1,110 @@
//package org.gcube.data.spd.flora;
//
//import java.io.IOException;
//import java.net.ConnectException;
//import java.net.MalformedURLException;
//import java.sql.ResultSet;
//import java.sql.SQLException;
//import org.gcube.common.core.utils.logging.GCUBELog;
//import org.gcube.data.spd.flora.dbconnection.Database;
//
//public class refill {
// static GCUBELog logger = new GCUBELog(refill.class);
//
//
//
// /**
// * @param args
// * @throws Exception
// */
// public static void main(String[] args) throws Exception {
// try {
// fill();
// } catch (ConnectException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
//
// }
//
// /**
// * Parse every path looking for synonyms
// * @throws IOException
// * @throws MalformedURLException
// */
// protected static void fill() throws SQLException, MalformedURLException, IOException {
//
//
// ResultSet res = null;
// Database database = null;
//
// try {
// database = new Database();
// database.connect();
//
// // logger.trace("select id, name, id_parent, path from " + FloraPlugin.tableName + " where citation=''");
// // res = database.get("select id, name, id_parent, path from " + FloraPlugin.tableName + " where citation=''");
//
// logger.trace("select path, id, scientific_name from " + FloraPlugin.tableName + " where id_parent='32' and path > 'Angiospermas_/Loranthaceae_JUS' and path < 'Angiospermas_/Oleaceae_' order by path limit 24" );
// res = database.get("select path, id, scientific_name from " + FloraPlugin.tableName + " where id_parent='32' and path > 'Angiospermas_/Loranthaceae_JUS' and path < 'Angiospermas_/Oleaceae_' order by path limit 24" );
//
//
// // res = database.get("select a.id, a.name, a.rank, b.name, c.name, a.id_parent from " + FloraPlugin.tableName + " as a join flora as b on (a.rank = 'SubSpecies' or a.rank = 'Species' or a.rank = 'Genus') and a.id_parent = b.id join flora as c on b.id_parent=c.id where a.status = 'Accepted name' ORDER BY a.id");
//
// while(res.next()) {
//
// String path = res.getString(1);
// String id_parent = res.getString(2);
// // if (id_parent.equals(176))
// // System.exit(0);
// String sName = res.getString(3);
// // logger.trace("http://checklist.florabrasil.net/service/TREE/VERSION/2012/FORMAT/xml/LANG/en/PATH/" + path);
// Utils.discoverPath("http://checklist.florabrasil.net/service/TREE/VERSION/2012/FORMAT/xml/LANG/en/PATH/" + path, id_parent, sName, null, true);
//
// // String id = res.getString(1);
// // String name = res.getString(2);
// // String id_parent = res.getString(3);
// // String path = res.getString(4);
// //
// // Map<String, String> values = Utils.findTags(id);
// // ArrayList<String> terms = new ArrayList<String>();
// //
// // terms.add(id);
// // terms.add(values.get("rank"));
// // terms.add(name);
// // terms.add(values.get("plain_name"));
// // terms.add(id_parent);
// // terms.add(values.get("author"));
// // terms.add(values.get("status"));
// // terms.add(path);
// // terms.add(values.get("qualifier"));
// //
// // String stmt = "";
// // stmt = ("update "+ FloraPlugin.tableName + " set id = ?, rank = ?, name = ?, scientific_name = ?, id_parent = ?, citation = ?, status = ?, path = ?, qualifier = ? where id = '" + id + "'");
// //
// //
// // if (!database.preStatement(stmt, terms)){
// // logger.trace("error");
// // }
// // terms.clear();
// // values.clear();
//
//
// }
// } catch (SQLException sqlExcept) {
//
// logger.error("SQL Exception", sqlExcept);
// }finally{
// if (res != null)
// res.close();
//
// database.shutDown();
// }
//
// }
//
//}

@ -0,0 +1,137 @@
package org.gcube.data.spd.flora;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.util.List;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.data.spd.model.exceptions.StreamException;
import org.gcube.data.spd.model.products.ResultItem;
import org.gcube.data.spd.plugin.fwk.writers.ObjectWriter;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import org.gcube.application.speciesmanager.stubs.model.ResultItem;
//import org.gcube.application.speciesmanager.stubs.pluginhelper.writers.ObjectWriter;
public class test {
static Logger logger = LoggerFactory.getLogger(test.class);
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// Discover a = new Discover();
// a.upInfo();
// a.copy_flora();
// Discover.copy_syn();
//update db
// new UpdateThread();
// ArrayList<ArrayList<String>> list = a.discoverPath("http://checklist.florabrasil.net/service/TREE/VERSION/2012/FORMAT/xml/LANG/en/", null, "", null, false);
// System.out.println(list.size());
// for (int n=list.size(); n>0;n--)
// {
//// System.out.println(list.get(1));
// new NewThread(list.remove(1), 6); // creo un nuovo thread
// }
FloraPlugin b = new FloraPlugin();
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Category/text() eq 'BiodiversityRepository' and $resource/Profile/Name eq 'BrazilianFlora' ");
ScopeProvider.instance.set("/gcube/devsec");
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> resources = client.submit(query);
System.out.println(resources.size());
if(resources.size() != 0) {
try {
b.initialize(resources.get(0));
} catch (Exception e) {
e.printStackTrace();
}
}
b.searchByScientificName("rosa", new ObjectWriter<ResultItem>() {
@Override
public boolean isAlive() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean write(ResultItem t) {
System.out.println(t.toString());
System.out.println(t.getScientificName());
return false;
}
@Override
public boolean write(StreamException arg0) {
// TODO Auto-generated method stub
return false;
}
});
b.searchByScientificName("s", new ObjectWriter<ResultItem>() {
@Override
public boolean isAlive() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean write(ResultItem arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean write(StreamException arg0) {
// TODO Auto-generated method stub
return false;
}
});
// Discover.createDB();
// new UpdateThread();
//create db
// if (!(Discover.SQLTableExists(FloraPlugin.tableName)) & !(Discover.SQLTableExists(FloraPlugin.tableSyn)))
// new CreateDBThread();
// else
// System.out.println("Tables already exist");
//
// new UpdateThread();
// Discover.syn();
//
// FloraPlugin b = new FloraPlugin();
////
// b.search("Calyptranthes");
}
}

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<Resource>
<ID></ID>
<Type>Service</Type>
<Profile>
<Description>Brazilian Flora Plugin</Description>
<Class>DataAccess</Class>
<Name>brazilian-flora-spd-plugin</Name>
<Version>1.0.0</Version>
<Packages>
<Software>
<Description>Brazilian Flora Plugin</Description>
<Name>brazilian-flora-spd-plugin</Name>
<Version>1.8.0-SNAPSHOT</Version>
<MavenCoordinates>
<groupId>org.gcube.data.spd</groupId>
<artifactId>brazilian-flora-spd-plugin</artifactId>
<version>1.8.0-SNAPSHOT</version>
</MavenCoordinates>
<Type>library</Type>
<Files>
<File>brazilian-flora-spd-plugin-1.8.0-SNAPSHOT.jar</File>
</Files>
</Software>
</Packages>
</Profile>
</Resource>
Loading…
Cancel
Save