diff --git a/.classpath b/.classpath index ae19729..afb1550 100644 --- a/.classpath +++ b/.classpath @@ -11,17 +11,17 @@ - + + - - + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b83d222 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/pom.xml b/pom.xml index 668791f..e930208 100644 --- a/pom.xml +++ b/pom.xml @@ -61,6 +61,12 @@ compile + + org.postgresql + postgresql + ${postgresVersion} + + org.gcube.data-publishing gcat-client diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/db/DBCaller.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/db/DBCaller.java index 7be49c9..f989172 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/db/DBCaller.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/db/DBCaller.java @@ -15,6 +15,13 @@ import org.gcube.datacatalogue.ckanutillibrary.shared.RolesCkanGroupOrOrg; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +// TODO: Auto-generated Javadoc +/** + * The Class DBCaller. + * + * @author Francesco Mangiacrapa at ISTI-CNR Pisa (Italy) + * Jun 1, 2020 + */ public class DBCaller { private static final Logger LOG = LoggerFactory.getLogger(DBCaller.class); @@ -25,9 +32,21 @@ public class DBCaller { private String CKAN_DB_USER; private String CKAN_DB_PASSWORD; + /** + * Instantiates a new DB caller. + */ public DBCaller() { } + /** + * Instantiates a new DB caller. + * + * @param cKAN_DB_URL the c KA N D B URL + * @param cKAN_DB_PORT the c KA N D B PORT + * @param cKAN_DB_NAME the c KA N D B NAME + * @param cKAN_DB_USER the c KA N D B USER + * @param cKAN_DB_PASSWORD the c KA N D B PASSWORD + */ public DBCaller(String cKAN_DB_URL, Integer cKAN_DB_PORT, String cKAN_DB_NAME, String cKAN_DB_USER, String cKAN_DB_PASSWORD) { CKAN_DB_URL = cKAN_DB_URL; @@ -39,14 +58,15 @@ public class DBCaller { /** - * Retrieve connection from the pool + * Retrieve connection from the pool. + * * @return a connection available within the pool - * @throws SQLException - * @throws ClassNotFoundException + * @throws SQLException the SQL exception + * @throws ClassNotFoundException the class not found exception */ private Connection getConnection() throws SQLException, ClassNotFoundException{ - LOG.debug("CONNECTION REQUEST"); + LOG.trace("CONNECTION REQUEST"); // create db connection Class.forName("org.postgresql.Driver"); @@ -57,6 +77,8 @@ public class DBCaller { dbBaseConnectionURL+="/" + CKAN_DB_NAME; + LOG.debug("DB CONNECTION URL: "+dbBaseConnectionURL); + Connection connection = DriverManager.getConnection(dbBaseConnectionURL, CKAN_DB_USER, CKAN_DB_PASSWORD); // Connection connection = DriverManager.getConnection( @@ -67,13 +89,13 @@ public class DBCaller { /** * Retrieve the map (groups id, capacity) with the groups to which the user belongs by querying directly the database. - * @param username - * @return - * @throws SQLException - * @throws ClassNotFoundException + * + * @param username the username + * @return the groups by user from DB */ - public Map getGroupsByUserFromDB(String userId){ - checkNotNull(userId); + public Map getGroupsByUserFromDB(String username){ + checkNotNull(username); + LOG.debug("Get groups by user called"); //couples (groups id, capacity) of the user in the group @@ -81,18 +103,20 @@ public class DBCaller { Connection connection = null; try{ - connection = getConnection(); - ResultSet rs; + String userId = getUserIdForUsername(username); + connection = getConnection(); + 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\"=?;"; - + + LOG.debug("Performing query: "+joinQuery); PreparedStatement preparedStatement = connection.prepareStatement(joinQuery); preparedStatement.setString(1, userId); preparedStatement.setBoolean(2, false); - rs = preparedStatement.executeQuery(); + ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { @@ -108,19 +132,62 @@ public class DBCaller { return toReturn; } + /** - * Retrieve the map (organisation id, capacity) with the organisations to which the user belongs by querying directly the database. - * @param username - * @return - * @throws SQLException - * @throws ClassNotFoundException + * Gets the user id for username. + * + * @param username the username + * @return the user id for username */ - public Map getOrganizationsByUserFromDB(String userId){ - checkNotNull(userId); + private String getUserIdForUsername(String username) { + LOG.debug("Get user id for username called"); + + Connection connection = null; + String userId = null; + try{ + + connection = getConnection(); + + String selQuery = "SELECT \"id\" FROM \"public\".\"user\" WHERE \"name\"=?;"; + + LOG.info("Performing query: "+selQuery); + + PreparedStatement preparedStatement = connection.prepareStatement(selQuery); + preparedStatement.setString(1, username); + ResultSet rs = preparedStatement.executeQuery(); + + while (rs.next()) { + userId = rs.getString("id"); + break; + } + + LOG.debug("For username "+username+" returning the user id: "+userId); + return userId; + + }catch(Exception e){ + LOG.error("Failed to retrieve the user id for username:"+username+". Error is " + e.getMessage()); + return null; + }finally{ + closeConnection(connection); + } + + } + + /** + * Retrieve the map (organisation id, capacity) with the organisations to which the user belongs by querying directly the database. + * + * @param username the username + * @return the organizations by user from DB + */ + public Map getOrganizationsByUserFromDB(String username){ + LOG.debug("Get organisations by user called"); + checkNotNull(username); Map toReturn = new HashMap(); Connection connection = null; try{ + + String userId = getUserIdForUsername(username); connection = getConnection(); ResultSet rs; @@ -150,6 +217,13 @@ public class DBCaller { } + /** + * Gets the api key from username. + * + * @param username the username + * @param state the state + * @return the api key from username + */ public String getApiKeyFromUsername(String username, String state) { LOG.debug("Request api key for user = " + username); @@ -191,14 +265,16 @@ public class DBCaller { /** - * Tries to close a connection - * @param connection + * Tries to close a connection. + * + * @param connection the connection */ private void closeConnection(Connection connection){ if(connection != null){ try{ connection.close(); + LOG.trace("CONNECTION CLOSED"); }catch(Exception e){ LOG.error("Unable to close this connection ", e); } diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogue.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogue.java index 800a459..e8b5494 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogue.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogue.java @@ -11,12 +11,19 @@ import eu.trentorise.opendata.jackan.model.CkanGroup; import eu.trentorise.opendata.jackan.model.CkanLicense; import eu.trentorise.opendata.jackan.model.CkanOrganization; +// TODO: Auto-generated Javadoc +/** + * The Interface DataCatalogue. + * + * @author Francesco Mangiacrapa at ISTI-CNR Pisa (Italy) + * Jun 1, 2020 + */ public interface DataCatalogue { /** - * Finds the id associated to the chosen license - * - * @param chosenLicense + * Finds the id associated to the chosen license. + * + * @param chosenLicense the chosen license * @return the id on success, null otherwise */ String findLicenseIdByLicenseTitle(String chosenLicense); @@ -26,8 +33,8 @@ public interface DataCatalogue { * contacting the uri resolver. If no uri resolver is available, an url that is * not guaranteed to be long term valid will be generated. Information are not * encrypted. - * - * @param datasetId + * + * @param datasetIdOrName the dataset id or name * @return The url of the dataset on success, null otherwise */ String getUnencryptedUrlFromDatasetIdOrName(String datasetIdOrName); @@ -37,23 +44,23 @@ public interface DataCatalogue { * values for the same custom field key. NOTE: unfortunately java doesn't * support overload in java interface methods (that's way I cannot use the same * name for the method) - * - * @param title + * + * @param title the title * @param name (unique identifier) - * @param organizationNameOrId - * @param author - * @param authorMail - * @param maintainer - * @param maintainerMail - * @param version - * @param description - * @param licenseId - * @param tags - * @param customFields - * @param resources + * @param organizationNameOrId the organization name or id + * @param author the author + * @param authorMail the author mail + * @param maintainer the maintainer + * @param maintainerMail the maintainer mail + * @param version the version + * @param description the description + * @param licenseId the license id + * @param tags the tags + * @param customFields the custom fields + * @param resources the resources * @param setPublic (manage visibility: Admin role is needed) * @return the id of the dataset on success, null otherwise - * @throws Exception + * @throws Exception the exception */ String createCKanDatasetMultipleCustomFields(String title, String name, String organizationNameOrId, String author, String authorMail, String maintainer, String maintainerMail, long version, String description, @@ -66,11 +73,11 @@ public interface DataCatalogue { * If a custom field with a given key already exists, and removeOld is set to * false, the new values are added at the end of the list. Otherwise they are * lost. - * - * @param productId - * @param customFieldsToChange - * @param removeOld - * @return + * + * @param productId the product id + * @param customFieldsToChange the custom fields to change + * @param removeOld the remove old + * @return true, if successful */ boolean patchProductCustomFields(String productId, Map> customFieldsToChange, boolean removeOld); @@ -78,40 +85,26 @@ public interface DataCatalogue { /** * Add a resource described by the bean to the dataset id into * resource.datasetId - * - * @param resource + * + * @param resource the resource * @return String the id of the resource on success, null otherwise + * @throws Exception the exception */ String addResourceToDataset(ResourceBean resource) throws Exception; /** * Remove the resource with id resourceId from the dataset in which it is. - * - * @param resourceId + * + * @param resourceId the resource id * @return true on success, false otherwise. */ boolean deleteResourceFromDataset(String resourceId); /** * Create a dataset with those information. - * - * @param apiKey - * @param title - * @param name (unique identifier) - * @param organizationNameOrId - * @param author - * @param authorMail - * @param maintainer - * @param maintainerMail - * @param version - * @param description - * @param licenseId - * @param tags - * @param customFields - * @param resources - * @param setPublic (manage visibility: Admin role is needed) + * + * @param nameOrId the name or id * @return the id of the dataset on success, null otherwise - * @throws Exception */ /** @@ -123,16 +116,6 @@ public interface DataCatalogue { */ boolean existProductWithNameOrId(String nameOrId); - /** - * The method returns the role the user has in the groups he/she belongs to (it - * uses the db, so it is much faster) - * - * @param username - * @param apiKey - * @return - */ - Map> getUserRoleByGroup(String username, String apiKey); - /** * Get the list of licenses' titles. * @@ -141,30 +124,30 @@ public interface DataCatalogue { List getLicenseTitles(); /** - * Retrieve ckan licenses - * - * @return + * Retrieve ckan licenses. + * + * @return the licenses */ List getLicenses(); /** - * Retrieve the url of the uri resolver for this catalogue instance/scope - * - * @return + * Retrieve the url of the uri resolver for this catalogue instance/scope. + * + * @return the uri resolver url */ String getUriResolverUrl(); /** - * Return the manage product property - * + * Return the manage product property. + * * @return the manage product property */ boolean isManageProductEnabled(); /** * Return the catalogue portlet for this context(i.e. scope) - * - * @return + * + * @return the portlet url */ String getPortletUrl(); @@ -177,24 +160,45 @@ public interface DataCatalogue { /** - * Returns the list of organizations to whom the user belongs (with any role) - * @param username + * Returns the list of organizations to whom the user belongs (with any role). + * + * @param username the username * @return a list of organizations */ List getOrganizationsByUser(String username); /** - * Returns the list of groups to whom the user belongs (with any role) - * @param username + * Returns the list of groups to whom the user belongs (with any role). + * + * @param username the username * @return a list of groups */ List getGroupsByUser(String username); /** - * Retrieve a ckan dataset given its id - * @param datasetId - * @return + * Retrieve a ckan dataset given its id. + * + * @param datasetId the dataset id + * @param apiKey the api key + * @return the dataset */ CkanDataset getDataset(String datasetId, String apiKey); + + /** + * Gets the user role by group. + * + * @param username the username + * @return the user role by group + */ + Map> getUserRoleByGroup(String username); + + /** + * The method returns the role the user has in the organizations he/she belongs to (it uses the db, so it is much faster) + * @param username + * @param apiKey + * @return + */ + Map> getUserRoleByOrganization(String username); + } diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.java index c2f2839..099f1e9 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.java @@ -280,14 +280,9 @@ public class DataCatalogueImpl implements DataCatalogue { } @Override - public Map> getUserRoleByGroup( - String username, String apiKey) { - + public Map> getUserRoleByGroup(String username) { + LOG.info("Get user role by group called. The username is: "+username); checkNotNull(username); - checkNotNull(apiKey); - - checkNotNull(username); - checkNotNull(apiKey); Map> toReturn = new HashMap>(); @@ -313,6 +308,36 @@ public class DataCatalogueImpl implements DataCatalogue { } + @Override + public Map> getUserRoleByOrganization( + String username) { + LOG.info("Get user role by organization called. The username is: "+username); + + checkNotNull(username); + + Map> toReturn = new HashMap>(); + + try{ + + String ckanUsername = CatalogueUtilMethods.fromUsernameToCKanUsername(username); + Map partialResult = dbCaller.getOrganizationsByUserFromDB(ckanUsername); + + for (String orgID : partialResult.keySet()) { + + CkanOrganization org = ckanCaller.getOrganization(orgID); + HashMap subMap = new HashMap(); + subMap.put(org, partialResult.get(orgID)); + toReturn.put(orgID, subMap); + } + + LOG.debug("Returning map " + toReturn); + }catch(Exception e){ + LOG.error("Failed to retrieve roles of user in his/her own groups",e); + } + + return toReturn; + } + /** * Retrieve an url for the tuple scope, entity, entity name diff --git a/src/test/java/TestDataCatalogueLib.java b/src/test/java/TestDataCatalogueLib.java new file mode 100644 index 0000000..cb00cbe --- /dev/null +++ b/src/test/java/TestDataCatalogueLib.java @@ -0,0 +1,615 @@ + + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.datacatalogue.ckanutillibrary.server.ApplicationProfileScopePerUrlReader; +import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogueFactory; +import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogueImpl; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.LoggerFactory; + +/** + * The Class TestDataCatalogueLib. + * + * @author Francesco Mangiacrapa at ISTI-CNR Pisa (Italy) + * Jun 1, 2020 + */ +public class TestDataCatalogueLib { + + private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestDataCatalogueLib.class); + + private DataCatalogueFactory factory; + private String scope = "/gcube/devsec/devVRE"; + //private String testUser = "costantino_perciante"; + private String testUser = "francesco_mangiacrapa"; + String subjectId = "aa_father4"; + String objectId = "bb_son4"; + + /** + * Before. + * + * @throws Exception the exception + */ + @Before + public void before() throws Exception{ + factory = DataCatalogueFactory.getFactory(); + } + + /** + * Factory test. + * + * @throws Exception the exception + */ + //@Test + public void factoryTest() throws Exception{ + + DataCatalogueFactory factory = DataCatalogueFactory.getFactory(); + + while(true){ + factory.getUtilsPerScope("/gcube"); + Thread.sleep(60* 1000 * 3); + factory.getUtilsPerScope("/gcube"); + break; + } + + for (int i = 0; i < 5; i++) { + Thread.sleep(1000); + factory.getUtilsPerScope("/gcube"); + } + + } + + + /** + * Gets the scope per url. + * + * @return the scope per url + */ + //@Test + public void getScopePerUrl(){ + + ScopeProvider.instance.set("/gcube"); + String url = "https://dev4.d4science.org/group/devvre/ckan"; + String scopeToUse = ApplicationProfileScopePerUrlReader.getScopePerUrl(url); + logger.debug("Retrieved scope is " + scopeToUse); + + ScopeProvider.instance.reset(); // the following sysout should print null + String url2 = "https://dev4.d4science.org/group/devvre/ckan"; + String scopeToUse2 = ApplicationProfileScopePerUrlReader.getScopePerUrl(url2); + logger.debug("Retrieved scope is " + scopeToUse2); + } + + + /** + * Gets the user role by group. + * + * @return the user role by group + * @throws Exception the exception + */ + @Test + public void getUserRoleByGroup() throws Exception{ + DataCatalogueImpl instance = factory.getUtilsPerScope(scope); + String username = testUser; + long init = System.currentTimeMillis(); + instance.getUserRoleByGroup(username); + long end = System.currentTimeMillis(); + logger.debug("Time taken " + (end - init)); + } + + /** + * Gets the user role by organization. + * + * @return the user role by organization + * @throws Exception the exception + */ + //@Test + public void getUserRoleByOrganization() throws Exception{ + DataCatalogueImpl instance = factory.getUtilsPerScope(scope); + String username = testUser; + long init = System.currentTimeMillis(); + instance.getUserRoleByOrganization(username); + long end = System.currentTimeMillis(); + logger.debug("Time taken " + (end - init)); + } + + + +// //@Test +// public void getSysadminEmail() throws Exception{ +// DataCatalogueImpl utils = factory.getUtilsPerScope(scope); +// System.out.println(utils.getCatalogueEmail()); +// } +// +// //@Test +// public void getStatistics() throws Exception{ +// +// DataCatalogueImpl utils = factory.getUtilsPerScope(scope); +// logger.debug("Statistics " + utils.getStatistics()); +// +// } +// +// //@Test +// public void getLandingPages() throws Exception{ +// +// DataCatalogueImpl utils = factory.getUtilsPerScope(scope); +// logger.debug("Landing pages " + utils.getLandingPages()); +// +// } +// +// //@Test +// public void getOrganizationForName() throws Exception{ +// +// String orgName = "nextnext"; +// +// DataCatalogueImpl utils = factory.getUtilsPerScope(scope); +// +// CkanOrganization org = utils.getOrganizationByName(orgName); +// +// logger.debug("The "+CkanOrganization.class.getSimpleName()+" is: "+org.getName()); +// logger.debug("LandingPages of "+CkanOrganization.class.getSimpleName()+" for name " + utils.getLandingPages()); +// if(org.getUsers()!=null) { +// +// for (CkanUser user : org.getUsers()) { +// logger.debug("User: "+user.getName()); +// } +// } +// } +// +// +// //@Test +// public void getRoleOfUserInOrganization() throws Exception{ +// +// String orgName = "nextnext"; +// +// DataCatalogueImpl utils = factory.getUtilsPerScope(scope); +// +// CkanOrganization org = utils.getOrganizationByName(orgName); +// +// String apiKey = utils.getApiKeyFromUsername(testUser); +// +// String role = utils.getRoleOfUserInOrganization(testUser, org.getName(), apiKey); +// +// logger.debug("The user "+testUser+" in the org "+org.getName() + " has the role "+role); +// } +// +// //@Test +// public void getGroupForName() throws Exception{ +// +// String groupName = "ariadne"; +// +// DataCatalogueImpl utils = factory.getUtilsPerScope(scope); +// +// CkanGroup org = utils.getGroupByName(groupName); +// +// logger.debug("The "+CkanGroup.class.getSimpleName()+" is: "+org.getName()); +// logger.debug("LandingPages of "+CkanGroup.class.getSimpleName()+" for name " + utils.getLandingPages()); +// if(org.getUsers()!=null) { +// +// for (CkanUser user : org.getUsers()) { +// logger.debug("User: "+user.getName()); +// } +// } +// } +// +// //@Test +// public void getDatasetIdsFromDB() throws Exception{ +// DataCatalogueImpl utils = factory.getUtilsPerScope(scope); +// List ids = utils.getProductsIdsInGroupOrOrg("aquamaps", true, 0, Integer.MAX_VALUE); +// logger.debug("Size is " + ids.size()); +// } +// +// //@Test +// public void searchInOrganization() throws Exception{ +// DataCatalogueImpl utils = factory.getUtilsPerScope(scope); +// String apiKey = utils.getApiKeyFromUsername(testUser); +// List matches = utils.searchForPackageInOrganization(apiKey, "\"asfis:HMC+eez:AGO;FAO+grsf-org:INT+eez:AGO;RFB+iso3:AGO+isscfg:01.1.1\"", 0, 10, "grsf_admin"); +// logger.debug("Size is " + matches.size()); +// } +// +// //@Test +// public void search() throws Exception{ +// DataCatalogueImpl utils = factory.getUtilsPerScope(scope); +// String apiKey = utils.getApiKeyFromUsername(testUser); +// List matches = utils.searchForPackage(apiKey, "\"asfis:HMC+eez:AGO;FAO+grsf-org:INT+eez:AGO;RFB+iso3:AGO+isscfg:01.1.1\"", 0, 10); +// logger.debug("Size is " + matches.size()); +// } +// +// // @Test +// public void testManageProduct() throws Exception{ +// +// DataCatalogueImpl catalogue = factory.getUtilsPerScope(scope); +// String apiKey = catalogue.getApiKeyFromUsername("costantino_perciante"); +// //Map> map = new HashMap>(); +// //map.put("a new custom field", Arrays.asList("a new custom field 2")); +// //catalogue.patchProductCustomFields("test-searchable-504043", apiKey, map); +// catalogue.removeCustomField("test-searchable-504043", "a new custom field", "a new custom field", apiKey); +// } +// +// //@Test +// public void getRole() throws Exception{ +// +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// instance.getOrganizationsAndRolesByUser(testUser, Arrays.asList(RolesCkanGroupOrOrg.ADMIN, RolesCkanGroupOrOrg.EDITOR, RolesCkanGroupOrOrg.MEMBER +// )); +// +// } +// +// //@Test +// public void datasetsRelationshipCreate() throws Exception{ +// +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// +// DatasetRelationships relation = DatasetRelationships.parent_of; +// +// boolean resC = instance.createDatasetRelationship(subjectId, objectId, relation, "Comment for this relationship", instance.getApiKeyFromUsername(testUser)); +// +// logger.debug("Res is " + resC); +// } +// +// //@Test +// public void datasetsRelationshipDelete() throws Exception{ +// +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// DatasetRelationships relation = DatasetRelationships.child_of; +// +// boolean resD = instance.deleteDatasetRelationship(subjectId, objectId, relation, instance.getApiKeyFromUsername(testUser)); +// +// logger.debug("ResD is " + resD); +// } +// +// //@Test +// public void datasetRelationshipRetrieve() throws Exception{ +// +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// +// List res = instance.getRelationshipDatasets(subjectId, objectId, instance.getApiKeyFromUsername(testUser)); +// +// logger.debug("Relationships " + res); +// +// } +// +// +// //@Test +// public void testgetApiKeyFromUser() throws Exception { +// +// logger.debug("Testing getApiKeyFromUser"); +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// +// String username = "francescomangiacrapa"; +// String key = instance.getApiKeyFromUsername(username); +// +// System.out.println("key for " + username + " is " + key); +// } +// +// //@Test +// public void testgetUserFromApiKey() throws Exception { +// +// logger.debug("Testing getApiKeyFromUser"); +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// +// String key = "put-your-key-here"; +// CKanUserWrapper user = instance.getUserFromApiKey(key); +// +// System.out.println("user for " + key + " is " + user); +// } +// +// //@Test +// public void getOrganizationsByUser() throws Exception { +// +// System.out.println("Testing getOrganizationsByUser"); +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// +// String username = "francescomangiacrapa"; +// List organizations = instance.getOrganizationsByUser(username); +// +// System.out.println("organizations for user " + username + " are: "); +// +// for (CkanOrganization ckanOrganization : organizations) { +// System.out.println("-" + ckanOrganization.getName()); +// } +// } +// +// //@Test +// public void getGroupsAndRolesByUser() throws Exception { +// +// logger.debug("Testing getGroupsAndRolesByUser"); +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// +// String username = "andrea.rossi"; +// instance = new DataCatalogueImpl("/gcube"); +// List rolesToMatch = new ArrayList(); +// rolesToMatch.add(RolesCkanGroupOrOrg.ADMIN); +// rolesToMatch.add(RolesCkanGroupOrOrg.MEMBER); +// rolesToMatch.add(RolesCkanGroupOrOrg.EDITOR); +// Map> map = instance.getOrganizationsAndRolesByUser(username, rolesToMatch); +// +// System.out.println("organizations for user " + username + " are " + map); +// } +// +// //@Test +// public void getUsers() throws Exception{ +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// +// List rolesToMatch = new ArrayList(); +// rolesToMatch.add(RolesCkanGroupOrOrg.ADMIN); +// rolesToMatch.add(RolesCkanGroupOrOrg.EDITOR); +// +// Map> orgs = instance.getOrganizationsAndRolesByUser("costantino_perciante", rolesToMatch); +// +// Iterator>> iterator = orgs.entrySet().iterator(); +// +// while (iterator.hasNext()) { +// Map.Entry> entry = (Map.Entry>) iterator +// .next(); +// +// logger.debug("Org is " + entry.getKey() + " and role is " + entry.getValue().get(0)); +// +// } +// } +// +// +// +// //@Test +// public void createUsers() throws Exception{ +// +// DataCatalogueImpl instance = factory.getUtilsPerScope("/gcube/devsec/devVRE"); +// CheckedCkanClient client = new CheckedCkanClient(instance.getCatalogueUrl(), instance.getApiKeyFromUsername("costantino_perciante")); +// CkanUser editorUser = new CkanUser("user_editor_devvre", "user_editor_devvre@test.it", ""); +// client.createUser(editorUser); +// CkanUser adminUser = new CkanUser("user_admin_devvre", "user_admin_devvre@test.it", ""); +// client.createUser(adminUser); +// CkanUser memberUser = new CkanUser("user_member_devvre", "user_member_devvre@test.it", ""); +// client.createUser(memberUser); +// +// } +// +// //@Test +// public void createAsEditor() throws Exception{ +// DataCatalogueImpl instance = factory.getUtilsPerScope("/gcube/devsec/devVRE"); +// boolean checkedEditor = instance.checkRoleIntoOrganization("user_editor_devvre", "devvre", RolesCkanGroupOrOrg.MEMBER); +// if(checkedEditor){ +// logger.debug("Created editor in devvre? " + checkedEditor); +// } +// boolean checkedAdmin = instance.checkRoleIntoOrganization("user_admin_devvre", "devvre", RolesCkanGroupOrOrg.MEMBER); +// if(checkedAdmin){ +// logger.debug("Created admin in devvre? " + checkedAdmin); +// } +// boolean checkedMember = instance.checkRoleIntoOrganization("user_member_devvre", "devvre", RolesCkanGroupOrOrg.MEMBER); +// if(checkedMember){ +// logger.debug("Created member in devvre? " + checkedMember); +// } +// } +// +// //@Test +// public void editorCreateDataset() throws Exception{ +// +// DataCatalogueImpl instance = factory.getUtilsPerScope("/gcube/devsec/devVRE"); +// instance.createCKanDataset(instance.getApiKeyFromUsername("user_editor_devvre"), "dataset_as_editor_devvre_private", null, "devvre", null, null, null, null, 1, null, null, null, null, null, false); +// +// } +// +// //@Test +// public void adminCreateDataset() throws Exception{ +// +// DataCatalogueImpl instance = factory.getUtilsPerScope("/gcube/devsec/devVRE"); +// //instance.createCKanDataset(instance.getApiKeyFromUsername("user_admin_devvre"), "dataset_as_admin_devvre", "devvre", null, null, null, null, 1, null, null, null, null, null, false); +// instance.createCKanDataset(instance.getApiKeyFromUsername("user_admin_devvre"), "dataset_as_admin_devvre_private", null, "devvre", null, null, null, null, 1, null, null, null, null, null, false); +// } +// +// //@Test +// public void adminChangeVisibility() throws Exception{ +// +// DataCatalogueImpl instance = factory.getUtilsPerScope("/gcube/devsec/devVRE"); +// //instance.createCKanDataset(instance.getApiKeyFromUsername("user_editor_devvre"), "dataset_as_editor_devvre", "devvre", null, null, null, null, 1, null, null, null, null, null, false); +// //instance.setDatasetPrivate(true, "3571cca5-b0ae-4dc6-b791-434a8e062ce5", "dataset_as_admin_devvre_public", instance.getApiKeyFromUsername("user_admin_devvre")); +// +// boolean res = instance.setDatasetPrivate(false, "3571cca5-b0ae-4dc6-b791-434a8e062ce5", "33bbdcb1-929f-441f-8718-a9e5134f517d", instance.getApiKeyFromUsername("user_admin_devvre")); +// logger.debug(""+res); +// +// // CheckedCkanClient client = new CheckedCkanClient(instance.getCatalogueUrl(), instance.getApiKeyFromUsername("user_admin_devvre")); +// // CkanDataset dataset = client.getDataset("dataset_as_admin_devvre_private"); +// // logger.debug("Current value for private: " + dataset.isPriv()); +// // dataset.setPriv(!dataset.isPriv()); +// // CkanDataset datasetUpd = client.updateDataset(dataset); +// // logger.debug("Private value is " + datasetUpd.isPriv()); +// // +// +// } +// +// //@Test +// public void testInvalidOrgRole() throws Exception{ +// +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// boolean res = instance.isRoleAlreadySet("francesco_mangiacrapa", "devvre_group", RolesCkanGroupOrOrg.ADMIN, true); +// logger.debug(""+res); +// +// // set to editor +// instance.checkRoleIntoGroup("francesco_mangiacrapa", "devvre_group", RolesCkanGroupOrOrg.EDITOR); +// +// } +// +// //@Test +// public void existProductWithNameOrId() throws Exception{ +// +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// boolean res = instance.existProductWithNameOrId("notification_portlet_2"); +// logger.debug(""+res); +// +// } +// +// //@Test +// public void testGroupAssociation() throws Exception{ +// +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// String datasetId = "test_grsf_group_stock_groups_sadsad"; +// String groupName = "assessment-unit"; +// instance.assignDatasetToGroup(groupName, datasetId, instance.getApiKeyFromUsername("costantino_perciante")); +// +// } +// +// //@Test +// public void testAddResource() throws Exception{ +// +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// String datasetId = "test_publish_folder_15_44"; +// //instance.assignDatasetToGroup(groupName, datasetId, instance.getApiKeyFromUsername("costantino_perciante")); +// +// String api = instance.getApiKeyFromUsername("costantino_perciante"); +// CheckedCkanClient client = new CheckedCkanClient(instance.getCatalogueUrl(), api); +// List randomName = Arrays.asList("FIRMS", "RAM", "FishSource"); +// for (int i = 0; i < 100; i++) { +// +// CkanResource resource = new CkanResource("https://goo.gl/FH5AQ5", datasetId); +// String name = randomName.get((int)Math.round(Math.ceil(Math.random() * 3))); +// +// resource.setName(name); +// client.createResource(resource); +// +// } +// } +// +// //@Test +// public void checkGroupRole() throws Exception{ +// +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// String role = instance.getRoleOfUserInGroup("francesco.mangiacrapa", "test-by-francesco", instance.getApiKeyFromUsername("francesco.mangiacrapa")); +// logger.debug("Role is " + role); +// } +// +// //@Test +// public void getURL() throws Exception{ +// DataCatalogueImpl util = factory.getUtilsPerScope("/gcube/devNext/NextNext"); +// +// CkanDataset dataset = new CkanDataset(); +// CheckedCkanClient client = new CheckedCkanClient(util.getCatalogueUrl(), util.getApiKeyFromUsername("costantino_perciante")); +// +// dataset.setName("random-test-" + UUID.randomUUID().toString().substring(0, 5)); +// dataset.setOwnerOrg(client.getOrganization("devvre").getId()); +// dataset.setOpen(true); +// dataset.setPriv(true); +// //CkanDataset id = client.createDataset(dataset); +// +// // util.setDatasetPrivate(true, client.getOrganization("devvre").getId(), id.getId(), util.getApiKeyFromUsername("costantino_perciante")); +// +// } +// +// //@Test +// public void deleteAndPurgeDataset() throws Exception{ +// +// DataCatalogueImpl util = factory.getUtilsPerScope("/gcube/devNext/NextNext"); +// boolean deleted = util.deleteProduct("random-test-56sadadsfsdf", util.getApiKeyFromUsername("user_admin_devvre"), true); +// logger.debug("Deleted ? " + deleted); +// +// } +// +// // @Test +// public void testSearchableSet() throws Exception{ +// +// DataCatalogueImpl util = factory.getUtilsPerScope("/gcube/devNext/NextNext"); +// boolean setSearchability = util.setSearchableField("1b261d07-9f9c-414f-ad8d-c5aa429548fc", false); +// logger.debug("Searchability set? " + setSearchability); +// } +// +// //@Test +// public void testNameConversion(){ +// +// logger.debug(CatalogueUtilMethods.fromCKanUsernameToUsername("costantino_perciante")); +// +// } +// +// //@Test +// public void getGroupsRoles() throws Exception{ +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// Map> res = instance.getRolesAndUsersGroup("abundance-level"); +// logger.debug(res.toString()); +// +// } +// +// //@Test +// public void testResourcePatch() throws Exception{ +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// String id = "858f5e77-80c2-4cb2-bcfc-77529693dc9a"; +// instance.patchResource(id, "http://ftp.d4science.org/previews/69cc0769-de6f-45eb-a842-7be2807e8887.jpg", "new_name_for_testing_patch.csv", "description test", "", instance.getApiKeyFromUsername("costantino_perciante")); +// } +// +// //@Test +// public void testPatchProduct() throws Exception{ +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// Map> customFieldsToChange = new HashMap>(); +// customFieldsToChange.put("Status", Arrays.asList("Pending")); +// instance.patchProductCustomFields("a-test-to-ignore", instance.getApiKeyFromUsername("costantino_perciante"), customFieldsToChange, false); +// } +// +// //@Test +// public void addTag()throws Exception{ +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// instance.addTag("test-after-tags-editing", instance.getApiKeyFromUsername("costantino_perciante"), "api add tag to test"); +// } +// +// //@Test +// public void removeTag()throws Exception{ +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// instance.removeTag("test-after-tags-editing", instance.getApiKeyFromUsername("costantino_perciante"), "api add tag to test"); +// +// } +// +// //@Test +// public void removeGroup()throws Exception{ +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// instance.removeDatasetFromGroup("pending", "test-after-tags-editing", instance.getApiKeyFromUsername("costantino_perciante")); +// +// } +// +// //@Test +// public void getGroups() throws Exception{ +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// instance.getParentGroups("abundance-level", instance.getApiKeyFromUsername("costantino_perciante")); +// +// } +// +// // @Test +// public void createGroupsAndSetAsFather() throws Exception{ +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// //CkanGroup childGroup = instance.createGroup("test-group-child", "A child group", null); +// //CkanGroup parentGroup = instance.createGroup("test-group-parent", "A parent group", null); +// instance.setGroupParent("test-group-parent", "test-group-child"); +// } +// +// //@Test +// public void testBelongsToGroup() throws Exception{ +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// boolean checked = instance.isDatasetInGroup("assessment-unit", "test-after-updates-17-1654"); +// logger.debug("Result is " + checked); +// } +// +// // @Test +// public void getDatasetsInGroup() throws Exception{ +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// List result = instance.getProductsInGroup("assessment-unit"); +// for (CkanDataset ckanDataset : result) { +// logger.debug("Dataset name is " + ckanDataset.getName()); +// } +// } +// +// +// //@Test +// public void getHigher(){ +// logger.debug("Max is " + RolesCkanGroupOrOrg.getHigher(RolesCkanGroupOrOrg.ADMIN, RolesCkanGroupOrOrg.ADMIN)); +// } +// +// //@Test +// public void getUrlProduct() throws Exception{ +// DataCatalogueImpl instance = factory.getUtilsPerScope(scope); +// String datasetName = "test_from_andrea_rossi"; +// String url = instance.getUrlFromDatasetIdOrName(datasetName); +// logger.debug("url is " + url); +// } +// +// //@Test +// public void getCatalogueURLs() throws Exception{ +// DataCatalogueImpl util = factory.getUtilsPerScope("/gcube/devNext/NextNext"); +// logger.debug("MAP ACCESS URL TO CATALOGUE:" +util.getMapAccessURLToCatalogue()); +// +// } + +} diff --git a/src/test/resources/.gitignore b/src/test/resources/.gitignore new file mode 100644 index 0000000..8f08e73 --- /dev/null +++ b/src/test/resources/.gitignore @@ -0,0 +1,7 @@ +/devNext.gcubekey +/devsec.gcubekey +/gcube.gcubekey +/howto.txt +/log4j.properties +/pred4s.gcubekey +/preprod.gcubekey diff --git a/target/classes/META-INF/MANIFEST.MF b/target/classes/META-INF/MANIFEST.MF index 0048ac8..e5cf844 100644 --- a/target/classes/META-INF/MANIFEST.MF +++ b/target/classes/META-INF/MANIFEST.MF @@ -6,7 +6,7 @@ Specification-Version: 0.1.0-SNAPSHOT Implementation-Title: CKan utility library Implementation-Version: 0.1.0-SNAPSHOT Implementation-Vendor-Id: org.gcube.datacatalogue -Build-Time: 20200601-134620 +Build-Time: 20200601-145428 Created-By: Maven Integration for Eclipse SCM-Branch: SCM-Revision: diff --git a/target/classes/META-INF/maven/org.gcube.datacatalogue/catalogue-util-library/pom.properties b/target/classes/META-INF/maven/org.gcube.datacatalogue/catalogue-util-library/pom.properties index c1f2a7e..bff563c 100644 --- a/target/classes/META-INF/maven/org.gcube.datacatalogue/catalogue-util-library/pom.properties +++ b/target/classes/META-INF/maven/org.gcube.datacatalogue/catalogue-util-library/pom.properties @@ -1,5 +1,5 @@ #Generated by Maven Integration for Eclipse -#Mon Jun 01 15:46:21 CEST 2020 +#Mon Jun 01 16:54:29 CEST 2020 version=0.1.0-SNAPSHOT groupId=org.gcube.datacatalogue m2e.projectName=catalogue-util-library diff --git a/target/classes/META-INF/maven/org.gcube.datacatalogue/catalogue-util-library/pom.xml b/target/classes/META-INF/maven/org.gcube.datacatalogue/catalogue-util-library/pom.xml index 668791f..e930208 100644 --- a/target/classes/META-INF/maven/org.gcube.datacatalogue/catalogue-util-library/pom.xml +++ b/target/classes/META-INF/maven/org.gcube.datacatalogue/catalogue-util-library/pom.xml @@ -61,6 +61,12 @@ compile + + org.postgresql + postgresql + ${postgresVersion} + + org.gcube.data-publishing gcat-client diff --git a/target/classes/org/gcube/datacatalogue/ckanutillibrary/db/DBCaller.class b/target/classes/org/gcube/datacatalogue/ckanutillibrary/db/DBCaller.class index ab42840..6147ada 100644 Binary files a/target/classes/org/gcube/datacatalogue/ckanutillibrary/db/DBCaller.class and b/target/classes/org/gcube/datacatalogue/ckanutillibrary/db/DBCaller.class differ diff --git a/target/classes/org/gcube/datacatalogue/ckanutillibrary/db/DBCaller.java b/target/classes/org/gcube/datacatalogue/ckanutillibrary/db/DBCaller.java index 7be49c9..f989172 100644 --- a/target/classes/org/gcube/datacatalogue/ckanutillibrary/db/DBCaller.java +++ b/target/classes/org/gcube/datacatalogue/ckanutillibrary/db/DBCaller.java @@ -15,6 +15,13 @@ import org.gcube.datacatalogue.ckanutillibrary.shared.RolesCkanGroupOrOrg; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +// TODO: Auto-generated Javadoc +/** + * The Class DBCaller. + * + * @author Francesco Mangiacrapa at ISTI-CNR Pisa (Italy) + * Jun 1, 2020 + */ public class DBCaller { private static final Logger LOG = LoggerFactory.getLogger(DBCaller.class); @@ -25,9 +32,21 @@ public class DBCaller { private String CKAN_DB_USER; private String CKAN_DB_PASSWORD; + /** + * Instantiates a new DB caller. + */ public DBCaller() { } + /** + * Instantiates a new DB caller. + * + * @param cKAN_DB_URL the c KA N D B URL + * @param cKAN_DB_PORT the c KA N D B PORT + * @param cKAN_DB_NAME the c KA N D B NAME + * @param cKAN_DB_USER the c KA N D B USER + * @param cKAN_DB_PASSWORD the c KA N D B PASSWORD + */ public DBCaller(String cKAN_DB_URL, Integer cKAN_DB_PORT, String cKAN_DB_NAME, String cKAN_DB_USER, String cKAN_DB_PASSWORD) { CKAN_DB_URL = cKAN_DB_URL; @@ -39,14 +58,15 @@ public class DBCaller { /** - * Retrieve connection from the pool + * Retrieve connection from the pool. + * * @return a connection available within the pool - * @throws SQLException - * @throws ClassNotFoundException + * @throws SQLException the SQL exception + * @throws ClassNotFoundException the class not found exception */ private Connection getConnection() throws SQLException, ClassNotFoundException{ - LOG.debug("CONNECTION REQUEST"); + LOG.trace("CONNECTION REQUEST"); // create db connection Class.forName("org.postgresql.Driver"); @@ -57,6 +77,8 @@ public class DBCaller { dbBaseConnectionURL+="/" + CKAN_DB_NAME; + LOG.debug("DB CONNECTION URL: "+dbBaseConnectionURL); + Connection connection = DriverManager.getConnection(dbBaseConnectionURL, CKAN_DB_USER, CKAN_DB_PASSWORD); // Connection connection = DriverManager.getConnection( @@ -67,13 +89,13 @@ public class DBCaller { /** * Retrieve the map (groups id, capacity) with the groups to which the user belongs by querying directly the database. - * @param username - * @return - * @throws SQLException - * @throws ClassNotFoundException + * + * @param username the username + * @return the groups by user from DB */ - public Map getGroupsByUserFromDB(String userId){ - checkNotNull(userId); + public Map getGroupsByUserFromDB(String username){ + checkNotNull(username); + LOG.debug("Get groups by user called"); //couples (groups id, capacity) of the user in the group @@ -81,18 +103,20 @@ public class DBCaller { Connection connection = null; try{ - connection = getConnection(); - ResultSet rs; + String userId = getUserIdForUsername(username); + connection = getConnection(); + 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\"=?;"; - + + LOG.debug("Performing query: "+joinQuery); PreparedStatement preparedStatement = connection.prepareStatement(joinQuery); preparedStatement.setString(1, userId); preparedStatement.setBoolean(2, false); - rs = preparedStatement.executeQuery(); + ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { @@ -108,19 +132,62 @@ public class DBCaller { return toReturn; } + /** - * Retrieve the map (organisation id, capacity) with the organisations to which the user belongs by querying directly the database. - * @param username - * @return - * @throws SQLException - * @throws ClassNotFoundException + * Gets the user id for username. + * + * @param username the username + * @return the user id for username */ - public Map getOrganizationsByUserFromDB(String userId){ - checkNotNull(userId); + private String getUserIdForUsername(String username) { + LOG.debug("Get user id for username called"); + + Connection connection = null; + String userId = null; + try{ + + connection = getConnection(); + + String selQuery = "SELECT \"id\" FROM \"public\".\"user\" WHERE \"name\"=?;"; + + LOG.info("Performing query: "+selQuery); + + PreparedStatement preparedStatement = connection.prepareStatement(selQuery); + preparedStatement.setString(1, username); + ResultSet rs = preparedStatement.executeQuery(); + + while (rs.next()) { + userId = rs.getString("id"); + break; + } + + LOG.debug("For username "+username+" returning the user id: "+userId); + return userId; + + }catch(Exception e){ + LOG.error("Failed to retrieve the user id for username:"+username+". Error is " + e.getMessage()); + return null; + }finally{ + closeConnection(connection); + } + + } + + /** + * Retrieve the map (organisation id, capacity) with the organisations to which the user belongs by querying directly the database. + * + * @param username the username + * @return the organizations by user from DB + */ + public Map getOrganizationsByUserFromDB(String username){ + LOG.debug("Get organisations by user called"); + checkNotNull(username); Map toReturn = new HashMap(); Connection connection = null; try{ + + String userId = getUserIdForUsername(username); connection = getConnection(); ResultSet rs; @@ -150,6 +217,13 @@ public class DBCaller { } + /** + * Gets the api key from username. + * + * @param username the username + * @param state the state + * @return the api key from username + */ public String getApiKeyFromUsername(String username, String state) { LOG.debug("Request api key for user = " + username); @@ -191,14 +265,16 @@ public class DBCaller { /** - * Tries to close a connection - * @param connection + * Tries to close a connection. + * + * @param connection the connection */ private void closeConnection(Connection connection){ if(connection != null){ try{ connection.close(); + LOG.trace("CONNECTION CLOSED"); }catch(Exception e){ LOG.error("Unable to close this connection ", e); } diff --git a/target/classes/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogue.class b/target/classes/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogue.class index dc5abaf..8b83f35 100644 Binary files a/target/classes/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogue.class and b/target/classes/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogue.class differ diff --git a/target/classes/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogue.java b/target/classes/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogue.java index 800a459..e8b5494 100644 --- a/target/classes/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogue.java +++ b/target/classes/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogue.java @@ -11,12 +11,19 @@ import eu.trentorise.opendata.jackan.model.CkanGroup; import eu.trentorise.opendata.jackan.model.CkanLicense; import eu.trentorise.opendata.jackan.model.CkanOrganization; +// TODO: Auto-generated Javadoc +/** + * The Interface DataCatalogue. + * + * @author Francesco Mangiacrapa at ISTI-CNR Pisa (Italy) + * Jun 1, 2020 + */ public interface DataCatalogue { /** - * Finds the id associated to the chosen license - * - * @param chosenLicense + * Finds the id associated to the chosen license. + * + * @param chosenLicense the chosen license * @return the id on success, null otherwise */ String findLicenseIdByLicenseTitle(String chosenLicense); @@ -26,8 +33,8 @@ public interface DataCatalogue { * contacting the uri resolver. If no uri resolver is available, an url that is * not guaranteed to be long term valid will be generated. Information are not * encrypted. - * - * @param datasetId + * + * @param datasetIdOrName the dataset id or name * @return The url of the dataset on success, null otherwise */ String getUnencryptedUrlFromDatasetIdOrName(String datasetIdOrName); @@ -37,23 +44,23 @@ public interface DataCatalogue { * values for the same custom field key. NOTE: unfortunately java doesn't * support overload in java interface methods (that's way I cannot use the same * name for the method) - * - * @param title + * + * @param title the title * @param name (unique identifier) - * @param organizationNameOrId - * @param author - * @param authorMail - * @param maintainer - * @param maintainerMail - * @param version - * @param description - * @param licenseId - * @param tags - * @param customFields - * @param resources + * @param organizationNameOrId the organization name or id + * @param author the author + * @param authorMail the author mail + * @param maintainer the maintainer + * @param maintainerMail the maintainer mail + * @param version the version + * @param description the description + * @param licenseId the license id + * @param tags the tags + * @param customFields the custom fields + * @param resources the resources * @param setPublic (manage visibility: Admin role is needed) * @return the id of the dataset on success, null otherwise - * @throws Exception + * @throws Exception the exception */ String createCKanDatasetMultipleCustomFields(String title, String name, String organizationNameOrId, String author, String authorMail, String maintainer, String maintainerMail, long version, String description, @@ -66,11 +73,11 @@ public interface DataCatalogue { * If a custom field with a given key already exists, and removeOld is set to * false, the new values are added at the end of the list. Otherwise they are * lost. - * - * @param productId - * @param customFieldsToChange - * @param removeOld - * @return + * + * @param productId the product id + * @param customFieldsToChange the custom fields to change + * @param removeOld the remove old + * @return true, if successful */ boolean patchProductCustomFields(String productId, Map> customFieldsToChange, boolean removeOld); @@ -78,40 +85,26 @@ public interface DataCatalogue { /** * Add a resource described by the bean to the dataset id into * resource.datasetId - * - * @param resource + * + * @param resource the resource * @return String the id of the resource on success, null otherwise + * @throws Exception the exception */ String addResourceToDataset(ResourceBean resource) throws Exception; /** * Remove the resource with id resourceId from the dataset in which it is. - * - * @param resourceId + * + * @param resourceId the resource id * @return true on success, false otherwise. */ boolean deleteResourceFromDataset(String resourceId); /** * Create a dataset with those information. - * - * @param apiKey - * @param title - * @param name (unique identifier) - * @param organizationNameOrId - * @param author - * @param authorMail - * @param maintainer - * @param maintainerMail - * @param version - * @param description - * @param licenseId - * @param tags - * @param customFields - * @param resources - * @param setPublic (manage visibility: Admin role is needed) + * + * @param nameOrId the name or id * @return the id of the dataset on success, null otherwise - * @throws Exception */ /** @@ -123,16 +116,6 @@ public interface DataCatalogue { */ boolean existProductWithNameOrId(String nameOrId); - /** - * The method returns the role the user has in the groups he/she belongs to (it - * uses the db, so it is much faster) - * - * @param username - * @param apiKey - * @return - */ - Map> getUserRoleByGroup(String username, String apiKey); - /** * Get the list of licenses' titles. * @@ -141,30 +124,30 @@ public interface DataCatalogue { List getLicenseTitles(); /** - * Retrieve ckan licenses - * - * @return + * Retrieve ckan licenses. + * + * @return the licenses */ List getLicenses(); /** - * Retrieve the url of the uri resolver for this catalogue instance/scope - * - * @return + * Retrieve the url of the uri resolver for this catalogue instance/scope. + * + * @return the uri resolver url */ String getUriResolverUrl(); /** - * Return the manage product property - * + * Return the manage product property. + * * @return the manage product property */ boolean isManageProductEnabled(); /** * Return the catalogue portlet for this context(i.e. scope) - * - * @return + * + * @return the portlet url */ String getPortletUrl(); @@ -177,24 +160,45 @@ public interface DataCatalogue { /** - * Returns the list of organizations to whom the user belongs (with any role) - * @param username + * Returns the list of organizations to whom the user belongs (with any role). + * + * @param username the username * @return a list of organizations */ List getOrganizationsByUser(String username); /** - * Returns the list of groups to whom the user belongs (with any role) - * @param username + * Returns the list of groups to whom the user belongs (with any role). + * + * @param username the username * @return a list of groups */ List getGroupsByUser(String username); /** - * Retrieve a ckan dataset given its id - * @param datasetId - * @return + * Retrieve a ckan dataset given its id. + * + * @param datasetId the dataset id + * @param apiKey the api key + * @return the dataset */ CkanDataset getDataset(String datasetId, String apiKey); + + /** + * Gets the user role by group. + * + * @param username the username + * @return the user role by group + */ + Map> getUserRoleByGroup(String username); + + /** + * The method returns the role the user has in the organizations he/she belongs to (it uses the db, so it is much faster) + * @param username + * @param apiKey + * @return + */ + Map> getUserRoleByOrganization(String username); + } diff --git a/target/classes/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.class b/target/classes/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.class index 80c093a..8f19b4d 100644 Binary files a/target/classes/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.class and b/target/classes/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.class differ diff --git a/target/classes/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.java b/target/classes/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.java index c2f2839..099f1e9 100644 --- a/target/classes/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.java +++ b/target/classes/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.java @@ -280,14 +280,9 @@ public class DataCatalogueImpl implements DataCatalogue { } @Override - public Map> getUserRoleByGroup( - String username, String apiKey) { - + public Map> getUserRoleByGroup(String username) { + LOG.info("Get user role by group called. The username is: "+username); checkNotNull(username); - checkNotNull(apiKey); - - checkNotNull(username); - checkNotNull(apiKey); Map> toReturn = new HashMap>(); @@ -313,6 +308,36 @@ public class DataCatalogueImpl implements DataCatalogue { } + @Override + public Map> getUserRoleByOrganization( + String username) { + LOG.info("Get user role by organization called. The username is: "+username); + + checkNotNull(username); + + Map> toReturn = new HashMap>(); + + try{ + + String ckanUsername = CatalogueUtilMethods.fromUsernameToCKanUsername(username); + Map partialResult = dbCaller.getOrganizationsByUserFromDB(ckanUsername); + + for (String orgID : partialResult.keySet()) { + + CkanOrganization org = ckanCaller.getOrganization(orgID); + HashMap subMap = new HashMap(); + subMap.put(org, partialResult.get(orgID)); + toReturn.put(orgID, subMap); + } + + LOG.debug("Returning map " + toReturn); + }catch(Exception e){ + LOG.error("Failed to retrieve roles of user in his/her own groups",e); + } + + return toReturn; + } + /** * Retrieve an url for the tuple scope, entity, entity name