ckan-content-moderator-widget/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/server/CkanContentModeratorService...

351 lines
13 KiB
Java
Raw Normal View History

2021-05-07 16:55:35 +02:00
package org.gcube.portlets.widgets.ckancontentmoderator.server;
2021-06-14 17:39:44 +02:00
import java.util.ArrayList;
import java.util.HashMap;
2021-06-01 18:36:18 +02:00
import java.util.List;
import java.util.Map;
2021-06-01 18:36:18 +02:00
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
2021-06-16 18:02:54 +02:00
import org.gcube.datacatalogue.utillibrary.server.DataCatalogueImpl;
2021-06-14 17:39:44 +02:00
import org.gcube.datacatalogue.utillibrary.server.cms.CatalogueContentModeratorSystem;
import org.gcube.datacatalogue.utillibrary.shared.ItemStatus;
import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanDataset;
2021-05-07 16:55:35 +02:00
import org.gcube.portlets.widgets.ckancontentmoderator.client.CkanContentModeratorService;
2022-02-21 15:59:13 +01:00
import org.gcube.portlets.widgets.ckancontentmoderator.shared.CMSUserRole;
2021-06-14 11:27:53 +02:00
import org.gcube.portlets.widgets.ckancontentmoderator.shared.CatalogueDataset;
2022-02-21 15:59:13 +01:00
import org.gcube.portlets.widgets.ckancontentmoderator.shared.ModerationUserRole;
2022-02-08 15:41:30 +01:00
import org.gcube.portlets.widgets.ckancontentmoderator.shared.OperationReport;
import org.gcube.portlets.widgets.ckancontentmoderator.shared.SearchedData;
2021-06-01 18:36:18 +02:00
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2021-05-07 16:55:35 +02:00
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
/**
2022-02-08 15:41:30 +01:00
* The Class CkanContentModeratorServiceImpl.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Jan 11, 2022
2021-05-07 16:55:35 +02:00
*/
@SuppressWarnings("serial")
2021-06-14 17:39:44 +02:00
public class CkanContentModeratorServiceImpl extends RemoteServiceServlet implements CkanContentModeratorService {
2021-06-14 11:27:53 +02:00
private static Logger LOG = LoggerFactory.getLogger(CkanContentModeratorServiceImpl.class);
2021-05-07 16:55:35 +02:00
2022-02-08 15:41:30 +01:00
/**
* Checks if is content moderator enabled.
*
* @return true, if is content moderator enabled
*/
2021-05-07 16:55:35 +02:00
@Override
public Boolean isContentModeratorEnabled() {
2021-06-15 16:52:27 +02:00
LOG.info("called isContentModeratorEnabled");
2022-02-08 15:41:30 +01:00
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;
}
/**
* Checks if is moderator role is assigned to working user in the context.
*
* @return the moderation user role
* @throws Exception the exception
*/
@Override
public Boolean isModeratorRoleAssigned() throws Exception {
LOG.info("called isModeratorRoleAssigned");
ModerationUserRole userRole = getCMSRolesForUserInTheContext();
boolean isCatalogueModerator = false;
if (userRole != null && userRole.getRoles() != null
&& userRole.getRoles().contains(CMSUserRole.CATALOGUE_MODERATOR)) {
LOG.info("called isModeratorRoleAssigned");
isCatalogueModerator = true;
}
LOG.info("is " + CMSUserRole.CATALOGUE_MODERATOR.getRoleName() + " assigned? " + isCatalogueModerator);
return isCatalogueModerator;
}
2022-02-21 15:59:13 +01:00
/**
* Gets the CMS roles for user in the context.
*
* @return the CMS roles for user in the context
* @throws Exception the exception
*/
@Override
public ModerationUserRole getCMSRolesForUserInTheContext() throws Exception {
LOG.info("called getCMSRolesForUserInTheContext");
setContexts();
List<CMSUserRole> roles = GcubeContextUtil.getCMSRoleForUserInTheScope(getThreadLocalRequest());
2022-02-21 15:59:13 +01:00
GcubeContextUtil.getCurrentUser(getThreadLocalRequest());
ModerationUserRole userRole = new ModerationUserRole(getServletInfo(), roles);
LOG.info("return: " + userRole);
2022-02-21 15:59:13 +01:00
return userRole;
}
2022-02-08 15:41:30 +01:00
/**
* Sets the contexts.
*
* @return the scope operating in
*/
private String setContexts() {
2022-02-21 15:59:13 +01:00
String scope = GcubeContextUtil.getCurrentScope(this.getThreadLocalRequest());
GCubeUser user = GcubeContextUtil.getCurrentUser(this.getThreadLocalRequest());
String token = GcubeContextUtil.getCurrentToken(scope, user.getUsername());
2022-02-08 15:41:30 +01:00
ScopeProvider.instance.set(scope);
SecurityTokenProvider.instance.set(token);
return scope;
2021-05-07 16:55:35 +02:00
}
/**
* Gets the list items for status.
*
* @param theStatus the the status
2021-06-24 15:02:49 +02:00
* @param offset the offset
* @param limit the limit
* @return the list items for status
* @throws Exception the exception
*/
2021-06-01 18:36:18 +02:00
@Override
public List<CatalogueDataset> getListItemsForStatus(ItemStatus theStatus, int offset, int limit) throws Exception {
LOG.info("called getListItemsForStatus with [status: " + theStatus + ", offset: " + offset + "], [limit: "
+ limit + "]");
2021-06-14 17:39:44 +02:00
List<CatalogueDataset> datasetList = null;
2021-06-01 18:36:18 +02:00
try {
2022-02-08 15:41:30 +01:00
String scope = setContexts();
2021-06-16 18:02:54 +02:00
DataCatalogueImpl catalogueImpl = CatalogueCMSFactory.getFactory().getCatalogueImplPerScope(scope);
2021-06-24 15:02:49 +02:00
CatalogueContentModeratorSystem cmsInstance = catalogueImpl.getCatalogueContentModerator();
2021-06-14 17:39:44 +02:00
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);
2021-06-24 15:02:49 +02:00
String datasetURL = String.format("%s/dataset/%s", catalogueImpl.getCatalogueUrl(), ds.getName());
2021-06-16 18:02:54 +02:00
ds.setUrl(datasetURL);
2021-06-14 17:39:44 +02:00
LOG.debug("converted dataset is: " + ds);
datasetList.add(ds);
}
}
2021-06-01 18:36:18 +02:00
} catch (Exception e) {
LOG.error("Error occurred on reading items for status: " + theStatus, e);
throw e;
2021-06-01 18:36:18 +02:00
}
2021-06-24 15:02:49 +02:00
LOG.info("returning " + datasetList.size() + " dataset");
2021-06-14 17:39:44 +02:00
return datasetList;
2021-06-01 18:36:18 +02:00
}
2022-02-18 14:17:19 +01:00
/**
* Sets the status.
*
* @param theStatus the the status
* @param itemNames the item names
* @return the operation report
* @throws Exception the exception
2022-02-18 14:17:19 +01:00
*/
@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);
Map<String, String> errorMapItems = new HashMap<String, String>();
2022-02-21 15:59:13 +01:00
List<String> changedStatusListItems = new ArrayList<String>();
2022-02-18 14:17:19 +01:00
for (String itemName : itemNames) {
try {
catalogueImpl.refreshDataset(itemName);
2022-02-21 15:59:13 +01:00
changedStatusListItems.add(itemName);
2022-02-18 14:17:19 +01:00
} catch (Exception e) {
LOG.warn("Error when setting status (updating) the itemName: " + itemName, e);
errorMapItems.put(itemName, e.getMessage());
2022-02-18 14:17:19 +01:00
}
}
return new OperationReport(theStatus.getLabel(), changedStatusListItems, errorMapItems);
2022-02-18 14:17:19 +01:00
} catch (Exception e) {
LOG.error(e.getMessage(), e);
2022-02-21 15:59:13 +01:00
throw new Exception("Error occurred on updating the status for item/s: " + itemNames + ". Caused by: "
+ e.getMessage());
2022-02-18 14:17:19 +01:00
}
}
2022-02-08 15:41:30 +01:00
/**
* Approve item.
*
* @param itemNames the item names
* @param moderatorMessage the moderator message
* @return the operation report
* @throws Exception the exception
*/
2021-05-07 16:55:35 +02:00
@Override
2022-02-08 15:41:30 +01:00
public OperationReport approveItem(List<String> itemNames, String moderatorMessage) throws Exception {
LOG.info("Called approveItem with name/s: " + itemNames);
2021-06-14 17:39:44 +02:00
2022-02-08 15:41:30 +01:00
try {
String scope = setContexts();
DataCatalogueImpl catalogueImpl = CatalogueCMSFactory.getFactory().getCatalogueImplPerScope(scope);
CatalogueContentModeratorSystem cmsInstance = catalogueImpl.getCatalogueContentModerator();
Map<String, String> errorMapItems = new HashMap<String, String>();
2022-02-08 15:41:30 +01:00
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);
errorMapItems.put(itemName, e.getMessage());
2022-02-08 15:41:30 +01:00
}
}
return new OperationReport(ItemStatus.APPROVED.getLabel(), approvedListItems, errorMapItems);
2022-02-08 15:41:30 +01:00
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throw new Exception("Error occurred on approving item/s: " + itemNames + ". Caused by: " + e.getMessage());
}
2021-05-07 16:55:35 +02:00
}
2022-02-08 15:41:30 +01:00
/**
* 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
*/
2021-05-07 16:55:35 +02:00
@Override
2022-02-08 15:41:30 +01:00
public OperationReport rejectItem(List<String> itemNames, boolean permanentlyDelete, String reasonMsg)
throws Exception {
LOG.info("Called rejectItem with name/s: " + itemNames + ", permanentlyDelete: " + permanentlyDelete);
2021-06-14 17:39:44 +02:00
2022-02-08 15:41:30 +01:00
try {
String scope = setContexts();
DataCatalogueImpl catalogueImpl = CatalogueCMSFactory.getFactory().getCatalogueImplPerScope(scope);
CatalogueContentModeratorSystem cmsInstance = catalogueImpl.getCatalogueContentModerator();
Map<String, String> errorMapItems = new HashMap<String, String>();
2022-02-08 15:41:30 +01:00
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);
errorMapItems.put(itemName, e.getMessage());
2022-02-08 15:41:30 +01:00
}
}
return new OperationReport(ItemStatus.REJECTED.getLabel(), passedListItems, errorMapItems);
2022-02-08 15:41:30 +01:00
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throw new Exception("Error occurred on rejecting item/s: " + itemNames + ". Caused by: " + e.getMessage());
}
2021-05-07 16:55:35 +02:00
}
2022-02-08 15:41:30 +01:00
/**
* Permanently delete.
*
* @param itemNames the item names
* @return the operation report
* @throws Exception the exception
*/
2021-05-07 16:55:35 +02:00
@Override
2022-02-08 15:41:30 +01:00
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();
Map<String, String> errorMapItems = new HashMap<String, String>();
2022-02-08 15:41:30 +01:00
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);
errorMapItems.put(itemName, e.getMessage());
2022-02-08 15:41:30 +01:00
}
}
return new OperationReport("Permanently Delete", passedListItems, errorMapItems);
2021-05-26 17:58:12 +02:00
2022-02-08 15:41:30 +01:00
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throw new Exception(
"Error occurred on permanently delete item/s: " + itemNames + ". Caused by: " + e.getMessage());
}
2021-06-14 17:39:44 +02:00
}
2022-02-08 15:41:30 +01:00
/**
* 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
2021-06-16 18:02:54 +02:00
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);
String scope = setContexts();
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) {
String error = "Error occurred on getting items for status: " + status;
LOG.error(error, e);
throw new Exception(error+". Cause: "+e.getMessage());
}
2021-06-16 18:02:54 +02:00
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;
}
LOG.debug("Returning: " + searchedData);
return searchedData;
}
2021-05-07 16:55:35 +02:00
}