updated the getDataset

This commit is contained in:
Francesco Mangiacrapa 2021-02-17 12:09:02 +01:00
parent 484f354170
commit 427e00903d
4 changed files with 321 additions and 134 deletions

View File

@ -40,14 +40,14 @@ public class GCatCaller {
/**
* Gets the dataset for name.
*
* @param datasetIdOrName the dataset id or name
* @param datasetName the dataset name
* @return the jsonValue
* @throws WebApplicationException
* @throws WebApplicationException the web application exception
* @throws MalformedURLException the malformed URL exception
*/
public String getDatasetForName(String datasetIdOrName) throws WebApplicationException, MalformedURLException {
LOG.debug("Get dataset for name "+datasetIdOrName+ "called");
return new Item().read(datasetIdOrName);
public String getDatasetForName(String datasetName) throws WebApplicationException, MalformedURLException {
LOG.debug("Get dataset for name "+datasetName+ "called");
return new Item().read(datasetName);
}
@ -57,7 +57,7 @@ public class GCatCaller {
* @param jsonDataset the json dataset
* @param socialPost if true sends the social post
* @return the jsonValue
* @throws WebApplicationException
* @throws WebApplicationException the web application exception
* @throws MalformedURLException the malformed URL exception
*/
public String createDataset(String jsonDataset, boolean socialPost) throws WebApplicationException, MalformedURLException {
@ -122,9 +122,8 @@ public class GCatCaller {
* @param datasetName the dataset name
* @param jsonObj the json obj
* @return the string
* @throws MalformedURLException
* @throws WebApplicationException
* @throws Exception the exception
* @throws WebApplicationException the web application exception
* @throws MalformedURLException the malformed URL exception
*/
public String patchDataset(String datasetName, JSONObject jsonObj) throws WebApplicationException, MalformedURLException {
LOG.debug("Patch dataset called");

View File

@ -109,11 +109,11 @@ public interface DataCatalogue {
/**
* Retrieve a ckan dataset given its id.
*
* @param datasetId the dataset id
* @param apiKey the api key. If null uses gCat to get the Dataset
* @param datasetIdOrName the dataset id or name
* @param username the username
* @return the dataset
*/
CkanDataset getDataset(String datasetId, String apiKey);
CkanDataset getDataset(String datasetIdOrName, String username);
/**
* Gets the user role by group.
@ -399,6 +399,21 @@ public interface DataCatalogue {
* @return true, if successful
*/
boolean assignDatasetToGroup(String groupNameOrId, String datasetNameOrId);
/**
* Patch a product with product id productId by using the couples in customFieldsToChange.
* NOTE: only the specified custom fields will be changed. 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 the product id
* @param username the username
* @param customFieldsToChange the custom fields to change
* @param removeOld the remove old
* @return true, if successful
*/
boolean patchProductCustomFields(String productId, String username, Map<String, List<String>> customFieldsToChange,
boolean removeOld);
/**
@ -425,4 +440,6 @@ public interface DataCatalogue {
*/
void assignRolesOtherOrganization(String username, String sourceOrganization, RolesCkanGroupOrOrg currentRole);
}

View File

@ -3,12 +3,6 @@ package org.gcube.datacatalogue.utillibrary.server;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
@ -31,7 +25,6 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.common.scope.impl.ScopeBean.Type;
@ -54,6 +47,7 @@ import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanDataset;
import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanGroup;
import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanLicense;
import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanOrganization;
import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanPair;
import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanResource;
import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanUser;
import org.gcube.datacatalogue.utillibrary.shared.jackan.model.exceptions.JackanException;
@ -118,8 +112,9 @@ public class DataCatalogueImpl implements DataCatalogue {
/**
* The ckan catalogue url and database will be discovered in this scope
* @param scope
* The ckan catalogue url and database will be discovered in this scope.
*
* @param scope the scope
* @throws Exception if unable to find datacatalogue info
*/
public DataCatalogueImpl(String scope) throws Exception{
@ -174,12 +169,18 @@ public class DataCatalogueImpl implements DataCatalogue {
ckanCaller = new ExtendCkanClient(CKAN_CATALOGUE_URL, CKAN_TOKEN_SYS);
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#getCatalogueUrl()
*/
@Override
public String getCatalogueUrl() {
return CKAN_CATALOGUE_URL;
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#getPortletUrl()
*/
@Override
public String getPortletUrl() {
@ -207,6 +208,9 @@ public class DataCatalogueImpl implements DataCatalogue {
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#findLicenseIdByLicenseTitle(java.lang.String)
*/
@Override
public String findLicenseIdByLicenseTitle(String chosenLicense) {
LOG.debug("Requested license id");
@ -225,6 +229,9 @@ public class DataCatalogueImpl implements DataCatalogue {
return null;
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#getLicenseTitles()
*/
@Override
public List<String> getLicenseTitles() {
@ -244,6 +251,9 @@ public class DataCatalogueImpl implements DataCatalogue {
return result;
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#getLicenses()
*/
@Override
public List<CkanLicense> getLicenses() {
LOG.debug("Request for CKAN licenses (original jackan objects are going to be retrieved)");
@ -251,38 +261,36 @@ public class DataCatalogueImpl implements DataCatalogue {
return ckanCaller.getLicenseList();
}
/*
* (non-Javadoc)
*
* @see
* org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue#getDataset(java.
* lang.String, java.lang.String)
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#getDataset(java.lang.String, java.lang.String)
*/
@Override
public CkanDataset getDataset(String datasetId, String apiKey) {
LOG.info("Request ckan dataset with id " + datasetId);
public CkanDataset getDataset(String datasetIdOrName, String username) {
LOG.info("Called getDataset with id or name" + datasetIdOrName+ " and username: "+username);
// checks
checkNotNull(datasetId);
checkArgument(!datasetId.isEmpty());
checkNotNull(datasetIdOrName);
checkArgument(!datasetIdOrName.isEmpty());
try {
if (apiKey != null && !apiKey.isEmpty()) {
LOG.info("API-KEY found. Calling the " + ExtendCkanClient.class.getSimpleName());
if (username != null && !username.isEmpty()) {
LOG.info("username found. Calling the " + ExtendCkanClient.class.getSimpleName());
String apiKey = getApiKeyFromUsername(username);
ExtendCkanClient client = new ExtendCkanClient(CKAN_CATALOGUE_URL, apiKey);
return client.getDataset(datasetId);
return client.getDataset(datasetIdOrName);
}
String authzToken = SecurityTokenProvider.instance.get();
if (authzToken != null && !authzToken.isEmpty()) {
LOG.info("gcube-token found. Calling the gCat client");
String jsonDataset = gCatCaller.getDatasetForName(datasetId);
return MarshUnmarshCkanObject.toCkanDataset(jsonDataset, METHOD.TO_READ);
}
// String authzToken = SecurityTokenProvider.instance.get();
// if (authzToken != null && !authzToken.isEmpty()) {
// LOG.info("gcube-token found. Calling the gCat client");
// String jsonDataset = gCatCaller.getDatasetForName(datasetId);
// return MarshUnmarshCkanObject.toCkanDataset(jsonDataset, METHOD.TO_READ);
// }
LOG.info("No api-key or gcube-token found. Calling Ckan Client without API-KEY");
return ckanCaller.getDataset(datasetId);
LOG.info("No username found. Calling Ckan Client without API-KEY");
return ckanCaller.getDataset(datasetIdOrName);
} catch (Exception e) {
LOG.error("Unable to retrieve such dataset, returning null ...", e);
@ -292,6 +300,9 @@ public class DataCatalogueImpl implements DataCatalogue {
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#getUnencryptedUrlFromDatasetIdOrName(java.lang.String)
*/
@Override
public String getUnencryptedUrlFromDatasetIdOrName(String datasetIdOrName) {
LOG.debug("Request coming for getting dataset url (not encrypted) of dataset with name/id " + datasetIdOrName);
@ -323,6 +334,9 @@ public class DataCatalogueImpl implements DataCatalogue {
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#getUserRoleByGroup(java.lang.String)
*/
@Override
public Map<String, Map<CkanGroup, RolesCkanGroupOrOrg>> getUserRoleByGroup(String username) {
LOG.info("Get user role by group called. The username is: " + username);
@ -354,6 +368,9 @@ public class DataCatalogueImpl implements DataCatalogue {
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#getUserRoleByOrganization(java.lang.String)
*/
@Override
public Map<String, Map<CkanOrganization, RolesCkanGroupOrOrg>> getUserRoleByOrganization(String username) {
LOG.info("Get user role by organization called. The username is: " + username);
@ -387,11 +404,12 @@ public class DataCatalogueImpl implements DataCatalogue {
/**
* Retrieve an url for the tuple scope, entity, entity name
*
* @param context
* @param entityContext
* @param entityName
* Retrieve an url for the tuple scope, entity, entity name.
*
* @param context the context
* @param entityContext the entity context
* @param entityName the entity name
* @return the url for product
*/
private String getUrlForProduct(String context, EntityContext entityContext, String entityName) {
@ -424,52 +442,9 @@ public class DataCatalogueImpl implements DataCatalogue {
return toReturn;
}
// /**
// * Retrieve an url for the tuple scope, entity, entity name
// * @param context
// * @param entityContext
// * @param entityName
// */
// private String getUrlForProduct(String context, EntityContext entityContext, String entityName){
//
// String toReturn = null;
//
// try {
// LOG.debug("URI-Resolver URL: "+getUriResolverUrl());
// URL url = new URL(getUriResolverUrl());
// URLConnection con = url.openConnection();
// HttpURLConnection http = (HttpURLConnection)con;
// http.setRequestMethod("POST"); // PUT is another valid option
// http.setDoOutput(true);
// http.setRequestProperty("Content-Type", ContentType.APPLICATION_JSON.toString());
//
// JSONObject requestEntity = new JSONObject();
// requestEntity.put("gcube_scope", context);
// requestEntity.put("entity_context", entityContext.toString());
// requestEntity.put("entity_name", entityName);
//
// try(OutputStream os = con.getOutputStream()) {
// byte[] input = requestEntity.toJSONString().getBytes("utf-8");
// os.write(input, 0, input.length);
// }
//
// try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))) {
// StringBuilder response = new StringBuilder();
// String responseLine = null;
// while ((responseLine = br.readLine()) != null) {
// response.append(responseLine.trim());
// }
// toReturn = response.toString();
// }
//
//
// }catch (Exception e) {
// e.printStackTrace();
// }
//
// return toReturn;
// }
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#getLandingPages()
*/
@Override
public LandingPages getLandingPages() throws Exception {
@ -481,20 +456,27 @@ public class DataCatalogueImpl implements DataCatalogue {
return landingPages;
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#getUriResolverUrl()
*/
@Override
public String getUriResolverUrl() {
return URI_RESOLVER_URL;
}
/**
* Check if the manage product is enabled
* @return
* Check if the manage product is enabled.
*
* @return true, if is manage product enabled
*/
@Override
public boolean isManageProductEnabled() {
return MANAGE_PRODUCT_BUTTON;
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#getGroupByName(java.lang.String)
*/
@Override
public CkanGroup getGroupByName(String name) {
String ckanName = CatalogueUtilMethods.fromGroupTitleToName(name);
@ -506,12 +488,18 @@ public class DataCatalogueImpl implements DataCatalogue {
return null;
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#isNotificationToUsersEnabled()
*/
@Override
public boolean isNotificationToUsersEnabled() {
return ALERT_USERS_ON_POST_CREATION;
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#getOrganizationsByUser(java.lang.String)
*/
@Override
public List<CkanOrganization> getOrganizationsByUser(String username) {
@ -541,6 +529,9 @@ public class DataCatalogueImpl implements DataCatalogue {
return toReturn;
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#getGroupsByUser(java.lang.String)
*/
@Override
public List<CkanGroup> getGroupsByUser(String username) {
LOG.debug("Requested groups for user " + username);
@ -569,6 +560,9 @@ public class DataCatalogueImpl implements DataCatalogue {
return toReturn;
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#getOrganizationsIds()
*/
@Override
public List<String> getOrganizationsIds() {
@ -583,6 +577,9 @@ public class DataCatalogueImpl implements DataCatalogue {
return toReturn;
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#getOrganizationsNames()
*/
@Override
public List<String> getOrganizationsNames() {
@ -597,6 +594,9 @@ public class DataCatalogueImpl implements DataCatalogue {
return toReturn;
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#getOrganizationsNamesByUser(java.lang.String)
*/
@Override
public List<String> getOrganizationsNamesByUser(String username) {
@ -619,6 +619,9 @@ public class DataCatalogueImpl implements DataCatalogue {
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#getRoleOfUserInOrganization(java.lang.String, java.lang.String)
*/
@Override
public String getRoleOfUserInOrganization(String username, String orgName) {
@ -660,6 +663,12 @@ public class DataCatalogueImpl implements DataCatalogue {
}
/**
* Gets the api key from username.
*
* @param username the username
* @return the api key from username
*/
private String getApiKeyFromUsername(String username) {
LOG.debug("Request api key for user = " + username);
@ -698,6 +707,9 @@ public class DataCatalogueImpl implements DataCatalogue {
return null;
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#existProductWithNameOrId(java.lang.String)
*/
@Override
public boolean existProductWithNameOrId(String nameOrId) {
@ -714,6 +726,9 @@ public class DataCatalogueImpl implements DataCatalogue {
}
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#getOrganizationByIdOrName(java.lang.String)
*/
@Override
public CkanOrganization getOrganizationByIdOrName(String idOrName) {
@ -733,12 +748,14 @@ public class DataCatalogueImpl implements DataCatalogue {
/**
* Check if the user has this role into the organization/group with
* groupOrOrganization name
*
* @param ckanUsername
* @param organizationName
* @param correspondentRoleToCheck
* groupOrOrganization name.
*
* @param ckanUsername the ckan username
* @param groupOrOrganization the group or organization
* @param correspondentRoleToCheck the correspondent role to check
* @param group the group
* @return true if he has the role, false otherwise
* @throws Exception the exception
*/
protected boolean isRoleAlreadySet(String ckanUsername, String groupOrOrganization,
RolesCkanGroupOrOrg correspondentRoleToCheck, boolean group) throws Exception {
@ -765,11 +782,10 @@ public class DataCatalogueImpl implements DataCatalogue {
/**
* Just check if the group exists
*
* @param nameOrId
* @param client
* @return
* Just check if the group exists.
*
* @param nameOrId the name or id
* @return the ckan group
*/
private CkanGroup groupExists(String nameOrId) {
@ -788,6 +804,9 @@ public class DataCatalogueImpl implements DataCatalogue {
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#getParentGroups(java.lang.String, java.lang.String)
*/
@Override
public List<CkanGroup> getParentGroups(String groupName, String apiKey) {
// checks
@ -804,6 +823,9 @@ public class DataCatalogueImpl implements DataCatalogue {
return null;
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#getOrganizationByName(java.lang.String)
*/
@Override
public CkanOrganization getOrganizationByName(String name) {
@ -821,29 +843,24 @@ public class DataCatalogueImpl implements DataCatalogue {
}
/*
*
*
*
*
*
*
*
/**
* *****************************************************************************
*
*
* WRITE OPERATIONS
*
*
*
*
*
*
*
*
*
*
* *****************************************************************************.
*
* @param username the username
* @param organizationName the organization name
* @param correspondentRoleToCheck the correspondent role to check
* @return true, if successful
*/
@Override
public boolean checkRoleIntoOrganization(String username, String organizationName,
RolesCkanGroupOrOrg correspondentRoleToCheck) {
@ -903,6 +920,9 @@ public class DataCatalogueImpl implements DataCatalogue {
return false;
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#checkRoleIntoGroup(java.lang.String, java.lang.String, org.gcube.datacatalogue.utillibrary.shared.RolesCkanGroupOrOrg)
*/
@Override
public boolean checkRoleIntoGroup(String username, String groupName, RolesCkanGroupOrOrg correspondentRoleToCheck) {
LOG.debug("Request for checking if " + username + " into group " + groupName + " has role " + correspondentRoleToCheck);
@ -958,6 +978,9 @@ public class DataCatalogueImpl implements DataCatalogue {
return false;
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#createCkanDatasetMultipleCustomFields(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, long, java.lang.String, java.lang.String, java.util.List, java.util.Map, java.util.List, boolean, boolean, boolean)
*/
@Override
public String createCkanDatasetMultipleCustomFields(String username, String title, String name, String organizationName,
String author, String authorMail, String maintainer, String maintainerMail, long version,
@ -1039,6 +1062,9 @@ public class DataCatalogueImpl implements DataCatalogue {
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#patchFieldsForDataset(java.lang.String, java.util.Map)
*/
@Override
public boolean patchFieldsForDataset(String datasetIdOrName, Map<String, String> mapFields) throws Exception {
LOG.info("Called patch the fields " + mapFields + " for dataset id/name: " + datasetIdOrName);
@ -1069,6 +1095,9 @@ public class DataCatalogueImpl implements DataCatalogue {
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#setSearchableFieldForDataset(java.lang.String, boolean)
*/
@Override
public boolean setSearchableFieldForDataset(String datasetId, boolean searchable) throws Exception {
LOG.info("Set searchalbe field as " + searchable + " for dataset id: " + datasetId);
@ -1093,6 +1122,9 @@ public class DataCatalogueImpl implements DataCatalogue {
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#addResourceToDataset(org.gcube.datacatalogue.utillibrary.shared.ResourceBean)
*/
@Override
public String addResourceToDataset(ResourceBean resourceBean) throws Exception {
LOG.info("Request to add a resource described by this bean " + resourceBean);
@ -1134,6 +1166,9 @@ public class DataCatalogueImpl implements DataCatalogue {
return null;
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#deleteResourceFromDataset(java.lang.String)
*/
@Override
public boolean deleteResourceFromDataset(String resourceId) throws Exception {
LOG.info("Request to delete a resource with id " + resourceId);
@ -1155,6 +1190,9 @@ public class DataCatalogueImpl implements DataCatalogue {
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#createGroup(java.lang.String, java.lang.String, java.lang.String)
*/
@Override
public CkanGroup createGroup(String name, String title, String description) throws Exception {
@ -1191,6 +1229,9 @@ public class DataCatalogueImpl implements DataCatalogue {
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#getRolesAndUsersGroup(java.lang.String)
*/
@Override
public Map<RolesCkanGroupOrOrg, List<String>> getRolesAndUsersGroup(String groupName) {
@ -1223,6 +1264,9 @@ public class DataCatalogueImpl implements DataCatalogue {
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#assignDatasetToGroup(java.lang.String, java.lang.String)
*/
//TODO HAS TO BE REVISITED by gCAT
@Override
public boolean assignDatasetToGroup(String groupNameOrId, String datasetNameOrId) {
@ -1230,13 +1274,134 @@ public class DataCatalogueImpl implements DataCatalogue {
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#patchProductCustomFields(java.lang.String, java.lang.String, java.util.Map, boolean)
*/
//TODO HAS TO BE REVISITED by gCAT
@Override
public boolean patchProductCustomFields(String productId, String username,
Map<String, List<String>> customFieldsToChange, boolean removeOld) {
// checks
checkNotNull(productId);
checkNotNull(username);
if(customFieldsToChange == null || customFieldsToChange.isEmpty()) // TODO.. remove all custom fields maybe?!
return true;
String apiKey = getApiKeyFromUsername(username);
LOG.info("Going to change product with id " + productId +"."
+ " Request comes from user with key " + apiKey.substring(0, 5) + "****************");
LOG.info("The new values are " + customFieldsToChange);
// Get already available custom fields
Map<String, List<String>> fromCKANCustomFields = new HashMap<String, List<String>>();
ExtendCkanClient client = new ExtendCkanClient(CKAN_CATALOGUE_URL, apiKey);
List<CkanPair> extras = client.getDataset(productId).getExtras();
if(extras == null)
extras = new ArrayList<CkanPair>();
// 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);
}
LOG.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()) {
Map.Entry<java.lang.String, java.util.List<java.lang.String>> entry = iteratorUserMap
.next();
String key = entry.getKey();
List<String> newValues = entry.getValue();
// get the unique set of values
Set<String> uniqueValues = new HashSet<String>();
if(fromCKANCustomFields.containsKey(key))
if(!removeOld)
uniqueValues.addAll(fromCKANCustomFields.get(key));
uniqueValues.addAll(newValues);
fromCKANCustomFields.put(key, new ArrayList<String>(uniqueValues));
}
LOG.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);
// Request parameters to be replaced
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()) {
Map.Entry<java.lang.String, java.util.List<java.lang.String>> entry = iteratorNewFields
.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);
LOG.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){
LOG.error("Failed to patch the product ", e);
}
return false;
}
/**
* The real body of the assignDatasetToGroup
* @param groupNameOrId
* @param datasetNameOrId
* @param apiKey
* @param addOnParents
* @return
* The real body of the assignDatasetToGroup.
*
* @param groupNameOrId the group name or id
* @param datasetNameOrId the dataset name or id
* @param addOnParents the add on parents
* @return true, if successful
*/
private boolean assignDatasetToGroupBody(String groupNameOrId, String datasetNameOrId, boolean addOnParents) {
@ -1336,10 +1501,10 @@ public class DataCatalogueImpl implements DataCatalogue {
}
/**
* Find the hierarchy of trees
* @param uniqueGroups
* @param catalogue
* @param user's api key
* Find the hierarchy of trees.
*
* @param groupsTitles the groups titles
* @param apiKey the api key
*/
private void findHierarchyGroups(
List<String> groupsTitles,
@ -1365,13 +1530,19 @@ public class DataCatalogueImpl implements DataCatalogue {
}
/*******************************************************************************
/**
* *****************************************************************************
*
*
* MANAGEMENT OPERATIONS
*
*
*******************************************************************************/
* *****************************************************************************.
*
* @param username the username
* @param sourceOrganization the source organization
* @param currentRole the current role
*/
@Override
public void assignRolesOtherOrganization(String username,

View File

@ -301,7 +301,7 @@ public class TestDataCatalogueLib {
SecurityTokenProvider.instance.set(authorizationToken);
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
CkanDataset dataset = instance.getDataset(productName, null);
CkanDataset dataset = instance.getDataset(productName, testUser);
for (int i = 1; i < 3; i++) {
ResourceBean resourceBean = new ResourceBean("https://data-dev.d4science.net/ie8Y", "resouce " + i,