new method for retrieving ids of products from a group/organization without limits added

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@140054 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-12-16 17:25:38 +00:00
parent 5c1ca13f14
commit 4239ed4c3b
2 changed files with 90 additions and 7 deletions

View File

@ -1,6 +1,7 @@
package org.gcube.datacatalogue.ckanutillibrary;
import java.io.File;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
@ -287,7 +288,7 @@ public interface DataCatalogue {
* @return the created CkanGroup on success, null otherwise
*/
CkanGroup createGroup(String nameOrId, String title, String description);
/**
* Associate a group with its parent.s
* @param parentName
@ -412,7 +413,7 @@ public interface DataCatalogue {
* @return true on success, false otherwise
*/
boolean patchProductCustomFields(String productId, String apiKey, Map<String, List<String>> customFieldsToChange);
/**
* Remove a custom field in the product that has a given key and value. If more than ones are present, the first one is removed.
* @return true on success, false otherwise.
@ -442,7 +443,7 @@ public interface DataCatalogue {
* @return the group parent, if any
*/
List<CkanGroup> getParentGroups(String groupName, String apiKey);
/**
* Check if a dataset is into the given group
* @param groupName
@ -450,7 +451,7 @@ public interface DataCatalogue {
* @return true if it belongs to the group, false otherwise
*/
boolean isDatasetInGroup(String groupName, String datasetId);
/**
* Retrieve products in a group. Please note that at most 1000 datasets are returned.
* @return a list of datasets in a group
@ -462,7 +463,7 @@ public interface DataCatalogue {
* @return
*/
String getUriResolverUrl();
/**
* Require to patch a product according to the content of the parameter jsonRequest
* @param productId
@ -471,4 +472,24 @@ public interface DataCatalogue {
* @return error message if any, null otherwise
*/
String patchProductWithJSON(String productId, JSONObject jsonRequest, String apiKey);
/**
* Ckan APIs group_show and organization_show with the property "include_datasets=true"
* return at most 1000 products. This method, instead, let retrieve all the
* products (ids) in a group or organization.
* The query is performed against the solr index.
* Take a look
* <ul>
* <li>https://github.com/ckan/ckan/blob/master/ckan/lib/dictization/model_dictize.py
* <li>https://github.com/ckan/ckan/blob/master/ckan/lib/search/query.py
* </ul>
* @param orgOrGroupName
* @param isOrganization: true if the owner entity is an organization, false if it is a group
* @param start the initial row (e.g., 0)
* @param rows the number of rows from start (e.g., 10)
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
List<String> getProductsIdsInGroupOrOrg(String orgOrGroupName, boolean isOrganization, int start, int rows) throws ClassNotFoundException, SQLException;
}

View File

@ -6,6 +6,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.sql.Connection;
@ -1931,7 +1932,7 @@ public class DataCatalogueImpl implements DataCatalogue{
Map<String, List<String>> fromCKANCustomFields = new HashMap<String, List<String>>();
CheckedCkanClient client = new CheckedCkanClient(CKAN_CATALOGUE_URL, apiKey);
List<CkanPair> extras = client.getDataset(productId).getExtras();
if(extras == null)
extras = new ArrayList<CkanPair>();
@ -2377,4 +2378,65 @@ public class DataCatalogueImpl implements DataCatalogue{
}
}
}
@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();
if(isOrganization){
String joinQuery = "SELECT \"package\".\"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 ?; ";
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));
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
toReturn.add(rs.getString("id"));
}
}else{
/**
* 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\" FROM \"package\" INNER JOIN \"member\" ON"
+ " \"member\".\"table_id\"=\"package\".\"id\" WHERE \"group_id\"=? "
+ "AND \"member\".\"state\"=? LIMIT ? OFFSET ?;";
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));
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
toReturn.add(rs.getString("table_id"));
}
}
return toReturn;
}
}