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

182 lines
7.5 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;
2021-06-01 18:36:18 +02:00
import java.util.List;
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;
2021-06-14 11:27:53 +02:00
import org.gcube.portlets.widgets.ckancontentmoderator.shared.CatalogueDataset;
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;
/**
* The server side implementation of the RPC service.
*/
@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
@Override
public boolean isContentModeratorEnabled() {
2021-06-15 16:52:27 +02:00
LOG.info("called isContentModeratorEnabled");
LOG.warn("isContentModeratorEnabled METHOD MUST BE IMPLEMENTED!!!");
return true;
2021-05-07 16:55:35 +02:00
}
@Override
2021-06-24 15:02:49 +02:00
public void setStatus(ItemStatus theStatus, List<String> itemNames) {
LOG.info("Called set status " + theStatus + " for Items with name: " + itemNames);
2021-06-14 17:39:44 +02:00
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 {
2021-06-14 17:39:44 +02:00
String scope = WsUtil.getCurrentScope(this.getThreadLocalRequest());
GCubeUser user = WsUtil.getCurrentUser(this.getThreadLocalRequest());
2021-06-01 18:36:18 +02:00
String token = WsUtil.getCurrentToken(scope, user.getUsername());
ScopeProvider.instance.set(scope);
SecurityTokenProvider.instance.set(token);
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) {
2021-06-14 17:39:44 +02:00
LOG.error(e.getMessage(), e);
throw new Exception(
"Error occurrend on reading items for status: " + theStatus + ". Caused by: " + e.getMessage());
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
}
2021-05-07 16:55:35 +02:00
@Override
2021-06-24 15:02:49 +02:00
public void approveItem(List<String> itemNames) {
LOG.info("Called approve Items with name: " + itemNames);
2021-06-14 17:39:44 +02:00
2021-05-07 16:55:35 +02:00
}
@Override
public void rejectItem(String itemId, boolean permanentlyDelete, String reasonMsg) {
// TODO Auto-generated method stub
2021-06-14 17:39:44 +02:00
2021-05-07 16:55:35 +02:00
}
@Override
2021-06-24 15:02:49 +02:00
public void permanentlyDelete(List<String> itemNames) {
LOG.info("Called approve Items with name: " + itemNames);
2021-05-26 17:58:12 +02:00
2021-06-14 17:39:44 +02:00
}
@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);
try {
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);
2021-06-16 18:02:54 +02:00
// 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);
2021-06-16 18:02:54 +02:00
LOG.info("totalItemsForStatus " + status + " are : " + totalItemsForStatus);
List<CatalogueDataset> listDataset = new ArrayList<CatalogueDataset>();
try {
2021-06-16 18:02:54 +02:00
LOG.debug("getListItemsForStatus with searchStartIndex: " + searchStartIndex + ", limit: " + limit);
listDataset = getListItemsForStatus(status, searchStartIndex, limit);
2021-06-16 18:02:54 +02:00
} catch (Exception e) {
LOG.error("Error occurred on gettin items for status: " + status, e);
}
2021-06-16 18:02:54 +02:00
int listDatasetSize = listDataset.size();
2021-06-16 18:02:54 +02:00
LOG.debug("Returned " + listDatasetSize + " with above parameters");
searchedData.setData(listDataset);
searchedData.setTotalItems(totalItemsForStatus);
2021-06-16 18:02:54 +02:00
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;
}
2021-06-16 18:02:54 +02:00
/*
* 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");
}
}
2021-05-07 16:55:35 +02:00
}