package org.gcube.datacatalogue.utillibrary.server.cms; import static com.google.common.base.Preconditions.checkNotNull; import java.io.IOException; import java.io.InvalidObjectException; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.List; import javax.ws.rs.WebApplicationException; import javax.xml.ws.WebServiceException; import org.gcube.datacatalogue.utillibrary.ckan.MarshUnmarshCkanObject; import org.gcube.datacatalogue.utillibrary.ckan.MarshUnmarshCkanObject.METHOD; import org.gcube.datacatalogue.utillibrary.gcat.GCatCaller; import org.gcube.datacatalogue.utillibrary.shared.ItemStatus; import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanDataset; import org.gcube.gcat.api.moderation.CMItemStatus; import org.json.simple.JSONArray; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * The Class DataCatalogueCMSImpl. * * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it * * Jun 14, 2021 */ public class DataCatalogueCMSImpl implements CatalogueContentModeratorSystem { private static final Logger LOG = LoggerFactory.getLogger(DataCatalogueCMSImpl.class); private String runningScope; private boolean isContentModeratorEnabled; private GCatCaller gCatCaller; /** * Instantiates a new data catalogue CMS impl. * * @param runningScope the running scope * @param isContentModeratorEnabled the is content moderator enabled * @param gCatCaller the g cat caller */ public DataCatalogueCMSImpl(String runningScope, boolean isContentModeratorEnabled, GCatCaller gCatCaller) { this.runningScope = runningScope; this.isContentModeratorEnabled = isContentModeratorEnabled; this.gCatCaller = gCatCaller; } /** * Checks if is content moderator enabled. * * @return true, if is content moderator enabled */ @Override public boolean isContentModeratorEnabled() { return isContentModeratorEnabled; } /** * Approve item. * * @param datasetName the dataset name * @param moderatorMessage the moderator message * @throws WebApplicationException the web application exception * @throws MalformedURLException the malformed URL exception */ @Override public void approveItem(String datasetName, String moderatorMessage) throws WebApplicationException, MalformedURLException { gCatCaller.approveItem(datasetName, moderatorMessage); } /** * Reject item. * * @param datasetName the dataset name * @param permanentlyDelete the permanently delete * @param moderatorMessage the moderator message * @throws WebServiceException the web service exception * @throws MalformedURLException the malformed URL exception */ @Override public void rejectItem(String datasetName, boolean permanentlyDelete, String moderatorMessage) throws WebServiceException, MalformedURLException { gCatCaller.rejectItem(datasetName, permanentlyDelete, moderatorMessage); } /** * Permanently delete. * * @param datasetName the dataset name * @throws WebServiceException the web service exception * @throws MalformedURLException the malformed URL exception */ @Override public void permanentlyDelete(String datasetName) throws WebServiceException, MalformedURLException { gCatCaller.deleteItem(datasetName, true); } /** * Gets the running scope. * * @return the running scope */ public String getRunningScope() { return runningScope; } /** * Gets the list items for status. * * @param theStatus the the status * @param limit the limit * @param offset the offset * @return the list items for status * @throws WebApplicationException the web application exception * @throws MalformedURLException the malformed URL exception * @throws InvalidObjectException the invalid object exception */ @Override public List getListItemsForStatus(ItemStatus theStatus, int limit, int offset) throws WebApplicationException, MalformedURLException, InvalidObjectException { LOG.info("called getListItemsForStatus with [status: " + theStatus + "], [limit: " + limit + "], [offset: " + offset + "]"); List listDataset = null; checkNotNull(theStatus); JSONArray jsonArray = getSourceArrayOfItemsForStatus(theStatus, limit, offset); if (jsonArray != null) { int size = jsonArray.size(); listDataset = new ArrayList(size); LOG.info("reading and converting " + size + " dataset..."); for (int i = 0; i < size; i++) { String datasetName = null; try { datasetName = (String) jsonArray.get(i); LOG.debug("reading dataset: " + datasetName); String jsonValueDataset = gCatCaller.getDatasetForName(datasetName); LOG.trace("the JSON dataset is: " + jsonValueDataset); CkanDataset toCkanDataset = MarshUnmarshCkanObject.toCkanDataset(jsonValueDataset, METHOD.TO_READ); LOG.debug("converted as dataset: " + toCkanDataset); listDataset.add(toCkanDataset); } catch (IOException e) { LOG.warn("Error on reading/converting the dataset name: " + datasetName, e); } } } if (listDataset == null) { LOG.info("no dataset returned with status: " + theStatus); return listDataset; } LOG.info("returning listDataset with size: " + listDataset.size()); return listDataset; } /** * Count list items for status. * * @param theStatus the the status * @return the number of items with the input status */ @Override public long countListItemsForStatus(ItemStatus theStatus) { LOG.info("called countListItemsForStatus with [status: " + theStatus + "]"); checkNotNull(theStatus); int count = 0; try { CMItemStatus cmStatus = toCMStatus(theStatus); count = gCatCaller.countListItemsForCMStatus(cmStatus); } catch (Exception e) { LOG.error("Error on counting the items for status " + theStatus, e); return -1; } return count; } /** * Gets the source array of items for status read by gCatClient. * * @param theStatus the the status * @param limit the limit * @param offset the offset * @return the source array of items for status * @throws WebApplicationException the web application exception * @throws MalformedURLException the malformed URL exception * @throws InvalidObjectException the invalid object exception */ protected org.json.simple.JSONArray getSourceArrayOfItemsForStatus(ItemStatus theStatus, int limit, int offset) throws WebApplicationException, MalformedURLException, InvalidObjectException { LOG.info("called getSourceArrayOfItemsForStatus with [status: " + theStatus + "], [limit: " + limit + "], [offset: " + offset + "]"); checkNotNull(theStatus); // TODO MUST BE CHANGED FOR THE STATUS org.json.simple.JSONArray jsonArray = null; CMItemStatus cmiStatus = toCMStatus(theStatus); String datasetNames = gCatCaller.getListItemsForCMStatus(cmiStatus, limit, offset); if (datasetNames != null) { LOG.debug("for status " + theStatus + " found dataset: " + datasetNames); JSONParser parser = new JSONParser(); try { jsonArray = (JSONArray) parser.parse(datasetNames); } catch (ParseException e) { LOG.error("error occurred reading " + datasetNames + " as JSONArray", e); throw new InvalidObjectException(e.getMessage()); } } return jsonArray; } /** * To CM status. * * @param theStatus the the status * @return the CM item status * @throws WebApplicationException the web application exception */ private CMItemStatus toCMStatus(ItemStatus theStatus) throws WebApplicationException { try { return CMItemStatus.valueOf(theStatus.name()); } catch (Exception e) { throw new WebApplicationException( "No value of " + theStatus.name() + " into enumerator: " + CMItemStatus.values()); } } }