added the method getObjectMapperForPosting into ExtendCkanClient.
This commit is contained in:
parent
87d1234798
commit
c1185a77b9
|
@ -6,6 +6,9 @@ import java.io.InputStream;
|
|||
import java.io.InputStreamReader;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
|
@ -43,6 +46,12 @@ public class ExtendCkanClient extends CkanClient implements PatchedCkan{
|
|||
@Nullable
|
||||
private static ObjectMapper objectMapper;
|
||||
private static final Logger logger = LoggerFactory.getLogger(ExtendCkanClient.class);
|
||||
|
||||
private static final Map<String, ObjectMapper> OBJECT_MAPPERS_FOR_POSTING = new HashMap();
|
||||
|
||||
private static enum HTTP_METHOD {
|
||||
GET, POST
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new simple extend ckan client.
|
||||
|
@ -148,6 +157,30 @@ public class ExtendCkanClient extends CkanClient implements PatchedCkan{
|
|||
return objectMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the Jackson object mapper configured for creation/update
|
||||
* operations. Internally, Object mapper is initialized at first call.
|
||||
*
|
||||
* @param clazz
|
||||
* the class you want to post. For generic class, just put
|
||||
* Object.class
|
||||
* @since 0.4.1
|
||||
*/
|
||||
static ObjectMapper getObjectMapperForPosting(Class clazz) {
|
||||
checkNotNull(clazz, "Invalid class! If you don't know the class just use Object.class");
|
||||
|
||||
if (OBJECT_MAPPERS_FOR_POSTING.get(clazz.getName()) == null) {
|
||||
logger.info("Generating ObjectMapper for posting class "+ clazz);
|
||||
ObjectMapper om = new ObjectMapper();
|
||||
configureObjectMapperForPosting(om, clazz);
|
||||
OBJECT_MAPPERS_FOR_POSTING.put(clazz.getName(), om);
|
||||
}
|
||||
|
||||
return OBJECT_MAPPERS_FOR_POSTING.get(clazz.getName());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Gets the http.
|
||||
*
|
||||
|
|
|
@ -2,9 +2,11 @@ package org.gcube.datacatalogue.ckanutillibrary.ckan;
|
|||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import eu.trentorise.opendata.jackan.model.CkanDataset;
|
||||
import eu.trentorise.opendata.jackan.model.CkanGroup;
|
||||
|
@ -19,17 +21,45 @@ import eu.trentorise.opendata.jackan.model.CkanResource;
|
|||
*/
|
||||
public class MarshUnmarshCkanObject {
|
||||
|
||||
|
||||
/**
|
||||
* The Enum METHOD.
|
||||
*
|
||||
* @author Francesco Mangiacrapa at ISTI-CNR Pisa (Italy)
|
||||
* Sep 2, 2020
|
||||
*/
|
||||
public static enum METHOD {TO_READ, TO_CREATE}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the object mapper.
|
||||
*
|
||||
* @param method the method
|
||||
* @param clazz the clazz
|
||||
* @return the object mapper
|
||||
*/
|
||||
private static ObjectMapper getObjectMapper(METHOD method, Class clazz) {
|
||||
ObjectMapper mapper = ExtendCkanClient.getObjectMapper();
|
||||
if(method!=null && method.equals(METHOD.TO_CREATE))
|
||||
return ExtendCkanClient.getObjectMapperForPosting(clazz);
|
||||
|
||||
return mapper;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* To ckan dataset.
|
||||
*
|
||||
* @param jsonDataset the json dataset
|
||||
* @param method the method. Passing TO_CREATE Method the Null fields will be ignored. No otherwise
|
||||
* @return the ckan dataset
|
||||
* @throws JsonParseException the json parse exception
|
||||
* @throws JsonMappingException the json mapping exception
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public static CkanDataset toCkanDataset(String jsonDataset) throws JsonParseException, JsonMappingException, IOException {
|
||||
return ExtendCkanClient.getObjectMapper().readValue(jsonDataset, CkanDataset.class);
|
||||
public static CkanDataset toCkanDataset(String jsonDataset, METHOD method) throws JsonParseException, JsonMappingException, IOException {
|
||||
ObjectMapper om = getObjectMapper(method, CkanDataset.class);
|
||||
return om.readValue(jsonDataset, CkanDataset.class);
|
||||
}
|
||||
|
||||
|
||||
|
@ -79,11 +109,13 @@ public class MarshUnmarshCkanObject {
|
|||
* To json value dataset.
|
||||
*
|
||||
* @param dataset the dataset
|
||||
* @param method the method
|
||||
* @return the string
|
||||
* @throws JsonProcessingException the json processing exception
|
||||
*/
|
||||
public static String toJsonValueDataset(CkanDataset dataset) throws JsonProcessingException {
|
||||
return ExtendCkanClient.getObjectMapper().writeValueAsString(dataset);
|
||||
public static String toJsonValueDataset(CkanDataset dataset, METHOD method) throws JsonProcessingException {
|
||||
ObjectMapper om = getObjectMapper(method, CkanDataset.class);
|
||||
return om.writeValueAsString(dataset);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -180,14 +180,19 @@ public interface DataCatalogue {
|
|||
* @return the capacity of the user into this organization or null
|
||||
*/
|
||||
String getRoleOfUserInOrganization(String username, String orgName);
|
||||
|
||||
/**
|
||||
* Check if the user is valid by checking if its API_KEY is present into DB.
|
||||
*
|
||||
* @param username the username
|
||||
* @return true, if successful
|
||||
*/
|
||||
boolean checkValidUser(String username);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* **************************************************************************
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*****************************************************************************
|
||||
*
|
||||
*
|
||||
*
|
||||
|
@ -195,34 +200,10 @@ public interface DataCatalogue {
|
|||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
* **************************************************************************.
|
||||
*
|
||||
* @param username the username
|
||||
* @param title the title
|
||||
* @param name the name
|
||||
* @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 customFieldsMultiple the custom fields multiple
|
||||
* @param resources the resources
|
||||
* @param setPublic the set public
|
||||
* @param setSearchable the set searchable
|
||||
* @param socialPost the social post
|
||||
* @return the string
|
||||
* @throws Exception the exception
|
||||
*/
|
||||
|
||||
|
||||
*****************************************************************************
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Create a dataset with those information. The method allows to have multiple
|
||||
* values for the same custom field key. NOTE: unfortunately java doesn't
|
||||
|
@ -307,14 +288,6 @@ public interface DataCatalogue {
|
|||
*/
|
||||
CkanGroup createGroup(String nameOrId, String title, String description) throws Exception;
|
||||
|
||||
/**
|
||||
* Check if the user is valid by checking if its API_KEY is present into DB.
|
||||
*
|
||||
* @param username the username
|
||||
* @return true, if successful
|
||||
*/
|
||||
boolean checkValidUser(String username);
|
||||
|
||||
|
||||
/**
|
||||
* Sets the searchable field for dataset.
|
||||
|
|
|
@ -17,6 +17,7 @@ import org.gcube.common.scope.impl.ScopeBean;
|
|||
import org.gcube.common.scope.impl.ScopeBean.Type;
|
||||
import org.gcube.datacatalogue.ckanutillibrary.ckan.ExtendCkanClient;
|
||||
import org.gcube.datacatalogue.ckanutillibrary.ckan.MarshUnmarshCkanObject;
|
||||
import org.gcube.datacatalogue.ckanutillibrary.ckan.MarshUnmarshCkanObject.METHOD;
|
||||
import org.gcube.datacatalogue.ckanutillibrary.db.DBCaller;
|
||||
import org.gcube.datacatalogue.ckanutillibrary.gcat.GCatCaller;
|
||||
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogueRunningCluster.ACCESS_LEVEL_TO_CATALOGUE_PORTLET;
|
||||
|
@ -257,7 +258,7 @@ public class DataCatalogueImpl implements DataCatalogue {
|
|||
if(authzToken!=null && !authzToken.isEmpty()) {
|
||||
LOG.info("gcube-token found. Calling the gCat client");
|
||||
String jsonDataset = gCatCaller.getDatasetForName(datasetId);
|
||||
return MarshUnmarshCkanObject.toCkanDataset(jsonDataset);
|
||||
return MarshUnmarshCkanObject.toCkanDataset(jsonDataset, METHOD.TO_READ);
|
||||
}
|
||||
|
||||
LOG.info("No api-key or gcube-token found. Calling Ckan Client without API-KEY");
|
||||
|
@ -712,34 +713,34 @@ public class DataCatalogueImpl implements DataCatalogue {
|
|||
resources, setPublic, setSearchable);
|
||||
|
||||
// trying to create by gCat
|
||||
String jsonValueDataset = MarshUnmarshCkanObject.toJsonValueDataset(dataset);
|
||||
LOG.debug("Serialized dataset is: " + jsonValueDataset);
|
||||
String jsonValueDataset = MarshUnmarshCkanObject.toJsonValueDataset(dataset,METHOD.TO_CREATE);
|
||||
LOG.info("Serialized dataset is: " + jsonValueDataset);
|
||||
jsonValueDataset = gCatCaller.createDataset(jsonValueDataset,socialPost);
|
||||
LOG.debug("Created dataset is: " + jsonValueDataset);
|
||||
|
||||
if(jsonValueDataset != null){
|
||||
CkanDataset toCkanDataset = MarshUnmarshCkanObject.toCkanDataset(jsonValueDataset);
|
||||
CkanDataset toCkanDataset = MarshUnmarshCkanObject.toCkanDataset(jsonValueDataset,METHOD.TO_READ);
|
||||
LOG.info("Dataset with name " + toCkanDataset.getName() + " has been created correctly");
|
||||
|
||||
//TODO FOLLOWING OPERATIONS NOT SHOULD LONGER NEEDED ON DATASET CREATION, MUST BE CHECKED with @LUCA
|
||||
/*
|
||||
// set visibility
|
||||
boolean visibilitySet = setDatasetPrivate(
|
||||
!setPublic, // swap to private
|
||||
res.getOrganization().getId(),
|
||||
res.getId(),
|
||||
CKAN_TOKEN_SYS); // use sysadmin api key to be sure it will be set
|
||||
|
||||
// // set visibility
|
||||
// boolean visibilitySet = setDatasetPrivate(
|
||||
// !setPublic, // swap to private
|
||||
// res.getOrganization().getId(),
|
||||
// res.getId(),
|
||||
// CKAN_TOKEN_SYS); // use sysadmin api key to be sure it will be set
|
||||
//
|
||||
// logger.info("Was visibility set to " + (setPublic ? "public" : "private") + "? " + visibilitySet);
|
||||
//
|
||||
// // 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);
|
||||
// }
|
||||
// return res.getId();
|
||||
|
||||
logger.info("Was visibility set to " + (setPublic ? "public" : "private") + "? " + visibilitySet);
|
||||
|
||||
// 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);
|
||||
}
|
||||
return res.getId();
|
||||
|
||||
*/
|
||||
|
||||
return toCkanDataset.getId();
|
||||
}
|
||||
|
||||
|
@ -1057,6 +1058,128 @@ public class DataCatalogueImpl implements DataCatalogue {
|
|||
// return false;
|
||||
// }
|
||||
//
|
||||
|
||||
// @Override
|
||||
public boolean assignDatasetToGroup(String groupNameOrId, String datasetNameOrId) {
|
||||
|
||||
return assignDatasetToGroupBody(groupNameOrId, datasetNameOrId, "", false);
|
||||
|
||||
}
|
||||
|
||||
// @Override
|
||||
public boolean assignDatasetToGroup(String groupNameOrId, String datasetNameOrId, boolean addOnParents) {
|
||||
|
||||
return assignDatasetToGroupBody(groupNameOrId, datasetNameOrId, "", addOnParents);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
|
||||
return false;
|
||||
// // checks
|
||||
// checkNotNull(groupNameOrId);
|
||||
// checkArgument(!groupNameOrId.isEmpty());
|
||||
// checkNotNull(datasetNameOrId);
|
||||
// checkArgument(!datasetNameOrId.isEmpty());
|
||||
// checkNotNull(apiKey);
|
||||
// checkArgument(!apiKey.isEmpty());
|
||||
//
|
||||
// String groupNameToCheck = CatalogueUtilMethods.fromGroupTitleToName(groupNameOrId);
|
||||
//
|
||||
// try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();){
|
||||
//
|
||||
// ExtendCkanClient client = new ExtendCkanClient(CKAN_CATALOGUE_URL, CKAN_TOKEN_SYS);
|
||||
//
|
||||
// // check the group exists
|
||||
// CkanGroup group = client.getGroup(groupNameToCheck);
|
||||
//
|
||||
// // move to a list
|
||||
// List<String> groupNames = new ArrayList<String>();
|
||||
// groupNames.add(group.getName());
|
||||
// if(group != null && addOnParents){
|
||||
// findHierarchyGroups(groupNames, CKAN_TOKEN_SYS);
|
||||
// }
|
||||
//
|
||||
// // 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);
|
||||
//
|
||||
// logger.debug("Response is " + response.getStatusLine().getStatusCode() + " and message is " + response.getStatusLine().getReasonPhrase());
|
||||
//
|
||||
// // 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
|
||||
// String jsonAsString = EntityUtils.toString(response.getEntity());
|
||||
// JSONParser parser = new JSONParser();
|
||||
// JSONObject json = (JSONObject) parser.parse(jsonAsString);
|
||||
// JSONObject resultJson = (JSONObject) json.get("result");
|
||||
// JSONArray groupsJson = (JSONArray)resultJson.get("groups");
|
||||
// Iterator<JSONObject> it = groupsJson.iterator();
|
||||
//
|
||||
// while (it.hasNext()) {
|
||||
// JSONObject object = it.next();
|
||||
// try{
|
||||
// if(object.containsKey("name"))
|
||||
// groupNames.add((String)object.get("name"));
|
||||
// }catch(Exception e){
|
||||
// logger.error("Error", e);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // remove duplicates
|
||||
// Set<String> groupNamesSet = new HashSet<String>(groupNames);
|
||||
//
|
||||
// logger.debug("Groups to be added are " + groupNamesSet);
|
||||
//
|
||||
// // now we patch the dataset with the new group
|
||||
// String pathUpdatePatch = CKAN_CATALOGUE_URL + "/api/3/action/package_patch";
|
||||
//
|
||||
// JSONObject req = new JSONObject();
|
||||
// req.put("id", datasetNameOrId);
|
||||
//
|
||||
// JSONArray groups = new JSONArray();
|
||||
// Iterator<String> iteratorNameSet = groupNamesSet.iterator();
|
||||
// while (iteratorNameSet.hasNext()) {
|
||||
// String groupName = iteratorNameSet.next();
|
||||
// JSONObject groupJSON = new JSONObject();
|
||||
// groupJSON.put("name", groupName);
|
||||
// groups.add(groupJSON);
|
||||
// }
|
||||
// req.put("groups", groups);
|
||||
//
|
||||
// logger.debug("Request for patch is going to be " + req.toJSONString());
|
||||
//
|
||||
// HttpPost request = new HttpPost(pathUpdatePatch);
|
||||
// request.addHeader("Authorization", CKAN_TOKEN_SYS);
|
||||
// StringEntity params = new StringEntity(req.toJSONString());
|
||||
// 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;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// }catch(Exception e){
|
||||
// logger.error("Unable to make this association", e);
|
||||
// }
|
||||
//
|
||||
// return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -185,7 +185,7 @@ public class TestDataCatalogueLib {
|
|||
|
||||
}
|
||||
|
||||
//@Test
|
||||
@Test
|
||||
public void createDataset() throws Exception{
|
||||
|
||||
try {
|
||||
|
@ -206,6 +206,8 @@ public class TestDataCatalogueLib {
|
|||
customFieldsMultiple.put("key-random-"+new Random().nextInt(10), values);
|
||||
}
|
||||
|
||||
customFieldsMultiple.put("system:type", Arrays.asList("EmptyProfile"));
|
||||
|
||||
boolean setSearchable = true;
|
||||
boolean setPublic = false;
|
||||
List<ResourceBean> resources = null;
|
||||
|
|
Loading…
Reference in New Issue