2013-05-07 11:27:17 +02:00
/ * *
2017-01-11 18:39:11 +01:00
*
2013-05-07 11:27:17 +02:00
* /
package org.gcube.portlets.user.speciesdiscovery.client ;
import static org.gcube.data.spd.client.plugins.AbstractPlugin.manager ;
import java.util.concurrent.TimeUnit ;
2013-11-26 15:28:22 +01:00
import org.gcube.application.framework.core.session.ASLSession ;
import org.gcube.application.framework.core.session.SessionManager ;
2013-05-07 11:27:17 +02:00
import org.gcube.common.scope.api.ScopeProvider ;
2017-01-11 18:39:11 +01:00
import org.gcube.data.spd.client.proxies.ManagerClient ;
2013-05-07 11:27:17 +02:00
import org.gcube.data.spd.model.exceptions.InvalidQueryException ;
import org.gcube.data.spd.model.products.ResultElement ;
2013-11-26 15:28:22 +01:00
import org.gcube.data.spd.model.products.ResultItem ;
2017-01-11 18:39:11 +01:00
import org.gcube.data.spd.model.service.exceptions.UnsupportedCapabilityException ;
import org.gcube.data.spd.model.service.exceptions.UnsupportedPluginException ;
2013-05-07 11:27:17 +02:00
import org.gcube.data.streams.Stream ;
2013-11-26 15:28:22 +01:00
import org.gcube.portlets.user.speciesdiscovery.server.service.ResultItemConverter ;
import org.gcube.portlets.user.speciesdiscovery.server.service.StreamIterator ;
import org.gcube.portlets.user.speciesdiscovery.server.stream.CastConverter ;
import org.gcube.portlets.user.speciesdiscovery.server.stream.CloseableIterator ;
import org.gcube.portlets.user.speciesdiscovery.server.stream.ConversionIterator ;
import org.gcube.portlets.user.speciesdiscovery.shared.ResultRow ;
2013-05-07 11:27:17 +02:00
2017-01-11 18:39:11 +01:00
2013-05-07 11:27:17 +02:00
/ * *
2017-01-11 18:39:11 +01:00
* The Class ServiceQuery .
2013-05-07 11:27:17 +02:00
*
2017-01-11 18:39:11 +01:00
* @author Francesco Mangiacrapa francesco . mangiacrapa @isti.cnr.it
* Jan 10 , 2017
2013-05-07 11:27:17 +02:00
* /
public class ServiceQuery {
/ * *
* @param args
2017-01-11 18:39:11 +01:00
* @throws UnsupportedPluginException
* @throws InvalidQueryException
* @throws UnsupportedCapabilityException
2013-05-07 11:27:17 +02:00
* /
2017-01-11 18:39:11 +01:00
2013-11-26 15:28:22 +01:00
private static String username = " test.user " ;
2017-01-11 18:39:11 +01:00
/ * *
* The main method .
*
* @param args the arguments
* @throws InvalidQueryException the invalid query exception
* @throws UnsupportedPluginException the unsupported plugin exception
* @throws UnsupportedCapabilityException the unsupported capability exception
* /
2013-09-05 12:19:32 +02:00
public static void main ( String [ ] args ) throws InvalidQueryException , UnsupportedPluginException , UnsupportedCapabilityException {
2013-05-07 11:27:17 +02:00
String scope = " /gcube/devsec " ;
2013-07-03 15:47:39 +02:00
// String scope = "/d4science.research-infrastructures.eu/gCubeApps/BiodiversityResearchEnvironment"; //Production
2013-05-07 11:27:17 +02:00
ScopeProvider . instance . set ( scope ) ;
2013-11-26 15:28:22 +01:00
ASLSession session = SessionManager . getInstance ( ) . getASLSession ( " 123 " , username ) ;
2017-01-11 18:39:11 +01:00
ManagerClient call = manager ( ) . withTimeout ( 3 , TimeUnit . MINUTES ) . build ( ) ;
2013-07-03 15:47:39 +02:00
// Manager call = manager().at(URI.create("http://node24.d.d4science.research-infrastructures.eu:9000")).withTimeout(3, TimeUnit.MINUTES).build();
2013-05-07 11:27:17 +02:00
//Stream<ResultElement> results = call.search("SEARCH BY CN 'shark' RESOLVE WITH OBIS EXPAND WITH ITIS RETURN Product");
2013-07-03 15:47:39 +02:00
// Stream<ResultElement> results = call.search("SEARCH BY CN 'shark' RESOLVE WITH OBIS EXPAND WITH ITIS WHERE coordinate <= 15.12, 16.12 RETURN Product");
// Stream<ResultElement> results = call.search("SEARCH BY SN 'sarda sarda' RESOLVE WITH OBIS EXPAND WITH ITIS WHERE coordinate <= 15.12, 16.12 RETURN Product");
System . out . println ( " start query... " ) ;
2013-11-26 15:28:22 +01:00
Stream < ResultElement > results = call . search ( " SEARCH BY SN 'Latimeria chalumnae' IN GBIF RETURN Product HAVING xpath( \" //product[type='Occurrence' and count>0] \" ) " ) ;
// Stream<ResultElement> results = call.search("SEARCH BY SN 'Palinurus elephas' IN WoRMS RETURN Taxon");
StreamIterator < ResultElement > input = new StreamIterator < ResultElement > ( results ) ;
2017-01-11 18:39:11 +01:00
2013-11-26 15:28:22 +01:00
System . out . println ( " Results from service... " ) ;
2013-07-03 15:47:39 +02:00
int i = 0 ;
2013-11-26 15:28:22 +01:00
while ( results . hasNext ( ) ) {
2013-07-03 15:47:39 +02:00
ResultElement elem = results . next ( ) ;
2013-11-26 15:28:22 +01:00
System . out . println ( + + i + " ) el: " + elem . getId ( ) + " type: " + elem . getType ( ) . name ( ) ) ;
}
2017-01-11 18:39:11 +01:00
2013-11-26 15:28:22 +01:00
System . out . println ( " Results from conversion... " ) ;
ConversionIterator < ResultElement , ResultItem > caster = buildCaster ( input ) ;
2017-01-11 18:39:11 +01:00
2013-11-26 15:28:22 +01:00
//from ResultItem to ResultRow
ResultItemConverter converter = new ResultItemConverter ( session ) ;
ConversionIterator < ResultItem , ResultRow > inputConverter = new ConversionIterator < ResultItem , ResultRow > ( caster , converter ) ;
while ( inputConverter . hasNext ( ) ) {
ResultRow row = inputConverter . next ( ) ;
System . out . println ( + + i + " ) row: " + row ) ;
2013-07-03 15:47:39 +02:00
}
2017-01-11 18:39:11 +01:00
2013-05-07 11:27:17 +02:00
results . close ( ) ;
System . out . println ( " DONE " ) ;
}
2017-01-11 18:39:11 +01:00
/ * *
* Builds the caster .
*
* @param < I > the generic type
* @param < O > the generic type
* @param input the input
* @return the conversion iterator
* /
2013-11-26 15:28:22 +01:00
protected static < I , O > ConversionIterator < I , O > buildCaster ( CloseableIterator < I > input )
2017-01-11 18:39:11 +01:00
{
2013-11-26 15:28:22 +01:00
CastConverter < I , O > elementConverter = new CastConverter < I , O > ( ) ;
ConversionIterator < I , O > caster = new ConversionIterator < I , O > ( input , elementConverter ) ;
return caster ;
}
2013-05-07 11:27:17 +02:00
}