catalogue-util-library/src/main/java/org/gcube/datacatalogue/utillibrary/gcat/GCatCaller.java

289 lines
8.8 KiB
Java

/*
*
*/
package org.gcube.datacatalogue.utillibrary.gcat;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.WebApplicationException;
import javax.xml.ws.WebServiceException;
import org.gcube.gcat.api.moderation.CMItemStatus;
import org.gcube.gcat.api.moderation.Moderated;
import org.gcube.gcat.client.Group;
import org.gcube.gcat.client.Item;
import org.gcube.gcat.client.Resource;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class GCatCaller.
*
* @author Francesco Mangiacrapa at ISTI-CNR Pisa (Italy) May 29, 2020
*/
public class GCatCaller {
private String catalogueURL;
private static final Logger LOG = LoggerFactory.getLogger(GCatCaller.class);
/**
* Instantiates a new g cat caller.
*
* @param catalogueURL the catalogue URL
*/
public GCatCaller(String catalogueURL) {
this.catalogueURL = catalogueURL;
}
/**
* Gets the catalogue URL.
*
* @return the catalogue URL
*/
public String getCatalogueURL() {
return catalogueURL;
}
/**
* Gets the dataset for name.
*
* @param datasetName the dataset name
* @return the jsonValue
* @throws WebApplicationException the web application exception
* @throws MalformedURLException the malformed URL exception
*/
public String getDatasetForName(String datasetName) throws WebApplicationException, MalformedURLException {
LOG.debug("GetDatasetForName called");
LOG.info("Get dataset for name " + datasetName + "called");
return new Item().read(datasetName);
}
/**
* Creates the dataset.
*
* @param jsonDataset the json dataset
* @param socialPost if true sends the social post
* @return the jsonValue
* @throws WebApplicationException the web application exception
* @throws MalformedURLException the malformed URL exception
*/
public String createDataset(String jsonDataset, boolean socialPost)
throws WebApplicationException, MalformedURLException {
LOG.debug("Create dataset called");
LOG.info("Calling create on: " + jsonDataset);
return new Item().create(jsonDataset, socialPost);
}
/**
* Adds the resource to dataset.
*
* @param datasetId the dataset id
* @param jsonValueResource the json value resource
* @return the string
* @throws MalformedURLException the malformed URL exception
*/
public String addResourceToDataset(String datasetId, String jsonValueResource) throws MalformedURLException {
LOG.debug("Create resource called");
LOG.info("Calling create resource: " + jsonValueResource + " for dataset id: " + datasetId);
return new Resource().create(datasetId, jsonValueResource);
}
/**
* Delete resource.
*
* @param datasetId the dataset id
* @param resourceId the resource id
* @throws MalformedURLException the malformed URL exception
*/
public void deleteResource(String datasetId, String resourceId) throws MalformedURLException {
LOG.debug("Delete resource called");
LOG.info("Calling delete resource with: " + resourceId + " for dataset id: " + datasetId);
new Resource().delete(datasetId, resourceId);
}
/**
* Creates the group.
*
* @param jsonGroup the json group
* @return the string
* @throws MalformedURLException the malformed URL exception
*/
public String createGroup(String jsonGroup) throws MalformedURLException {
LOG.debug("Create group called");
LOG.info("Calling create group: " + jsonGroup);
return new Group().create(jsonGroup);
}
/**
* Patch dataset.
*
* @param datasetName the dataset name
* @param jsonObj the json obj
* @return the string
* @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");
LOG.info("Calling patch dataset with name: " + datasetName);
return new Item().patch(datasetName, jsonObj.toJSONString());
}
/**
* Approve item.
*
* @param datasetName the dataset name
* @param moderatorMessage the moderator message
* @return the string
* @throws WebApplicationException the web application exception
* @throws MalformedURLException the malformed URL exception
*/
public String approveItem(String datasetName, String moderatorMessage)
throws WebApplicationException, MalformedURLException {
LOG.debug("Approve item called");
LOG.info("Calling approve item with name: " + datasetName + ", and msg: " + moderatorMessage);
return new Item().approve(datasetName, moderatorMessage);
}
/**
* Reject item.
*
* @param datasetName the dataset name
* @param permanentlyDelete the permanently delete
* @param moderatorMessage the moderator message
* @return the string
* @throws WebServiceException the web service exception
* @throws MalformedURLException the malformed URL exception
*/
public String rejectItem(String datasetName, boolean permanentlyDelete, String moderatorMessage)
throws WebServiceException, MalformedURLException {
LOG.debug("Reject item called");
LOG.info("Calling reject item with name: " + datasetName + ", and msg: " + moderatorMessage);
String toReturnMsg = new Item().reject(datasetName, moderatorMessage);
if (permanentlyDelete) {
deleteItem(datasetName, true);
}
return toReturnMsg;
}
/**
* Delete item.
*
* @param datasetName the dataset name
* @param purge the purge
* @throws WebServiceException the web service exception
* @throws MalformedURLException the malformed URL exception
*/
public void deleteItem(String datasetName, boolean purge) throws WebServiceException, MalformedURLException {
LOG.debug("Delete item called");
LOG.info("Calling delete item with name: " + datasetName + ", and purge: " + purge);
new Item().delete(datasetName, purge);
return;
}
/**
* Gets the list items for CM status.
*
* @param status the status
* @param limit the limit
* @param offset the offset
* @return the list items for CM status
* @throws WebServiceException the web service exception
* @throws MalformedURLException the malformed URL exception
*/
public String getListItemsForCMStatus(CMItemStatus status, int limit, int offset)
throws WebServiceException, MalformedURLException {
LOG.debug("getListItemsForStatus called");
LOG.info("Calling list items for status: " + status);
Map<String, String> queryParams = new HashMap<String, String>();
// queryParams.put(Moderated.SYSTEM_CM_ITEM_STATUS, status.getValue());
String modStatusField = Moderated.SYSTEM_CM_ITEM_STATUS.replaceAll(":", "_");
queryParams.put("q", "extras_" + modStatusField + ":" + status.getValue());
if (limit < 0)
limit = 10;
if (offset < 0)
offset = 0;
queryParams.put("limit", limit + "");
queryParams.put("offset", offset + "");
return getListItemsForQuery(queryParams);
}
/**
* Count list items for CM status.
*
* @param status the status
* @return the int
* @throws WebServiceException the web service exception
* @throws MalformedURLException the malformed URL exception
*/
public int countListItemsForCMStatus(CMItemStatus status) throws WebServiceException, MalformedURLException {
LOG.debug("countListItemsForCMStatus called");
LOG.info("Calling coun list items for status: " + status);
Map<String, String> queryParams = new HashMap<String, String>();
// queryParams.put(Moderated.SYSTEM_CM_ITEM_STATUS, status.getValue());
String modStatusField = Moderated.SYSTEM_CM_ITEM_STATUS.replaceAll(":", "_");
queryParams.put("q", "extras_" + modStatusField + ":" + status.getValue());
queryParams.put("count", "true");
String theSize = getListItemsForQuery(queryParams);
int count = 0;
try {
count = Integer.parseInt(theSize);
} catch (Exception e) {
LOG.warn("Size retured by gCat is not an integer, returning: " + count);
}
return count;
}
/**
* Gets the list items.
*
* @param limit the limit
* @param offset the offset
* @return the list items
* @throws WebServiceException the web service exception
* @throws MalformedURLException the malformed URL exception
*/
public String getListItems(int limit, int offset) throws WebServiceException, MalformedURLException {
LOG.debug("getListItems called");
LOG.info("Calling list items with limit: " + limit + ", offset: " + offset);
return new Item().list(limit, offset);
}
/**
* Gets the list items for query.
*
* @param queryParams the query params
* @return the list items for query
* @throws WebServiceException the web service exception
* @throws MalformedURLException the malformed URL exception
*/
public String getListItemsForQuery(Map<String, String> queryParams)
throws WebServiceException, MalformedURLException {
LOG.debug("getListItems forQuery called");
LOG.info("Calling list items for query: " + queryParams);
return new Item().list(queryParams);
}
}