2017-01-26 18:21:33 +01:00
|
|
|
package org.gcube.datacatalogue.grsf_manage_widget.server.manage;
|
|
|
|
|
2017-11-09 14:49:07 +01:00
|
|
|
import java.util.ArrayList;
|
2017-01-26 18:21:33 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2017-12-12 18:54:45 +01:00
|
|
|
import java.util.UUID;
|
2017-01-26 18:21:33 +01:00
|
|
|
|
2018-02-02 15:28:35 +01:00
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
|
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
2017-01-26 18:21:33 +01:00
|
|
|
import org.gcube.common.portal.PortalContext;
|
2018-02-02 15:28:35 +01:00
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2017-01-26 18:21:33 +01:00
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue;
|
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogueFactory;
|
2018-02-23 15:21:33 +01:00
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.server.utils.CatalogueUtilMethods;
|
2017-10-27 17:30:56 +02:00
|
|
|
import org.gcube.datacatalogue.common.Constants;
|
2017-11-15 19:22:20 +01:00
|
|
|
import org.gcube.datacatalogue.common.enums.Product_Type;
|
2017-11-09 14:49:07 +01:00
|
|
|
import org.gcube.datacatalogue.common.enums.Sources;
|
2017-10-27 17:30:56 +02:00
|
|
|
import org.gcube.datacatalogue.common.enums.Status;
|
2017-11-09 14:49:07 +01:00
|
|
|
import org.gcube.datacatalogue.grsf_manage_widget.client.GRSFManageWidgetService;
|
2017-12-12 18:54:45 +01:00
|
|
|
import org.gcube.datacatalogue.grsf_manage_widget.shared.ConnectedBean;
|
2017-01-26 18:21:33 +01:00
|
|
|
import org.gcube.datacatalogue.grsf_manage_widget.shared.ManageProductBean;
|
2018-02-04 17:42:34 +01:00
|
|
|
import org.gcube.datacatalogue.grsf_manage_widget.shared.RevertableOperationInfo;
|
|
|
|
import org.gcube.datacatalogue.grsf_manage_widget.shared.RevertableOperations;
|
2017-11-09 14:49:07 +01:00
|
|
|
import org.gcube.datacatalogue.grsf_manage_widget.shared.SimilarGRSFRecord;
|
|
|
|
import org.gcube.datacatalogue.grsf_manage_widget.shared.SourceRecord;
|
2017-01-26 18:21:33 +01:00
|
|
|
import org.gcube.datacatalogue.grsf_manage_widget.shared.ex.NoGRSFRecordException;
|
2017-11-09 14:49:07 +01:00
|
|
|
import org.gcube.vomanagement.usermanagement.RoleManager;
|
2017-02-16 18:32:19 +01:00
|
|
|
import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager;
|
2018-02-05 17:16:14 +01:00
|
|
|
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
|
2017-03-03 18:26:35 +01:00
|
|
|
import org.gcube.vomanagement.usermanagement.model.GCubeTeam;
|
2018-02-02 15:28:35 +01:00
|
|
|
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
|
2017-01-26 18:21:33 +01:00
|
|
|
|
|
|
|
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
|
2018-02-03 19:17:37 +01:00
|
|
|
import com.liferay.portal.kernel.log.Log;
|
|
|
|
import com.liferay.portal.kernel.log.LogFactoryUtil;
|
2017-01-26 18:21:33 +01:00
|
|
|
|
2018-02-02 15:28:35 +01:00
|
|
|
import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.HttpClientBuilder;
|
2017-01-26 18:21:33 +01:00
|
|
|
import eu.trentorise.opendata.jackan.model.CkanDataset;
|
|
|
|
import eu.trentorise.opendata.jackan.model.CkanPair;
|
2017-11-09 14:49:07 +01:00
|
|
|
import eu.trentorise.opendata.jackan.model.CkanResource;
|
2017-01-26 18:21:33 +01:00
|
|
|
|
|
|
|
/**
|
2017-11-09 14:49:07 +01:00
|
|
|
* Endpoint for sending update records information to GRSF KnowledgeBase.
|
2017-01-26 18:21:33 +01:00
|
|
|
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
|
|
|
*/
|
|
|
|
public class GRSFNotificationService extends RemoteServiceServlet implements GRSFManageWidgetService{
|
|
|
|
|
|
|
|
private static final long serialVersionUID = -4534905087994875893L;
|
2018-02-03 19:17:37 +01:00
|
|
|
private static final Log logger = LogFactoryUtil.getLog(GRSFNotificationService.class);
|
|
|
|
//private static final Logger logger = LoggerFactory.getLogger(GRSFNotificationService.class);
|
2017-11-15 19:22:20 +01:00
|
|
|
|
2017-01-26 18:21:33 +01:00
|
|
|
/**
|
|
|
|
* Instanciate the ckan util library.
|
|
|
|
* Since it needs the scope, we need to check if it is null or not
|
|
|
|
* @param discoverScope if you want to the discover the utils library in this specified scope
|
2017-11-09 14:49:07 +01:00
|
|
|
* @return DataCatalogue object
|
|
|
|
* @throws Exception
|
2017-01-26 18:21:33 +01:00
|
|
|
*/
|
2017-11-09 14:49:07 +01:00
|
|
|
public DataCatalogue getCatalogue(String discoverScope) throws Exception{
|
2017-11-14 18:37:07 +01:00
|
|
|
String currentScope = Utils.getCurrentContext(getThreadLocalRequest(), true);
|
2017-01-26 18:21:33 +01:00
|
|
|
DataCatalogue instance = null;
|
|
|
|
try{
|
2018-02-02 15:28:35 +01:00
|
|
|
String scopeInWhichDiscover = (discoverScope != null && !discoverScope.isEmpty()) ? discoverScope : currentScope;
|
2017-01-26 18:21:33 +01:00
|
|
|
logger.debug("Discovering ckan utils library into scope " + scopeInWhichDiscover);
|
|
|
|
instance = DataCatalogueFactory.getFactory().getUtilsPerScope(scopeInWhichDiscover);
|
|
|
|
}catch(Exception e){
|
2018-02-03 19:17:37 +01:00
|
|
|
logger.error("Unable to retrieve ckan utils. Error was ", e);
|
2017-11-09 14:49:07 +01:00
|
|
|
throw e;
|
2017-01-26 18:21:33 +01:00
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-02-27 17:13:41 +01:00
|
|
|
public ManageProductBean getProductBeanById(String productIdentifier, boolean requestForRevertingMerge) throws Exception {
|
2017-01-26 18:21:33 +01:00
|
|
|
|
2017-12-12 18:54:45 +01:00
|
|
|
ManageProductBean toReturn = null;
|
2017-12-18 11:08:02 +01:00
|
|
|
|
2018-02-03 19:17:37 +01:00
|
|
|
// check into user's session first
|
2018-02-02 15:28:35 +01:00
|
|
|
HttpSession httpSession = getThreadLocalRequest().getSession();
|
|
|
|
|
2018-02-26 19:44:59 +01:00
|
|
|
// testing case...
|
2017-12-12 18:54:45 +01:00
|
|
|
if(!Utils.isIntoPortal()){
|
|
|
|
|
2018-02-05 17:16:14 +01:00
|
|
|
Thread.sleep(2000);
|
|
|
|
|
2017-12-12 18:54:45 +01:00
|
|
|
toReturn = new ManageProductBean();
|
|
|
|
toReturn.setCatalogueIdentifier(UUID.randomUUID().toString());
|
|
|
|
List<ConnectedBean> connectTo = new ArrayList<>();
|
2018-02-03 19:17:37 +01:00
|
|
|
// these are the records alread connected
|
2018-02-02 15:28:35 +01:00
|
|
|
connectTo.add(new ConnectedBean(
|
2018-02-03 19:17:37 +01:00
|
|
|
"uuid-of-a-connected-bean",
|
|
|
|
"Random description",
|
|
|
|
"Random shortName",
|
|
|
|
"Random Title",
|
|
|
|
"http://data.d4science.org/ctlg/GRSF_Admin/uuid-of-a-connected-bean",
|
|
|
|
"semantic identifier of the record",
|
|
|
|
"Fishery"
|
2018-02-02 15:28:35 +01:00
|
|
|
));
|
|
|
|
toReturn.setCurrentConnections(connectTo);
|
2018-02-03 19:17:37 +01:00
|
|
|
|
|
|
|
// these are the "suggested connections"
|
|
|
|
List<ConnectedBean> suggestionsForConnections = new ArrayList<>();
|
|
|
|
suggestionsForConnections.add(new ConnectedBean(
|
|
|
|
"uuid-of-a-connected-bean-suggested",
|
|
|
|
"Random description",
|
|
|
|
"Random shortName",
|
|
|
|
"Random Title",
|
|
|
|
"http://data.d4science.org/ctlg/GRSF_Admin/uuid-of-a-connected-bean-suggested",
|
|
|
|
"semantic identifier of the record suggested for connection",
|
|
|
|
"Fishery"
|
|
|
|
));
|
|
|
|
toReturn.setSuggestedByKnowledgeBaseConnections(suggestionsForConnections);
|
2018-02-05 17:16:14 +01:00
|
|
|
toReturn.setDomain("Stock");
|
2018-02-26 19:44:59 +01:00
|
|
|
toReturn.setCurrentGrsfType("Assessment Unit");
|
2018-02-05 17:16:14 +01:00
|
|
|
toReturn.setKnowledgeBaseId("91f1e413-dc9f-3b4e-b1c5-0e8560177253");
|
2017-12-12 18:54:45 +01:00
|
|
|
toReturn.setShortName("Widow rockfish - US West Coast");
|
|
|
|
toReturn.setShortNameUpdated("Widow rockfish - US West Coast");
|
2018-02-05 17:16:14 +01:00
|
|
|
toReturn.setTitle("sebastes entomelas FAO 77 FAO 67");
|
2017-12-12 18:54:45 +01:00
|
|
|
toReturn.setTraceabilityFlag(true);
|
|
|
|
toReturn.setCurrentStatus(Status.Pending);
|
|
|
|
toReturn.setSemanticIdentifier("asfis:WRO+fao:67;FAO");
|
|
|
|
ArrayList<SourceRecord> sources = new ArrayList<SourceRecord>();
|
|
|
|
sources.add(new SourceRecord("RAM", "http://www.google.it"));
|
2017-12-13 18:34:45 +01:00
|
|
|
sources.add(new SourceRecord("FIRMS", "http://www.google.it"));
|
|
|
|
sources.add(new SourceRecord("FishSource", "http://www.google.it"));
|
2017-12-12 18:54:45 +01:00
|
|
|
toReturn.setSources(sources);
|
|
|
|
List<SimilarGRSFRecord> similarGrsfRecords = new ArrayList<SimilarGRSFRecord>();
|
2018-02-03 19:17:37 +01:00
|
|
|
similarGrsfRecords.add(new SimilarGRSFRecord(
|
|
|
|
"uuid-similar-record-1",
|
|
|
|
"description similar record",
|
|
|
|
"short name similar record 1",
|
|
|
|
"title similar record 1",
|
|
|
|
"http://data.d4science.org/ctlg/GRSF_Admin/uuid-similar-record-1",
|
|
|
|
"semantic identifier record 1",
|
|
|
|
"Stock 1"
|
|
|
|
));
|
|
|
|
similarGrsfRecords.add(new SimilarGRSFRecord(
|
|
|
|
"uuid-similar-record-2",
|
|
|
|
"description similar record",
|
|
|
|
"short name similar record 2",
|
|
|
|
"title similar record 2",
|
|
|
|
"http://data.d4science.org/ctlg/GRSF_Admin/uuid-similar-record-2",
|
|
|
|
"semantic identifier record 2",
|
|
|
|
"Stock 2"
|
|
|
|
));
|
|
|
|
similarGrsfRecords.add(new SimilarGRSFRecord(
|
|
|
|
"uuid-similar-record-3",
|
|
|
|
"description similar record",
|
|
|
|
"short name similar record 3",
|
|
|
|
"title similar record 3",
|
|
|
|
"http://data.d4science.org/ctlg/GRSF_Admin/uuid-similar-record-3",
|
|
|
|
"semantic identifier record 3",
|
|
|
|
"Stock 3"
|
|
|
|
));
|
|
|
|
similarGrsfRecords.add(new SimilarGRSFRecord(
|
|
|
|
"uuid-similar-record-4",
|
|
|
|
"description similar record",
|
|
|
|
"short name similar record 4",
|
|
|
|
"title similar record 4",
|
|
|
|
"http://data.d4science.org/ctlg/GRSF_Admin/uuid-similar-record-4",
|
|
|
|
"semantic identifier record 4",
|
|
|
|
"Stock 4"
|
|
|
|
));
|
|
|
|
similarGrsfRecords.add(new SimilarGRSFRecord(
|
|
|
|
"uuid-similar-record-5",
|
|
|
|
"description similar record",
|
|
|
|
"short name similar record 5",
|
|
|
|
"title similar record 5",
|
|
|
|
"http://data.d4science.org/ctlg/GRSF_Admin/uuid-similar-record-5",
|
|
|
|
"semantic identifier record 5",
|
|
|
|
"Stock 5"
|
|
|
|
));
|
|
|
|
similarGrsfRecords.add(new SimilarGRSFRecord(
|
|
|
|
"uuid-similar-record-6",
|
|
|
|
"description similar record",
|
|
|
|
"short name similar record 6",
|
|
|
|
"title similar record 6",
|
|
|
|
"http://data.d4science.org/ctlg/GRSF_Admin/uuid-similar-record-6",
|
|
|
|
"semantic identifier record 6",
|
|
|
|
"Stock 6"
|
|
|
|
));
|
2017-12-12 18:54:45 +01:00
|
|
|
toReturn.setSimilarGrsfRecords(similarGrsfRecords);
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
String scopePerCurrentUrl = Utils.getScopeFromClientUrl(getThreadLocalRequest());
|
|
|
|
DataCatalogue catalogue = getCatalogue(scopePerCurrentUrl);
|
|
|
|
String username = Utils.getCurrentUser(getThreadLocalRequest()).getUsername();
|
2018-02-02 15:28:35 +01:00
|
|
|
String apiKey = catalogue.getApiKeyFromUsername(username);
|
|
|
|
CkanDataset record = catalogue.getDataset(productIdentifier, apiKey);
|
2017-12-12 18:54:45 +01:00
|
|
|
|
|
|
|
// it cannot be enabled in this case ...
|
|
|
|
if(record == null)
|
|
|
|
throw new Exception("Unable to retrieve information for the selected record, sorry");
|
|
|
|
else{
|
2017-01-26 18:21:33 +01:00
|
|
|
|
2018-02-02 15:28:35 +01:00
|
|
|
logger.debug("Trying to fetch the record....");
|
2017-12-12 18:54:45 +01:00
|
|
|
|
|
|
|
// check it is a grsf record (Source records have a different System Type)
|
2018-02-02 15:28:35 +01:00
|
|
|
Map<String, String> extrasAsMap = record.getExtrasAsHashMap();
|
2017-12-12 18:54:45 +01:00
|
|
|
|
2018-02-02 15:28:35 +01:00
|
|
|
String systemType = extrasAsMap.get(Constants.SYSTEM_TYPE_CUSTOM_KEY);
|
|
|
|
if(systemType == null || systemType.isEmpty() || systemType.equals(Constants.SYSTEM_TYPE_FOR_SOURCES_VALUE))
|
2018-02-03 19:17:37 +01:00
|
|
|
throw new NoGRSFRecordException("This is not a GRSF Record");
|
2018-02-02 15:28:35 +01:00
|
|
|
|
|
|
|
boolean isStock = record.getExtrasAsHashMap().get(Constants.DOMAIN_CUSTOM_KEY).contains(Product_Type.STOCK.getOrigName());
|
|
|
|
|
2017-12-12 18:54:45 +01:00
|
|
|
// fetch map for namespaces
|
2018-02-02 15:28:35 +01:00
|
|
|
Map<String, String> fieldsNamespacesMap =
|
|
|
|
Utils.getFieldToFieldNameSpaceMapping(httpSession, isStock ?
|
|
|
|
Constants.GENERIC_RESOURCE_NAME_MAP_KEY_NAMESPACES_STOCK : Constants.GENERIC_RESOURCE_NAME_MAP_KEY_NAMESPACES_FISHERY);
|
2017-12-12 18:54:45 +01:00
|
|
|
|
2018-02-03 19:17:37 +01:00
|
|
|
// get extras as pairs
|
|
|
|
List<CkanPair> extrasAsPairs = record.getExtras();
|
2017-12-12 18:54:45 +01:00
|
|
|
Map<String, List<String>> extrasWithoutNamespaces = Utils.replaceFieldsKey(extrasAsPairs, fieldsNamespacesMap);
|
|
|
|
String catalogueIdentifier = record.getId();
|
2018-02-05 17:16:14 +01:00
|
|
|
String description = record.getNotes();
|
2018-02-02 15:28:35 +01:00
|
|
|
Status status = Status.fromString(extrasWithoutNamespaces.get(Constants.STATUS_OF_THE_GRSF_RECORD_CUSTOM_KEY).get(0));
|
2018-02-27 17:13:41 +01:00
|
|
|
|
|
|
|
if(status.equals(Status.To_be_Merged) && !requestForRevertingMerge)
|
|
|
|
throw new Exception("Records under merging activities cannot be managed without explicit authorisation!");
|
|
|
|
|
2017-12-12 18:54:45 +01:00
|
|
|
String uuidKB = extrasWithoutNamespaces.get(Constants.UUID_KB_CUSTOM_KEY).get(0);
|
|
|
|
String grsfDomain = extrasWithoutNamespaces.get(Constants.DOMAIN_CUSTOM_KEY).get(0);
|
|
|
|
String semanticId = extrasWithoutNamespaces.get(Constants.GRSF_SEMANTIC_IDENTIFIER_CUSTOM_KEY).get(0);
|
|
|
|
String shortName = extrasWithoutNamespaces.get(Constants.SHORT_NAME_CUSTOM_KEY).get(0);
|
|
|
|
String grsfType = extrasWithoutNamespaces.get(Constants.GRSF_TYPE_CUSTOM_KEY).get(0);
|
2018-02-02 15:28:35 +01:00
|
|
|
String recordUrl = extrasWithoutNamespaces.get(Constants.ITEM_URL_FIELD).get(0);
|
|
|
|
String grsfName = extrasWithoutNamespaces.get(grsfDomain.contains(Product_Type.STOCK.getOrigName()) ? Constants.STOCK_NAME_CUSTOM_KEY : Constants.FISHERY_NAME_CUSTOM_KEY).get(0);
|
|
|
|
boolean traceabilityFlag = false;
|
|
|
|
try{
|
|
|
|
traceabilityFlag = extrasWithoutNamespaces.get(Constants.TRACEABILITY_FLAG_CUSTOM_KEY).get(0).equalsIgnoreCase("true");
|
|
|
|
}catch(Exception e){
|
2018-02-03 19:17:37 +01:00
|
|
|
logger.warn("Unable to fetch traceability flag. Setting it to false", e);
|
2018-02-02 15:28:35 +01:00
|
|
|
}
|
2017-12-12 18:54:45 +01:00
|
|
|
|
2018-02-26 19:44:59 +01:00
|
|
|
boolean sdgFlag = false;
|
|
|
|
try{
|
|
|
|
sdgFlag = extrasWithoutNamespaces.get(Constants.SDG_FLAG_CUSTOM_KEY).get(0).equalsIgnoreCase("true");
|
|
|
|
}catch(Exception e){
|
|
|
|
logger.warn("Unable to fetch sdg flag. Setting it to false", e);
|
|
|
|
}
|
|
|
|
|
2017-12-12 18:54:45 +01:00
|
|
|
// Get similar GRSF records, if any (each of which should have name, description, url and id(i.e semantic identifier))
|
|
|
|
List<String> similarGrsfRecordsAsStrings = extrasWithoutNamespaces.containsKey(Constants.SIMILAR_GRSF_RECORDS_CUSTOM_KEY) ? extrasWithoutNamespaces.get(Constants.SIMILAR_GRSF_RECORDS_CUSTOM_KEY): null;
|
|
|
|
|
|
|
|
List<SimilarGRSFRecord> similarRecords = new ArrayList<SimilarGRSFRecord>(0);
|
2018-02-03 19:17:37 +01:00
|
|
|
if(similarGrsfRecordsAsStrings != null && !similarGrsfRecordsAsStrings.isEmpty()){
|
|
|
|
if(!similarGrsfRecordsAsStrings.get(0).equals(Constants.NO_SIMILAR_GRSF_RECORDS)){
|
|
|
|
for (String similarGRSFRecord : similarGrsfRecordsAsStrings) {
|
2018-02-26 19:44:59 +01:00
|
|
|
similarRecords.add(Utils.similarGRSFRecordFromJson(similarGRSFRecord, catalogue, apiKey, httpSession));
|
2018-02-03 19:17:37 +01:00
|
|
|
}
|
2017-12-12 18:54:45 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-26 18:21:33 +01:00
|
|
|
|
2017-12-12 18:54:45 +01:00
|
|
|
logger.debug("SimilarGRSFRecords are " + similarRecords);
|
2017-01-26 18:21:33 +01:00
|
|
|
|
2018-02-02 15:28:35 +01:00
|
|
|
// get connected records (and the proposed ones)
|
|
|
|
List<String> connectedBeanUrls =
|
|
|
|
extrasWithoutNamespaces.containsKey(Constants.CONNECTED_CUSTOM_KEY) ? extrasWithoutNamespaces.get(Constants.CONNECTED_CUSTOM_KEY): null;
|
|
|
|
|
|
|
|
List<ConnectedBean> connectedBeans = new ArrayList<ConnectedBean>(0);
|
2018-02-03 19:17:37 +01:00
|
|
|
if(connectedBeanUrls != null && !connectedBeanUrls.isEmpty()){
|
|
|
|
if(!connectedBeanUrls.get(0).equals(Constants.NO_CONNECTED_RECORDS)){
|
|
|
|
for (String connectedBean : connectedBeanUrls) {
|
|
|
|
ConnectedBean builtBean = Utils.connectedBeanRecordFromUrl(connectedBean, catalogue, apiKey, httpSession);
|
|
|
|
if(builtBean != null)
|
|
|
|
connectedBeans.add(builtBean);
|
|
|
|
}
|
2018-02-02 15:28:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug("Already connected records are " + connectedBeans);
|
|
|
|
|
|
|
|
// get the connections the knowledge base suggests
|
|
|
|
List<ConnectedBean> suggestedConnectionsByKnowledgeBase = new ArrayList<ConnectedBean>(0);
|
|
|
|
List<String> exploitedResourcesUrls = isStock ?
|
2018-02-06 12:02:40 +01:00
|
|
|
(extrasWithoutNamespaces.containsKey(Constants.EXPLOITING_FISHERY_CUSTOM_KEY) ?
|
|
|
|
extrasWithoutNamespaces.get(Constants.EXPLOITING_FISHERY_CUSTOM_KEY) : null):
|
|
|
|
(extrasWithoutNamespaces.containsKey(Constants.RESOURCES_EXPLOITED_CUSTOM_KEY) ?
|
|
|
|
extrasWithoutNamespaces.get(Constants.RESOURCES_EXPLOITED_CUSTOM_KEY) : null);
|
2018-02-02 15:28:35 +01:00
|
|
|
|
|
|
|
if(exploitedResourcesUrls != null && !exploitedResourcesUrls.isEmpty()){
|
|
|
|
for (String exploited : exploitedResourcesUrls) {
|
2018-02-02 17:32:30 +01:00
|
|
|
ConnectedBean builtBean = Utils.connectedBeanRecordFromUrl(exploited, catalogue, apiKey, httpSession);
|
2018-02-02 15:28:35 +01:00
|
|
|
if(builtBean != null)
|
|
|
|
suggestedConnectionsByKnowledgeBase.add(builtBean);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug("Knowledge base suggests " + suggestedConnectionsByKnowledgeBase);
|
|
|
|
|
|
|
|
// Get sources
|
|
|
|
List<CkanResource> resources = record.getResources();
|
|
|
|
List<SourceRecord> sources = new ArrayList<SourceRecord>(3);
|
|
|
|
for (CkanResource ckanResource : resources) {
|
|
|
|
if(Sources.getListNames().contains(ckanResource.getName()))
|
|
|
|
sources.add(new SourceRecord(ckanResource.getName(), ckanResource.getUrl()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the values
|
2018-02-03 19:17:37 +01:00
|
|
|
toReturn = new ManageProductBean(
|
|
|
|
semanticId, catalogueIdentifier, uuidKB, grsfType,
|
2018-02-26 19:44:59 +01:00
|
|
|
grsfDomain, shortName, description, grsfName,traceabilityFlag, sdgFlag,
|
2018-02-03 19:17:37 +01:00
|
|
|
status, recordUrl, sources, similarRecords,
|
|
|
|
connectedBeans, suggestedConnectionsByKnowledgeBase);
|
2017-12-12 18:54:45 +01:00
|
|
|
|
|
|
|
}
|
2017-01-26 18:21:33 +01:00
|
|
|
}
|
2017-12-12 18:54:45 +01:00
|
|
|
|
2018-02-03 19:17:37 +01:00
|
|
|
logger.debug("Returning item bean " + toReturn);
|
2017-12-12 18:54:45 +01:00
|
|
|
return toReturn;
|
2017-01-26 18:21:33 +01:00
|
|
|
}
|
|
|
|
|
2017-02-16 18:32:19 +01:00
|
|
|
@Override
|
|
|
|
public boolean isAdminUser() {
|
|
|
|
try{
|
2017-08-01 18:00:19 +02:00
|
|
|
Boolean inSession = (Boolean)getThreadLocalRequest().getSession().getAttribute(Constants.GRSF_ADMIN_SESSION_KEY);
|
2017-02-16 18:32:19 +01:00
|
|
|
if(inSession != null)
|
|
|
|
return inSession;
|
|
|
|
else{
|
2017-11-15 19:22:20 +01:00
|
|
|
|
2017-02-16 18:32:19 +01:00
|
|
|
boolean toSetInSession = false;
|
2018-02-02 15:28:35 +01:00
|
|
|
if(!Utils.isIntoPortal()){
|
|
|
|
toSetInSession = true;
|
|
|
|
}else{
|
|
|
|
PortalContext pContext = PortalContext.getConfiguration();
|
|
|
|
RoleManager roleManager = new LiferayRoleManager();
|
|
|
|
String username = pContext.getCurrentUser(getThreadLocalRequest()).getUsername();
|
|
|
|
long userId = pContext.getCurrentUser(getThreadLocalRequest()).getUserId();
|
|
|
|
long groupId = pContext.getCurrentGroupId(getThreadLocalRequest());
|
2018-02-03 19:17:37 +01:00
|
|
|
List<GCubeTeam> teamRolesByUser = roleManager.listTeamsByUserAndGroup(userId, groupId);
|
|
|
|
toSetInSession = isEditor(username, teamRolesByUser) | isReviewer(username, teamRolesByUser);
|
2017-02-16 18:32:19 +01:00
|
|
|
}
|
2017-08-01 18:00:19 +02:00
|
|
|
getThreadLocalRequest().getSession().setAttribute(Constants.GRSF_ADMIN_SESSION_KEY, toSetInSession);
|
2017-02-16 18:32:19 +01:00
|
|
|
return toSetInSession;
|
|
|
|
}
|
|
|
|
}catch(Exception e){
|
2018-01-31 18:28:47 +01:00
|
|
|
logger.error("Failed to check if the user belongs to team " + Constants.GRSF_CATALOGUE_EDITOR_ROLE + " or " + Constants.GRSF_CATALOGUE_REVIEWER_ROLE +"!", e);
|
2017-02-16 18:32:19 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-09 14:49:07 +01:00
|
|
|
|
2017-11-23 20:04:47 +01:00
|
|
|
@Override
|
2018-02-05 17:16:14 +01:00
|
|
|
public void notifyProductUpdate(ManageProductBean bean) throws Exception{
|
2017-11-23 20:04:47 +01:00
|
|
|
|
2018-02-02 15:28:35 +01:00
|
|
|
logger.info("Creating notification for the bean " + bean + " to send to the knowledge base");
|
2018-02-05 17:16:14 +01:00
|
|
|
if(!Utils.isIntoPortal()){
|
|
|
|
Thread.sleep(2500);
|
|
|
|
return;
|
|
|
|
}
|
2018-02-02 15:28:35 +01:00
|
|
|
try{
|
|
|
|
|
|
|
|
String context = Utils.getScopeFromClientUrl(getThreadLocalRequest());
|
|
|
|
String token = SecurityTokenProvider.instance.get();
|
|
|
|
DataCatalogue catalogue = getCatalogue(context);
|
|
|
|
String administratorFullName = Utils.getCurrentUser(getThreadLocalRequest()).getFullname();
|
|
|
|
String username = Utils.getCurrentUser(getThreadLocalRequest()).getUsername();
|
2018-02-03 19:17:37 +01:00
|
|
|
|
2018-02-02 15:28:35 +01:00
|
|
|
// check if the base url of the service is in session
|
2018-02-23 15:21:33 +01:00
|
|
|
String keyPerContext = CatalogueUtilMethods.concatenateSessionKeyScope(Constants.GRSF_UPDATER_SERVICE, context);
|
2018-02-02 15:28:35 +01:00
|
|
|
HttpServletRequest threadRequest = getThreadLocalRequest();
|
|
|
|
String baseUrl = (String)threadRequest.getSession().getAttribute(keyPerContext);
|
|
|
|
if(baseUrl == null || baseUrl.isEmpty()){
|
|
|
|
baseUrl = GRSFUpdaterServiceClient.discoverEndPoint(context);
|
|
|
|
threadRequest.getSession().setAttribute(keyPerContext, baseUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove it from the session
|
|
|
|
String sessionProductKey = ScopeProvider.instance.get() + bean.getCatalogueIdentifier();
|
|
|
|
threadRequest.getSession().removeAttribute(sessionProductKey);
|
|
|
|
|
2018-02-05 17:16:14 +01:00
|
|
|
Utils.updateRecord(baseUrl, bean, catalogue, username, administratorFullName, threadRequest,
|
|
|
|
PortalContext.getConfiguration().getCurrentGroupId(threadRequest), context, token);
|
2018-02-02 15:28:35 +01:00
|
|
|
|
|
|
|
}catch(Exception e){
|
|
|
|
logger.error("Unable to update the product", e);
|
|
|
|
throw e;
|
|
|
|
}
|
2017-11-23 20:04:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-02-04 17:42:34 +01:00
|
|
|
public RevertableOperationInfo validateRevertOperation(String encryptedUrl) throws Exception {
|
|
|
|
|
|
|
|
if(!Utils.isIntoPortal()){
|
2018-02-05 17:16:14 +01:00
|
|
|
Thread.sleep(2000);
|
|
|
|
|
|
|
|
// random result
|
|
|
|
boolean throwException = Math.random() > 0.5;
|
|
|
|
|
|
|
|
if(throwException)
|
|
|
|
throw new Exception("Unable to parse the inserted url");
|
|
|
|
|
2018-02-04 17:42:34 +01:00
|
|
|
String baseUrl = "url of the record here";
|
|
|
|
String fullName = "Andrea Rossi";
|
2018-02-09 15:27:37 +01:00
|
|
|
String usernameCurrent = "andrea.rossi";
|
2018-02-04 17:42:34 +01:00
|
|
|
String uuid = UUID.randomUUID().toString();
|
|
|
|
String adminInUrl = "costantino.perciante";
|
2018-02-05 17:16:14 +01:00
|
|
|
String adminInUrlFullName = "Costantino Perciante";
|
2018-02-04 17:42:34 +01:00
|
|
|
long timestamp = System.currentTimeMillis() - 1000 * ((long)(Math.random() * 10 * 60 * 60));
|
2018-02-05 17:16:14 +01:00
|
|
|
return new RevertableOperationInfo(
|
2018-02-09 15:27:37 +01:00
|
|
|
baseUrl, fullName, usernameCurrent, uuid, adminInUrlFullName, adminInUrl, timestamp, RevertableOperations.MERGE);
|
2018-02-04 17:42:34 +01:00
|
|
|
}
|
2018-02-02 15:28:35 +01:00
|
|
|
|
|
|
|
PortalContext pContext = PortalContext.getConfiguration();
|
|
|
|
String context = Utils.getScopeFromClientUrl(getThreadLocalRequest());
|
|
|
|
RoleManager roleManager = new LiferayRoleManager();
|
|
|
|
GCubeUser user = pContext.getCurrentUser(getThreadLocalRequest());
|
|
|
|
String username = user.getUsername();
|
|
|
|
String fullName = user.getFullname();
|
|
|
|
long userId = pContext.getCurrentUser(getThreadLocalRequest()).getUserId();
|
|
|
|
long groupId = pContext.getCurrentGroupId(getThreadLocalRequest());
|
2018-02-03 19:17:37 +01:00
|
|
|
List<GCubeTeam> teamRolesByUser = roleManager.listTeamsByUserAndGroup(userId, groupId);
|
2018-02-02 15:28:35 +01:00
|
|
|
|
2018-02-03 19:17:37 +01:00
|
|
|
boolean isEditor = isEditor(username, teamRolesByUser);
|
|
|
|
boolean isReviewer = isReviewer(username, teamRolesByUser);
|
2018-02-02 15:28:35 +01:00
|
|
|
|
|
|
|
if(!(isEditor | isReviewer))
|
|
|
|
throw new Exception("You are not allowed to perform this operation!");
|
|
|
|
|
|
|
|
// decrypt the url
|
|
|
|
RevertOperationUrl decryptedUrl = new RevertOperationUrl(encryptedUrl);
|
2018-02-05 17:16:14 +01:00
|
|
|
String userNameadminInUrl = decryptedUrl.getAdmin(); // this is the username
|
|
|
|
String fullNameadminInUrl = new LiferayUserManager().getUserByUsername(userNameadminInUrl).getFullname(); // this is the fullname
|
2018-02-02 15:28:35 +01:00
|
|
|
String uuid = decryptedUrl.getUuid();
|
|
|
|
|
2018-02-05 17:16:14 +01:00
|
|
|
logger.info("User " + username + " has requested to invert an operation on record with id " + uuid + " and admin in url is " + userNameadminInUrl);
|
2018-02-02 15:28:35 +01:00
|
|
|
|
|
|
|
// we need to check the timestamp (it has 24h validity)
|
|
|
|
boolean isValidTimestamp = decryptedUrl.isTimestampValid();
|
|
|
|
|
|
|
|
if(!isValidTimestamp)
|
|
|
|
throw new Exception("This operation can no longer be reverted (link expired)!");
|
2018-02-05 17:16:14 +01:00
|
|
|
|
2018-02-04 17:42:34 +01:00
|
|
|
DataCatalogue catalogue = getCatalogue(context);
|
2018-02-08 16:57:52 +01:00
|
|
|
Map<String, String> extras = catalogue.getDataset(uuid, catalogue.getApiKeyFromUsername(username)).getExtrasAsHashMap();
|
|
|
|
String recordUrl = extras.get(Constants.ITEM_URL_FIELD);
|
|
|
|
String currentStatus = extras.get(Constants.STATUS_OF_THE_GRSF_RECORD_CUSTOM_KEY);
|
|
|
|
|
|
|
|
// check current record status
|
|
|
|
if(!currentStatus.equals(Status.To_be_Merged.getOrigName()))
|
|
|
|
throw new Exception("This record is no longer involved in a merge operation!");
|
2018-02-02 15:28:35 +01:00
|
|
|
|
2018-02-04 17:42:34 +01:00
|
|
|
// check if it is a reviewer, than he can do what he wants (no matter the admin)
|
|
|
|
if(isReviewer){
|
|
|
|
return new RevertableOperationInfo(recordUrl,
|
2018-02-09 15:27:37 +01:00
|
|
|
fullName, username, uuid, fullNameadminInUrl, userNameadminInUrl, decryptedUrl.getTimestamp(), decryptedUrl.getOperation());
|
2018-02-04 17:42:34 +01:00
|
|
|
}else{
|
|
|
|
|
2018-02-05 17:16:14 +01:00
|
|
|
if(!username.equals(userNameadminInUrl))
|
2018-02-04 17:42:34 +01:00
|
|
|
throw new Exception("You are not the editor allowed to perform this operation!");
|
|
|
|
else
|
|
|
|
return new RevertableOperationInfo(recordUrl,
|
2018-02-09 15:27:37 +01:00
|
|
|
fullName, username, uuid, fullNameadminInUrl, userNameadminInUrl, decryptedUrl.getTimestamp(), decryptedUrl.getOperation());
|
2018-02-02 15:28:35 +01:00
|
|
|
}
|
2017-11-23 20:04:47 +01:00
|
|
|
|
2018-02-04 17:42:34 +01:00
|
|
|
}
|
2017-12-07 12:37:21 +01:00
|
|
|
|
2018-02-04 17:42:34 +01:00
|
|
|
@Override
|
|
|
|
public Boolean performRevertOperation(RevertableOperationInfo rInfo)
|
|
|
|
throws Exception {
|
2018-02-05 17:16:14 +01:00
|
|
|
|
2018-02-04 17:42:34 +01:00
|
|
|
if(!Utils.isIntoPortal()){
|
|
|
|
// random result
|
|
|
|
boolean toReturn = Math.random() > 0.5;
|
2018-02-05 17:16:14 +01:00
|
|
|
|
2018-02-04 17:42:34 +01:00
|
|
|
if(toReturn){
|
2018-02-05 17:16:14 +01:00
|
|
|
|
2018-02-04 17:42:34 +01:00
|
|
|
boolean throwException = Math.random() > 0.5;
|
|
|
|
if(throwException)
|
|
|
|
throw new Exception("Unable to execute request for XYZ");
|
2018-02-05 17:16:14 +01:00
|
|
|
|
2018-02-04 17:42:34 +01:00
|
|
|
}
|
|
|
|
return toReturn;
|
|
|
|
}
|
2018-02-05 17:16:14 +01:00
|
|
|
|
2018-02-04 17:42:34 +01:00
|
|
|
HttpServletRequest threadRequest = getThreadLocalRequest();
|
|
|
|
String context = Utils.getScopeFromClientUrl(threadRequest);
|
|
|
|
String token = SecurityTokenProvider.instance.get();
|
2018-02-05 17:16:14 +01:00
|
|
|
|
2018-02-02 15:28:35 +01:00
|
|
|
try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();){
|
2018-02-04 17:42:34 +01:00
|
|
|
|
2018-02-23 15:21:33 +01:00
|
|
|
String keyPerContext = CatalogueUtilMethods.concatenateSessionKeyScope(Constants.GRSF_UPDATER_SERVICE, context);
|
2018-02-04 17:42:34 +01:00
|
|
|
String baseUrl = (String)getThreadLocalRequest().getSession().getAttribute(keyPerContext);
|
|
|
|
if(baseUrl == null || baseUrl.isEmpty()){
|
|
|
|
baseUrl = GRSFUpdaterServiceClient.discoverEndPoint(context);
|
|
|
|
getThreadLocalRequest().getSession().setAttribute(keyPerContext, baseUrl);
|
2017-11-23 20:04:47 +01:00
|
|
|
}
|
2018-02-04 17:42:34 +01:00
|
|
|
|
|
|
|
if(baseUrl == null || baseUrl.isEmpty())
|
|
|
|
throw new Exception("Unable to discover grsf-updater service!");
|
2018-02-05 17:16:14 +01:00
|
|
|
|
|
|
|
Utils.revertOperation(httpClient, baseUrl, threadRequest, rInfo, token, context,
|
|
|
|
PortalContext.getConfiguration().getCurrentGroupId(threadRequest));
|
|
|
|
|
2018-02-04 17:42:34 +01:00
|
|
|
}
|
|
|
|
catch(Exception e){
|
|
|
|
logger.error("Unable to revert operation ", e);
|
2018-02-02 15:28:35 +01:00
|
|
|
throw e;
|
2017-11-23 20:04:47 +01:00
|
|
|
}
|
2018-02-04 17:42:34 +01:00
|
|
|
return true;
|
2017-11-23 20:04:47 +01:00
|
|
|
}
|
|
|
|
|
2018-02-02 15:28:35 +01:00
|
|
|
/**
|
|
|
|
* Check if the current user is an editor
|
|
|
|
* @param username
|
|
|
|
* @param teamRoles
|
|
|
|
* @return true if he/she is an editor, false otherwise
|
|
|
|
*/
|
2018-02-03 19:17:37 +01:00
|
|
|
private boolean isEditor(String username, List<GCubeTeam> teamRolesByUser){
|
2017-11-23 20:04:47 +01:00
|
|
|
|
2018-02-03 19:17:37 +01:00
|
|
|
for (GCubeTeam team : teamRolesByUser) {
|
2018-02-02 15:28:35 +01:00
|
|
|
if(team.getTeamName().equals(Constants.GRSF_CATALOGUE_EDITOR_ROLE)){
|
|
|
|
logger.info("User " + username + " is allowed to modify GRSF records as editor");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2017-11-23 20:04:47 +01:00
|
|
|
}
|
|
|
|
|
2018-02-02 15:28:35 +01:00
|
|
|
/**
|
|
|
|
* Check if the current user is a reviewer
|
|
|
|
* @param username
|
|
|
|
* @param teamRoles
|
|
|
|
* @return true if he/she is an reviewer, false otherwise
|
|
|
|
*/
|
2018-02-03 19:17:37 +01:00
|
|
|
private boolean isReviewer(String username, List<GCubeTeam> teamRolesByUser){
|
2018-02-02 15:28:35 +01:00
|
|
|
|
2018-02-03 19:17:37 +01:00
|
|
|
for (GCubeTeam team : teamRolesByUser) {
|
2018-02-08 16:57:52 +01:00
|
|
|
if(team.getTeamName().equals(Constants.GRSF_CATALOGUE_REVIEWER_ROLE)){
|
2018-02-02 15:28:35 +01:00
|
|
|
logger.info("User " + username + " is allowed to modify GRSF records as reviewer");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-07 12:37:21 +01:00
|
|
|
|
|
|
|
@Override
|
2018-01-23 18:25:16 +01:00
|
|
|
public String checkIdentifierExists(String id)
|
2017-12-07 12:37:21 +01:00
|
|
|
throws Exception {
|
|
|
|
String scopePerCurrentUrl = Utils.getScopeFromClientUrl(getThreadLocalRequest());
|
|
|
|
DataCatalogue catalogue = getCatalogue(scopePerCurrentUrl);
|
|
|
|
String username = Utils.getCurrentUser(getThreadLocalRequest()).getUsername();
|
2018-01-23 18:25:16 +01:00
|
|
|
CkanDataset dataset = catalogue.getDataset(id, catalogue.getApiKeyFromUsername(username));
|
|
|
|
if(dataset == null)
|
2018-02-03 19:17:37 +01:00
|
|
|
throw new Exception("A GRSF record with id " + id + " doesn't exist");
|
|
|
|
|
|
|
|
if(!dataset.getOrganization().getName().equals(Constants.GRSF_ADMIN_ORGANIZATION_NAME))
|
|
|
|
throw new Exception("The suggested record is not a GRSF record");
|
|
|
|
|
2018-01-23 18:25:16 +01:00
|
|
|
return dataset.getExtrasAsHashMap().get(Constants.ITEM_URL_FIELD);
|
2017-12-07 12:37:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-01-23 18:25:16 +01:00
|
|
|
public String checkIdentifierExistsInDomain(String id,
|
2018-02-14 16:25:23 +01:00
|
|
|
String acceptedDomain) throws Exception {
|
2018-02-04 17:42:34 +01:00
|
|
|
|
2018-02-03 20:44:21 +01:00
|
|
|
if(!Utils.isIntoPortal()){
|
|
|
|
boolean throwException = Math.random() > 0.5;
|
2018-02-04 17:42:34 +01:00
|
|
|
|
2018-02-03 20:44:21 +01:00
|
|
|
// simulate some delay...
|
|
|
|
Thread.sleep(2500);
|
2018-02-04 17:42:34 +01:00
|
|
|
|
2018-02-03 20:44:21 +01:00
|
|
|
if(throwException)
|
|
|
|
throw new Exception("The suggested record is not a GRSF record");
|
2018-02-04 17:42:34 +01:00
|
|
|
|
2018-02-03 20:44:21 +01:00
|
|
|
return "http://data.d4science.org/catalogue/grsf_admin/" + id;
|
|
|
|
}
|
2018-02-04 17:42:34 +01:00
|
|
|
|
2017-12-07 12:37:21 +01:00
|
|
|
String scopePerCurrentUrl = Utils.getScopeFromClientUrl(getThreadLocalRequest());
|
|
|
|
DataCatalogue catalogue = getCatalogue(scopePerCurrentUrl);
|
|
|
|
String username = Utils.getCurrentUser(getThreadLocalRequest()).getUsername();
|
|
|
|
CkanDataset dataset = catalogue.getDataset(id, catalogue.getApiKeyFromUsername(username));
|
2018-02-03 19:17:37 +01:00
|
|
|
|
2017-12-07 12:37:21 +01:00
|
|
|
if(dataset == null)
|
2018-02-02 15:28:35 +01:00
|
|
|
throw new Exception("A record with id " + id + " doesn't exist");
|
2018-02-14 16:25:23 +01:00
|
|
|
|
|
|
|
Map<String, String> extras = dataset.getExtrasAsHashMap();
|
|
|
|
String systemType = extras.get(Constants.SYSTEM_TYPE_CUSTOM_KEY);
|
|
|
|
String domain = extras.get(Constants.DOMAIN_CUSTOM_KEY);
|
|
|
|
String url = extras.get(Constants.ITEM_URL_FIELD);
|
|
|
|
|
|
|
|
if(systemType.equals(Constants.SYSTEM_TYPE_FOR_SOURCES_VALUE))
|
|
|
|
throw new Exception("This record is not a GRSF record!");
|
|
|
|
|
|
|
|
if(!acceptedDomain.equalsIgnoreCase(domain))
|
|
|
|
throw new Exception("You are suggesting a " + domain + " record instead of a " + acceptedDomain + " record!");
|
|
|
|
|
|
|
|
return url;
|
2017-12-07 12:37:21 +01:00
|
|
|
}
|
|
|
|
|
2017-01-26 18:21:33 +01:00
|
|
|
}
|