|
|
@ -16,7 +16,12 @@ package org.gcube.portlets.user.cataloguebadge;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.text.DecimalFormat;
|
|
|
|
import java.text.DecimalFormat;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import java.util.Base64;
|
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.portlet.PortletPreferences;
|
|
|
|
import javax.portlet.RenderRequest;
|
|
|
|
import javax.portlet.RenderRequest;
|
|
|
|
import javax.portlet.RenderResponse;
|
|
|
|
import javax.portlet.RenderResponse;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
@ -29,6 +34,10 @@ import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogueImpl;
|
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.shared.Statistics;
|
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.shared.Statistics;
|
|
|
|
import org.gcube.vomanagement.usermanagement.GroupManager;
|
|
|
|
import org.gcube.vomanagement.usermanagement.GroupManager;
|
|
|
|
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
|
|
|
|
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
|
|
|
|
|
|
|
|
import org.jsoup.Jsoup;
|
|
|
|
|
|
|
|
import org.jsoup.nodes.Document;
|
|
|
|
|
|
|
|
import org.jsoup.nodes.Element;
|
|
|
|
|
|
|
|
import org.jsoup.select.Elements;
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
import org.springframework.ui.Model;
|
|
|
|
import org.springframework.ui.Model;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
@ -36,7 +45,9 @@ import org.springframework.web.portlet.bind.annotation.RenderMapping;
|
|
|
|
|
|
|
|
|
|
|
|
import com.liferay.portal.kernel.log.Log;
|
|
|
|
import com.liferay.portal.kernel.log.Log;
|
|
|
|
import com.liferay.portal.kernel.log.LogFactoryUtil;
|
|
|
|
import com.liferay.portal.kernel.log.LogFactoryUtil;
|
|
|
|
|
|
|
|
import com.liferay.portal.kernel.util.GetterUtil;
|
|
|
|
import com.liferay.portal.kernel.util.ReleaseInfo;
|
|
|
|
import com.liferay.portal.kernel.util.ReleaseInfo;
|
|
|
|
|
|
|
|
import com.liferay.portal.kernel.util.StringPool;
|
|
|
|
import com.liferay.portal.model.Group;
|
|
|
|
import com.liferay.portal.model.Group;
|
|
|
|
import com.liferay.portal.service.GroupLocalServiceUtil;
|
|
|
|
import com.liferay.portal.service.GroupLocalServiceUtil;
|
|
|
|
import com.liferay.portal.util.PortalUtil;
|
|
|
|
import com.liferay.portal.util.PortalUtil;
|
|
|
@ -45,60 +56,119 @@ import com.liferay.portal.util.PortalUtil;
|
|
|
|
@RequestMapping("VIEW")
|
|
|
|
@RequestMapping("VIEW")
|
|
|
|
public class PortletViewController {
|
|
|
|
public class PortletViewController {
|
|
|
|
private static Log _log = LogFactoryUtil.getLog(PortletViewController.class);
|
|
|
|
private static Log _log = LogFactoryUtil.getLog(PortletViewController.class);
|
|
|
|
private DataCatalogueFactory factory = DataCatalogueFactory.getFactory();
|
|
|
|
private static final String IMAGES_BASE_URL = "https://catalogue.d4science.org/";
|
|
|
|
private static final long K = 1000;
|
|
|
|
private static final long K = 1000;
|
|
|
|
private static final long M = K * K;
|
|
|
|
private static final long M = K * K;
|
|
|
|
private static final long G = M * K;
|
|
|
|
private static final long G = M * K;
|
|
|
|
|
|
|
|
private DataCatalogueFactory factory = DataCatalogueFactory.getFactory();
|
|
|
|
private static GroupManager gm = new LiferayGroupManager();
|
|
|
|
private static GroupManager gm = new LiferayGroupManager();
|
|
|
|
|
|
|
|
|
|
|
|
@RenderMapping
|
|
|
|
@RenderMapping
|
|
|
|
public String question(RenderRequest request,RenderResponse response, Model model) {
|
|
|
|
public String question(RenderRequest request,RenderResponse response, Model model) {
|
|
|
|
model.addAttribute("releaseInfo", ReleaseInfo.getReleaseInfo());
|
|
|
|
model.addAttribute("releaseInfo", ReleaseInfo.getReleaseInfo());
|
|
|
|
|
|
|
|
PortletPreferences prefs = request.getPreferences();
|
|
|
|
|
|
|
|
String catalogueBaseURL = GetterUtil.getString(prefs.getValue("catalogueURL", StringPool.BLANK));
|
|
|
|
|
|
|
|
HttpServletRequest httpServletRequest = PortalUtil.getHttpServletRequest(request);
|
|
|
|
|
|
|
|
long groupId = -1;
|
|
|
|
|
|
|
|
boolean isRootVO = false;
|
|
|
|
|
|
|
|
String catalogueURL = "";
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
HttpServletRequest httpServletRequest = PortalUtil.getHttpServletRequest(request);
|
|
|
|
groupId = PortalUtil.getScopeGroupId(request);
|
|
|
|
|
|
|
|
isRootVO = gm.isRootVO(groupId);
|
|
|
|
long groupId = PortalUtil.getScopeGroupId(request);
|
|
|
|
catalogueURL = getCatalougeFriendlyURL(GroupLocalServiceUtil.getGroup(groupId));
|
|
|
|
PortalContext pContext = PortalContext.getConfiguration();
|
|
|
|
} catch (Exception e1) {
|
|
|
|
String context = pContext.getCurrentScope(""+groupId);
|
|
|
|
e1.printStackTrace();
|
|
|
|
String catalogueURL = getCatalougeFriendlyURL(GroupLocalServiceUtil.getGroup(groupId));
|
|
|
|
}
|
|
|
|
DataCatalogueImpl utils = null;
|
|
|
|
model.addAttribute("isRootVO", isRootVO);
|
|
|
|
if (gm.isRootVO(groupId)) {
|
|
|
|
if (isRootVO) {
|
|
|
|
String gatewaySiteURL = pContext.getGatewayURL(httpServletRequest);
|
|
|
|
try {
|
|
|
|
if (!gatewaySiteURL.startsWith("https"))
|
|
|
|
List<CatalogueType> theTypes = parseTypes(catalogueBaseURL+"/type", catalogueURL);
|
|
|
|
gatewaySiteURL = gatewaySiteURL.replaceAll("http:", "https:");
|
|
|
|
Collections.sort(theTypes, Collections.reverseOrder());
|
|
|
|
String siteLandingPage = pContext.getSiteLandingPagePath(httpServletRequest);
|
|
|
|
List<CatalogueType> theFiveTypes = new ArrayList<>();
|
|
|
|
String clientURL = gatewaySiteURL+siteLandingPage;
|
|
|
|
for (int i = 0; i < 6; i++) {
|
|
|
|
try {
|
|
|
|
theFiveTypes.add(theTypes.get(i));
|
|
|
|
String appPerScopeURL = ApplicationProfileScopePerUrlReader.getScopePerUrl(clientURL);
|
|
|
|
|
|
|
|
_log.info("Catalogue for this Gateway is in this scope: " + appPerScopeURL);
|
|
|
|
|
|
|
|
utils = factory.getUtilsPerScope(appPerScopeURL);
|
|
|
|
|
|
|
|
_log.info("Here I instanciated factory.getUtilsPerScope with scope " + appPerScopeURL);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
_log.warn("Returning default catalogue for the context, could not find the catologue for this Gateway: " + clientURL);
|
|
|
|
|
|
|
|
utils = factory.getUtilsPerScope(context);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
model.addAttribute("catalogueTypes", theFiveTypes);
|
|
|
|
|
|
|
|
model.addAttribute("catalogueURL", catalogueURL);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
}
|
|
|
|
utils = factory.getUtilsPerScope(context);
|
|
|
|
else { //in VRE
|
|
|
|
_log.info("regular factory.getUtilsPerScope with context: " + context);
|
|
|
|
try {
|
|
|
|
|
|
|
|
PortalContext pContext = PortalContext.getConfiguration();
|
|
|
|
|
|
|
|
String context = pContext.getCurrentScope(""+groupId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DataCatalogueImpl utils = null;
|
|
|
|
|
|
|
|
if (gm.isRootVO(groupId)) {
|
|
|
|
|
|
|
|
String gatewaySiteURL = pContext.getGatewayURL(httpServletRequest);
|
|
|
|
|
|
|
|
if (!gatewaySiteURL.startsWith("https"))
|
|
|
|
|
|
|
|
gatewaySiteURL = gatewaySiteURL.replaceAll("http:", "https:");
|
|
|
|
|
|
|
|
String siteLandingPage = pContext.getSiteLandingPagePath(httpServletRequest);
|
|
|
|
|
|
|
|
String clientURL = gatewaySiteURL+siteLandingPage;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
String appPerScopeURL = ApplicationProfileScopePerUrlReader.getScopePerUrl(clientURL);
|
|
|
|
|
|
|
|
_log.info("Catalogue for this Gateway is in this scope: " + appPerScopeURL);
|
|
|
|
|
|
|
|
utils = factory.getUtilsPerScope(appPerScopeURL);
|
|
|
|
|
|
|
|
_log.info("Here I instanciated factory.getUtilsPerScope with scope " + appPerScopeURL);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
_log.warn("Returning default catalogue for the context, could not find the catologue for this Gateway: " + clientURL);
|
|
|
|
|
|
|
|
utils = factory.getUtilsPerScope(context);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
utils = factory.getUtilsPerScope(context);
|
|
|
|
|
|
|
|
_log.info("regular factory.getUtilsPerScope with context: " + context);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Statistics stats = utils.getStatistics();
|
|
|
|
|
|
|
|
_log.info("Got Statistics ... ");
|
|
|
|
|
|
|
|
model.addAttribute("itemsNo", convertToStringRepresentation(stats.getNumItems()));
|
|
|
|
|
|
|
|
model.addAttribute("groupsNo", stats.getNumGroups());
|
|
|
|
|
|
|
|
model.addAttribute("organisationsNo", stats.getNumOrganizations());
|
|
|
|
|
|
|
|
model.addAttribute("typesNo", stats.getNumTypes());
|
|
|
|
|
|
|
|
model.addAttribute("catalogueURL", catalogueURL);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Statistics stats = utils.getStatistics();
|
|
|
|
|
|
|
|
_log.info("Got Statistics ... ");
|
|
|
|
|
|
|
|
model.addAttribute("itemsNo", convertToStringRepresentation(stats.getNumItems()));
|
|
|
|
|
|
|
|
model.addAttribute("groupsNo", stats.getNumGroups());
|
|
|
|
|
|
|
|
model.addAttribute("organisationsNo", stats.getNumOrganizations());
|
|
|
|
|
|
|
|
model.addAttribute("typesNo", stats.getNumTypes());
|
|
|
|
|
|
|
|
model.addAttribute("catalogueURL", catalogueURL);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return "Catalogue-badge-portlet/view";
|
|
|
|
return "Catalogue-badge-portlet/view";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private List<CatalogueType> parseTypes(String url, String catalogueURL) throws Exception {
|
|
|
|
|
|
|
|
List<CatalogueType> toReturn = new ArrayList<>();
|
|
|
|
|
|
|
|
Document doc = Jsoup.connect(url)
|
|
|
|
|
|
|
|
.userAgent("Mozilla")
|
|
|
|
|
|
|
|
.timeout(5000)
|
|
|
|
|
|
|
|
.referrer("http://www.d4science.org")
|
|
|
|
|
|
|
|
.header("headersecurity", "xyz123")
|
|
|
|
|
|
|
|
.get();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Elements types = doc.select("article li");
|
|
|
|
|
|
|
|
for (Element type : types) {
|
|
|
|
|
|
|
|
Element imgElement = type.select("div img").first();
|
|
|
|
|
|
|
|
String imgUrl = imgElement.attr("src");
|
|
|
|
|
|
|
|
Element aElement = type.select("h3 a").first();
|
|
|
|
|
|
|
|
Element h3Element = type.select("h3").first();
|
|
|
|
|
|
|
|
//String href = aElement.attr("href");
|
|
|
|
|
|
|
|
String name = aElement.text();
|
|
|
|
|
|
|
|
String nameAndOccurrence = h3Element.text();
|
|
|
|
|
|
|
|
int occurrence = 0;
|
|
|
|
|
|
|
|
String query = "systemtype="+name;
|
|
|
|
|
|
|
|
String encodedQuery = Base64.getEncoder().encodeToString(query.getBytes());
|
|
|
|
|
|
|
|
String hrefPortlet = catalogueURL+"?path=dataset&query="+encodedQuery;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
int bracketOpen = nameAndOccurrence.indexOf("(");
|
|
|
|
|
|
|
|
int brackeClosed = nameAndOccurrence.indexOf(")");
|
|
|
|
|
|
|
|
String numberToParse = nameAndOccurrence.substring(bracketOpen+1, brackeClosed);
|
|
|
|
|
|
|
|
occurrence = Integer.parseInt(numberToParse);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
_log.error("Could not find occurrence in string "+ nameAndOccurrence);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
toReturn.add(new CatalogueType(name, IMAGES_BASE_URL+imgUrl, hrefPortlet, occurrence));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return toReturn;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String convertToStringRepresentation(final long value){
|
|
|
|
public static String convertToStringRepresentation(final long value){
|
|
|
|
final long[] dividers = new long[] { G, M, K, 1 };
|
|
|
|
final long[] dividers = new long[] { G, M, K, 1 };
|
|
|
|
final String[] units = new String[] {"Giga", "M", "K", ""};
|
|
|
|
final String[] units = new String[] {"Giga", "M", "K", ""};
|
|
|
@ -136,4 +206,5 @@ public class PortletViewController {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|