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 org.gcube.datacatalogue.utillibrary.ckan.MarshUnmarshCkanObject; import org.gcube.datacatalogue.utillibrary.ckan.MarshUnmarshCkanObject.METHOD; import org.gcube.datacatalogue.utillibrary.shared.ItemStatus; import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanDataset; import org.gcube.gcat.client.Item; import org.json.JSONArray; import org.json.JSONException; 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); /** * Checks if is content moderator enabled. * * @return true, if is content moderator enabled */ @Override public boolean isContentModeratorEnabled() { // TODO Auto-generated method stub return false; } /** * Sets the status. * * @param itemId the item id * @param theStatus the the status */ @Override public void setStatus(String itemId, ItemStatus theStatus) { // TODO Auto-generated method stub } /** * 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 JSONException */ @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.length(); listDataset = new ArrayList(size); LOG.debug("converting " + size + " dataset..."); for (int i = 0; i < size; i++) { String datasetName = null; try { datasetName = jsonArray.getString(i); LOG.trace("reading dataset: " + datasetName); String jsonValueDataset = new Item().read(datasetName); CkanDataset toCkanDataset = MarshUnmarshCkanObject.toCkanDataset(jsonValueDataset, METHOD.TO_READ); LOG.trace("converted as dataset: " + toCkanDataset); listDataset.add(toCkanDataset); } catch (JSONException | 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; } /** * 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 JSONException the JSON exception */ protected 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 JSONArray jsonArray = null; String datasetNames = new Item().list(limit, offset); if (datasetNames != null) { LOG.debug("for status " + theStatus + " found dataset: " + datasetNames); try { jsonArray = new JSONArray(datasetNames); } catch (JSONException e) { LOG.error("error occurred reading " + datasetNames + " as JSONArray",e); throw new InvalidObjectException(e.getMessage()); } } return jsonArray; } /** * Count list items for status. * * @param theStatus the the status * @return the long */ @Override public long countListItemsForStatus(ItemStatus theStatus) { LOG.info("called countListItemsForStatus with [status: " + theStatus + "]"); checkNotNull(theStatus); int count = 0; try { // TODO MUST BE CHANGED FOR THE STATUS count = new Item().count(); } catch (Exception e) { LOG.error("Error on couting list items for status " + theStatus, e); return -1; } return count; } /** * Approve item. * * @param itemId the item id */ @Override public void approveItem(String itemId) { // TODO Auto-generated method stub } /** * Reject item. * * @param itemId the item id * @param permanentlyDelete the permanently delete * @param reasonMsg the reason msg */ @Override public void rejectItem(String itemId, boolean permanentlyDelete, String reasonMsg) { // TODO Auto-generated method stub } /** * Permanently delete. * * @param itemId the item id */ @Override public void permanentlyDelete(String itemId) { // TODO Auto-generated method stub } }