2017-01-24 17:24:22 +01:00
package org.gcube.datacatalogue.ckanutillibrary.server ;
2016-06-07 16:00:05 +02:00
2016-08-04 18:24:39 +02:00
import static com.google.common.base.Preconditions.checkArgument ;
import static com.google.common.base.Preconditions.checkNotNull ;
2016-11-23 12:52:08 +01:00
import java.io.File ;
2016-12-16 18:25:38 +01:00
import java.math.BigDecimal ;
2016-12-06 19:01:21 +01:00
import java.net.URLEncoder ;
2016-11-23 12:52:08 +01:00
import java.nio.charset.Charset ;
2016-06-07 16:00:05 +02:00
import java.sql.Connection ;
2016-07-04 16:44:41 +02:00
import java.sql.DriverManager ;
2016-06-07 16:00:05 +02:00
import java.sql.PreparedStatement ;
import java.sql.ResultSet ;
2016-06-17 22:17:42 +02:00
import java.sql.SQLException ;
2016-06-07 16:00:05 +02:00
import java.util.ArrayList ;
2017-05-15 11:46:51 +02:00
import java.util.Arrays ;
2016-06-07 16:00:05 +02:00
import java.util.HashMap ;
2016-12-02 22:42:17 +01:00
import java.util.HashSet ;
2016-06-14 12:31:19 +02:00
import java.util.Iterator ;
2016-06-07 16:00:05 +02:00
import java.util.List ;
2017-05-15 11:46:51 +02:00
import java.util.ListIterator ;
2016-06-07 16:00:05 +02:00
import java.util.Map ;
2016-06-14 12:31:19 +02:00
import java.util.Map.Entry ;
2016-12-02 22:42:17 +01:00
import java.util.Set ;
2016-10-14 21:23:47 +02:00
import java.util.concurrent.ConcurrentHashMap ;
2016-06-14 12:31:19 +02:00
2018-01-12 17:05:56 +01:00
import org.apache.solr.client.solrj.SolrQuery ;
import org.apache.solr.client.solrj.impl.HttpSolrServer ;
import org.apache.solr.client.solrj.response.FacetField ;
import org.apache.solr.client.solrj.response.FacetField.Count ;
import org.apache.solr.client.solrj.response.QueryResponse ;
2018-02-23 14:20:17 +01:00
import org.gcube.common.scope.impl.ScopeBean ;
import org.gcube.datacatalogue.ckanutillibrary.server.utils.CatalogueUtilMethods ;
2017-01-24 17:24:22 +01:00
import org.gcube.datacatalogue.ckanutillibrary.server.utils.url.EntityContext ;
2017-03-08 16:35:56 +01:00
import org.gcube.datacatalogue.ckanutillibrary.shared.CKanUserWrapper ;
import org.gcube.datacatalogue.ckanutillibrary.shared.CkanDatasetRelationship ;
import org.gcube.datacatalogue.ckanutillibrary.shared.DatasetRelationships ;
2018-01-12 17:11:54 +01:00
import org.gcube.datacatalogue.ckanutillibrary.shared.LandingPages ;
2017-03-08 16:35:56 +01:00
import org.gcube.datacatalogue.ckanutillibrary.shared.ResourceBean ;
2017-01-24 17:24:22 +01:00
import org.gcube.datacatalogue.ckanutillibrary.shared.RolesCkanGroupOrOrg ;
2017-03-08 16:35:56 +01:00
import org.gcube.datacatalogue.ckanutillibrary.shared.State ;
2018-01-12 17:05:56 +01:00
import org.gcube.datacatalogue.ckanutillibrary.shared.Statistics ;
2016-08-04 18:24:39 +02:00
import org.json.simple.JSONArray ;
import org.json.simple.JSONObject ;
2016-11-24 17:55:00 +01:00
import org.json.simple.JSONValue ;
2016-08-04 18:24:39 +02:00
import org.json.simple.parser.JSONParser ;
2016-06-07 16:00:05 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import eu.trentorise.opendata.jackan.CkanClient ;
2017-11-02 10:23:12 +01:00
import eu.trentorise.opendata.jackan.CkanQuery ;
2016-09-26 18:34:48 +02:00
import eu.trentorise.opendata.jackan.exceptions.JackanException ;
2016-11-23 12:52:08 +01:00
import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpEntity ;
2016-06-10 16:26:01 +02:00
import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpResponse ;
import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpStatus ;
2016-10-11 10:05:12 +02:00
import eu.trentorise.opendata.jackan.internal.org.apache.http.client.methods.HttpGet ;
2016-06-10 16:26:01 +02:00
import eu.trentorise.opendata.jackan.internal.org.apache.http.client.methods.HttpPost ;
2016-11-23 12:52:08 +01:00
import eu.trentorise.opendata.jackan.internal.org.apache.http.entity.ContentType ;
2016-06-10 16:26:01 +02:00
import eu.trentorise.opendata.jackan.internal.org.apache.http.entity.StringEntity ;
2016-11-23 12:52:08 +01:00
import eu.trentorise.opendata.jackan.internal.org.apache.http.entity.mime.MultipartEntityBuilder ;
2016-06-10 16:26:01 +02:00
import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.CloseableHttpClient ;
import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.HttpClientBuilder ;
2016-11-24 17:55:00 +01:00
import eu.trentorise.opendata.jackan.internal.org.apache.http.util.EntityUtils ;
2016-06-14 12:31:19 +02:00
import eu.trentorise.opendata.jackan.model.CkanDataset ;
2016-09-26 18:34:48 +02:00
import eu.trentorise.opendata.jackan.model.CkanGroup ;
2016-06-07 16:00:05 +02:00
import eu.trentorise.opendata.jackan.model.CkanLicense ;
import eu.trentorise.opendata.jackan.model.CkanOrganization ;
2016-06-14 12:31:19 +02:00
import eu.trentorise.opendata.jackan.model.CkanPair ;
import eu.trentorise.opendata.jackan.model.CkanResource ;
import eu.trentorise.opendata.jackan.model.CkanTag ;
2016-06-14 17:05:24 +02:00
import eu.trentorise.opendata.jackan.model.CkanUser ;
2019-04-08 11:49:49 +02:00
import net.htmlparser.jericho.Renderer ;
import net.htmlparser.jericho.Segment ;
import net.htmlparser.jericho.Source ;
2016-06-07 16:00:05 +02:00
/ * *
* This is the Ckan Utils implementation class .
2016-06-14 12:31:19 +02:00
* @author Costantino Perciante at ISTI - CNR ( costantino . perciante @isti.cnr.it )
2016-06-07 16:00:05 +02:00
* /
2017-03-08 16:35:56 +01:00
@SuppressWarnings ( " unchecked " )
2016-09-28 14:49:40 +02:00
public class DataCatalogueImpl implements DataCatalogue {
2016-06-07 16:00:05 +02:00
2016-09-28 14:49:40 +02:00
private static final Logger logger = LoggerFactory . getLogger ( DataCatalogueImpl . class ) ;
2016-06-07 16:00:05 +02:00
private String CKAN_CATALOGUE_URL ;
private String CKAN_DB_NAME ;
private String CKAN_DB_USER ;
private String CKAN_DB_PASSWORD ;
2016-06-10 14:15:38 +02:00
private String CKAN_DB_URL ;
2016-06-07 16:00:05 +02:00
private Integer CKAN_DB_PORT ;
2016-06-29 11:44:24 +02:00
private String PORTLET_URL_FOR_SCOPE ;
2018-01-12 17:05:56 +01:00
private String SOLR_URL ;
2016-06-16 18:09:26 +02:00
private String CKAN_TOKEN_SYS ;
2018-01-24 18:45:20 +01:00
private String CKAN_EMAIL ;
2016-12-06 19:01:21 +01:00
private String URI_RESOLVER_URL ;
2016-11-29 10:28:50 +01:00
private boolean MANAGE_PRODUCT_BUTTON ;
2018-09-19 10:10:23 +02:00
private boolean SOCIAL_POST ;
2017-05-08 11:42:44 +02:00
private boolean ALERT_USERS_ON_POST_CREATION ;
2016-12-08 18:26:38 +01:00
private String CONTEXT ;
2017-09-18 18:43:31 +02:00
private Map < String , String > extendRoleInOrganization ;
2016-06-07 16:00:05 +02:00
2017-03-08 16:35:56 +01:00
private final static String PATH_SET_PRIVATE_DATASET = " /api/3/action/bulk_update_private " ;
private final static String PATH_SET_PUBLIC_DATASET = " /api/3/action/bulk_update_public " ;
2018-02-23 14:20:17 +01:00
private static final String CATALOGUE_TAB_ENDING_URL = " /catalogue " ;
2017-03-08 16:35:56 +01:00
2016-06-18 17:12:15 +02:00
// ckan client
private CkanClient client ;
2016-06-07 16:00:05 +02:00
2016-10-14 21:23:47 +02:00
// hashmap for ckan api keys
2016-10-22 18:07:17 +02:00
private ConcurrentHashMap < String , CKANTokenBean > apiKeysMap ;
// apikey bean expires after X minutes in the above map
2017-03-08 16:35:56 +01:00
private static final int EXPIRE_KEY_TIME = 60 * 60 * 1000 ;
2016-10-22 18:07:17 +02:00
private class CKANTokenBean {
private String apiKey ;
private long timestamp ;
public CKANTokenBean ( String apiKey , long timestamp ) {
super ( ) ;
this . apiKey = apiKey ;
this . timestamp = timestamp ;
}
}
2016-10-14 21:23:47 +02:00
2016-06-17 22:17:42 +02:00
/ * *
* The ckan catalogue url and database will be discovered in this scope
* @param scope
* @throws Exception if unable to find datacatalogue info
* /
2016-09-28 14:49:40 +02:00
public DataCatalogueImpl ( String scope ) throws Exception {
2016-06-14 12:31:19 +02:00
2016-09-28 14:49:40 +02:00
DataCatalogueRunningCluster runningInstance = new DataCatalogueRunningCluster ( scope ) ;
2016-06-18 16:59:07 +02:00
2016-06-17 22:17:42 +02:00
// save information
2016-10-06 14:40:37 +02:00
CKAN_DB_URL = runningInstance . getDatabaseHosts ( ) . get ( 0 ) . trim ( ) ;
CKAN_DB_NAME = runningInstance . getDataBaseName ( ) . trim ( ) ;
CKAN_DB_USER = runningInstance . getDataBaseUser ( ) . trim ( ) ;
CKAN_DB_PASSWORD = runningInstance . getDataBasePassword ( ) . trim ( ) ;
CKAN_TOKEN_SYS = runningInstance . getSysAdminToken ( ) . trim ( ) ;
2018-01-24 18:45:20 +01:00
CKAN_EMAIL = runningInstance . getEmailCatalogue ( ) . trim ( ) ;
2016-06-16 18:09:26 +02:00
CKAN_DB_PORT = runningInstance . getDatabasePorts ( ) . get ( 0 ) ;
2016-10-06 14:40:37 +02:00
CKAN_CATALOGUE_URL = runningInstance . getDataCatalogueUrl ( ) . get ( 0 ) . trim ( ) ;
PORTLET_URL_FOR_SCOPE = runningInstance . getPortletUrl ( ) . trim ( ) ;
2016-11-29 10:28:50 +01:00
MANAGE_PRODUCT_BUTTON = runningInstance . isManageProductEnabled ( ) ;
2016-12-06 19:01:21 +01:00
URI_RESOLVER_URL = runningInstance . getUrlResolver ( ) ;
2018-09-19 10:10:23 +02:00
SOCIAL_POST = runningInstance . isSocialPostEnabled ( ) ;
2017-05-08 11:42:44 +02:00
ALERT_USERS_ON_POST_CREATION = runningInstance . isAlertEnabled ( ) ;
2018-01-12 17:05:56 +01:00
SOLR_URL = runningInstance . getUrlSolr ( ) ;
2016-06-18 16:59:07 +02:00
2016-06-26 21:59:26 +02:00
logger . debug ( " Plain sys admin token first 3 chars are " + CKAN_TOKEN_SYS . substring ( 0 , 3 ) ) ;
logger . debug ( " Plain db password first 3 chars are " + CKAN_DB_PASSWORD . substring ( 0 , 3 ) ) ;
2016-06-18 17:12:15 +02:00
// build the client
2018-06-27 12:19:32 +02:00
client = new CkanClient ( CKAN_CATALOGUE_URL ) ;
2016-06-16 18:09:26 +02:00
2016-10-14 21:23:47 +02:00
// init map
2016-10-22 18:07:17 +02:00
apiKeysMap = new ConcurrentHashMap < String , CKANTokenBean > ( ) ;
2016-12-07 18:32:07 +01:00
2016-12-06 19:01:21 +01:00
// save the context
2016-12-08 18:26:38 +01:00
CONTEXT = scope ;
2017-09-19 11:04:32 +02:00
2017-09-18 18:43:31 +02:00
// extended roles
extendRoleInOrganization = runningInstance . getExtendRoleInOrganization ( ) ;
2016-06-17 22:17:42 +02:00
}
2016-06-18 16:59:07 +02:00
2016-12-06 19:01:21 +01:00
@Override
public String getCatalogueUrl ( ) {
return CKAN_CATALOGUE_URL ;
}
@Override
public String getPortletUrl ( ) {
2018-11-09 11:35:22 +01:00
/ *
2018-02-23 14:20:17 +01:00
ScopeBean context = new ScopeBean ( CONTEXT ) ;
String buildedUrl = PORTLET_URL_FOR_SCOPE . endsWith ( " / " ) ? PORTLET_URL_FOR_SCOPE : PORTLET_URL_FOR_SCOPE + " / " ;
buildedUrl + = context . name ( ) . toLowerCase ( ) + CATALOGUE_TAB_ENDING_URL ;
return buildedUrl ;
2018-11-09 11:35:22 +01:00
* /
2018-11-09 11:52:17 +01:00
//PATCHED By Francesco
2018-11-09 11:35:22 +01:00
ScopeBean context = new ScopeBean ( CONTEXT ) ;
2018-11-09 11:52:17 +01:00
String vreNameLower = context . name ( ) . toLowerCase ( ) ;
2018-11-09 11:35:22 +01:00
//CHECKING IF THE PORTLET URL CONTAINS THE VRE NAME INTO URL
2018-11-09 11:52:17 +01:00
if ( PORTLET_URL_FOR_SCOPE . toLowerCase ( ) . contains ( vreNameLower ) ) {
//THE PORLTET URL READ FROM GENERIC RESOUCE 'CkanPortlet' SHOULD BE ALREADY VALID, POITING TO CKAN PORTLET
2018-11-09 11:35:22 +01:00
return PORTLET_URL_FOR_SCOPE ;
} else {
//ADDING VRE NAME AND THE SUFFIX 'CATALOGUE_TAB_ENDING_URL' TO URL
String buildedUrl = PORTLET_URL_FOR_SCOPE . endsWith ( " / " ) ? PORTLET_URL_FOR_SCOPE : PORTLET_URL_FOR_SCOPE + " / " ;
2018-11-09 11:56:42 +01:00
String defaultSuffix = vreNameLower + CATALOGUE_TAB_ENDING_URL ;
buildedUrl + = defaultSuffix ;
logger . warn ( " The Portlet URL read from Generic Resource 'Ckan-Porltet' does not contain the portlet suffix added the default: " + defaultSuffix ) ;
2018-11-09 11:35:22 +01:00
return buildedUrl ;
}
2016-12-06 19:01:21 +01:00
}
@Override
public String getUriResolverUrl ( ) {
return URI_RESOLVER_URL ;
}
2017-03-08 16:35:56 +01:00
/ * *
* Check if the manage product is enabled
* @return
* /
@Override
public boolean isManageProductEnabled ( ) {
return MANAGE_PRODUCT_BUTTON ;
}
2016-06-17 22:17:42 +02:00
/ * *
* Retrieve connection from the pool
2016-06-18 16:59:07 +02:00
* @return a connection available within the pool
2018-06-27 12:04:03 +02:00
* @throws SQLException
* @throws ClassNotFoundException
2016-06-17 22:17:42 +02:00
* /
2016-07-04 16:44:41 +02:00
private Connection getConnection ( ) throws SQLException , ClassNotFoundException {
2016-06-18 16:59:07 +02:00
2016-06-20 12:13:12 +02:00
logger . debug ( " CONNECTION REQUEST " ) ;
2016-07-04 16:44:41 +02:00
// create db connection
Class . forName ( " org.postgresql.Driver " ) ;
Connection connection = DriverManager . getConnection (
" jdbc:postgresql:// " + CKAN_DB_URL + " : " + CKAN_DB_PORT + " / " + CKAN_DB_NAME , CKAN_DB_USER , CKAN_DB_PASSWORD ) ;
return connection ;
2016-06-20 11:29:58 +02:00
}
2016-11-30 10:33:58 +01:00
2016-06-20 11:29:58 +02:00
/ * *
* Tries to close a connection
* @param connection
* /
private void closeConnection ( Connection connection ) {
if ( connection ! = null ) {
try {
connection . close ( ) ;
} catch ( Exception e ) {
logger . error ( " Unable to close this connection " , e ) ;
}
2016-06-26 21:59:26 +02:00
}
2016-06-18 16:59:07 +02:00
2016-06-07 16:00:05 +02:00
}
@Override
2016-06-16 18:09:26 +02:00
public String getApiKeyFromUsername ( String username ) {
2016-06-14 17:05:24 +02:00
2016-06-17 22:17:42 +02:00
logger . debug ( " Request api key for user = " + username ) ;
2016-06-18 16:59:07 +02:00
2016-08-04 18:24:39 +02:00
// checks
checkNotNull ( username ) ;
checkArgument ( ! username . isEmpty ( ) ) ;
2016-06-14 12:31:19 +02:00
// in order to avoid errors, the username is always converted
2018-02-23 14:20:17 +01:00
String ckanUsername = CatalogueUtilMethods . fromUsernameToCKanUsername ( username ) ;
2016-06-14 17:05:24 +02:00
2016-10-14 21:23:47 +02:00
// check in the hashmap first
2016-10-22 18:07:17 +02:00
if ( apiKeysMap . containsKey ( ckanUsername ) ) {
CKANTokenBean bean = apiKeysMap . get ( ckanUsername ) ;
2018-06-27 12:04:03 +02:00
if ( bean . timestamp + EXPIRE_KEY_TIME > System . currentTimeMillis ( ) ) { // it's still ok
2016-10-22 18:07:17 +02:00
return bean . apiKey ;
2016-11-15 10:24:29 +01:00
}
2016-10-22 18:07:17 +02:00
}
2016-11-17 12:25:43 +01:00
2016-11-15 10:24:29 +01:00
logger . debug ( " Api key was not in cache or it expired " ) ;
2016-11-17 12:25:43 +01:00
2016-10-22 18:07:17 +02:00
// the connection
Connection connection = null ;
String apiToReturn = null ;
2016-06-20 16:05:29 +02:00
2016-10-22 18:07:17 +02:00
try {
2016-06-20 16:05:29 +02:00
2016-10-22 18:07:17 +02:00
connection = getConnection ( ) ;
2016-06-07 16:00:05 +02:00
2016-10-22 18:07:17 +02:00
String query = " SELECT \" apikey \" FROM \" user \" WHERE \" name \" =? and \" state \" =?; " ;
PreparedStatement preparedStatement = connection . prepareStatement ( query ) ;
preparedStatement . setString ( 1 , ckanUsername ) ;
preparedStatement . setString ( 2 , State . ACTIVE . toString ( ) . toLowerCase ( ) ) ;
2016-09-14 18:00:10 +02:00
2016-10-22 18:07:17 +02:00
ResultSet rs = preparedStatement . executeQuery ( ) ;
while ( rs . next ( ) ) {
apiToReturn = rs . getString ( " apikey " ) ;
logger . debug ( " Api key retrieved for user " + ckanUsername ) ;
break ;
}
2016-06-20 16:05:29 +02:00
2016-10-22 18:07:17 +02:00
// save into the hashmap
if ( apiToReturn ! = null )
apiKeysMap . put ( ckanUsername , new CKANTokenBean ( apiToReturn , System . currentTimeMillis ( ) ) ) ;
2016-06-20 16:05:29 +02:00
2016-11-15 10:24:29 +01:00
return apiToReturn ;
2016-10-22 18:07:17 +02:00
} catch ( Exception e ) {
logger . error ( " Unable to retrieve key for user " + ckanUsername , e ) ;
} finally {
closeConnection ( connection ) ;
2016-06-07 16:00:05 +02:00
}
2017-03-08 16:35:56 +01:00
return null ;
2016-06-07 16:00:05 +02:00
}
@Override
public CKanUserWrapper getUserFromApiKey ( String apiKey ) {
2017-03-08 16:35:56 +01:00
2016-09-14 18:00:10 +02:00
logger . debug ( " Request user whose api key is = " + apiKey . substring ( 0 , 3 ) + " ************* " ) ;
2016-08-04 18:24:39 +02:00
// checks
checkNotNull ( apiKey ) ;
checkArgument ( ! apiKey . isEmpty ( ) ) ;
2016-09-26 18:34:48 +02:00
CKanUserWrapper user = null ;
2016-06-20 16:05:29 +02:00
2016-06-20 11:29:58 +02:00
// the connection
Connection connection = null ;
2016-06-07 16:00:05 +02:00
try {
2016-06-20 16:05:29 +02:00
2016-06-20 11:29:58 +02:00
connection = getConnection ( ) ;
2016-06-14 17:05:24 +02:00
String query = " SELECT * FROM \" user \" WHERE \" apikey \" =? and \" state \" =?; " ;
2016-06-20 11:29:58 +02:00
PreparedStatement preparedStatement = connection . prepareStatement ( query ) ;
2016-06-07 16:00:05 +02:00
preparedStatement . setString ( 1 , apiKey ) ;
2016-06-16 18:09:26 +02:00
preparedStatement . setString ( 2 , State . ACTIVE . toString ( ) . toLowerCase ( ) ) ;
2016-06-14 17:05:24 +02:00
2016-06-07 16:00:05 +02:00
ResultSet rs = preparedStatement . executeQuery ( ) ;
while ( rs . next ( ) ) {
2016-09-26 18:34:48 +02:00
user = new CKanUserWrapper ( ) ;
2016-06-07 16:00:05 +02:00
user . setId ( rs . getString ( " id " ) ) ;
user . setName ( rs . getString ( " name " ) ) ;
user . setApiKey ( apiKey ) ;
user . setCreationTimestamp ( rs . getTimestamp ( " created " ) . getTime ( ) ) ;
user . setAbout ( rs . getString ( " about " ) ) ;
user . setOpenId ( rs . getString ( " openid " ) ) ;
user . setFullName ( rs . getString ( " fullname " ) ) ;
user . setEmail ( rs . getString ( " email " ) ) ;
user . setAdmin ( rs . getBoolean ( " sysadmin " ) ) ;
2016-06-16 18:09:26 +02:00
logger . debug ( " User retrieved " ) ;
2016-06-07 16:00:05 +02:00
break ;
}
} catch ( Exception e ) {
2016-06-13 14:18:37 +02:00
logger . error ( " Unable to retrieve user with api key " + apiKey , e ) ;
2016-06-20 11:29:58 +02:00
} finally {
closeConnection ( connection ) ;
2016-06-07 16:00:05 +02:00
}
return user ;
}
@Override
public List < CkanOrganization > getOrganizationsByUser ( String username ) {
logger . debug ( " Requested organizations for user " + username ) ;
2016-06-14 17:05:24 +02:00
2016-08-04 18:24:39 +02:00
// checks
checkNotNull ( username ) ;
2016-06-14 12:31:19 +02:00
// in order to avoid errors, the username is always converted
2018-02-23 14:20:17 +01:00
String ckanUsername = CatalogueUtilMethods . fromUsernameToCKanUsername ( username ) ;
2016-06-14 17:05:24 +02:00
2016-06-07 16:00:05 +02:00
// list to return
List < CkanOrganization > toReturn = new ArrayList < CkanOrganization > ( ) ;
try {
2016-06-20 16:05:29 +02:00
2016-06-26 21:59:26 +02:00
// get the list of all organizations
List < CkanOrganization > organizations = client . getOrganizationList ( ) ;
// iterate over them
for ( CkanOrganization ckanOrganization : organizations ) {
2016-06-27 10:04:07 +02:00
// get the list of users in it (if you try ckanOrganization.getUsers() it returns null.. maybe a bug TODO)
List < CkanUser > users = client . getOrganization ( ckanOrganization . getName ( ) ) . getUsers ( ) ;
2016-06-26 21:59:26 +02:00
// check if the current user is among them
for ( CkanUser ckanUser : users ) {
if ( ckanUser . getName ( ) . equals ( ckanUsername ) ) {
logger . debug ( " User " + ckanUsername + " is into " + ckanOrganization . getName ( ) ) ;
toReturn . add ( ckanOrganization ) ;
break ;
}
2016-06-07 16:00:05 +02:00
}
}
} catch ( Exception e ) {
logger . error ( " Unable to get user's organizations " , e ) ;
}
return toReturn ;
}
2016-11-23 12:52:08 +01:00
2016-11-21 09:50:43 +01:00
@Override
public List < CkanGroup > getGroupsByUser ( String username ) {
logger . debug ( " Requested groups for user " + username ) ;
// checks
checkNotNull ( username ) ;
// in order to avoid errors, the username is always converted
2018-02-23 14:20:17 +01:00
String ckanUsername = CatalogueUtilMethods . fromUsernameToCKanUsername ( username ) ;
2016-11-21 09:50:43 +01:00
// list to return
List < CkanGroup > toReturn = new ArrayList < CkanGroup > ( ) ;
try {
// get the list of all organizations
List < CkanGroup > groups = client . getGroupList ( ) ;
// iterate over them
for ( CkanGroup ckanGroup : groups ) {
List < CkanUser > users = client . getGroup ( ckanGroup . getName ( ) ) . getUsers ( ) ;
// check if the current user is among them
for ( CkanUser ckanUser : users ) {
if ( ckanUser . getName ( ) . equals ( ckanUsername ) ) {
logger . debug ( " User " + ckanUsername + " is into " + ckanGroup . getName ( ) ) ;
toReturn . add ( ckanGroup ) ;
break ;
}
}
}
} catch ( Exception e ) {
2016-11-21 10:17:31 +01:00
logger . error ( " Unable to get user's groups " , e ) ;
2016-11-21 09:50:43 +01:00
}
return toReturn ;
}
2016-06-07 16:00:05 +02:00
@Override
2016-09-28 11:40:12 +02:00
public Map < String , List < RolesCkanGroupOrOrg > > getOrganizationsAndRolesByUser (
String username , List < RolesCkanGroupOrOrg > rolesToMatch ) {
2016-06-07 16:00:05 +02:00
2016-08-04 18:24:39 +02:00
// checks
checkNotNull ( username ) ;
checkArgument ( ! rolesToMatch . isEmpty ( ) ) ;
2016-06-18 16:59:07 +02:00
logger . debug ( " Requested roles that the user " + username + " has into his organizations " ) ;
2016-06-07 16:00:05 +02:00
logger . debug ( " Roles to check are " + rolesToMatch ) ;
2016-09-28 11:40:12 +02:00
Map < String , List < RolesCkanGroupOrOrg > > toReturn = new HashMap < String , List < RolesCkanGroupOrOrg > > ( ) ;
2016-06-20 11:29:58 +02:00
2016-06-07 16:00:05 +02:00
try {
2016-06-18 16:59:07 +02:00
// in order to avoid errors, the username is always converted
2018-02-23 14:20:17 +01:00
String ckanUsername = CatalogueUtilMethods . fromUsernameToCKanUsername ( username ) ;
2016-06-18 16:59:07 +02:00
2016-06-26 21:59:26 +02:00
// get the list of organizations in which the user is present
List < CkanOrganization > organizationsByUser = getOrganizationsByUser ( ckanUsername ) ;
// iterate over them
for ( CkanOrganization ckanOrganization : organizationsByUser ) {
2016-06-27 10:04:07 +02:00
// get the list of users in it (if you try ckanOrganization.getUsers() it returns null.. maybe a bug TODO)
List < CkanUser > users = client . getOrganization ( ckanOrganization . getName ( ) ) . getUsers ( ) ;
2016-06-26 21:59:26 +02:00
// check if the current user is among them
for ( CkanUser ckanUser : users ) {
if ( ckanUser . getName ( ) . equals ( ckanUsername ) ) {
2016-06-07 16:00:05 +02:00
2016-06-26 21:59:26 +02:00
// the role (admin, editor, member)
String capacity = ckanUser . getCapacity ( ) ;
// list of roles
2016-09-28 11:40:12 +02:00
List < RolesCkanGroupOrOrg > rolesIntoOrg = new ArrayList < RolesCkanGroupOrOrg > ( ) ;
2016-06-26 21:59:26 +02:00
2016-11-17 14:03:33 +01:00
if ( rolesToMatch . contains ( RolesCkanGroupOrOrg . convertFromCapacity ( capacity ) ) ) {
RolesCkanGroupOrOrg enumRole = RolesCkanGroupOrOrg . convertFromCapacity ( capacity ) ;
2016-06-26 21:59:26 +02:00
rolesIntoOrg . add ( enumRole ) ;
2018-06-27 12:04:03 +02:00
logger . debug ( " User " + ckanUsername + " has role " + enumRole +
2016-06-26 21:59:26 +02:00
" into organization with name " + ckanOrganization . getName ( ) ) ;
}
// save it
if ( ! rolesIntoOrg . isEmpty ( ) ) {
String orgName = ckanOrganization . getName ( ) ;
toReturn . put ( orgName , rolesIntoOrg ) ;
}
break ;
}
2016-06-17 22:42:31 +02:00
}
2016-06-07 16:00:05 +02:00
}
2016-06-20 11:29:58 +02:00
2016-06-20 10:30:18 +02:00
logger . debug ( " Result is " + toReturn ) ;
2016-06-18 16:59:07 +02:00
return toReturn ;
2016-06-07 16:00:05 +02:00
} catch ( Exception e ) {
logger . error ( " Unable to analyze user's roles " , e ) ;
}
2016-06-18 16:59:07 +02:00
return null ;
2016-06-07 16:00:05 +02:00
}
2016-06-18 10:12:47 +02:00
@Override
public List < String > getOrganizationsIds ( ) {
2016-06-16 18:09:26 +02:00
2016-06-07 16:00:05 +02:00
List < String > toReturn = new ArrayList < String > ( ) ;
2016-06-16 18:09:26 +02:00
List < CkanOrganization > orgs = client . getOrganizationList ( ) ;
2016-06-07 16:00:05 +02:00
2016-06-16 18:09:26 +02:00
for ( CkanOrganization ckanOrganization : orgs ) {
logger . debug ( " Retrieved org " + ckanOrganization . getName ( ) ) ;
toReturn . add ( ckanOrganization . getId ( ) ) ;
2016-06-07 16:00:05 +02:00
}
2016-06-16 18:09:26 +02:00
return toReturn ;
2016-06-10 14:15:38 +02:00
}
2016-06-18 16:59:07 +02:00
2016-06-18 10:12:47 +02:00
@Override
public List < String > getOrganizationsNames ( ) {
List < String > toReturn = new ArrayList < String > ( ) ;
List < CkanOrganization > orgs = client . getOrganizationList ( ) ;
for ( CkanOrganization ckanOrganization : orgs ) {
logger . debug ( " Retrieved org " + ckanOrganization . getName ( ) ) ;
toReturn . add ( ckanOrganization . getName ( ) ) ;
}
return toReturn ;
}
2016-06-10 16:26:01 +02:00
2016-06-07 16:00:05 +02:00
@Override
public List < String > getOrganizationsNamesByUser ( String username ) {
2016-06-14 17:05:24 +02:00
2016-06-14 12:31:19 +02:00
logger . debug ( " Requested organizations for user " + username ) ;
2016-06-14 17:05:24 +02:00
2016-08-04 18:24:39 +02:00
// checks
checkNotNull ( username ) ;
2016-06-14 12:31:19 +02:00
// in order to avoid errors, the username is always converted
2018-02-23 14:20:17 +01:00
String ckanUsername = CatalogueUtilMethods . fromUsernameToCKanUsername ( username ) ;
2016-06-14 17:05:24 +02:00
2016-06-14 12:31:19 +02:00
List < CkanOrganization > orgs = getOrganizationsByUser ( ckanUsername ) ;
2016-06-17 22:17:42 +02:00
2016-06-07 16:00:05 +02:00
List < String > orgsName = new ArrayList < String > ( ) ;
2016-06-26 21:59:26 +02:00
2016-06-07 16:00:05 +02:00
for ( CkanOrganization ckanOrganization : orgs ) {
orgsName . add ( ckanOrganization . getName ( ) ) ;
logger . debug ( " Organization name is " + ckanOrganization . getName ( ) ) ;
}
return orgsName ;
2016-06-08 18:31:25 +02:00
2016-06-07 16:00:05 +02:00
}
@Override
2016-09-12 10:39:49 +02:00
public String findLicenseIdByLicenseTitle ( String chosenLicense ) {
2016-06-07 16:00:05 +02:00
logger . debug ( " Requested license id " ) ;
2016-08-04 18:24:39 +02:00
// checks
checkNotNull ( chosenLicense ) ;
2016-06-07 16:00:05 +02:00
//retrieve the list of available licenses
List < CkanLicense > licenses = client . getLicenseList ( ) ;
for ( CkanLicense ckanLicense : licenses ) {
if ( ckanLicense . getTitle ( ) . equals ( chosenLicense ) )
return ckanLicense . getId ( ) ;
}
return null ;
}
@Override
public List < String > getLicenseTitles ( ) {
2016-06-14 12:31:19 +02:00
logger . debug ( " Request for CKAN licenses " ) ;
2016-06-07 16:00:05 +02:00
// get the url and the api key of the user
List < String > result = new ArrayList < String > ( ) ;
2016-06-20 11:29:58 +02:00
2016-06-07 16:00:05 +02:00
//retrieve the list of available licenses
List < CkanLicense > licenses = client . getLicenseList ( ) ;
for ( CkanLicense ckanLicense : licenses ) {
result . add ( ckanLicense . getTitle ( ) ) ;
logger . debug ( " License is " + ckanLicense . getTitle ( ) + " and id " + ckanLicense . getId ( ) ) ;
}
2016-06-08 18:31:25 +02:00
2016-06-07 16:00:05 +02:00
return result ;
}
2016-07-21 22:09:25 +02:00
2016-07-13 16:21:24 +02:00
@Override
public List < CkanLicense > getLicenses ( ) {
logger . debug ( " Request for CKAN licenses (original jackan objects are going to be retrieved) " ) ;
//retrieve the list of available licenses
2016-09-28 11:40:12 +02:00
return client . getLicenseList ( ) ;
2016-07-13 16:21:24 +02:00
}
2016-06-10 14:15:38 +02:00
2016-09-28 11:40:12 +02:00
/ * *
2017-03-08 16:35:56 +01:00
* Set dataset private / public
2016-09-28 11:40:12 +02:00
* @param priv
* @param organizationId ( NOTE : The ID , not the name ! )
* @param datasetId ( NOTE : The ID , not the name ! )
* @param apiKey the user ' s api key
* @return true on success , false otherwise
* /
protected boolean setDatasetPrivate ( boolean priv , String organizationId ,
2016-06-16 18:09:26 +02:00
String datasetId , String apiKey ) {
2016-06-10 16:26:01 +02:00
2016-08-04 18:24:39 +02:00
// checks
checkNotNull ( organizationId ) ;
checkNotNull ( apiKey ) ;
checkNotNull ( datasetId ) ;
checkArgument ( ! apiKey . isEmpty ( ) ) ;
checkArgument ( ! datasetId . isEmpty ( ) ) ;
checkArgument ( ! organizationId . isEmpty ( ) ) ;
2017-03-08 16:35:56 +01:00
JSONObject obj = new JSONObject ( ) ;
obj . put ( " org_id " , organizationId ) ;
2016-06-14 17:05:24 +02:00
2017-03-08 16:35:56 +01:00
JSONArray array = new JSONArray ( ) ;
array . add ( datasetId ) ;
obj . put ( " datasets " , array ) ;
2016-06-10 16:26:01 +02:00
2017-03-08 16:35:56 +01:00
try ( CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . build ( ) ; ) {
HttpPost request ;
2016-06-10 16:26:01 +02:00
2017-03-08 16:35:56 +01:00
if ( priv )
request = new HttpPost ( CKAN_CATALOGUE_URL + PATH_SET_PRIVATE_DATASET ) ;
else
request = new HttpPost ( CKAN_CATALOGUE_URL + PATH_SET_PUBLIC_DATASET ) ;
2016-06-10 16:26:01 +02:00
2017-03-08 16:35:56 +01:00
logger . info ( " Excuting request for making dataset with id " + datasetId + " " + ( priv ? " private " : " public " ) ) ;
2016-06-10 16:26:01 +02:00
2017-03-08 16:35:56 +01:00
request . addHeader ( " Authorization " , apiKey ) ;
StringEntity params = new StringEntity ( obj . toJSONString ( ) ) ;
request . setEntity ( params ) ;
HttpResponse response = httpClient . execute ( request ) ;
2016-09-14 18:00:10 +02:00
2017-03-08 16:35:56 +01:00
logger . info ( " Response code is " + response . getStatusLine ( ) . getStatusCode ( ) + " and response message is " + response . getStatusLine ( ) . getReasonPhrase ( ) ) ;
2016-09-14 18:00:10 +02:00
2017-03-08 16:35:56 +01:00
if ( response . getStatusLine ( ) . getStatusCode ( ) = = HttpStatus . SC_OK )
return true ;
} catch ( Exception ex ) {
logger . error ( " Error while trying to set private the dataset " , ex ) ;
2016-09-14 18:00:10 +02:00
}
2016-06-10 16:26:01 +02:00
2017-03-08 16:35:56 +01:00
2016-06-10 16:26:01 +02:00
return false ;
}
2016-06-14 12:31:19 +02:00
@Override
2017-04-05 15:59:48 +02:00
public String addResourceToDataset ( ResourceBean resourceBean , String apiKey ) throws Exception {
2016-06-14 12:31:19 +02:00
logger . debug ( " Request to add a resource described by this bean " + resourceBean ) ;
2016-08-04 18:24:39 +02:00
// checks
checkNotNull ( resourceBean ) ;
checkNotNull ( apiKey ) ;
checkArgument ( ! apiKey . isEmpty ( ) ) ;
2018-02-23 14:20:17 +01:00
if ( CatalogueUtilMethods . resourceExists ( resourceBean . getUrl ( ) ) ) {
2016-06-14 17:05:24 +02:00
2017-04-05 15:59:48 +02:00
// in order to avoid errors, the username is always converted
2018-02-23 14:20:17 +01:00
String ckanUsername = CatalogueUtilMethods . fromUsernameToCKanUsername ( resourceBean . getOwner ( ) ) ;
2016-06-14 17:05:24 +02:00
2017-04-05 15:59:48 +02:00
CkanResource resource = new CkanResource ( CKAN_CATALOGUE_URL , resourceBean . getDatasetId ( ) ) ;
resource . setName ( resourceBean . getName ( ) ) ;
2016-06-14 12:31:19 +02:00
2017-04-05 15:59:48 +02:00
// escape description
Source description = new Source ( resourceBean . getDescription ( ) ) ;
Segment htmlSeg = new Segment ( description , 0 , description . length ( ) ) ;
Renderer htmlRend = new Renderer ( htmlSeg ) ;
2016-06-14 12:31:19 +02:00
2017-04-05 15:59:48 +02:00
resource . setDescription ( htmlRend . toString ( ) ) ;
resource . setUrl ( resourceBean . getUrl ( ) ) ;
resource . setOwner ( ckanUsername ) ;
2016-06-14 12:31:19 +02:00
2017-04-05 15:59:48 +02:00
// Checked client
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , apiKey ) ;
2017-04-05 15:59:48 +02:00
CkanResource createdRes = client . createResource ( resource ) ;
2016-06-14 12:31:19 +02:00
2017-04-05 15:59:48 +02:00
if ( createdRes ! = null ) {
2016-06-14 12:31:19 +02:00
2017-04-05 15:59:48 +02:00
logger . debug ( " Resource " + createdRes . getName ( ) + " is now available " ) ;
2016-06-14 12:31:19 +02:00
2017-04-05 15:59:48 +02:00
return createdRes . getId ( ) ;
}
} else
throw new Exception ( " It seems there is no is no resource at this url " + resourceBean . getUrl ( ) ) ;
2016-06-14 12:31:19 +02:00
return null ;
}
@Override
2016-06-16 18:09:26 +02:00
public boolean deleteResourceFromDataset ( String resourceId , String apiKey ) {
2016-06-14 17:05:24 +02:00
2016-06-16 18:09:26 +02:00
logger . error ( " Request to delete a resource with id " + resourceId + " coming by user with key " + apiKey ) ;
2016-06-14 12:31:19 +02:00
2016-08-04 18:24:39 +02:00
// checks
checkNotNull ( apiKey ) ;
checkNotNull ( resourceId ) ;
checkArgument ( ! apiKey . isEmpty ( ) ) ;
checkArgument ( ! resourceId . isEmpty ( ) ) ;
2016-06-14 12:31:19 +02:00
try {
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , apiKey ) ;
2016-06-14 12:31:19 +02:00
client . deleteResource ( resourceId ) ;
return true ;
} catch ( Exception e ) {
logger . error ( " Unable to delete resource whose id is " + resourceId , e ) ;
}
return false ;
}
@Override
2016-07-14 10:45:24 +02:00
public String createCKanDataset ( String apiKey ,
2016-11-04 11:13:18 +01:00
String title , String name , String organizationNameOrId , String author ,
2016-06-14 12:31:19 +02:00
String authorMail , String maintainer , String maintainerMail ,
long version , String description , String licenseId ,
List < String > tags , Map < String , String > customFields ,
2017-03-08 16:35:56 +01:00
List < ResourceBean > resources , boolean setPublic ) throws Exception {
2016-06-14 12:31:19 +02:00
2016-10-24 10:40:27 +02:00
// delegate the private method
return createCkanDatasetBody ( apiKey ,
2016-11-04 11:13:18 +01:00
title , name , organizationNameOrId , author ,
2016-10-24 10:40:27 +02:00
authorMail , maintainer , maintainerMail ,
version , description , licenseId ,
tags , customFields , null ,
resources , setPublic ) ;
}
@Override
public String createCKanDatasetMultipleCustomFields ( String apiKey ,
2016-11-04 11:13:18 +01:00
String title , String name , String organizationNameOrId , String author ,
2016-10-24 10:40:27 +02:00
String authorMail , String maintainer , String maintainerMail ,
long version , String description , String licenseId ,
List < String > tags , Map < String , List < String > > customFieldsMultiple ,
2017-03-08 16:35:56 +01:00
List < ResourceBean > resources , boolean setPublic ) throws Exception {
2016-10-24 10:40:27 +02:00
// delegate the private method
return createCkanDatasetBody ( apiKey ,
2016-11-04 11:13:18 +01:00
title , name , organizationNameOrId , author ,
2016-10-24 10:40:27 +02:00
authorMail , maintainer , maintainerMail ,
version , description , licenseId ,
tags , null , customFieldsMultiple ,
resources , setPublic ) ;
}
// the body of the actual dataset creation methods
private String createCkanDatasetBody ( String apiKey ,
2016-11-04 11:13:18 +01:00
String title , String name , String organizationNameOrId , String author ,
2016-10-24 10:40:27 +02:00
String authorMail , String maintainer , String maintainerMail ,
long version , String description , String licenseId ,
List < String > tags , Map < String , String > customFields ,
Map < String , List < String > > customFieldsMultipleValues ,
2017-03-08 16:35:56 +01:00
List < ResourceBean > resources , boolean setPublic ) throws Exception {
2016-10-24 10:40:27 +02:00
2016-08-04 18:24:39 +02:00
// checks (minimum)
checkNotNull ( apiKey ) ;
checkNotNull ( organizationNameOrId ) ;
checkArgument ( ! apiKey . isEmpty ( ) ) ;
checkArgument ( ! organizationNameOrId . isEmpty ( ) ) ;
2018-06-27 12:04:03 +02:00
checkArgument ( ! ( title = = null & & name = = null | | title . isEmpty ( ) & & name . isEmpty ( ) ) , " Name and Title cannot be empty/null at the same time! " ) ;
2016-08-04 18:24:39 +02:00
2016-06-14 12:31:19 +02:00
logger . debug ( " Request for dataset creation " ) ;
2016-06-14 17:05:24 +02:00
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , apiKey ) ;
2016-06-14 12:31:19 +02:00
2016-06-16 18:09:26 +02:00
String ckanUsername = getUserFromApiKey ( apiKey ) . getName ( ) ;
2016-06-14 12:31:19 +02:00
CkanDataset dataset = new CkanDataset ( ) ;
2016-11-14 18:07:47 +01:00
2016-11-04 11:13:18 +01:00
String nameToUse = name ;
if ( nameToUse = = null )
2018-02-23 14:20:17 +01:00
nameToUse = CatalogueUtilMethods . fromProductTitleToName ( title ) ;
2016-11-14 18:07:47 +01:00
2016-11-11 19:27:41 +01:00
logger . debug ( " Name of the dataset is going to be " + nameToUse + " . Title is going to be " + title ) ;
2017-03-08 16:35:56 +01:00
2016-11-04 11:13:18 +01:00
dataset . setName ( nameToUse ) ;
2016-06-14 12:31:19 +02:00
dataset . setTitle ( title ) ;
CkanOrganization orgOwner = client . getOrganization ( organizationNameOrId ) ;
dataset . setOwnerOrg ( orgOwner . getId ( ) ) ;
dataset . setAuthor ( author ) ;
dataset . setAuthorEmail ( authorMail ) ;
dataset . setMaintainer ( maintainer ) ;
dataset . setMaintainerEmail ( maintainerMail ) ;
dataset . setVersion ( String . valueOf ( version ) ) ;
// description must be escaped
2016-09-09 16:38:05 +02:00
if ( description ! = null & & ! description . isEmpty ( ) ) {
Source descriptionEscaped = new Source ( description ) ;
Segment htmlSeg = new Segment ( descriptionEscaped , 0 , descriptionEscaped . length ( ) ) ;
Renderer htmlRend = new Renderer ( htmlSeg ) ;
dataset . setNotes ( htmlRend . toString ( ) ) ;
2016-06-14 12:31:19 +02:00
2017-03-08 16:35:56 +01:00
logger . debug ( " Description escaped " + htmlRend . toString ( ) ) ;
2016-09-09 16:38:05 +02:00
}
2016-06-14 12:31:19 +02:00
dataset . setLicenseId ( licenseId ) ;
// set the tags, if any
if ( tags ! = null & & ! tags . isEmpty ( ) ) {
List < CkanTag > ckanTags = new ArrayList < CkanTag > ( tags . size ( ) ) ;
for ( String stringTag : tags ) {
ckanTags . add ( new CkanTag ( stringTag ) ) ;
}
dataset . setTags ( ckanTags ) ;
}
// set the custom fields, if any
2017-03-08 16:35:56 +01:00
List < CkanPair > extras = new ArrayList < CkanPair > ( ) ;
2016-06-14 12:31:19 +02:00
if ( customFields ! = null & & ! customFields . isEmpty ( ) ) {
Iterator < Entry < String , String > > iterator = customFields . entrySet ( ) . iterator ( ) ;
while ( iterator . hasNext ( ) ) {
2018-06-27 12:04:03 +02:00
Map . Entry < String , String > entry = iterator . next ( ) ;
2016-06-14 12:31:19 +02:00
extras . add ( new CkanPair ( entry . getKey ( ) , entry . getValue ( ) ) ) ;
}
2016-10-24 10:40:27 +02:00
} else if ( customFieldsMultipleValues ! = null & & ! customFieldsMultipleValues . isEmpty ( ) ) {
Iterator < Entry < String , List < String > > > iterator = customFieldsMultipleValues . entrySet ( ) . iterator ( ) ;
while ( iterator . hasNext ( ) ) {
2018-06-27 12:04:03 +02:00
Map . Entry < String , List < String > > entry = iterator . next ( ) ;
2016-10-24 10:40:27 +02:00
List < String > valuesForEntry = entry . getValue ( ) ;
for ( String value : valuesForEntry ) {
extras . add ( new CkanPair ( entry . getKey ( ) , value ) ) ;
}
}
2017-03-08 16:35:56 +01:00
}
2016-10-24 10:40:27 +02:00
2017-03-08 16:35:56 +01:00
dataset . setExtras ( extras ) ;
2016-06-14 12:31:19 +02:00
// check if we need to add the resources
if ( resources ! = null & & ! resources . isEmpty ( ) ) {
logger . debug ( " We need to add resources to the dataset " ) ;
try {
List < CkanResource > resourcesCkan = new ArrayList < CkanResource > ( ) ;
for ( ResourceBean resource : resources ) {
2017-07-04 15:19:02 +02:00
logger . debug ( " Going to add resource described by " + resource ) ;
2016-06-14 12:31:19 +02:00
CkanResource newResource = new CkanResource ( ) ;
newResource . setDescription ( resource . getDescription ( ) ) ;
newResource . setId ( resource . getId ( ) ) ;
newResource . setUrl ( resource . getUrl ( ) ) ;
newResource . setName ( resource . getName ( ) ) ;
newResource . setMimetype ( resource . getMimeType ( ) ) ;
2017-07-04 15:19:02 +02:00
newResource . setFormat ( resource . getMimeType ( ) ) ;
2016-06-14 12:31:19 +02:00
newResource . setOwner ( ckanUsername ) ;
resourcesCkan . add ( newResource ) ;
}
// add to the dataset
dataset . setResources ( resourcesCkan ) ;
} catch ( Exception e ) {
logger . error ( " Unable to add those resources to the dataset " , e ) ;
}
}
// try to create
CkanDataset res = null ;
2017-04-05 14:38:14 +02:00
res = client . createDataset ( dataset ) ;
2016-06-14 12:31:19 +02:00
2017-04-05 14:38:14 +02:00
if ( res ! = null ) {
logger . debug ( " Dataset with name " + res . getName ( ) + " has been created. Setting visibility " ) ;
2016-06-14 12:31:19 +02:00
2017-04-05 14:38:14 +02:00
// set visibility
boolean visibilitySet = setDatasetPrivate (
2018-06-27 12:04:03 +02:00
! setPublic , // swap to private
res . getOrganization ( ) . getId ( ) ,
res . getId ( ) ,
2017-04-05 14:38:14 +02:00
CKAN_TOKEN_SYS ) ; // use sysadmin api key to be sure it will be set
2016-09-14 18:00:10 +02:00
2017-04-05 14:38:14 +02:00
logger . info ( " Was visibility set to " + ( setPublic ? " public " : " private " ) + " ? " + visibilitySet ) ;
2016-10-28 10:55:13 +02:00
2017-04-05 14:38:14 +02:00
// set searchable to true if dataset visibility is private
if ( ! setPublic ) { // (swap to private)
boolean searchableSet = setSearchableField ( res . getId ( ) , true ) ;
logger . info ( " Was searchable set to True? " + searchableSet ) ;
2016-06-14 12:31:19 +02:00
}
2017-04-05 14:38:14 +02:00
return res . getId ( ) ;
2016-06-14 12:31:19 +02:00
}
2017-04-05 15:59:48 +02:00
2016-06-14 12:31:19 +02:00
return null ;
}
2017-07-12 16:44:02 +02:00
@Override
public String updateCKanDataset ( String apiKey ,
String id , String title , String name , String organizationNameOrId ,
String author , String authorMail , String maintainer ,
String maintainerMail , long version , String description ,
String licenseId , List < String > tags , List < String > groupNames ,
Map < String , List < String > > customFields ,
List < ResourceBean > resources , boolean setPublic ) throws Exception {
// checks (minimum)
checkNotNull ( apiKey ) ;
checkNotNull ( id ) ;
checkNotNull ( organizationNameOrId ) ;
checkArgument ( ! apiKey . isEmpty ( ) ) ;
checkArgument ( ! organizationNameOrId . isEmpty ( ) ) ;
checkArgument ( ! id . isEmpty ( ) ) ;
2018-06-27 12:04:03 +02:00
checkArgument ( ! ( title = = null & & name = = null | | title . isEmpty ( ) & & name . isEmpty ( ) ) , " Name and Title cannot be empty/null at the same time! " ) ;
2017-07-12 16:44:02 +02:00
logger . debug ( " Request for dataset update " ) ;
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , apiKey ) ;
2017-07-12 16:44:02 +02:00
String ckanUsername = getUserFromApiKey ( apiKey ) . getName ( ) ;
// get dataset from id
CkanDataset dataset = client . getDataset ( id ) ;
CkanOrganization orgOwner = client . getOrganization ( organizationNameOrId ) ;
dataset . setOwnerOrg ( orgOwner . getId ( ) ) ;
dataset . setAuthor ( author ) ;
dataset . setAuthorEmail ( authorMail ) ;
dataset . setMaintainer ( maintainer ) ;
dataset . setMaintainerEmail ( maintainerMail ) ;
dataset . setVersion ( String . valueOf ( version ) ) ;
2019-03-28 17:00:46 +01:00
dataset . setTitle ( title ) ;
2017-07-12 16:44:02 +02:00
// description must be escaped
dataset . setNotes ( " " ) ;
if ( description ! = null & & ! description . isEmpty ( ) ) {
Source descriptionEscaped = new Source ( description ) ;
Segment htmlSeg = new Segment ( descriptionEscaped , 0 , descriptionEscaped . length ( ) ) ;
Renderer htmlRend = new Renderer ( htmlSeg ) ;
dataset . setNotes ( htmlRend . toString ( ) ) ;
}
2017-07-13 17:31:25 +02:00
// update license id
2017-07-12 16:44:02 +02:00
dataset . setLicenseId ( licenseId ) ;
// remove any tags
dataset . setTags ( new ArrayList < CkanTag > ( 0 ) ) ;
// set the tags, if any
if ( tags ! = null & & ! tags . isEmpty ( ) ) {
List < CkanTag > ckanTags = new ArrayList < CkanTag > ( tags . size ( ) ) ;
for ( String stringTag : tags ) {
ckanTags . add ( new CkanTag ( stringTag ) ) ;
}
dataset . setTags ( ckanTags ) ;
}
// remove extras
dataset . setExtras ( new ArrayList < CkanPair > ( 0 ) ) ;
if ( customFields ! = null & & ! customFields . isEmpty ( ) ) {
2017-09-19 11:04:32 +02:00
2017-07-13 17:31:25 +02:00
logger . debug ( " Provided custom fields are " + customFields ) ;
2017-09-19 11:04:32 +02:00
2017-07-12 16:44:02 +02:00
// set the custom fields, if any
List < CkanPair > extras = new ArrayList < CkanPair > ( ) ;
Iterator < Entry < String , List < String > > > iterator = customFields . entrySet ( ) . iterator ( ) ;
while ( iterator . hasNext ( ) ) {
2018-06-27 12:04:03 +02:00
Map . Entry < String , List < String > > entry = iterator . next ( ) ;
2017-07-12 16:44:02 +02:00
List < String > valuesForEntry = entry . getValue ( ) ;
for ( String value : valuesForEntry ) {
extras . add ( new CkanPair ( entry . getKey ( ) , value ) ) ;
}
}
dataset . setExtras ( extras ) ;
}
// remove resources
dataset . setResources ( new ArrayList < CkanResource > ( 0 ) ) ;
// check if we need to add the resources
if ( resources ! = null & & ! resources . isEmpty ( ) ) {
try {
List < CkanResource > resourcesCkan = new ArrayList < CkanResource > ( ) ;
for ( ResourceBean resource : resources ) {
logger . trace ( " Going to add resource described by " + resource ) ;
CkanResource newResource = new CkanResource ( ) ;
newResource . setDescription ( resource . getDescription ( ) ) ;
newResource . setId ( resource . getId ( ) ) ;
newResource . setUrl ( resource . getUrl ( ) ) ;
newResource . setName ( resource . getName ( ) ) ;
newResource . setMimetype ( resource . getMimeType ( ) ) ;
newResource . setFormat ( resource . getMimeType ( ) ) ;
newResource . setOwner ( ckanUsername ) ;
resourcesCkan . add ( newResource ) ;
}
// add to the dataset
dataset . setResources ( resourcesCkan ) ;
} catch ( Exception e ) {
logger . error ( " Unable to add those resources to the dataset " , e ) ;
}
}
// remove groups
dataset . setGroups ( new ArrayList < CkanGroup > ( 0 ) ) ;
// add groups if any
if ( groupNames ! = null ) {
List < CkanGroup > groups = new ArrayList < CkanGroup > ( ) ;
for ( String groupName : groupNames ) {
CkanGroup group = client . getGroup ( groupName ) ;
groups . add ( group ) ;
}
dataset . setGroups ( groups ) ;
}
// try to update
CkanDataset updated = client . updateDataset ( dataset ) ;
// reset visibility and searchability
if ( updated ! = null ) {
logger . debug ( " Dataset with name " + updated . getName ( ) + " has been updated. Setting visibility " ) ;
// set visibility
boolean visibilitySet = setDatasetPrivate (
2018-06-27 12:04:03 +02:00
! setPublic , // swap to private
updated . getOrganization ( ) . getId ( ) ,
updated . getId ( ) ,
2017-07-12 16:44:02 +02:00
CKAN_TOKEN_SYS ) ; // use sysadmin api key to be sure it will be set
logger . info ( " Was visibility set to " + ( setPublic ? " public " : " private " ) + " ? " + visibilitySet ) ;
2018-06-27 12:04:03 +02:00
if ( ! setPublic ) {
2017-07-12 16:44:02 +02:00
boolean searchableSet = setSearchableField ( updated . getId ( ) , true ) ;
logger . info ( " Was searchable set to True? " + searchableSet ) ;
}
return updated . getId ( ) ;
}
return null ;
}
2016-06-14 12:31:19 +02:00
@Override
2016-12-06 19:01:21 +01:00
public String getUrlFromDatasetIdOrName ( String datasetIdOrName ) {
2016-06-14 12:31:19 +02:00
2016-12-19 17:01:05 +01:00
logger . debug ( " Request coming for getting dataset url (encrypted) of dataset with name/id " + datasetIdOrName ) ;
2016-06-14 17:05:24 +02:00
2016-08-04 18:24:39 +02:00
// checks
checkNotNull ( datasetIdOrName ) ;
checkArgument ( ! datasetIdOrName . isEmpty ( ) ) ;
2016-12-07 11:22:48 +01:00
String url = null ;
2016-06-14 12:31:19 +02:00
try {
2016-06-26 21:59:26 +02:00
// get the dataset from name
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , CKAN_TOKEN_SYS ) ;
2016-06-14 12:31:19 +02:00
CkanDataset dataset = client . getDataset ( datasetIdOrName ) ;
2016-12-06 19:01:21 +01:00
String name = dataset . getName ( ) ;
2016-06-14 12:31:19 +02:00
if ( dataset ! = null ) {
2016-12-07 18:32:07 +01:00
2016-12-06 19:01:21 +01:00
if ( getUriResolverUrl ( ) ! = null )
2019-04-08 11:49:49 +02:00
url = getUrlForProduct ( CONTEXT , EntityContext . PRODUCT , name ) ;
2016-12-19 17:01:05 +01:00
if ( url = = null | | url . isEmpty ( ) )
url = getPortletUrl ( ) + " ? " + URLEncoder . encode ( " path=/dataset/ " + name , " UTF-8 " ) ;
}
} catch ( Exception e ) {
logger . error ( " Error while retrieving dataset with id/name= " + datasetIdOrName , e ) ;
}
return url ;
}
@Override
public String getUnencryptedUrlFromDatasetIdOrName ( String datasetIdOrName ) {
logger . debug ( " Request coming for getting dataset url (not encrypted) of dataset with name/id " + datasetIdOrName ) ;
// checks
checkNotNull ( datasetIdOrName ) ;
checkArgument ( ! datasetIdOrName . isEmpty ( ) ) ;
String url = null ;
try {
// get the dataset from name
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , CKAN_TOKEN_SYS ) ;
2016-12-19 17:01:05 +01:00
CkanDataset dataset = client . getDataset ( datasetIdOrName ) ;
String name = dataset . getName ( ) ;
if ( dataset ! = null ) {
if ( getUriResolverUrl ( ) ! = null )
2019-04-08 11:49:49 +02:00
url = getUrlForProduct ( CONTEXT , EntityContext . PRODUCT , name ) ;
2016-12-06 19:01:21 +01:00
2016-12-07 11:22:48 +01:00
if ( url = = null | | url . isEmpty ( ) )
url = getPortletUrl ( ) + " ? " + URLEncoder . encode ( " path=/dataset/ " + name , " UTF-8 " ) ;
2016-12-07 18:32:07 +01:00
2016-06-14 12:31:19 +02:00
}
} catch ( Exception e ) {
logger . error ( " Error while retrieving dataset with id/name= " + datasetIdOrName , e ) ;
2019-04-08 11:49:49 +02:00
} //requestEntity.put("clear_url", Boolean.toString(unencrypted));
2016-12-07 11:22:48 +01:00
return url ;
2016-06-14 12:31:19 +02:00
}
2016-12-07 18:32:07 +01:00
2016-12-06 19:01:21 +01:00
/ * *
* Retrieve an url for the tuple scope , entity , entity name
* @param context
* @param entityContext
* @param entityName
* /
2019-04-08 11:49:49 +02:00
private String getUrlForProduct ( String context , EntityContext entityContext , String entityName ) {
2016-12-06 19:01:21 +01:00
String toReturn = null ;
try ( CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . build ( ) ) {
HttpPost httpPostRequest = new HttpPost ( getUriResolverUrl ( ) ) ;
2016-12-07 18:32:07 +01:00
2016-12-06 19:01:21 +01:00
JSONObject requestEntity = new JSONObject ( ) ;
requestEntity . put ( " gcube_scope " , context ) ;
requestEntity . put ( " entity_context " , entityContext . toString ( ) ) ;
requestEntity . put ( " entity_name " , entityName ) ;
StringEntity params = new StringEntity ( requestEntity . toJSONString ( ) , ContentType . APPLICATION_JSON ) ;
httpPostRequest . setEntity ( params ) ;
HttpResponse response = httpClient . execute ( httpPostRequest ) ;
2016-12-07 18:32:07 +01:00
2016-12-06 19:01:21 +01:00
if ( response . getStatusLine ( ) . getStatusCode ( ) ! = 200 )
throw new Exception ( " There was an error while creating an url " + response . getStatusLine ( ) ) ;
2016-12-07 18:32:07 +01:00
2016-12-07 11:22:48 +01:00
toReturn = EntityUtils . toString ( response . getEntity ( ) ) ;
logger . debug ( " Result is " + toReturn ) ;
2016-12-06 19:01:21 +01:00
} catch ( Exception e ) {
logger . error ( " Failed to get an url for this product " , e ) ;
}
return toReturn ;
}
2016-06-14 17:05:24 +02:00
@Override
2016-09-28 11:40:12 +02:00
public boolean checkRoleIntoOrganization ( String username , String organizationName ,
RolesCkanGroupOrOrg correspondentRoleToCheck ) {
2016-06-16 18:09:26 +02:00
2016-09-28 11:40:12 +02:00
logger . debug ( " Request for checking if " + username + " into organization " + organizationName + " has role " + correspondentRoleToCheck ) ;
2016-06-16 18:09:26 +02:00
2016-08-04 18:24:39 +02:00
// checks
checkNotNull ( username ) ;
checkNotNull ( organizationName ) ;
checkNotNull ( correspondentRoleToCheck ) ;
checkArgument ( ! username . isEmpty ( ) ) ;
checkArgument ( ! organizationName . isEmpty ( ) ) ;
2016-06-16 18:09:26 +02:00
// convert ckan username
2018-02-23 14:20:17 +01:00
String ckanUsername = CatalogueUtilMethods . fromUsernameToCKanUsername ( username ) ;
2016-06-16 18:09:26 +02:00
2016-06-18 16:59:07 +02:00
// check if this role is already present in ckan for this user within the organization
2016-09-28 11:40:12 +02:00
String organizationNameToCheck = organizationName . toLowerCase ( ) ;
2016-07-21 22:09:25 +02:00
2016-09-15 17:41:33 +02:00
try {
2016-09-28 11:40:12 +02:00
boolean alreadyPresent = isRoleAlreadySet ( ckanUsername , organizationNameToCheck , correspondentRoleToCheck , false ) ;
2016-06-16 18:09:26 +02:00
2016-09-15 17:41:33 +02:00
if ( alreadyPresent )
return true ; // just return
else {
2016-06-16 18:09:26 +02:00
2016-09-15 17:41:33 +02:00
// we need to use the apis to make it
String path = " /api/3/action/organization_member_create " ;
2016-06-16 18:09:26 +02:00
2017-03-08 16:35:56 +01:00
JSONObject obj = new JSONObject ( ) ;
obj . put ( " id " , organizationNameToCheck ) ;
obj . put ( " username " , ckanUsername ) ;
obj . put ( " role " , RolesCkanGroupOrOrg . convertToCkanCapacity ( correspondentRoleToCheck ) ) ;
2016-06-16 18:09:26 +02:00
2017-03-08 16:35:56 +01:00
logger . debug ( " API request for organization membership is going to be " + obj . toJSONString ( ) ) ;
2016-06-18 16:59:07 +02:00
2016-09-15 17:41:33 +02:00
try ( CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . build ( ) ; ) {
HttpPost request = new HttpPost ( CKAN_CATALOGUE_URL + path ) ;
request . addHeader ( " Authorization " , CKAN_TOKEN_SYS ) ; // sys token
2017-03-08 16:35:56 +01:00
StringEntity params = new StringEntity ( obj . toJSONString ( ) ) ;
2016-09-15 17:41:33 +02:00
request . setEntity ( params ) ;
HttpResponse response = httpClient . execute ( request ) ;
logger . debug ( " Response code is " + response . getStatusLine ( ) . getStatusCode ( ) + " and response message is " + response . getStatusLine ( ) . getReasonPhrase ( ) ) ;
2016-06-16 18:09:26 +02:00
2018-06-27 12:04:03 +02:00
return response . getStatusLine ( ) . getStatusCode ( ) = = HttpStatus . SC_OK ;
2016-06-18 16:59:07 +02:00
2016-09-15 17:41:33 +02:00
} catch ( Exception ex ) {
logger . error ( " Error while trying to change the role for this user " , ex ) ;
}
2016-06-18 16:59:07 +02:00
}
2016-09-15 17:41:33 +02:00
} catch ( Exception ex ) {
logger . error ( " Unable to check if this role was already set, please check your parameters! " , ex ) ;
2016-06-18 16:59:07 +02:00
}
2016-09-15 17:41:33 +02:00
2016-06-18 16:59:07 +02:00
return false ;
}
/ * *
2016-09-28 11:40:12 +02:00
* Check if the user has this role into the organization / group with groupOrOrganization name
2016-06-18 16:59:07 +02:00
* @param ckanUsername
* @param organizationName
* @param correspondentRoleToCheck
* @return true if he has the role , false otherwise
* /
2016-09-28 11:40:12 +02:00
protected boolean isRoleAlreadySet ( String ckanUsername , String groupOrOrganization , RolesCkanGroupOrOrg correspondentRoleToCheck , boolean group ) throws Exception {
2016-06-20 16:05:29 +02:00
2016-09-15 17:41:33 +02:00
// get the users (if you try ckanOrganization.getUsers() it returns null.. maybe a bug TODO)
2016-09-28 11:40:12 +02:00
List < CkanUser > users ;
if ( group )
users = client . getGroup ( groupOrOrganization ) . getUsers ( ) ;
else
users = client . getOrganization ( groupOrOrganization ) . getUsers ( ) ;
2016-06-18 16:59:07 +02:00
2016-09-15 17:41:33 +02:00
for ( CkanUser ckanUser : users ) {
2016-09-28 11:40:12 +02:00
if ( ckanUser . getName ( ) . equals ( ckanUsername ) )
2016-11-17 14:03:33 +01:00
if ( ckanUser . getCapacity ( ) . equals ( RolesCkanGroupOrOrg . convertToCkanCapacity ( correspondentRoleToCheck ) ) )
2016-09-28 11:40:12 +02:00
return true ;
else
break ;
2016-06-16 18:09:26 +02:00
}
2016-06-18 10:05:10 +02:00
return false ;
2016-06-14 17:05:24 +02:00
}
@Override
2016-09-28 11:40:12 +02:00
public boolean checkRoleIntoGroup ( String username , String groupName , RolesCkanGroupOrOrg correspondentRoleToCheck ) {
logger . debug ( " Request for checking if " + username + " into group " + groupName + " has role " + correspondentRoleToCheck ) ;
// checks
checkNotNull ( username ) ;
checkNotNull ( groupName ) ;
checkNotNull ( correspondentRoleToCheck ) ;
checkArgument ( ! username . isEmpty ( ) ) ;
checkArgument ( ! groupName . isEmpty ( ) ) ;
// convert ckan username
2018-02-23 14:20:17 +01:00
String ckanUsername = CatalogueUtilMethods . fromUsernameToCKanUsername ( username ) ;
2016-09-28 11:40:12 +02:00
// check if this role is already present in ckan for this user within the group
2018-02-23 14:20:17 +01:00
String groupNameToCheck = CatalogueUtilMethods . fromGroupTitleToName ( groupName ) ;
2016-09-28 11:40:12 +02:00
try {
boolean alreadyPresent = isRoleAlreadySet ( ckanUsername , groupNameToCheck , correspondentRoleToCheck , true ) ;
if ( alreadyPresent )
return true ; // just return
else {
// we need to use the apis to make it
String path = " /api/3/action/group_member_create " ;
2017-03-08 16:35:56 +01:00
JSONObject obj = new JSONObject ( ) ;
obj . put ( " id " , groupNameToCheck ) ;
obj . put ( " username " , ckanUsername ) ;
obj . put ( " role " , RolesCkanGroupOrOrg . convertToCkanCapacity ( correspondentRoleToCheck ) ) ;
2016-09-28 11:40:12 +02:00
2017-03-08 16:35:56 +01:00
logger . debug ( " API request for organization membership is going to be " + obj . toJSONString ( ) ) ;
2016-09-28 11:40:12 +02:00
try ( CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . build ( ) ; ) {
HttpPost request = new HttpPost ( CKAN_CATALOGUE_URL + path ) ;
request . addHeader ( " Authorization " , CKAN_TOKEN_SYS ) ; // sys token
2017-03-08 16:35:56 +01:00
StringEntity params = new StringEntity ( obj . toJSONString ( ) ) ;
2016-09-28 11:40:12 +02:00
request . setEntity ( params ) ;
HttpResponse response = httpClient . execute ( request ) ;
logger . debug ( " Response code is " + response . getStatusLine ( ) . getStatusCode ( ) + " and response message is " + response . getStatusLine ( ) . getReasonPhrase ( ) ) ;
2018-06-27 12:04:03 +02:00
return response . getStatusLine ( ) . getStatusCode ( ) = = HttpStatus . SC_OK ;
2016-09-28 11:40:12 +02:00
} catch ( Exception ex ) {
logger . error ( " Error while trying to change the role for this user " , ex ) ;
}
}
} catch ( Exception ex ) {
logger . error ( " Unable to check if this role was already set, please check your parameters! " , ex ) ;
}
return false ;
}
@Override
public boolean isSysAdmin ( String username ) {
2016-06-16 18:09:26 +02:00
2016-08-04 18:24:39 +02:00
// checks
checkNotNull ( username ) ;
checkArgument ( ! username . isEmpty ( ) ) ;
2016-06-18 10:05:10 +02:00
// in order to avoid errors, the username is always converted
2018-02-23 14:20:17 +01:00
String ckanUsername = CatalogueUtilMethods . fromUsernameToCKanUsername ( username ) ;
2016-06-16 18:09:26 +02:00
try {
2016-06-26 21:59:26 +02:00
return client . getUser ( ckanUsername ) . isSysadmin ( ) ;
2016-06-16 18:09:26 +02:00
} catch ( Exception e ) {
logger . error ( " Failed to check if the user " + username + " has role sysadmin " , e ) ;
}
return false ;
2016-06-14 17:05:24 +02:00
}
2016-08-04 18:24:39 +02:00
@Override
public boolean createDatasetRelationship ( String datasetIdSubject ,
String datasetIdObject , DatasetRelationships relation , String relationComment , String apiKey ) {
// checks
checkNotNull ( datasetIdSubject ) ;
checkNotNull ( datasetIdObject ) ;
checkNotNull ( relation ) ;
checkNotNull ( apiKey ) ;
checkArgument ( ! datasetIdSubject . isEmpty ( ) ) ;
checkArgument ( ! datasetIdObject . isEmpty ( ) ) ;
checkArgument ( ! apiKey . isEmpty ( ) ) ;
try ( CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . build ( ) ; ) {
// we need to use the apis to make it
String path = " /api/3/action/package_relationship_create " ;
2017-03-08 16:35:56 +01:00
JSONObject obj = new JSONObject ( ) ;
obj . put ( " subject " , datasetIdSubject ) ;
obj . put ( " object " , datasetIdObject ) ;
obj . put ( " type " , relation . toString ( ) ) ;
2016-08-04 18:24:39 +02:00
if ( relationComment ! = null & & ! relationComment . isEmpty ( ) )
2017-03-08 16:35:56 +01:00
obj . put ( " comment " , relationComment ) ;
2016-08-04 18:24:39 +02:00
2017-03-08 16:35:56 +01:00
logger . debug ( " API request for relationship create is going to be " + obj . toJSONString ( ) ) ;
2016-08-04 18:24:39 +02:00
HttpPost request = new HttpPost ( CKAN_CATALOGUE_URL + path ) ;
request . addHeader ( " Authorization " , apiKey ) ;
2017-03-08 16:35:56 +01:00
StringEntity params = new StringEntity ( obj . toJSONString ( ) ) ;
2016-08-04 18:24:39 +02:00
request . setEntity ( params ) ;
HttpResponse response = httpClient . execute ( request ) ;
logger . debug ( " Response code is " + response . getStatusLine ( ) . getStatusCode ( ) + " and response message is " + response . getStatusLine ( ) . getReasonPhrase ( ) ) ;
2018-06-27 12:04:03 +02:00
return response . getStatusLine ( ) . getStatusCode ( ) = = HttpStatus . SC_OK ;
2016-08-04 18:24:39 +02:00
} catch ( Exception e ) {
2018-06-27 12:04:03 +02:00
logger . error ( " Failed to create the relationship between dataset subject = " + datasetIdSubject
2016-08-04 18:24:39 +02:00
+ " and " + " dataset subject " + datasetIdObject , e ) ;
}
return false ;
}
@Override
public boolean deleteDatasetRelationship ( String datasetIdSubject ,
String datasetIdObject , DatasetRelationships relation , String apiKey ) {
// checks
checkNotNull ( datasetIdSubject ) ;
checkNotNull ( datasetIdObject ) ;
checkNotNull ( relation ) ;
checkNotNull ( apiKey ) ;
checkArgument ( ! datasetIdSubject . isEmpty ( ) ) ;
checkArgument ( ! datasetIdObject . isEmpty ( ) ) ;
checkArgument ( ! apiKey . isEmpty ( ) ) ;
try ( CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . build ( ) ; ) {
// we need to use the apis to make it
String path = " /api/3/action/package_relationship_delete " ;
2017-03-08 16:35:56 +01:00
JSONObject obj = new JSONObject ( ) ;
obj . put ( " subject " , datasetIdSubject ) ;
obj . put ( " object " , datasetIdObject ) ;
obj . put ( " type " , relation . toString ( ) ) ;
2016-08-04 18:24:39 +02:00
2017-03-08 16:35:56 +01:00
logger . debug ( " API request for delete relationship is going to be " + obj . toJSONString ( ) ) ;
2016-08-04 18:24:39 +02:00
HttpPost request = new HttpPost ( CKAN_CATALOGUE_URL + path ) ;
request . addHeader ( " Authorization " , apiKey ) ;
2017-03-08 16:35:56 +01:00
StringEntity params = new StringEntity ( obj . toJSONString ( ) ) ;
2016-08-04 18:24:39 +02:00
request . setEntity ( params ) ;
HttpResponse response = httpClient . execute ( request ) ;
logger . debug ( " Response code is " + response . getStatusLine ( ) . getStatusCode ( ) + " and response message is " + response . getStatusLine ( ) . getReasonPhrase ( ) ) ;
2018-06-27 12:04:03 +02:00
return response . getStatusLine ( ) . getStatusCode ( ) = = HttpStatus . SC_OK ;
2016-08-04 18:24:39 +02:00
} catch ( Exception e ) {
2018-06-27 12:04:03 +02:00
logger . error ( " Failed to delete the relationship between dataset subject = " + datasetIdSubject
2016-08-04 18:24:39 +02:00
+ " and " + " dataset subject " + datasetIdObject , e ) ;
}
return false ;
}
@Override
public List < CkanDatasetRelationship > getRelationshipDatasets (
String datasetIdSubject , String datasetIdObject , String apiKey ) {
// checks
checkNotNull ( datasetIdSubject ) ;
checkNotNull ( apiKey ) ;
checkArgument ( ! datasetIdSubject . isEmpty ( ) ) ;
checkArgument ( ! apiKey . isEmpty ( ) ) ;
try ( CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . build ( ) ; ) {
List < CkanDatasetRelationship > toReturn = new ArrayList < CkanDatasetRelationship > ( ) ;
// we need to use the apis to make it
String path = " /api/3/action/package_relationships_list " ;
2017-03-08 16:35:56 +01:00
JSONObject obj = new JSONObject ( ) ;
obj . put ( " id " , datasetIdSubject ) ;
2016-09-02 11:18:11 +02:00
2016-08-04 18:24:39 +02:00
// Request parameters to be replaced
2016-09-02 11:18:11 +02:00
if ( datasetIdObject ! = null & & ! datasetIdObject . isEmpty ( ) )
2017-03-08 16:35:56 +01:00
obj . put ( " id2 " , datasetIdObject ) ;
2016-08-04 18:24:39 +02:00
2017-03-08 16:35:56 +01:00
logger . debug ( " API request for getting relationship is going to be " + obj . toJSONString ( ) ) ;
2016-08-04 18:24:39 +02:00
HttpPost request = new HttpPost ( CKAN_CATALOGUE_URL + path ) ;
request . addHeader ( " Authorization " , apiKey ) ;
2017-03-08 16:35:56 +01:00
StringEntity params = new StringEntity ( obj . toJSONString ( ) ) ;
2016-08-04 18:24:39 +02:00
request . setEntity ( params ) ;
HttpResponse response = httpClient . execute ( request ) ;
logger . debug ( " Response code is " + response . getStatusLine ( ) . getStatusCode ( ) + " and response message is " + response . getStatusLine ( ) . getReasonPhrase ( ) ) ;
if ( response . getStatusLine ( ) . getStatusCode ( ) = = HttpStatus . SC_OK ) {
2017-03-08 16:35:56 +01:00
String res = EntityUtils . toString ( response . getEntity ( ) ) ;
2016-08-04 18:24:39 +02:00
// parse the json object returned
JSONParser parser = new JSONParser ( ) ;
JSONObject json = ( JSONObject ) parser . parse ( res ) ;
JSONArray resultJson = ( JSONArray ) json . get ( " result " ) ;
Iterator < JSONObject > it = resultJson . iterator ( ) ;
while ( it . hasNext ( ) ) {
2018-06-27 12:04:03 +02:00
JSONObject object = it . next ( ) ;
2016-08-04 18:24:39 +02:00
try {
CkanDatasetRelationship relation = new CkanDatasetRelationship ( object ) ;
toReturn . add ( relation ) ;
} catch ( Exception e ) {
logger . error ( " Error while building CkanRelationship bean from object " + object , e ) ;
}
}
}
return toReturn ;
} catch ( Exception e ) {
2018-06-27 12:04:03 +02:00
logger . error ( " Failed to retrieve the relationship between dataset subject = " + datasetIdSubject
2016-09-28 11:40:12 +02:00
+ " and " + " dataset object " + datasetIdObject , e ) ;
2016-08-04 18:24:39 +02:00
}
return null ;
}
2016-09-23 14:37:32 +02:00
@Override
2016-09-28 11:40:12 +02:00
public boolean existProductWithNameOrId ( String nameOrId ) {
2016-09-23 14:37:32 +02:00
// checks
checkNotNull ( nameOrId ) ;
checkArgument ( ! nameOrId . isEmpty ( ) ) ;
try {
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , CKAN_TOKEN_SYS ) ;
2016-09-23 14:37:32 +02:00
CkanDataset product = client . getDataset ( nameOrId ) ;
return product ! = null ;
} catch ( Exception e ) {
2016-09-28 16:48:43 +02:00
logger . debug ( " A dataset with name " + nameOrId + " doesn't exist " ) ;
2016-09-23 14:37:32 +02:00
return false ;
}
}
2016-09-26 18:34:48 +02:00
@Override
2016-09-28 11:40:12 +02:00
public CkanGroup createGroup ( String nameOrId , String title , String description ) {
2016-09-26 18:34:48 +02:00
// checks
checkNotNull ( nameOrId ) ;
2016-09-28 14:49:40 +02:00
checkArgument ( ! nameOrId . trim ( ) . isEmpty ( ) ) ;
2016-09-26 18:34:48 +02:00
// check if it exists
CkanGroup toCreate = null ;
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , CKAN_TOKEN_SYS ) ;
2016-09-28 14:49:40 +02:00
2016-09-28 11:40:12 +02:00
logger . debug ( " Request for creating group with name " + nameOrId + " title " + title + " and description " + description ) ;
2016-09-26 18:34:48 +02:00
2018-02-23 14:20:17 +01:00
String name = CatalogueUtilMethods . fromGroupTitleToName ( nameOrId ) ;
2016-09-28 14:49:40 +02:00
if ( ( toCreate = groupExists ( name , client ) ) ! = null )
2016-09-26 18:34:48 +02:00
return toCreate ;
else {
try {
2016-09-28 14:49:40 +02:00
CkanGroup group = new CkanGroup ( name ) ;
2016-09-26 18:34:48 +02:00
group . setTitle ( title ) ;
group . setDisplayName ( title ) ;
group . setDescription ( description ) ;
toCreate = client . createGroup ( group ) ;
} catch ( JackanException je ) {
2016-09-28 11:40:12 +02:00
logger . error ( " Unable to create such a group " , je ) ;
2016-09-26 18:34:48 +02:00
}
}
return toCreate ;
}
/ * *
* Just check if the group exists
* @param nameOrId
* @param client
* @return
* /
2018-06-27 12:04:03 +02:00
private CkanGroup groupExists ( String nameOrId , ExtendCkanClient client ) {
2016-09-26 18:34:48 +02:00
CkanGroup toReturn = null ;
try {
toReturn = client . getGroup ( nameOrId ) ;
} catch ( JackanException je ) {
2016-09-28 16:53:38 +02:00
logger . error ( " This group doesn't exist " ) ;
2016-09-26 18:34:48 +02:00
}
return toReturn ;
}
2016-09-28 14:49:40 +02:00
@Override
public boolean assignDatasetToGroup ( String groupNameOrId , String datasetNameOrId , String apiKey ) {
2017-05-15 11:46:51 +02:00
return assignDatasetToGroupBody ( groupNameOrId , datasetNameOrId , apiKey , false ) ;
}
@Override
public boolean assignDatasetToGroup ( String groupNameOrId , String datasetNameOrId , String apiKey , boolean addOnParents ) {
return assignDatasetToGroupBody ( groupNameOrId , datasetNameOrId , apiKey , addOnParents ) ;
}
/ * *
* Find the hierarchy of trees
* @param uniqueGroups
* @param catalogue
* @param user ' s api key
* /
private void findHierarchyGroups (
2018-06-27 12:04:03 +02:00
List < String > groupsTitles ,
2017-05-15 11:46:51 +02:00
String apiKey ) {
ListIterator < String > iterator = groupsTitles . listIterator ( ) ;
while ( iterator . hasNext ( ) ) {
2018-06-27 12:04:03 +02:00
String group = iterator . next ( ) ;
2017-05-15 11:46:51 +02:00
List < CkanGroup > parents = getParentGroups ( group , apiKey ) ;
if ( parents = = null | | parents . isEmpty ( ) )
return ;
for ( CkanGroup ckanGroup : parents ) {
List < String > parentsList = new ArrayList < String > ( Arrays . asList ( ckanGroup . getName ( ) ) ) ;
findHierarchyGroups ( parentsList , apiKey ) ;
for ( String parent : parentsList ) {
2018-06-27 12:04:03 +02:00
iterator . add ( parent ) ;
2017-05-15 11:46:51 +02:00
}
}
}
}
/ * *
* The real body of the assignDatasetToGroup
* @param groupNameOrId
* @param datasetNameOrId
* @param apiKey
* @param addOnParents
* @return
* /
private boolean assignDatasetToGroupBody ( String groupNameOrId , String datasetNameOrId , String apiKey , boolean addOnParents ) {
2016-09-28 14:49:40 +02:00
// checks
checkNotNull ( groupNameOrId ) ;
checkArgument ( ! groupNameOrId . isEmpty ( ) ) ;
checkNotNull ( datasetNameOrId ) ;
checkArgument ( ! datasetNameOrId . isEmpty ( ) ) ;
checkNotNull ( apiKey ) ;
checkArgument ( ! apiKey . isEmpty ( ) ) ;
2016-10-11 10:05:12 +02:00
2018-02-23 14:20:17 +01:00
String groupNameToCheck = CatalogueUtilMethods . fromGroupTitleToName ( groupNameOrId ) ;
2016-09-28 14:49:40 +02:00
2016-10-11 10:05:12 +02:00
try ( CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . build ( ) ; ) {
2016-12-10 10:48:33 +01:00
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , CKAN_TOKEN_SYS ) ;
2016-10-11 10:05:12 +02:00
// check the group exists
CkanGroup group = client . getGroup ( groupNameToCheck ) ;
2016-09-28 14:49:40 +02:00
2017-05-15 11:46:51 +02:00
// move to a list
List < String > groupNames = new ArrayList < String > ( ) ;
groupNames . add ( group . getName ( ) ) ;
if ( group ! = null & & addOnParents ) {
findHierarchyGroups ( groupNames , CKAN_TOKEN_SYS ) ;
}
2016-10-11 10:05:12 +02:00
// we need to use the apis to make it
String pathPackageShow = CKAN_CATALOGUE_URL + " /api/3/action/package_show?id= " + datasetNameOrId ;
HttpGet getRequest = new HttpGet ( pathPackageShow ) ;
getRequest . addHeader ( " Authorization " , CKAN_TOKEN_SYS ) ;
HttpResponse response = httpClient . execute ( getRequest ) ;
2016-09-28 14:49:40 +02:00
2016-10-11 10:05:12 +02:00
logger . debug ( " Response is " + response . getStatusLine ( ) . getStatusCode ( ) + " and message is " + response . getStatusLine ( ) . getReasonPhrase ( ) ) ;
2016-09-28 14:49:40 +02:00
2016-10-11 10:05:12 +02:00
// read the json dataset and fetch the groups and fetch the groups' names, if any
if ( response . getStatusLine ( ) . getStatusCode ( ) = = HttpStatus . SC_OK ) {
// parse the json and convert to java beans
2016-12-08 18:26:38 +01:00
String jsonAsString = EntityUtils . toString ( response . getEntity ( ) ) ;
2016-10-11 10:05:12 +02:00
JSONParser parser = new JSONParser ( ) ;
2016-12-08 18:26:38 +01:00
JSONObject json = ( JSONObject ) parser . parse ( jsonAsString ) ;
2016-10-11 10:05:12 +02:00
JSONObject resultJson = ( JSONObject ) json . get ( " result " ) ;
JSONArray groupsJson = ( JSONArray ) resultJson . get ( " groups " ) ;
Iterator < JSONObject > it = groupsJson . iterator ( ) ;
while ( it . hasNext ( ) ) {
2018-06-27 12:04:03 +02:00
JSONObject object = it . next ( ) ;
2016-10-11 10:05:12 +02:00
try {
if ( object . containsKey ( " name " ) )
2017-05-15 11:46:51 +02:00
groupNames . add ( ( String ) object . get ( " name " ) ) ;
2016-10-11 10:05:12 +02:00
} catch ( Exception e ) {
2017-05-15 11:46:51 +02:00
logger . error ( " Error " , e ) ;
2016-10-11 10:05:12 +02:00
}
}
2017-05-15 11:46:51 +02:00
// remove duplicates
Set < String > groupNamesSet = new HashSet < String > ( groupNames ) ;
2016-10-11 10:05:12 +02:00
2017-05-15 11:46:51 +02:00
logger . debug ( " Groups to be added are " + groupNamesSet ) ;
2016-10-11 10:05:12 +02:00
// now we patch the dataset with the new group
String pathUpdatePatch = CKAN_CATALOGUE_URL + " /api/3/action/package_patch " ;
2017-04-05 14:38:14 +02:00
2017-03-08 16:35:56 +01:00
JSONObject req = new JSONObject ( ) ;
req . put ( " id " , datasetNameOrId ) ;
2017-04-05 14:38:14 +02:00
2017-03-08 16:35:56 +01:00
JSONArray groups = new JSONArray ( ) ;
2017-05-15 11:46:51 +02:00
Iterator < String > iteratorNameSet = groupNamesSet . iterator ( ) ;
while ( iteratorNameSet . hasNext ( ) ) {
2018-06-27 12:04:03 +02:00
String groupName = iteratorNameSet . next ( ) ;
2017-03-08 16:35:56 +01:00
JSONObject groupJSON = new JSONObject ( ) ;
2017-05-15 11:46:51 +02:00
groupJSON . put ( " name " , groupName ) ;
2017-03-08 16:35:56 +01:00
groups . add ( groupJSON ) ;
2016-10-11 10:05:12 +02:00
}
2017-03-08 16:35:56 +01:00
req . put ( " groups " , groups ) ;
2016-10-11 10:05:12 +02:00
2017-03-08 16:35:56 +01:00
logger . debug ( " Request for patch is going to be " + req . toJSONString ( ) ) ;
2016-10-11 10:05:12 +02:00
HttpPost request = new HttpPost ( pathUpdatePatch ) ;
request . addHeader ( " Authorization " , CKAN_TOKEN_SYS ) ;
2017-03-08 16:35:56 +01:00
StringEntity params = new StringEntity ( req . toJSONString ( ) ) ;
2016-10-11 10:05:12 +02:00
request . setEntity ( params ) ;
HttpResponse responsePatch = httpClient . execute ( request ) ;
logger . debug ( " Response code is " + responsePatch . getStatusLine ( ) . getStatusCode ( ) + " and response message is " + responsePatch . getStatusLine ( ) . getReasonPhrase ( ) ) ;
if ( responsePatch . getStatusLine ( ) . getStatusCode ( ) = = HttpStatus . SC_OK ) {
logger . info ( " Dataset Added to the group!! " ) ;
return true ;
}
}
2016-09-28 14:49:40 +02:00
} catch ( Exception e ) {
logger . error ( " Unable to make this association " , e ) ;
}
2016-11-30 15:10:30 +01:00
return false ;
}
2017-04-05 14:38:14 +02:00
2016-11-30 15:10:30 +01:00
@Override
public boolean removeDatasetFromGroup ( String groupNameOrId ,
String datasetNameOrId , String apiKey ) {
// checks
checkNotNull ( groupNameOrId ) ;
checkNotNull ( datasetNameOrId ) ;
checkNotNull ( apiKey ) ;
try ( CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . build ( ) ) {
String apiRequestUrl = CKAN_CATALOGUE_URL + " /api/3/action/package_patch " ;
HttpPost httpPostRequest = new HttpPost ( apiRequestUrl ) ;
httpPostRequest . setHeader ( " Authorization " , CKAN_TOKEN_SYS ) ;
2018-06-27 12:04:03 +02:00
// Request parameters to be replaced
2016-11-30 15:10:30 +01:00
JSONObject jsonRequest = new JSONObject ( ) ;
JSONArray groupsAsJson = new JSONArray ( ) ;
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , CKAN_TOKEN_SYS ) ;
2016-11-30 15:10:30 +01:00
CkanDataset dataset = client . getDataset ( datasetNameOrId ) ;
List < CkanGroup > currentGroups = dataset . getGroups ( ) ;
2016-12-02 22:42:17 +01:00
2016-11-30 15:10:30 +01:00
// get the id of the group to be removed
String groupId = client . getGroup ( groupNameOrId ) . getId ( ) ;
for ( CkanGroup ckanGroup : currentGroups ) {
if ( ! ckanGroup . getId ( ) . equals ( groupId ) ) {
JSONObject obj = new JSONObject ( ) ;
obj . put ( " display_name " , ckanGroup . getDisplayName ( ) ) ;
obj . put ( " description " , ckanGroup . getDescription ( ) ) ;
obj . put ( " image_display_url " , ckanGroup . getImageDisplayUrl ( ) ) ;
obj . put ( " title " , ckanGroup . getTitle ( ) ) ;
obj . put ( " id " , ckanGroup . getId ( ) ) ;
obj . put ( " name " , ckanGroup . getName ( ) ) ;
groupsAsJson . add ( obj ) ;
}
}
// perform the request
jsonRequest . put ( " id " , datasetNameOrId ) ;
jsonRequest . put ( " groups " , groupsAsJson ) ;
logger . debug ( " Request param is going to be " + jsonRequest ) ;
StringEntity params = new StringEntity ( jsonRequest . toJSONString ( ) , ContentType . APPLICATION_JSON ) ;
httpPostRequest . setEntity ( params ) ;
HttpResponse response = httpClient . execute ( httpPostRequest ) ;
if ( response . getStatusLine ( ) . getStatusCode ( ) < 200 | | response . getStatusLine ( ) . getStatusCode ( ) > = 300 ) {
throw new RuntimeException ( " failed to patch the product. response status line from "
+ apiRequestUrl + " was: " + response . getStatusLine ( ) ) ;
}
return true ;
} catch ( Exception e ) {
logger . error ( " Failed to remove the group " + groupNameOrId + " from product " + datasetNameOrId , e ) ;
}
2016-12-02 22:42:17 +01:00
2016-09-28 14:49:40 +02:00
return false ;
}
2016-09-26 18:34:48 +02:00
@Override
2016-09-28 11:40:12 +02:00
public Map < String , List < String > > getRolesAndUsersOrganization ( String organizationName ) {
2016-09-26 18:34:48 +02:00
// checks
checkNotNull ( organizationName ) ;
checkArgument ( ! organizationName . isEmpty ( ) ) ;
Map < String , List < String > > capacityAndUsers = new HashMap < String , List < String > > ( ) ;
CkanOrganization org = client . getOrganization ( organizationName ) ;
List < CkanUser > users = org . getUsers ( ) ;
for ( CkanUser ckanUser : users ) {
2016-09-28 11:40:12 +02:00
2016-09-26 18:34:48 +02:00
logger . debug ( ckanUser . getName ( ) ) ;
List < String > listUsers ;
if ( capacityAndUsers . containsKey ( ckanUser . getCapacity ( ) ) ) {
listUsers = capacityAndUsers . get ( ckanUser . getCapacity ( ) ) ;
} else
listUsers = new ArrayList < String > ( ) ;
listUsers . add ( ckanUser . getName ( ) ) ;
capacityAndUsers . put ( ckanUser . getCapacity ( ) , listUsers ) ;
}
return capacityAndUsers ;
}
2016-09-28 11:40:12 +02:00
2016-09-26 18:34:48 +02:00
@Override
2016-11-16 18:00:48 +01:00
public Map < RolesCkanGroupOrOrg , List < String > > getRolesAndUsersGroup ( String groupName ) {
2016-09-26 18:34:48 +02:00
// checks
checkNotNull ( groupName ) ;
checkArgument ( ! groupName . isEmpty ( ) ) ;
2016-11-17 12:25:43 +01:00
Map < RolesCkanGroupOrOrg , List < String > > capacityAndUsers = null ;
2018-02-23 14:20:17 +01:00
String groupNameToCheck = CatalogueUtilMethods . fromGroupTitleToName ( groupName ) ;
2016-09-26 18:34:48 +02:00
2016-11-17 12:25:43 +01:00
CkanGroup group = client . getGroup ( groupNameToCheck ) ;
2016-11-23 12:52:08 +01:00
2016-11-17 12:25:43 +01:00
if ( group ! = null ) {
capacityAndUsers = new HashMap < RolesCkanGroupOrOrg , List < String > > ( ) ;
List < CkanUser > users = group . getUsers ( ) ;
for ( CkanUser ckanUser : users ) {
List < String > listUsers ;
2016-11-17 14:03:33 +01:00
if ( capacityAndUsers . containsKey ( RolesCkanGroupOrOrg . convertFromCapacity ( ckanUser . getCapacity ( ) ) ) ) {
listUsers = capacityAndUsers . get ( RolesCkanGroupOrOrg . convertFromCapacity ( ckanUser . getCapacity ( ) ) ) ;
2016-11-17 12:25:43 +01:00
} else
listUsers = new ArrayList < String > ( ) ;
2016-09-26 18:34:48 +02:00
2016-11-17 12:25:43 +01:00
listUsers . add ( ckanUser . getName ( ) ) ;
2016-11-17 14:03:33 +01:00
capacityAndUsers . put ( RolesCkanGroupOrOrg . convertFromCapacity ( ckanUser . getCapacity ( ) ) , listUsers ) ;
2016-09-26 18:34:48 +02:00
2016-11-17 12:25:43 +01:00
}
2016-11-23 12:52:08 +01:00
2016-11-17 12:25:43 +01:00
logger . info ( " Returning " + capacityAndUsers ) ;
2016-09-26 18:34:48 +02:00
}
return capacityAndUsers ;
}
2016-09-28 11:40:12 +02:00
@Override
public String getRoleOfUserInOrganization ( String username , String orgName , String apiKey ) {
String toReturn = null ;
2018-02-23 14:20:17 +01:00
String usernameCkan = CatalogueUtilMethods . fromUsernameToCKanUsername ( username ) ;
2016-09-28 11:40:12 +02:00
try {
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , apiKey ) ;
2016-09-28 11:40:12 +02:00
List < CkanUser > users = client . getOrganization ( orgName ) . getUsers ( ) ;
for ( CkanUser ckanUser : users ) {
if ( ckanUser . getName ( ) . equals ( usernameCkan ) ) {
toReturn = ckanUser . getCapacity ( ) ;
break ;
}
}
} catch ( Exception e ) {
logger . error ( " Unable to retrieve the role the user has into this organization " , e ) ;
}
return toReturn ;
}
2016-09-28 14:49:40 +02:00
@Override
public String getRoleOfUserInGroup ( String username , String groupName , String apiKey ) {
String toReturn = null ;
2018-02-23 14:20:17 +01:00
String usernameCkan = CatalogueUtilMethods . fromUsernameToCKanUsername ( username ) ;
2016-09-28 14:49:40 +02:00
try {
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , apiKey ) ;
2016-09-28 14:49:40 +02:00
List < CkanUser > users = client . getGroup ( groupName ) . getUsers ( ) ;
for ( CkanUser ckanUser : users ) {
if ( ckanUser . getName ( ) . equals ( usernameCkan ) ) {
toReturn = ckanUser . getCapacity ( ) ;
break ;
}
}
} catch ( Exception e ) {
logger . error ( " Unable to retrieve the role the user has into this group " , e ) ;
}
return toReturn ;
}
2017-04-05 14:38:14 +02:00
2016-10-14 17:10:43 +02:00
@Override
public boolean deleteProduct ( String datasetId , String apiKey , boolean purge ) {
// checks
checkNotNull ( datasetId ) ;
checkArgument ( ! datasetId . isEmpty ( ) ) ;
checkNotNull ( apiKey ) ;
checkArgument ( ! apiKey . isEmpty ( ) ) ;
logger . debug ( " Incoming request of deleting dataset with id " + datasetId ) ;
try {
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , apiKey ) ;
2016-10-14 17:10:43 +02:00
client . deleteDataset ( datasetId ) ;
logger . info ( " Dataset with id " + datasetId + " deleted! " ) ;
if ( purge ) {
logger . debug ( " Purging also .... " ) ;
try ( CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . build ( ) ; ) {
String path = CKAN_CATALOGUE_URL + " /api/3/action/dataset_purge " ;
HttpPost request = new HttpPost ( path ) ;
request . addHeader ( " Authorization " , CKAN_TOKEN_SYS ) ; // this must be a sys_admin key
2017-03-08 16:35:56 +01:00
JSONObject object = new JSONObject ( ) ;
object . put ( " id " , datasetId ) ;
StringEntity params = new StringEntity ( object . toJSONString ( ) ) ;
2016-10-14 17:10:43 +02:00
request . setEntity ( params ) ;
HttpResponse response = httpClient . execute ( request ) ;
logger . debug ( " Response code is " + response . getStatusLine ( ) . getStatusCode ( ) + " and response message is " + response . getStatusLine ( ) . getReasonPhrase ( ) ) ;
if ( response . getStatusLine ( ) . getStatusCode ( ) = = 200 ) {
logger . info ( " Dataset with id " + datasetId + " delete and purged! " ) ;
2016-10-14 17:25:58 +02:00
return true ;
2016-10-14 17:10:43 +02:00
}
2016-10-14 21:23:47 +02:00
2016-10-14 17:25:58 +02:00
return false ;
2016-10-14 17:10:43 +02:00
}
2016-10-14 17:25:58 +02:00
2016-10-14 17:10:43 +02:00
}
return true ;
} catch ( Exception e ) {
logger . error ( " Unable to delete such dataset " , e ) ;
}
return false ;
}
2018-02-08 16:57:31 +01:00
2018-01-24 21:28:42 +01:00
@Override
public CkanResource getResource ( String id , String apiKey ) {
logger . info ( " Request ckan resource with id " + id ) ;
// checks
checkNotNull ( id ) ;
checkArgument ( ! id . isEmpty ( ) ) ;
checkNotNull ( apiKey ) ;
checkArgument ( ! apiKey . isEmpty ( ) ) ;
try {
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , apiKey ) ;
2018-01-24 21:28:42 +01:00
return client . getResource ( id ) ;
} catch ( Exception e ) {
logger . error ( " Unable to retrieve such resource, returning null ... " , e ) ;
}
return null ;
}
2016-10-14 17:10:43 +02:00
@Override
public CkanDataset getDataset ( String datasetId , String apiKey ) {
logger . info ( " Request ckan dataset with id " + datasetId ) ;
// checks
checkNotNull ( datasetId ) ;
checkArgument ( ! datasetId . isEmpty ( ) ) ;
checkNotNull ( apiKey ) ;
checkArgument ( ! apiKey . isEmpty ( ) ) ;
try {
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , apiKey ) ;
2016-10-14 17:10:43 +02:00
return client . getDataset ( datasetId ) ;
} catch ( Exception e ) {
logger . error ( " Unable to retrieve such dataset, returning null ... " , e ) ;
}
return null ;
}
2017-04-11 15:18:18 +02:00
@Override
public CkanDataset getDataset ( String datasetId ) throws Exception {
logger . info ( " Request ckan dataset with id " + datasetId ) ;
// checks
checkNotNull ( datasetId ) ;
checkArgument ( ! datasetId . isEmpty ( ) ) ;
CkanClient client = new CkanClient ( CKAN_CATALOGUE_URL ) ;
return client . getDataset ( datasetId ) ;
2017-05-05 12:52:23 +02:00
2017-04-11 15:18:18 +02:00
}
2016-10-28 10:55:13 +02:00
@Override
public boolean setSearchableField ( String datasetId , boolean searchable ) {
// checks
checkNotNull ( datasetId ) ;
checkArgument ( ! datasetId . isEmpty ( ) ) ;
String searchableAsString = searchable ? " True " : " False " ;
// Patch package path
String patchPackage = CKAN_CATALOGUE_URL + " /api/3/action/package_patch " ;
2017-03-08 16:35:56 +01:00
JSONObject obj = new JSONObject ( ) ;
obj . put ( " id " , datasetId ) ;
obj . put ( " searchable " , searchableAsString ) ;
2017-04-05 14:38:14 +02:00
2016-10-28 10:55:13 +02:00
try ( CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . build ( ) ; ) {
HttpPost request = new HttpPost ( patchPackage ) ;
request . addHeader ( " Authorization " , CKAN_TOKEN_SYS ) ;
2017-03-08 16:35:56 +01:00
StringEntity params = new StringEntity ( obj . toJSONString ( ) ) ;
2016-10-28 10:55:13 +02:00
request . setEntity ( params ) ;
HttpResponse response = httpClient . execute ( request ) ;
logger . debug ( " Response code is " + response . getStatusLine ( ) . getStatusCode ( ) + " and response message is " + response . getStatusLine ( ) . getReasonPhrase ( ) ) ;
if ( response . getStatusLine ( ) . getStatusCode ( ) = = HttpStatus . SC_OK )
return true ;
} catch ( Exception ex ) {
logger . error ( " Error while trying to set searchable the dataset " , ex ) ;
}
return false ;
}
2016-11-14 18:07:47 +01:00
@Override
public List < CkanGroup > getGroups ( ) {
try {
CkanClient client = new CkanClient ( CKAN_CATALOGUE_URL ) ;
return client . getGroupList ( ) ;
} catch ( Exception e ) {
logger . error ( " Failed to retrieve the list groups " , e ) ;
return null ;
}
}
2016-11-23 12:52:08 +01:00
@Override
2016-11-24 17:55:00 +01:00
public CkanResource uploadResourceFile ( File file , String packageId ,
2017-07-31 15:45:33 +02:00
String token , String name , String description , String mimeType , String format ) {
2016-11-24 15:48:43 +01:00
2016-11-23 12:52:08 +01:00
// checks
checkNotNull ( file ) ;
checkNotNull ( packageId ) ;
checkNotNull ( token ) ;
checkNotNull ( name ) ;
2016-11-30 10:33:58 +01:00
2016-11-24 17:55:00 +01:00
String returnedId = null ;
2016-11-23 12:52:08 +01:00
try ( CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . build ( ) ) {
String apiRequestUrl = CKAN_CATALOGUE_URL + " /api/3/action/resource_create " ;
HttpPost httpPostRequest = new HttpPost ( apiRequestUrl ) ;
httpPostRequest . setHeader ( " Authorization " , token ) ;
2018-06-27 12:04:03 +02:00
HttpEntity mpEntity =
2016-11-23 12:52:08 +01:00
MultipartEntityBuilder . create ( )
2016-11-25 17:26:56 +01:00
. addTextBody ( " package_id " , packageId , ContentType . TEXT_PLAIN )
. addTextBody ( " url " , " upload " , ContentType . TEXT_PLAIN )
. addTextBody ( " description " , description = = null ? " " : description , ContentType . TEXT_PLAIN )
. addTextBody ( " name " , name , ContentType . TEXT_PLAIN )
2017-07-31 15:45:33 +02:00
. addTextBody ( " mimetype " , mimeType )
. addTextBody ( " format " , format )
2018-06-27 12:04:03 +02:00
. addBinaryBody ( " upload " , file ,
2016-11-23 12:52:08 +01:00
ContentType . create (
" application/octet-stream " ,
2018-06-27 12:04:03 +02:00
Charset . forName ( " UTF-8 " ) ) ,
2016-11-23 12:52:08 +01:00
name )
2016-11-24 15:48:43 +01:00
. build ( ) ;
2016-11-23 12:52:08 +01:00
httpPostRequest . setEntity ( mpEntity ) ;
HttpResponse response = httpClient . execute ( httpPostRequest ) ;
if ( response . getStatusLine ( ) . getStatusCode ( ) < 200 | | response . getStatusLine ( ) . getStatusCode ( ) > = 300 ) {
throw new RuntimeException ( " failed to add the file to CKAN storage. response status line from "
+ apiRequestUrl + " was: " + response . getStatusLine ( ) ) ;
}
2016-11-30 10:33:58 +01:00
2016-11-25 17:26:56 +01:00
logger . info ( " Returned message is " + response . getStatusLine ( ) ) ;
2016-11-30 10:33:58 +01:00
2016-11-24 17:55:00 +01:00
String json = EntityUtils . toString ( response . getEntity ( ) ) ;
Object obj = JSONValue . parse ( json ) ;
JSONObject finalResult = ( JSONObject ) obj ;
JSONObject result = ( JSONObject ) finalResult . get ( " result " ) ;
logger . debug ( " Returned json is " + result . get ( " id " ) ) ;
returnedId = ( String ) result . get ( " id " ) ;
2018-06-27 12:04:03 +02:00
return new ExtendCkanClient ( CKAN_CATALOGUE_URL , token ) . getResource ( returnedId ) ;
2016-11-23 12:52:08 +01:00
} catch ( Exception e ) {
logger . error ( " Error while uploading file " ) ;
2016-11-24 17:55:00 +01:00
return null ;
2016-11-23 12:52:08 +01:00
}
}
2016-11-24 15:48:43 +01:00
2017-04-05 14:38:14 +02:00
2016-11-24 15:48:43 +01:00
@Override
public boolean patchResource ( String resourceId , String url ,
String name , String description , String urlType , String apiKey ) {
// checks
checkNotNull ( resourceId ) ;
checkNotNull ( apiKey ) ;
logger . debug ( " Going to change resource with id " + resourceId + " . "
+ " Request comes from user with key " + apiKey . substring ( 0 , 5 ) + " **************** " ) ;
try ( CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . build ( ) ) {
String apiRequestUrl = CKAN_CATALOGUE_URL + " /api/3/action/resource_patch " ;
HttpPost httpPostRequest = new HttpPost ( apiRequestUrl ) ;
httpPostRequest . setHeader ( " Authorization " , apiKey ) ;
2018-06-27 12:04:03 +02:00
// Request parameters to be replaced
2016-11-24 15:48:43 +01:00
JSONObject jsonRequest = new JSONObject ( ) ;
Map < String , String > requestMap = new HashMap < String , String > ( ) ;
requestMap . put ( " id " , resourceId ) ;
if ( url ! = null & & ! url . isEmpty ( ) )
requestMap . put ( " url " , url ) ;
if ( description ! = null )
requestMap . put ( " description " , description ) ;
if ( name ! = null & & ! name . isEmpty ( ) )
requestMap . put ( " name " , name ) ;
if ( urlType ! = null )
requestMap . put ( " url_type " , urlType ) ;
jsonRequest . putAll ( requestMap ) ;
StringEntity params = new StringEntity ( jsonRequest . toJSONString ( ) ) ;
httpPostRequest . setEntity ( params ) ;
HttpResponse response = httpClient . execute ( httpPostRequest ) ;
if ( response . getStatusLine ( ) . getStatusCode ( ) < 200 | | response . getStatusLine ( ) . getStatusCode ( ) > = 300 ) {
throw new RuntimeException ( " failed to patch the resource. response status line from "
+ apiRequestUrl + " was: " + response . getStatusLine ( ) ) ;
}
return true ;
} catch ( Exception e ) {
logger . error ( " Failed to update the resource " , e ) ;
}
return false ;
}
2016-11-29 10:28:50 +01:00
2016-11-30 10:33:58 +01:00
@Override
public boolean patchProductCustomFields ( String productId , String apiKey ,
2018-02-08 16:57:31 +01:00
Map < String , List < String > > customFieldsToChange , boolean removeOld ) {
2016-11-30 10:33:58 +01:00
// checks
checkNotNull ( productId ) ;
checkNotNull ( apiKey ) ;
if ( customFieldsToChange = = null | | customFieldsToChange . isEmpty ( ) ) // TODO.. remove all custom fields maybe?!
return true ;
logger . info ( " Going to change product with id " + productId + " . "
+ " Request comes from user with key " + apiKey . substring ( 0 , 5 ) + " **************** " ) ;
logger . info ( " The new values are " + customFieldsToChange ) ;
// Get already available custom fields
Map < String , List < String > > fromCKANCustomFields = new HashMap < String , List < String > > ( ) ;
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , apiKey ) ;
2016-11-30 10:33:58 +01:00
List < CkanPair > extras = client . getDataset ( productId ) . getExtras ( ) ;
2016-12-16 18:25:38 +01:00
2016-12-12 17:39:30 +01:00
if ( extras = = null )
extras = new ArrayList < CkanPair > ( ) ;
2016-11-30 10:33:58 +01:00
// fill the above map with these values
for ( CkanPair ckanPair : extras ) {
List < String > forThisValue = null ;
String key = ckanPair . getKey ( ) ;
if ( fromCKANCustomFields . containsKey ( key ) )
forThisValue = fromCKANCustomFields . get ( key ) ;
else
forThisValue = new ArrayList < String > ( ) ;
forThisValue . add ( ckanPair . getValue ( ) ) ;
fromCKANCustomFields . put ( key , forThisValue ) ;
}
logger . info ( " The generated map from jackan looks like " + fromCKANCustomFields + " . Going to merge them " ) ;
// merge them with the new values
Iterator < Entry < String , List < String > > > iteratorUserMap = customFieldsToChange . entrySet ( ) . iterator ( ) ;
while ( iteratorUserMap . hasNext ( ) ) {
2018-06-27 12:04:03 +02:00
Map . Entry < java . lang . String , java . util . List < java . lang . String > > entry = iteratorUserMap
2016-11-30 10:33:58 +01:00
. next ( ) ;
String key = entry . getKey ( ) ;
List < String > newValues = entry . getValue ( ) ;
2016-12-10 10:48:33 +01:00
2016-12-07 18:32:07 +01:00
// get the unique set of values
Set < String > uniqueValues = new HashSet < String > ( ) ;
2016-12-10 10:48:33 +01:00
2016-12-07 18:32:07 +01:00
if ( fromCKANCustomFields . containsKey ( key ) )
2018-02-08 16:57:31 +01:00
if ( ! removeOld )
uniqueValues . addAll ( fromCKANCustomFields . get ( key ) ) ;
2016-12-10 10:48:33 +01:00
2016-12-07 18:32:07 +01:00
uniqueValues . addAll ( newValues ) ;
fromCKANCustomFields . put ( key , new ArrayList < String > ( uniqueValues ) ) ;
2016-11-30 10:33:58 +01:00
}
logger . info ( " After merging it is " + fromCKANCustomFields ) ;
try ( CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . build ( ) ) {
String apiRequestUrl = CKAN_CATALOGUE_URL + " /api/3/action/package_patch " ;
HttpPost httpPostRequest = new HttpPost ( apiRequestUrl ) ;
httpPostRequest . setHeader ( " Authorization " , apiKey ) ;
2018-06-27 12:04:03 +02:00
// Request parameters to be replaced
2016-11-30 10:33:58 +01:00
JSONObject jsonRequest = new JSONObject ( ) ;
// build the json array for the "extras" field.. each object looks like {"key": ..., "value": ...}
JSONArray extrasObject = new JSONArray ( ) ;
Iterator < Entry < String , List < String > > > iteratorNewFields = fromCKANCustomFields . entrySet ( ) . iterator ( ) ;
while ( iteratorNewFields . hasNext ( ) ) {
2018-06-27 12:04:03 +02:00
Map . Entry < java . lang . String , java . util . List < java . lang . String > > entry = iteratorNewFields
2016-11-30 10:33:58 +01:00
. next ( ) ;
String key = entry . getKey ( ) ;
List < String > values = entry . getValue ( ) ;
for ( String value : values ) {
JSONObject obj = new JSONObject ( ) ;
obj . put ( " value " , value ) ;
obj . put ( " key " , key ) ;
extrasObject . add ( obj ) ;
}
}
// perform the request
jsonRequest . put ( " id " , productId ) ;
jsonRequest . put ( " extras " , extrasObject ) ;
logger . debug ( " Request param is going to be " + jsonRequest ) ;
StringEntity params = new StringEntity ( jsonRequest . toJSONString ( ) , ContentType . APPLICATION_JSON ) ;
httpPostRequest . setEntity ( params ) ;
HttpResponse response = httpClient . execute ( httpPostRequest ) ;
if ( response . getStatusLine ( ) . getStatusCode ( ) < 200 | | response . getStatusLine ( ) . getStatusCode ( ) > = 300 ) {
throw new RuntimeException ( " failed to patch the product. response status line from "
+ apiRequestUrl + " was: " + response . getStatusLine ( ) ) ;
}
return true ;
} catch ( Exception e ) {
logger . error ( " Failed to patch the product " , e ) ;
}
return false ;
}
2017-04-05 14:38:14 +02:00
2016-12-07 18:32:07 +01:00
@Override
public boolean removeCustomField ( String productId , String key ,
String value , String apiKey ) {
// checks
checkNotNull ( productId ) ;
checkNotNull ( apiKey ) ;
checkNotNull ( value ) ;
checkNotNull ( key ) ;
logger . info ( " Going to change product with id " + productId + " . "
+ " Request comes from user with key " + apiKey . substring ( 0 , 5 ) + " **************** " ) ;
logger . info ( " The couple key/value to remove from custom fields is [ " + key + " , " + value + " ] " ) ;
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , apiKey ) ;
2016-12-07 18:32:07 +01:00
List < CkanPair > extras = client . getDataset ( productId ) . getExtras ( ) ;
Iterator < CkanPair > iterator = extras . iterator ( ) ;
while ( iterator . hasNext ( ) ) {
2018-06-27 12:04:03 +02:00
CkanPair ckanPair = iterator . next ( ) ;
2016-12-07 18:32:07 +01:00
if ( ckanPair . getKey ( ) . equals ( key ) & & ckanPair . getValue ( ) . equals ( value ) ) {
logger . info ( " Removed from the ckan pairs list " ) ;
iterator . remove ( ) ;
break ;
}
}
try ( CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . build ( ) ) {
String apiRequestUrl = CKAN_CATALOGUE_URL + " /api/3/action/package_patch " ;
HttpPost httpPostRequest = new HttpPost ( apiRequestUrl ) ;
httpPostRequest . setHeader ( " Authorization " , apiKey ) ;
2018-06-27 12:04:03 +02:00
// Request parameters to be replaced
2016-12-07 18:32:07 +01:00
JSONObject jsonRequest = new JSONObject ( ) ;
// build the json array for the "extras" field.. each object looks like {"key": ..., "value": ...}
JSONArray extrasObject = new JSONArray ( ) ;
for ( CkanPair extra : extras ) {
JSONObject obj = new JSONObject ( ) ;
obj . put ( " value " , extra . getValue ( ) ) ;
obj . put ( " key " , extra . getKey ( ) ) ;
extrasObject . add ( obj ) ;
}
// perform the request
jsonRequest . put ( " id " , productId ) ;
jsonRequest . put ( " extras " , extrasObject ) ;
logger . debug ( " Request param is going to be " + jsonRequest ) ;
StringEntity params = new StringEntity ( jsonRequest . toJSONString ( ) , ContentType . APPLICATION_JSON ) ;
httpPostRequest . setEntity ( params ) ;
HttpResponse response = httpClient . execute ( httpPostRequest ) ;
if ( response . getStatusLine ( ) . getStatusCode ( ) < 200 | | response . getStatusLine ( ) . getStatusCode ( ) > = 300 ) {
throw new RuntimeException ( " failed to patch the product. response status line from "
+ apiRequestUrl + " was: " + response . getStatusLine ( ) ) ;
}
return true ;
} catch ( Exception e ) {
logger . error ( " Failed to remove the custom field for this product " , e ) ;
}
return false ;
}
2016-11-30 14:54:21 +01:00
@Override
public boolean removeTag ( String productId , String apiKey , String tagToRemove ) {
// checks
checkNotNull ( productId ) ;
checkNotNull ( apiKey ) ;
checkNotNull ( tagToRemove ) ;
try ( CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . build ( ) ) {
String apiRequestUrl = CKAN_CATALOGUE_URL + " /api/3/action/package_patch " ;
HttpPost httpPostRequest = new HttpPost ( apiRequestUrl ) ;
httpPostRequest . setHeader ( " Authorization " , apiKey ) ;
2018-06-27 12:04:03 +02:00
// Request parameters to be replaced
2016-11-30 14:54:21 +01:00
JSONObject jsonRequest = new JSONObject ( ) ;
JSONArray tagsAsJson = new JSONArray ( ) ;
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , apiKey ) ;
2016-11-30 14:54:21 +01:00
CkanDataset dataset = client . getDataset ( productId ) ;
List < CkanTag > currentTags = dataset . getTags ( ) ;
2018-06-27 12:04:03 +02:00
// build the json array for the "tags" field.. each object looks like
2016-11-30 14:54:21 +01:00
// {
//
// "vocabulary_id": null,
// "state": "active",
// "display_name": "....",
// "id": "....",
// "name": "..."
//
// }
for ( CkanTag ckanTag : currentTags ) {
if ( ! ckanTag . getName ( ) . equals ( tagToRemove ) ) {
JSONObject obj = new JSONObject ( ) ;
obj . put ( " vocabulary_id " , ckanTag . getVocabularyId ( ) ) ;
obj . put ( " state " , ckanTag . getState ( ) . toString ( ) ) ;
obj . put ( " display_name " , ckanTag . getDisplayName ( ) ) ;
obj . put ( " id " , ckanTag . getId ( ) ) ;
obj . put ( " name " , ckanTag . getName ( ) ) ;
tagsAsJson . add ( obj ) ;
}
}
// perform the request
jsonRequest . put ( " id " , productId ) ;
jsonRequest . put ( " tags " , tagsAsJson ) ;
logger . debug ( " Request param is going to be " + jsonRequest ) ;
StringEntity params = new StringEntity ( jsonRequest . toJSONString ( ) , ContentType . APPLICATION_JSON ) ;
httpPostRequest . setEntity ( params ) ;
HttpResponse response = httpClient . execute ( httpPostRequest ) ;
if ( response . getStatusLine ( ) . getStatusCode ( ) < 200 | | response . getStatusLine ( ) . getStatusCode ( ) > = 300 ) {
throw new RuntimeException ( " failed to patch the product. response status line from "
+ apiRequestUrl + " was: " + response . getStatusLine ( ) ) ;
}
return true ;
} catch ( Exception e ) {
logger . error ( " Failed to remove the tag " + tagToRemove , e ) ;
}
return false ;
}
@Override
public boolean addTag ( String productId , String apiKey , String tagToAdd ) {
// checks
checkNotNull ( productId ) ;
checkNotNull ( apiKey ) ;
checkNotNull ( tagToAdd ) ;
try {
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , apiKey ) ;
2016-11-30 14:54:21 +01:00
CkanDataset dataset = client . getDataset ( productId ) ;
List < CkanTag > currentTags = dataset . getTags ( ) ;
Iterator < CkanTag > tagsIterator = currentTags . iterator ( ) ;
boolean added = true ;
// check if it is already there ...
while ( tagsIterator . hasNext ( ) ) {
2018-06-27 12:04:03 +02:00
CkanTag ckanTag = tagsIterator . next ( ) ;
2016-11-30 14:54:21 +01:00
if ( ckanTag . getName ( ) . equals ( tagToAdd ) ) {
added = false ;
break ;
}
}
if ( added ) {
currentTags . add ( new CkanTag ( tagToAdd ) ) ;
dataset . setTags ( currentTags ) ;
client . patchUpdateDataset ( dataset ) ;
}
return true ;
} catch ( Exception e ) {
logger . error ( " Failed to add the tag " + tagToAdd , e ) ;
}
return false ;
}
2016-12-01 08:26:51 +01:00
@Override
public List < CkanGroup > getParentGroups ( String groupName , String apiKey ) {
// checks
checkNotNull ( groupName ) ;
checkNotNull ( apiKey ) ;
2016-12-02 22:42:17 +01:00
2016-12-01 08:26:51 +01:00
try {
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , apiKey ) ;
2016-12-01 08:26:51 +01:00
return client . getGroup ( groupName ) . getGroups ( ) ;
} catch ( Exception e ) {
logger . error ( " Something went wrong, returning null " , e ) ;
}
2016-12-02 22:42:17 +01:00
2016-12-01 08:26:51 +01:00
return null ;
}
2017-04-05 14:38:14 +02:00
2016-12-02 22:42:17 +01:00
@Override
public boolean setGroupParent ( String parentName , String groupName ) {
// checks
checkNotNull ( parentName ) ;
checkNotNull ( groupName ) ;
try ( CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . build ( ) ) {
String apiRequestUrl = CKAN_CATALOGUE_URL + " /api/3/action/group_patch " ;
HttpPost httpPostRequest = new HttpPost ( apiRequestUrl ) ;
httpPostRequest . setHeader ( " Authorization " , CKAN_TOKEN_SYS ) ;
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , CKAN_TOKEN_SYS ) ;
2016-12-02 22:42:17 +01:00
List < CkanGroup > alreadyAvailableParents = client . getGroup ( groupName ) . getGroups ( ) ;
Set < String > parentsNames = new HashSet < String > ( ) ;
parentsNames . add ( parentName ) ;
for ( CkanGroup alreadyAvailableParent : alreadyAvailableParents ) {
parentsNames . add ( alreadyAvailableParent . getName ( ) ) ;
}
logger . info ( " Setting as parents of group " + groupName + " : " + parentsNames ) ;
2018-06-27 12:04:03 +02:00
// Request parameters to be replaced
2016-12-02 22:42:17 +01:00
JSONObject jsonRequest = new JSONObject ( ) ;
JSONArray parentsAsJson = new JSONArray ( ) ;
for ( String parent : parentsNames ) {
JSONObject obj = new JSONObject ( ) ;
obj . put ( " name " , parent ) ;
parentsAsJson . add ( obj ) ;
}
// perform the request
jsonRequest . put ( " id " , groupName ) ;
jsonRequest . put ( " groups " , parentsAsJson ) ;
logger . debug ( " Request param is going to be " + jsonRequest ) ;
StringEntity params = new StringEntity ( jsonRequest . toJSONString ( ) , ContentType . APPLICATION_JSON ) ;
httpPostRequest . setEntity ( params ) ;
HttpResponse response = httpClient . execute ( httpPostRequest ) ;
if ( response . getStatusLine ( ) . getStatusCode ( ) < 200 | | response . getStatusLine ( ) . getStatusCode ( ) > = 300 ) {
throw new RuntimeException ( " failed to patch the group. response status line from "
+ apiRequestUrl + " was: " + response . getStatusLine ( ) ) ;
}
return true ;
} catch ( Exception e ) {
logger . error ( " Failed to set parents groups " , e ) ;
}
return false ;
}
2016-12-06 15:58:38 +01:00
@Override
public boolean isDatasetInGroup ( String groupName , String datasetId ) {
2016-12-06 17:03:39 +01:00
2016-12-06 15:58:38 +01:00
// checks
checkNotNull ( datasetId ) ;
checkNotNull ( groupName ) ;
2016-12-06 17:03:39 +01:00
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , CKAN_TOKEN_SYS ) ;
2016-12-06 15:58:38 +01:00
List < CkanGroup > groups = client . getDataset ( datasetId ) . getGroups ( ) ;
2016-12-06 17:03:39 +01:00
2016-12-06 15:58:38 +01:00
for ( CkanGroup ckanGroup : groups ) {
if ( ckanGroup . getName ( ) . equals ( groupName ) )
return true ;
}
2016-12-06 17:03:39 +01:00
2016-12-06 15:58:38 +01:00
return false ;
}
2016-12-06 17:03:39 +01:00
@Override
public List < CkanDataset > getProductsInGroup ( String groupName ) {
checkNotNull ( groupName ) ;
List < CkanDataset > toReturn = null ;
try ( CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . build ( ) ) {
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , CKAN_TOKEN_SYS ) ;
2016-12-06 17:03:39 +01:00
String apiRequestUrl = CKAN_CATALOGUE_URL + " /api/3/action/group_show " + " ?id= " + groupName + " &include_datasets=true " ;
HttpGet httpGetRequest = new HttpGet ( apiRequestUrl ) ;
httpGetRequest . setHeader ( " Authorization " , CKAN_TOKEN_SYS ) ;
HttpResponse response = httpClient . execute ( httpGetRequest ) ;
String jsonString = EntityUtils . toString ( response . getEntity ( ) ) ;
JSONParser parser = new JSONParser ( ) ;
JSONObject parsedJson = ( JSONObject ) parser . parse ( jsonString ) ;
logger . debug ( " JSONObject looks like " + parsedJson ) ;
// get "packages" array
toReturn = new ArrayList < CkanDataset > ( ) ;
JSONObject result = ( JSONObject ) parsedJson . get ( " result " ) ;
JSONArray packages = ( JSONArray ) result . get ( " packages " ) ;
2016-12-07 18:32:07 +01:00
2016-12-06 17:03:39 +01:00
logger . debug ( " Packages looks like " + packages ) ;
for ( int i = 0 , size = packages . size ( ) ; i < size ; i + + ) {
JSONObject objectInArray = ( JSONObject ) packages . get ( i ) ;
String packageId = ( String ) objectInArray . get ( " id " ) ;
toReturn . add ( client . getDataset ( packageId ) ) ;
}
} catch ( Exception e ) {
logger . error ( " Failed to get groups information " , e ) ;
}
return toReturn ;
}
2016-12-10 10:48:33 +01:00
@Override
public String patchProductWithJSON ( String productId , JSONObject jsonRequest ,
String apiKey ) {
checkNotNull ( productId ) ;
checkNotNull ( jsonRequest ) ;
checkNotNull ( apiKey ) ;
logger . info ( " Request of patching product " + productId + " with json " + jsonRequest ) ;
try ( CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . build ( ) ; ) {
String pathUpdatePatch = getCatalogueUrl ( ) + " /api/3/action/package_patch " ;
HttpPost httpPostRequest = new HttpPost ( pathUpdatePatch ) ;
2016-12-12 10:25:18 +01:00
httpPostRequest . setHeader ( " Authorization " , CKAN_TOKEN_SYS ) ;
2016-12-10 10:48:33 +01:00
2017-08-02 12:06:07 +02:00
// be sure the id of the item is there
jsonRequest . put ( " id " , productId ) ;
2016-12-10 10:48:33 +01:00
StringEntity params = new StringEntity ( jsonRequest . toJSONString ( ) , ContentType . APPLICATION_JSON ) ;
httpPostRequest . setEntity ( params ) ;
HttpResponse response = httpClient . execute ( httpPostRequest ) ;
if ( response . getStatusLine ( ) . getStatusCode ( ) < 200 | | response . getStatusLine ( ) . getStatusCode ( ) > = 300 ) {
throw new RuntimeException ( " failed to patch the product. response status line from "
+ pathUpdatePatch + " was: " + response . getStatusLine ( ) ) ;
}
return null ;
} catch ( Exception e ) {
logger . error ( " Error while trying to patch grsf record " + e . getMessage ( ) ) ;
return e . getMessage ( ) ;
}
}
2016-12-16 18:25:38 +01:00
@Override
public List < String > getProductsIdsInGroupOrOrg ( String orgOrGroupName , boolean isOrganization , int start , int rows ) throws ClassNotFoundException , SQLException {
List < String > toReturn = new ArrayList < String > ( ) ;
checkNotNull ( orgOrGroupName ) ;
checkArgument ( start > = 0 ) ;
checkArgument ( rows > = 0 ) ;
Connection connection = getConnection ( ) ;
2016-12-16 18:30:17 +01:00
try {
2016-12-16 18:25:38 +01:00
2016-12-16 18:30:17 +01:00
ResultSet rs ;
if ( isOrganization ) {
2016-12-16 18:25:38 +01:00
2016-12-16 18:30:17 +01:00
String joinQuery = " SELECT \" package \" . \" id \" AS \" dataset_id \" FROM \" package \" INNER JOIN \" group \" ON "
+ " \" package \" . \" owner_org \" = \" group \" . \" id \" WHERE \" group \" . \" name \" =? "
+ " AND \" group \" . \" is_organization \" =? AND \" package \" . \" type \" =? AND \" package \" . \" state \" =? LIMIT ? OFFSET ?; " ;
2016-12-16 18:25:38 +01:00
2016-12-16 18:30:17 +01:00
PreparedStatement preparedStatement = connection . prepareStatement ( joinQuery ) ;
preparedStatement . setString ( 1 , orgOrGroupName ) ;
preparedStatement . setBoolean ( 2 , isOrganization ) ;
preparedStatement . setString ( 3 , " dataset " ) ;
preparedStatement . setString ( 4 , " active " ) ;
preparedStatement . setBigDecimal ( 5 , new BigDecimal ( rows ) ) ;
preparedStatement . setBigDecimal ( 6 , new BigDecimal ( start ) ) ;
2016-12-16 18:25:38 +01:00
2016-12-16 18:30:17 +01:00
rs = preparedStatement . executeQuery ( ) ;
2016-12-16 18:25:38 +01:00
2016-12-16 18:30:17 +01:00
} else {
2016-12-16 18:25:38 +01:00
2016-12-16 18:30:17 +01:00
/ * *
* Inner join between the member table and the package table.
* Basically every time a dataset is added to a group , a new row is added to the table
* where table_id is the package_id , and group_id is the owner group identifier ( not the name )
* /
String groupId = client . getGroup ( orgOrGroupName ) . getId ( ) ;
String joinQuery = " SELECT \" table_id \" AS \" dataset_id \" FROM \" package \" INNER JOIN \" member \" ON "
+ " \" member \" . \" table_id \" = \" package \" . \" id \" WHERE \" group_id \" =? "
+ " AND \" member \" . \" state \" =? LIMIT ? OFFSET ?; " ;
2016-12-16 18:25:38 +01:00
2016-12-16 18:30:17 +01:00
PreparedStatement preparedStatement = connection . prepareStatement ( joinQuery ) ;
preparedStatement . setString ( 1 , groupId ) ;
preparedStatement . setString ( 2 , " active " ) ;
preparedStatement . setBigDecimal ( 3 , new BigDecimal ( rows ) ) ;
preparedStatement . setBigDecimal ( 4 , new BigDecimal ( start ) ) ;
2016-12-16 18:25:38 +01:00
2016-12-16 18:30:17 +01:00
rs = preparedStatement . executeQuery ( ) ;
2016-12-16 18:25:38 +01:00
2016-12-16 18:30:17 +01:00
}
2016-12-16 18:25:38 +01:00
while ( rs . next ( ) ) {
2016-12-16 18:30:17 +01:00
toReturn . add ( rs . getString ( " dataset_id " ) ) ;
2016-12-16 18:25:38 +01:00
}
2016-12-16 18:30:17 +01:00
} catch ( Exception e ) {
logger . error ( " Failed to retrieve the ids of products in group/org. Error is " + e . getMessage ( ) ) ;
return null ;
} finally {
closeConnection ( connection ) ;
2016-12-16 18:25:38 +01:00
}
return toReturn ;
}
2017-01-17 15:14:28 +01:00
@Override
public boolean deleteGroup ( String groupName , boolean purge ) {
2017-01-27 11:08:02 +01:00
2017-01-17 15:14:28 +01:00
checkNotNull ( groupName ) ;
logger . info ( " Request of deleting group " + groupName + " . Purge is " + Boolean . toString ( purge ) ) ;
try ( CloseableHttpClient httpClient = HttpClientBuilder . create ( ) . build ( ) ; ) {
String deletePath = getCatalogueUrl ( ) + " /api/3/action/group_delete " ;
String purgePath = getCatalogueUrl ( ) + " /api/3/action/group_purge " ;
2017-01-27 11:08:02 +01:00
2017-01-17 15:14:28 +01:00
String requestToPerform = purge ? purgePath : deletePath ;
2017-01-27 11:08:02 +01:00
2017-01-17 15:14:28 +01:00
HttpPost httpPostRequest = new HttpPost ( requestToPerform ) ;
httpPostRequest . setHeader ( " Authorization " , CKAN_TOKEN_SYS ) ;
2017-01-27 11:08:02 +01:00
2017-01-17 15:14:28 +01:00
JSONObject obj = new JSONObject ( ) ;
obj . put ( " id " , groupName ) ;
StringEntity params = new StringEntity ( obj . toJSONString ( ) , ContentType . APPLICATION_JSON ) ;
httpPostRequest . setEntity ( params ) ;
HttpResponse response = httpClient . execute ( httpPostRequest ) ;
if ( response . getStatusLine ( ) . getStatusCode ( ) < 200 | | response . getStatusLine ( ) . getStatusCode ( ) > = 300 ) {
throw new RuntimeException ( " failed to delete/purge the group. response status line from "
+ requestToPerform + " was: " + response . getStatusLine ( ) ) ;
}
return true ;
} catch ( Exception e ) {
logger . error ( " Error while trying to delete/purge the group " + e . getMessage ( ) ) ;
return false ;
}
}
2017-01-26 16:46:19 +01:00
@Override
public CkanOrganization getOrganizationByName ( String name ) {
2017-01-27 11:08:02 +01:00
2017-01-26 16:46:19 +01:00
checkNotNull ( name ) ;
2017-01-27 11:08:02 +01:00
2017-01-26 16:46:19 +01:00
String ckanName = name . toLowerCase ( ) ;
try {
2017-01-27 11:08:02 +01:00
2017-01-26 16:46:19 +01:00
return client . getOrganization ( ckanName ) ;
2017-01-27 11:08:02 +01:00
2017-01-26 16:46:19 +01:00
} catch ( Exception e ) {
2018-09-28 11:19:25 +02:00
logger . warn ( " Failed to retrieve the organization with name " + name + " on the ckan: " + client . getCatalogUrl ( ) ) ;
//OLD PRINTING. FIXING Task #12500
//logger.error("Failed to retrieve the organization with name" + name, e);
2017-01-26 16:46:19 +01:00
}
return null ;
}
2017-01-27 11:08:02 +01:00
@Override
public Map < String , Map < CkanGroup , RolesCkanGroupOrOrg > > getUserRoleByGroup (
String username , String apiKey ) {
checkNotNull ( username ) ;
checkNotNull ( apiKey ) ;
Map < String , Map < CkanGroup , RolesCkanGroupOrOrg > > toReturn = new HashMap < String , Map < CkanGroup , RolesCkanGroupOrOrg > > ( ) ;
try {
2018-02-23 14:20:17 +01:00
String ckanUsername = CatalogueUtilMethods . fromUsernameToCKanUsername ( username ) ;
2017-01-27 11:08:02 +01:00
Map < CkanGroup , RolesCkanGroupOrOrg > partialResult = getGroupsByUserFromDB ( ckanUsername ) ;
Set < Entry < CkanGroup , RolesCkanGroupOrOrg > > set = partialResult . entrySet ( ) ;
for ( Entry < CkanGroup , RolesCkanGroupOrOrg > entry : set ) {
2017-02-01 18:29:06 +01:00
2017-01-27 11:08:02 +01:00
String groupName = entry . getKey ( ) . getName ( ) ;
HashMap < CkanGroup , RolesCkanGroupOrOrg > subMap = new HashMap < CkanGroup , RolesCkanGroupOrOrg > ( ) ;
subMap . put ( entry . getKey ( ) , entry . getValue ( ) ) ;
toReturn . put ( groupName , subMap ) ;
2017-02-01 18:29:06 +01:00
2017-01-27 11:08:02 +01:00
}
logger . debug ( " Returning map " + toReturn ) ;
} catch ( Exception e ) {
logger . error ( " Failed to retrieve roles of user in his/her own groups " , e ) ;
}
return toReturn ;
}
2017-02-01 18:29:06 +01:00
2017-01-27 11:08:02 +01:00
@Override
public Map < String , Map < CkanOrganization , RolesCkanGroupOrOrg > > getUserRoleByOrganization (
String username , String apiKey ) {
checkNotNull ( username ) ;
checkNotNull ( apiKey ) ;
Map < String , Map < CkanOrganization , RolesCkanGroupOrOrg > > toReturn = new HashMap < String , Map < CkanOrganization , RolesCkanGroupOrOrg > > ( ) ;
try {
2018-02-23 14:20:17 +01:00
String ckanUsername = CatalogueUtilMethods . fromUsernameToCKanUsername ( username ) ;
2017-01-27 11:08:02 +01:00
Map < CkanOrganization , RolesCkanGroupOrOrg > partialResult = getOrganizationsByUserFromDB ( ckanUsername ) ;
Set < Entry < CkanOrganization , RolesCkanGroupOrOrg > > set = partialResult . entrySet ( ) ;
for ( Entry < CkanOrganization , RolesCkanGroupOrOrg > entry : set ) {
2017-02-01 18:29:06 +01:00
2017-01-27 11:08:02 +01:00
String groupName = entry . getKey ( ) . getName ( ) ;
HashMap < CkanOrganization , RolesCkanGroupOrOrg > subMap = new HashMap < CkanOrganization , RolesCkanGroupOrOrg > ( ) ;
subMap . put ( entry . getKey ( ) , entry . getValue ( ) ) ;
toReturn . put ( groupName , subMap ) ;
2017-02-01 18:29:06 +01:00
2017-01-27 11:08:02 +01:00
}
logger . debug ( " Returning map " + toReturn ) ;
} catch ( Exception e ) {
logger . error ( " Failed to retrieve roles of user in his/her own groups " , e ) ;
}
return toReturn ;
}
/ * *
* Retrieve the list of groups ( plus capacities ) the user belongs by querying directly the database .
* @param username
* @return
2018-06-27 12:04:03 +02:00
* @throws SQLException
* @throws ClassNotFoundException
2017-01-27 11:08:02 +01:00
* /
private Map < CkanGroup , RolesCkanGroupOrOrg > getGroupsByUserFromDB ( String username ) {
checkNotNull ( username ) ;
Map < CkanGroup , RolesCkanGroupOrOrg > toReturn = new HashMap < CkanGroup , RolesCkanGroupOrOrg > ( ) ;
Connection connection = null ;
try {
connection = getConnection ( ) ;
ResultSet rs ;
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , CKAN_TOKEN_SYS ) ;
2017-01-27 11:08:02 +01:00
String userId = client . getUser ( username ) . getId ( ) ;
String joinQuery = " SELECT \" group_id \" , \" capacity \" FROM \" public \" . \" member \" "
+ " JOIN \" public \" . \" group \" ON \" member \" . \" group_id \" = \" group \" . \" id \" where \" table_id \" =? "
+ " and \" table_name \" ='user' and \" member \" . \" state \" ='active' and \" group \" . \" state \" ='active' and \" group \" . \" is_organization \" =?; " ;
PreparedStatement preparedStatement = connection . prepareStatement ( joinQuery ) ;
preparedStatement . setString ( 1 , userId ) ;
preparedStatement . setBoolean ( 2 , false ) ;
rs = preparedStatement . executeQuery ( ) ;
while ( rs . next ( ) ) {
toReturn . put ( client . getGroup ( rs . getString ( " group_id " ) ) , RolesCkanGroupOrOrg . convertFromCapacity ( rs . getString ( " capacity " ) ) ) ;
}
} catch ( Exception e ) {
logger . error ( " Failed to retrieve the groups to whom the user belongs. Error is " + e . getMessage ( ) ) ;
return null ;
} finally {
closeConnection ( connection ) ;
}
return toReturn ;
}
/ * *
* Retrieve the list of organizations ( plus capacities ) the user belongs by querying directly the database .
* @param username
* @return
2018-06-27 12:04:03 +02:00
* @throws SQLException
* @throws ClassNotFoundException
2017-01-27 11:08:02 +01:00
* /
private Map < CkanOrganization , RolesCkanGroupOrOrg > getOrganizationsByUserFromDB ( String username ) {
checkNotNull ( username ) ;
Map < CkanOrganization , RolesCkanGroupOrOrg > toReturn = new HashMap < CkanOrganization , RolesCkanGroupOrOrg > ( ) ;
Connection connection = null ;
try {
connection = getConnection ( ) ;
ResultSet rs ;
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , CKAN_TOKEN_SYS ) ;
2017-01-27 11:08:02 +01:00
String userId = client . getUser ( username ) . getId ( ) ;
String joinQuery = " SELECT \" group_id \" , \" capacity \" FROM \" public \" . \" member \" "
+ " JOIN \" public \" . \" group \" ON \" member \" . \" group_id \" = \" group \" . \" id \" where \" table_id \" =? "
+ " and \" table_name \" ='user' and \" member \" . \" state \" ='active' and \" group \" . \" state \" ='active' and \" group \" . \" is_organization \" =?; " ;
PreparedStatement preparedStatement = connection . prepareStatement ( joinQuery ) ;
preparedStatement . setString ( 1 , userId ) ;
preparedStatement . setBoolean ( 2 , true ) ;
rs = preparedStatement . executeQuery ( ) ;
while ( rs . next ( ) ) {
toReturn . put ( client . getOrganization ( rs . getString ( " group_id " ) ) , RolesCkanGroupOrOrg . convertFromCapacity ( rs . getString ( " capacity " ) ) ) ;
}
} catch ( Exception e ) {
logger . error ( " Failed to retrieve the groups to whom the user belongs. Error is " + e . getMessage ( ) ) ;
return null ;
} finally {
closeConnection ( connection ) ;
}
return toReturn ;
}
2017-04-11 15:18:18 +02:00
2017-05-06 19:22:05 +02:00
@Override
public CkanGroup getGroupByName ( String name ) {
2018-02-23 14:20:17 +01:00
String ckanName = CatalogueUtilMethods . fromGroupTitleToName ( name ) ;
2017-05-06 19:22:05 +02:00
try {
return client . getGroup ( ckanName ) ;
} catch ( Exception e ) {
logger . error ( " Failed to retrieve the group with name " + name , e ) ;
}
return null ;
}
2017-05-08 11:42:44 +02:00
@Override
public boolean isNotificationToUsersEnabled ( ) {
return ALERT_USERS_ON_POST_CREATION ;
}
2017-09-18 18:43:31 +02:00
@Override
public void assignRolesOtherOrganization ( String username ,
String sourceOrganization , RolesCkanGroupOrOrg currentRole ) {
2017-09-19 11:04:32 +02:00
2017-09-18 18:43:31 +02:00
checkNotNull ( username ) ;
checkNotNull ( sourceOrganization ) ;
checkNotNull ( currentRole ) ;
2017-09-19 11:04:32 +02:00
2017-09-19 10:33:20 +02:00
logger . info ( " Request for assigning other roles for user " + username + " , whose current role is " + currentRole + " and organization " + sourceOrganization ) ;
2017-09-19 11:04:32 +02:00
2017-09-18 18:43:31 +02:00
Iterator < Entry < String , String > > iterator = extendRoleInOrganization . entrySet ( ) . iterator ( ) ;
2017-09-19 11:04:32 +02:00
logger . debug ( " List of entries to check is " + extendRoleInOrganization ) ;
2017-09-18 18:43:31 +02:00
while ( iterator . hasNext ( ) ) {
2018-06-27 12:04:03 +02:00
Map . Entry < java . lang . String , java . lang . String > entry = iterator
2017-09-18 18:43:31 +02:00
. next ( ) ;
2017-09-19 11:04:32 +02:00
String sourceOrg = entry . getKey ( ) ;
logger . debug ( " Found organization source " + sourceOrg ) ;
2017-09-18 18:43:31 +02:00
if ( sourceOrg . equals ( sourceOrganization ) ) {
2017-09-19 11:04:32 +02:00
String [ ] values = entry . getValue ( ) . split ( DataCatalogueRunningCluster . TUPLES_SEPARATOR ) ;
for ( int i = 0 ; i < values . length ; i + + ) {
2017-09-19 11:33:54 +02:00
String destOrg = values [ i ] . split ( " \\ " + DataCatalogueRunningCluster . ROLE_ORGANIZATION_SEPARATOR ) [ 0 ] ;
String role = values [ i ] . split ( " \\ " + DataCatalogueRunningCluster . ROLE_ORGANIZATION_SEPARATOR ) [ 1 ] ;
logger . debug ( " Role is " + role + " and organization is " + destOrg ) ;
2017-09-19 11:04:32 +02:00
RolesCkanGroupOrOrg ckanRole ;
if ( role . equals ( DataCatalogueRunningCluster . CKAN_GENERIC_ROLE ) )
ckanRole = currentRole ;
else
ckanRole = RolesCkanGroupOrOrg . convertFromCapacity ( role ) ;
logger . info ( " Checking for extra role: role is " + ckanRole + " and organization is " + destOrg ) ;
checkRoleIntoOrganization ( username , destOrg , ckanRole ) ;
}
2017-09-18 18:43:31 +02:00
}
2017-09-19 11:04:32 +02:00
2017-09-18 18:43:31 +02:00
}
2017-09-19 11:04:32 +02:00
2017-09-18 18:43:31 +02:00
}
2018-01-12 17:05:56 +01:00
2017-11-02 10:23:12 +01:00
@Override
public List < CkanDataset > searchForPackage ( String apiKey , String query , int start , int offset ) throws Exception {
2018-01-12 17:05:56 +01:00
2017-11-02 10:23:12 +01:00
checkNotNull ( apiKey ) ;
checkNotNull ( query ) ;
checkArgument ( start > = 0 ) ;
checkArgument ( offset > = 0 ) ;
2018-01-12 17:05:56 +01:00
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , apiKey ) ;
2018-01-12 17:05:56 +01:00
2017-11-02 10:23:12 +01:00
try {
2018-01-12 17:05:56 +01:00
2017-11-02 10:23:12 +01:00
CkanQuery queryCkan = CkanQuery . filter ( ) . byText ( query ) ;
return client . searchDatasets ( queryCkan , offset , start ) . getResults ( ) ;
2018-01-12 17:05:56 +01:00
2017-11-02 10:23:12 +01:00
} catch ( Exception e ) {
logger . error ( " Error while executing query " , e ) ;
throw e ;
}
}
@Override
public List < CkanDataset > searchForPackageInOrganization ( String apiKey , String query , int start , int offset , String organization ) throws Exception {
2018-01-12 17:05:56 +01:00
2017-11-02 10:23:12 +01:00
checkNotNull ( apiKey ) ;
checkNotNull ( query ) ;
checkArgument ( start > = 0 ) ;
checkArgument ( offset > = 0 ) ;
2018-01-12 17:05:56 +01:00
2018-06-27 12:04:03 +02:00
ExtendCkanClient client = new ExtendCkanClient ( CKAN_CATALOGUE_URL , apiKey ) ;
2018-01-12 17:05:56 +01:00
2017-11-02 10:23:12 +01:00
try {
2018-01-12 17:05:56 +01:00
2017-11-02 10:36:10 +01:00
CkanQuery queryCkan = CkanQuery . filter ( ) . byOrganizationName ( organization . toLowerCase ( ) ) . byText ( query ) ;
2017-11-02 10:23:12 +01:00
return client . searchDatasets ( queryCkan , offset , start ) . getResults ( ) ;
2018-01-12 17:05:56 +01:00
2017-11-02 10:23:12 +01:00
} catch ( Exception e ) {
logger . error ( " Error while executing query " , e ) ;
throw e ;
}
}
2018-01-12 17:05:56 +01:00
@Override
public Statistics getStatistics ( ) throws Exception {
Statistics stats = new Statistics ( ) ;
int numGroups = getGroups ( ) . size ( ) ;
int numOrganizations = getOrganizationsNames ( ) . size ( ) ;
2018-02-08 16:57:31 +01:00
2018-01-12 17:05:56 +01:00
logger . debug ( " SOLR address is " + SOLR_URL ) ;
HttpSolrServer solr = new HttpSolrServer ( SOLR_URL ) ;
// ask solr for the current counter of the dataset -> dataset_type:"dataset" AND state:"active"
SolrQuery queryNumItems = new SolrQuery ( ) ;
queryNumItems . setRows ( 0 ) ; // do not require data
queryNumItems . setQuery ( " dataset_type: \" dataset \" AND state: \" active \" " ) ;
long numItems ;
try {
QueryResponse response = solr . query ( queryNumItems ) ;
numItems = response . getResults ( ) . getNumFound ( ) ;
} catch ( Exception e ) {
logger . error ( " Failed to retrieve the number of items " , e ) ;
throw e ;
}
// ask solr for types
SolrQuery queryNumTypes = new SolrQuery ( " dataset_type: \" dataset \" AND state: \" active \" " ) ;
queryNumTypes . addFacetField ( " systemtype " ) ;
queryNumItems . setRows ( 0 ) ; // do not require data
queryNumTypes . setFacet ( true ) ;
long numTypes = 0 ;
try {
QueryResponse response = solr . query ( queryNumTypes ) ;
Set < String > notEmptyTypes = new HashSet < String > ( ) ;
List < FacetField > facet = response . getFacetFields ( ) ;
for ( FacetField facetField : facet ) {
List < Count > values = facetField . getValues ( ) ; // the different types, even the ones with zero datasets associated
for ( Count count : values ) {
if ( count . getCount ( ) > 0 )
notEmptyTypes . add ( count . getName ( ) ) ;
}
}
numTypes = notEmptyTypes . size ( ) ;
} catch ( Exception e ) {
logger . error ( " Failed to retrieve the number of types " , e ) ;
throw e ;
}
// build the urls
stats . setNumGroups ( numGroups ) ;
stats . setNumItems ( numItems ) ;
stats . setNumOrganizations ( numOrganizations ) ;
stats . setNumTypes ( numTypes ) ;
2018-02-08 16:57:31 +01:00
2018-01-12 17:05:56 +01:00
return stats ;
}
2018-01-12 17:11:54 +01:00
@Override
public LandingPages getLandingPages ( ) throws Exception {
2018-02-08 16:57:31 +01:00
2018-01-12 17:11:54 +01:00
LandingPages landingPages = new LandingPages ( ) ;
landingPages . setUrlGroups ( PORTLET_URL_FOR_SCOPE + " ?path=/group/ " ) ;
2018-01-17 15:46:53 +01:00
landingPages . setUrlItems ( PORTLET_URL_FOR_SCOPE + " ?path=/dataset/ " ) ;
2018-01-12 17:11:54 +01:00
landingPages . setUrlOrganizations ( PORTLET_URL_FOR_SCOPE + " ?path=/organization/ " ) ;
landingPages . setUrlTypes ( PORTLET_URL_FOR_SCOPE + " ?path=/type/ " ) ;
return landingPages ;
}
2018-01-24 18:45:20 +01:00
@Override
public String getCatalogueEmail ( ) {
2018-02-08 16:57:31 +01:00
2018-01-24 18:45:20 +01:00
return CKAN_EMAIL ;
2018-02-08 16:57:31 +01:00
2018-01-24 18:45:20 +01:00
}
2018-09-19 10:10:23 +02:00
@Override
public boolean isSocialPostEnabled ( ) {
2018-12-18 17:18:31 +01:00
return SOCIAL_POST ;
2018-09-19 10:10:23 +02:00
}
2016-12-16 18:25:38 +01:00
}