package gis; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.gcube.common.encryption.StringEncrypter; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.spatial.data.geonetwork.GeoNetwork; import org.gcube.spatial.data.geonetwork.GeoNetworkPublisher; import org.gcube.spatial.data.geonetwork.GeoNetworkReader; import org.gcube.spatial.data.geonetwork.LoginLevel; import org.gcube.spatial.data.geonetwork.configuration.Configuration; import org.gcube.spatial.data.geonetwork.model.Account; import org.gcube.spatial.data.geonetwork.model.Account.Type; import org.junit.Test; import it.geosolutions.geonetwork.util.GNSearchRequest; import it.geosolutions.geonetwork.util.GNSearchResponse; import it.geosolutions.geonetwork.util.GNSearchResponse.GNMetadata; /** * The Class GeonetworkQueryTest. * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * Aug 31, 2016 */ public class GeonetworkQueryTest { private static final int MAX = 30; //private String[] scopes = {"/gcube/devsec/devVRE"}; //private String[] scopes = {"/gcube/devsec/StaTabTest"}; //private String[] scopes = {"/gcube/devsec/devVRE"}; //private String[] scopes = {"/pred4s/preprod/preVRE"}; //private String[] scopes = {"/d4science.research-infrastructures.eu"}; //private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/fisheriesandecosystematmii"}; //private String[] scopesProd = {"/d4science.research-infrastructures.eu/D4Research"}; //private String[] scopesProd = {"/d4science.research-infrastructures.eu"}; //private String[] scopes = {"/d4science.research-infrastructures.eu/gCubeApps/BlueBridgeProject"}; //private String[] scopes = {"/d4science.research-infrastructures.eu/gCubeApps/AquacultureAtlasGeneration"}; //private String[] scopes = {"/d4science.research-infrastructures.eu/gCubeApps/FrenchTropicalTunaAtlas"}; private String[] scopes = {"/d4science.research-infrastructures.eu/gCubeApps/BiodiversityLab"}; //private String[] scopes = {"/d4science.research-infrastructures.eu/gCubeApps/SIASPA"}; //private String[] scopes = {"/d4science.research-infrastructures.eu/gCubeApps/ICCAT_BFT-E"}; private LoginLevel loginLevel = LoginLevel.CKAN; private Type accountType = Type.CKAN; private String textToSearch = "geo_fea"; /** * Gets the count. * * @return the count * @throws Exception the exception */ //@Test public void getCount() throws Exception{ try{ for(String scope:scopes){ ScopeProvider.instance.set(scope); GeoNetworkPublisher reader=GeoNetwork.get(); Configuration config = reader.getConfiguration(); Account account=config.getScopeConfiguration().getAccounts().get(accountType); // Configuration config = gnInstance.getGeonetworkPublisher().getConfiguration(); // Account account = config.getScopeConfiguration().getAccounts().get(Type.CKAN); //System.out.println("User: "+account.getUser()+", Pwd: "+account.getPassword()); System.out.println("Admin: "+config.getAdminAccount().getUser()+", Pwd: "+config.getAdminAccount().getPassword()); System.out.println("USER: "+account.getUser()+", Pwd: "+account.getPassword()); System.out.println("GN: "+config.getGeoNetworkEndpoint()); try{ String decryptedPassword = StringEncrypter.getEncrypter().decrypt(account.getPassword()); System.out.println("Decrypted Password: "+decryptedPassword); }catch(Exception e){ System.out.println("ignoring exception during pwd decrypting"); } // req.addParam("keyword", "Thredds"); final GNSearchRequest req=new GNSearchRequest(); // req.addParam(GNSearchRequest.Param.any,"Thredds"); GNSearchResponse resp = reader.query(req); int publicCount=resp.getCount(); System.out.println("Public UUIDs are: "+publicCount); Map publicUUIDs = getUUIDs(resp); System.out.println("Filled list of Public UUIDs with: "+publicUUIDs.size()); System.out.println("Public layers are: "); printLayers(publicUUIDs); reader.login(loginLevel); resp = reader.query(req); int totalCount=resp.getCount(); System.out.println("All UUIDs are: "+totalCount); Map allUUIDs = getUUIDs(resp); System.out.println("Filled list of All UUIDs with: "+allUUIDs.size()); System.out.println("SCOPE "+scope+" found "+totalCount+" (public : "+publicCount+", private :"+(totalCount-publicCount)+")"); System.out.println("Private layers are: "); allUUIDs.keySet().removeAll(publicUUIDs.keySet()); printLayers(allUUIDs); /*if(totalCount==0) return; try{ int last = totalCount layersUUIDs) { int index = 0; for (Long key : layersUUIDs.keySet()) { index++; System.out.println(index+") ID: "+key+" - UUID: "+layersUUIDs.get(key)); } } /** * Gets the UUI ds. * * @param resp the resp * @return the UUI ds */ public static Map getUUIDs(GNSearchResponse resp){ Map listUUIDs = new HashMap(); Iterator iterator = resp.iterator(); while(iterator.hasNext()) { GNMetadata meta = iterator.next(); //listUUIDs.add(new GN_META_REF(meta.getId(), meta.getUUID())); listUUIDs.put(meta.getId(), meta.getUUID()); //System.out.println("Added: "+meta.getUUID()); } return listUUIDs; } /** * Gets the layers by search. * * @return the layers by search * @throws Exception the exception */ //@Test public void getLayersBySearch() throws Exception{ try{ for(String scope:scopes){ ScopeProvider.instance.set(scope); GeoNetworkPublisher reader=GeoNetwork.get(); Configuration config = reader.getConfiguration(); Account account=config.getScopeConfiguration().getAccounts().get(accountType); //System.out.println("User: "+account.getUser()+", Pwd: "+account.getPassword()); System.out.println("Admin: "+config.getAdminAccount().getUser()+", Pwd: "+config.getAdminAccount().getPassword()); try{ String decryptedPassword = StringEncrypter.getEncrypter().decrypt(account.getPassword()); System.out.println("Decrypted Password: "+decryptedPassword); }catch(Exception e){ System.out.println("ignoring exception during pwd decrypting"); } // req.addParam("keyword", "Thredds"); //final GNSearchRequest req=new GNSearchRequest(); final GNSearchRequest req = getRequest(true, textToSearch); // req.addParam(GNSearchRequest.Param.any,"Thredds"); GNSearchResponse resp = reader.query(req); int publicCount=resp.getCount(); reader.login(loginLevel); int totalCount=reader.query(req).getCount(); System.out.println("QUERY "+textToSearch); System.out.println("SCOPE "+scope+" found "+totalCount+" (public : "+publicCount+", private :"+(totalCount-publicCount)+")"); // if(totalCount==0) // return; // try{ // int last = totalCount>MAX?totalCount:MAX; // // for(int i=0; i