342 lines
13 KiB
Java
342 lines
13 KiB
Java
package org.gcube.portlets.widgets.ckancontentmoderator.server;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
import org.gcube.datacatalogue.utillibrary.server.DataCatalogueImpl;
|
|
import org.gcube.datacatalogue.utillibrary.server.cms.CatalogueContentModeratorSystem;
|
|
import org.gcube.datacatalogue.utillibrary.shared.ItemStatus;
|
|
import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanDataset;
|
|
import org.gcube.portlets.widgets.ckancontentmoderator.client.CkanContentModeratorService;
|
|
import org.gcube.portlets.widgets.ckancontentmoderator.shared.CatalogueDataset;
|
|
import org.gcube.portlets.widgets.ckancontentmoderator.shared.OperationReport;
|
|
import org.gcube.portlets.widgets.ckancontentmoderator.shared.SearchedData;
|
|
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
|
|
|
|
/**
|
|
* The Class CkanContentModeratorServiceImpl.
|
|
*
|
|
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
|
|
*
|
|
* Jan 11, 2022
|
|
*/
|
|
@SuppressWarnings("serial")
|
|
public class CkanContentModeratorServiceImpl extends RemoteServiceServlet implements CkanContentModeratorService {
|
|
|
|
private static Logger LOG = LoggerFactory.getLogger(CkanContentModeratorServiceImpl.class);
|
|
|
|
/**
|
|
* Checks if is content moderator enabled.
|
|
*
|
|
* @return true, if is content moderator enabled
|
|
*/
|
|
@Override
|
|
public boolean isContentModeratorEnabled() {
|
|
LOG.info("called isContentModeratorEnabled");
|
|
String scope = setContexts();
|
|
boolean isContentModeratorEnabled = false;
|
|
try {
|
|
DataCatalogueImpl catalogueImpl = CatalogueCMSFactory.getFactory().getCatalogueImplPerScope(scope);
|
|
CatalogueContentModeratorSystem cmsInstance = catalogueImpl.getCatalogueContentModerator();
|
|
isContentModeratorEnabled = cmsInstance.isContentModeratorEnabled();
|
|
} catch (Exception e) {
|
|
LOG.error("Error occured on cheching isContentModeratorEnabled, so returning false", e);
|
|
return false;
|
|
}
|
|
return isContentModeratorEnabled;
|
|
}
|
|
|
|
/**
|
|
* Sets the contexts.
|
|
*
|
|
* @return the scope operating in
|
|
*/
|
|
private String setContexts() {
|
|
String scope = WsUtil.getCurrentScope(this.getThreadLocalRequest());
|
|
GCubeUser user = WsUtil.getCurrentUser(this.getThreadLocalRequest());
|
|
String token = WsUtil.getCurrentToken(scope, user.getUsername());
|
|
ScopeProvider.instance.set(scope);
|
|
SecurityTokenProvider.instance.set(token);
|
|
return scope;
|
|
}
|
|
|
|
/**
|
|
* Gets the list items for status.
|
|
*
|
|
* @param theStatus the the status
|
|
* @param offset the offset
|
|
* @param limit the limit
|
|
* @return the list items for status
|
|
* @throws Exception the exception
|
|
*/
|
|
@Override
|
|
public List<CatalogueDataset> getListItemsForStatus(ItemStatus theStatus, int offset, int limit) throws Exception {
|
|
LOG.info("called getListItemsForStatus with [status: " + theStatus + ", offset: " + offset + "], [limit: "
|
|
+ limit + "]");
|
|
List<CatalogueDataset> datasetList = null;
|
|
try {
|
|
String scope = setContexts();
|
|
DataCatalogueImpl catalogueImpl = CatalogueCMSFactory.getFactory().getCatalogueImplPerScope(scope);
|
|
CatalogueContentModeratorSystem cmsInstance = catalogueImpl.getCatalogueContentModerator();
|
|
List<CkanDataset> datasets = cmsInstance.getListItemsForStatus(theStatus, limit, offset);
|
|
if (datasets != null) {
|
|
int size = datasets.size();
|
|
datasetList = new ArrayList<CatalogueDataset>(size);
|
|
LOG.info("datasetList for input parameters returned by CMS has size: " + size);
|
|
for (CkanDataset ckanDataset : datasets) {
|
|
CatalogueDataset ds = CatalogueBeansConverter.toCatalogueDataset.apply(ckanDataset);
|
|
String datasetURL = String.format("%s/dataset/%s", catalogueImpl.getCatalogueUrl(), ds.getName());
|
|
ds.setUrl(datasetURL);
|
|
LOG.debug("converted dataset is: " + ds);
|
|
datasetList.add(ds);
|
|
}
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
LOG.error(e.getMessage(), e);
|
|
throw new Exception(
|
|
"Error occurred on reading items for status: " + theStatus + ". Caused by: " + e.getMessage());
|
|
}
|
|
|
|
LOG.info("returning " + datasetList.size() + " dataset");
|
|
return datasetList;
|
|
}
|
|
|
|
|
|
/**
|
|
* Sets the status.
|
|
*
|
|
* @param theStatus the the status
|
|
* @param itemNames the item names
|
|
* @return the operation report
|
|
* @throws Exception
|
|
*/
|
|
@Override
|
|
public OperationReport setStatus(ItemStatus theStatus, List<String> itemNames) throws Exception {
|
|
LOG.info("Called set status " + theStatus + " for Items with name: " + itemNames);
|
|
|
|
try {
|
|
String scope = setContexts();
|
|
DataCatalogueImpl catalogueImpl = CatalogueCMSFactory.getFactory().getCatalogueImplPerScope(scope);
|
|
List<String> errorListItems = new ArrayList<String>();
|
|
List<String> approvedListItems = new ArrayList<String>();
|
|
for (String itemName : itemNames) {
|
|
try {
|
|
catalogueImpl.refreshDataset(itemName);
|
|
approvedListItems.add(itemName);
|
|
} catch (Exception e) {
|
|
LOG.warn("Error when setting status (updating) the itemName: " + itemName, e);
|
|
errorListItems.add(itemName);
|
|
}
|
|
}
|
|
|
|
return new OperationReport(theStatus.getLabel(), errorListItems, approvedListItems);
|
|
|
|
} catch (Exception e) {
|
|
LOG.error(e.getMessage(), e);
|
|
throw new Exception("Error occurred on updating the status for item/s: " + itemNames + ". Caused by: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Approve item.
|
|
*
|
|
* @param itemNames the item names
|
|
* @param moderatorMessage the moderator message
|
|
* @return the operation report
|
|
* @throws Exception the exception
|
|
*/
|
|
@Override
|
|
public OperationReport approveItem(List<String> itemNames, String moderatorMessage) throws Exception {
|
|
LOG.info("Called approveItem with name/s: " + itemNames);
|
|
|
|
try {
|
|
String scope = setContexts();
|
|
DataCatalogueImpl catalogueImpl = CatalogueCMSFactory.getFactory().getCatalogueImplPerScope(scope);
|
|
CatalogueContentModeratorSystem cmsInstance = catalogueImpl.getCatalogueContentModerator();
|
|
|
|
List<String> errorListItems = new ArrayList<String>();
|
|
List<String> approvedListItems = new ArrayList<String>();
|
|
for (String itemName : itemNames) {
|
|
try {
|
|
cmsInstance.approveItem(itemName, moderatorMessage);
|
|
approvedListItems.add(itemName);
|
|
} catch (Exception e) {
|
|
LOG.warn("Error when approving itemName: " + itemName, e);
|
|
errorListItems.add(itemName);
|
|
}
|
|
}
|
|
|
|
return new OperationReport(ItemStatus.APPROVED.getLabel(), errorListItems, approvedListItems);
|
|
|
|
} catch (Exception e) {
|
|
LOG.error(e.getMessage(), e);
|
|
throw new Exception("Error occurred on approving item/s: " + itemNames + ". Caused by: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Reject item.
|
|
*
|
|
* @param itemNames the item names
|
|
* @param permanentlyDelete the permanently delete
|
|
* @param reasonMsg the reason msg
|
|
* @return the operation report
|
|
* @throws Exception the exception
|
|
*/
|
|
@Override
|
|
public OperationReport rejectItem(List<String> itemNames, boolean permanentlyDelete, String reasonMsg)
|
|
throws Exception {
|
|
LOG.info("Called rejectItem with name/s: " + itemNames + ", permanentlyDelete: " + permanentlyDelete);
|
|
|
|
try {
|
|
String scope = setContexts();
|
|
DataCatalogueImpl catalogueImpl = CatalogueCMSFactory.getFactory().getCatalogueImplPerScope(scope);
|
|
CatalogueContentModeratorSystem cmsInstance = catalogueImpl.getCatalogueContentModerator();
|
|
|
|
List<String> errorListItems = new ArrayList<String>();
|
|
List<String> passedListItems = new ArrayList<String>();
|
|
for (String itemName : itemNames) {
|
|
try {
|
|
cmsInstance.rejectItem(itemName, permanentlyDelete, reasonMsg);
|
|
passedListItems.add(itemName);
|
|
} catch (Exception e) {
|
|
LOG.warn("Error when rejecting itemName: " + itemName, e);
|
|
errorListItems.add(itemName);
|
|
}
|
|
}
|
|
|
|
return new OperationReport(ItemStatus.REJECTED.getLabel(), errorListItems, passedListItems);
|
|
|
|
} catch (Exception e) {
|
|
LOG.error(e.getMessage(), e);
|
|
throw new Exception("Error occurred on rejecting item/s: " + itemNames + ". Caused by: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Permanently delete.
|
|
*
|
|
* @param itemNames the item names
|
|
* @return the operation report
|
|
* @throws Exception the exception
|
|
*/
|
|
@Override
|
|
public OperationReport permanentlyDelete(List<String> itemNames) throws Exception {
|
|
LOG.info("Called permanently delete Items with name/s: " + itemNames);
|
|
|
|
try {
|
|
String scope = setContexts();
|
|
DataCatalogueImpl catalogueImpl = CatalogueCMSFactory.getFactory().getCatalogueImplPerScope(scope);
|
|
CatalogueContentModeratorSystem cmsInstance = catalogueImpl.getCatalogueContentModerator();
|
|
|
|
List<String> errorListItems = new ArrayList<String>();
|
|
List<String> passedListItems = new ArrayList<String>();
|
|
for (String itemName : itemNames) {
|
|
try {
|
|
cmsInstance.permanentlyDelete(itemName);
|
|
passedListItems.add(itemName);
|
|
} catch (Exception e) {
|
|
LOG.warn("Error when deleting permanently the itemName: " + itemName, e);
|
|
errorListItems.add(itemName);
|
|
}
|
|
}
|
|
|
|
return new OperationReport("Permanently Delete", errorListItems, passedListItems);
|
|
|
|
} catch (Exception e) {
|
|
LOG.error(e.getMessage(), e);
|
|
throw new Exception(
|
|
"Error occurred on permanently delete item/s: " + itemNames + ". Caused by: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets the data for status.
|
|
*
|
|
* @param status the status
|
|
* @param offset the offset
|
|
* @param limit the limit
|
|
* @param serverStartIndex the server start index
|
|
* @return the data for status
|
|
* @throws Exception the exception
|
|
*/
|
|
@Override
|
|
public SearchedData getDataForStatus(ItemStatus status, int offset, int limit, int serverStartIndex)
|
|
throws Exception {
|
|
LOG.info("called getDataForStatus [status: " + status + ", offset: " + offset + ", limit: " + limit
|
|
+ ", serverIndex: " + serverStartIndex);
|
|
|
|
try {
|
|
String scope = setContexts();
|
|
// int searchStartIndex = limit < serverStartIndex? serverStartIndex : offset;
|
|
|
|
int searchStartIndex = offset;
|
|
|
|
CatalogueContentModeratorSystem cmsInstance = CatalogueCMSFactory.getFactory().getCMSPerScope(scope);
|
|
SearchedData searchedData = new SearchedData(offset, limit, searchStartIndex, false);
|
|
long totalItemsForStatus = cmsInstance.countListItemsForStatus(status);
|
|
LOG.info("totalItemsForStatus " + status + " are : " + totalItemsForStatus);
|
|
List<CatalogueDataset> listDataset = new ArrayList<CatalogueDataset>();
|
|
try {
|
|
LOG.debug("getListItemsForStatus with searchStartIndex: " + searchStartIndex + ", limit: " + limit);
|
|
listDataset = getListItemsForStatus(status, searchStartIndex, limit);
|
|
} catch (Exception e) {
|
|
LOG.error("Error occurred on gettin items for status: " + status, e);
|
|
}
|
|
|
|
int listDatasetSize = listDataset.size();
|
|
LOG.debug("Returned " + listDatasetSize + " with above parameters");
|
|
searchedData.setData(listDataset);
|
|
searchedData.setTotalItems(totalItemsForStatus);
|
|
|
|
if (listDatasetSize == limit || listDatasetSize == 0) {
|
|
LOG.debug("Page completed returning " + listDatasetSize + " items");
|
|
int newOffset = searchStartIndex + offset;
|
|
searchedData.setServerSearchFinished(newOffset > totalItemsForStatus || listDatasetSize == 0);
|
|
LOG.debug("is Search finished: " + searchedData.isServerSearchFinished());
|
|
return searchedData;
|
|
}
|
|
|
|
/*
|
|
* int offsetStartIndex = searchStartIndex; boolean pageOffsetOut = false;
|
|
* while(listDatasetSize < offset && !searchedData.isServerSearchFinished() &&
|
|
* !pageOffsetOut){ //&& SEARCH NOT ULTIMATED
|
|
* LOG.debug("MyLg new WHILE Items count: "
|
|
* +totalItemsForStatus+" is less than limit..");
|
|
*
|
|
* int newOffsetStartIndex = offsetStartIndex+limit+1;
|
|
* LOG.debug("MyLg NewStartIndex is startIndex+limit: "+newOffsetStartIndex);
|
|
*
|
|
* //THERE ARE OTHER CHILDREN OVER NEW START INDEX if(newOffsetStartIndex <
|
|
* totalItemsForStatus){ //newLimit = limit - childrenToReturn.size();
|
|
* LOG.debug("MyLg getting items with index start: "+newOffsetStartIndex +
|
|
* ", limit: "+offset);
|
|
*
|
|
* int diff = (int) (offset - totalItemsForStatus); //How items are remaining
|
|
* //int offset = 0; LOG.debug("MyLg new search start: "+newOffsetStartIndex +
|
|
* ", diff: "+diff+ ", retrieved: "+listDatasetSize); if(diff >=
|
|
* listDatasetSize){ }else{ pageOffsetOut = true; } offsetStartIndex =
|
|
* newOffsetStartIndex; LOG.debug("MyLg items count is: "+totalItemsForStatus +
|
|
* " serverEndIndex: "+offsetStartIndex);
|
|
* searchedData.setServerEndIndex(offsetStartIndex); }else{ LOG.
|
|
* debug("New start index (oldStartIndex+limit) is grather than total items count, search is finished"
|
|
* ); searchedData.setServerSearchFinished(true); } }
|
|
*/
|
|
LOG.debug("Returning: " + searchedData);
|
|
return searchedData;
|
|
|
|
} catch (Exception e) {
|
|
LOG.error("Error during folder retrieving", e);
|
|
throw new Exception("Sorry, an error occurred on loading items");
|
|
}
|
|
}
|
|
}
|