From 6d75e2931f09bbf7066d87e49c92a3aea946af13 Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Sat, 1 Apr 2017 10:11:21 +0000 Subject: [PATCH] almost finished: implemented client side and improved server side code git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/widgets/catalogue-sharing-widget@146508 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 5 +- pom.xml | 11 +++ .../client/ShareCatalogueWidget.java | 89 ++++++++++++----- .../client/ShareCatalogueWidget.ui.xml | 49 +++++++++- .../server/ServerUtils.java | 95 +++++++++++++++++++ .../server/ShareServicesImpl.java | 91 +++--------------- .../shared/ItemUrls.java | 27 +++++- .../ShareCatalogue.gwt.xml | 29 +++--- 8 files changed, 273 insertions(+), 123 deletions(-) create mode 100644 src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/server/ServerUtils.java diff --git a/.classpath b/.classpath index e1a1961..a708ee4 100644 --- a/.classpath +++ b/.classpath @@ -1,6 +1,6 @@ - + @@ -44,10 +44,11 @@ - + + diff --git a/pom.xml b/pom.xml index 8665042..fc9d320 100644 --- a/pom.xml +++ b/pom.xml @@ -60,6 +60,12 @@ gwt-user provided + + org.gcube.dvos + usermanagement-core + [2.0.0-SNAPSHOT,) + provided + org.gcube.core common-scope-maps @@ -87,6 +93,11 @@ [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT) compile + + com.github.gwtbootstrap + gwt-bootstrap + 2.3.2.0 + org.gcube.common.portal portal-manager diff --git a/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/client/ShareCatalogueWidget.java b/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/client/ShareCatalogueWidget.java index bd1c5ee..5b18bf0 100644 --- a/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/client/ShareCatalogueWidget.java +++ b/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/client/ShareCatalogueWidget.java @@ -1,48 +1,93 @@ package org.gcube.portlets_widgets.catalogue_sharing_widget.client; +import org.gcube.portlets_widgets.catalogue_sharing_widget.shared.ItemUrls; + +import com.github.gwtbootstrap.client.ui.AlertBlock; +import com.github.gwtbootstrap.client.ui.Form; +import com.github.gwtbootstrap.client.ui.Well; +import com.github.gwtbootstrap.client.ui.base.TextBox; import com.google.gwt.core.client.GWT; -import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.uibinder.client.UiBinder; import com.google.gwt.uibinder.client.UiField; -import com.google.gwt.uibinder.client.UiHandler; -import com.google.gwt.user.client.Window; -import com.google.gwt.user.client.ui.Button; +import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.ui.Composite; -import com.google.gwt.user.client.ui.HasText; import com.google.gwt.user.client.ui.Widget; -public class ShareCatalogueWidget extends Composite implements HasText { +public class ShareCatalogueWidget extends Composite { private static ShareCatalogueWidgetUiBinder uiBinder = GWT .create(ShareCatalogueWidgetUiBinder.class); interface ShareCatalogueWidgetUiBinder extends - UiBinder { + UiBinder { } - public ShareCatalogueWidget() { - initWidget(uiBinder.createAndBindUi(this)); - } + public static final ShareServicesAsync ckanServices = GWT.create(ShareServices.class); + + public static final String SERVICE_UNAVAILABLE = "The service is currently unavailable, retry later"; @UiField - Button button; + Well loadingIcon; - public ShareCatalogueWidget(String firstName) { + @UiField + AlertBlock errorBlock; + + @UiField + Form formWithInformation; + + @UiField + TextBox itemShortUrl; + + @UiField + TextBox itemTitle; + + @UiField + TextBox itemName; + + @UiField + TextBox itemLongUrl; + + + public ShareCatalogueWidget(String itemUUID) { initWidget(uiBinder.createAndBindUi(this)); - button.setText(firstName); + + ckanServices.getPackageUrl(itemUUID, new AsyncCallback() { + + @Override + public void onSuccess(ItemUrls result) { + + if(result == null){ + + onError(null); + + }else{ + + loadingIcon.setVisible(false); + formWithInformation.setVisible(true); + itemShortUrl.setText(result.getShortUrl() == null ? "" : result.getShortUrl()); + itemLongUrl.setText(result.getUrl() == null ? "" : result.getUrl()); + itemTitle.setText(result.getProductTitle() == null ? "" : result.getProductTitle()); + itemName.setText(result.getProductName() == null ? "" : result.getProductName()); + } + + } + + @Override + public void onFailure(Throwable caught) { + + onError(caught.getMessage()); + + } + }); } - @UiHandler("button") - void onClick(ClickEvent e) { - Window.alert("Hello!"); - } + private void onError(String msg) { - public void setText(String text) { - button.setText(text); - } + loadingIcon.setVisible(false); + formWithInformation.setVisible(false); + errorBlock.setText(SERVICE_UNAVAILABLE + (msg == null ? "" : "(" + msg + ")")); + errorBlock.setVisible(true); - public String getText() { - return button.getText(); } } diff --git a/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/client/ShareCatalogueWidget.ui.xml b/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/client/ShareCatalogueWidget.ui.xml index ea1495f..3a463f6 100644 --- a/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/client/ShareCatalogueWidget.ui.xml +++ b/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/client/ShareCatalogueWidget.ui.xml @@ -1,10 +1,53 @@ + xmlns:g="urn:import:com.google.gwt.user.client.ui" xmlns:b="urn:import:com.github.gwtbootstrap.client.ui"> + +
+ + + + - - + + + + Item's title: + + + + + + Item's name: + + + + + + Item's url: + + + + + + Item's short url: + + + + + + +
+
\ No newline at end of file diff --git a/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/server/ServerUtils.java b/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/server/ServerUtils.java new file mode 100644 index 0000000..f02924e --- /dev/null +++ b/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/server/ServerUtils.java @@ -0,0 +1,95 @@ +package org.gcube.portlets_widgets.catalogue_sharing_widget.server; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.datacatalogue.ckanutillibrary.server.ApplicationProfileScopePerUrlReader; + +import com.liferay.portal.kernel.log.Log; +import com.liferay.portal.kernel.log.LogFactoryUtil; + +public class ServerUtils { + + private static final Log logger = LogFactoryUtil.getLog(ServerUtils.class); + public static final String GCUBE_REQUEST_URL = "gcube-request-url"; + + /** + * Get the scope in which ckan information needs to be discovered from the url + * @param httpServletRequest + * @return + */ + public static String getScopeFromClientUrl(HttpServletRequest httpServletRequest){ + + if(httpServletRequest == null) + throw new IllegalArgumentException("HttpServletRequest is null!"); + + String scopeToReturn = null; + try{ + String clientUrl = getCurrentClientUrl(httpServletRequest).split("\\?")[0]; + logger.debug("Client url is " + clientUrl); + + // check if this information is in session, otherwise set it and return + HttpSession session = httpServletRequest.getSession(); + + if((scopeToReturn = (String) session.getAttribute(clientUrl)) != null){ + logger.debug("Scope to return is " + scopeToReturn); + }else{ + // ask to the ckan library and set it + scopeToReturn = ApplicationProfileScopePerUrlReader.getScopePerUrl(clientUrl); + logger.debug("Scope to return is " + scopeToReturn); + session.setAttribute(clientUrl, scopeToReturn); + } + }catch(Exception e){ + scopeToReturn = getCurrentContext(httpServletRequest, false); + logger.warn("Failed to determine the scope from the client url, returning the current one: " + scopeToReturn); + } + return scopeToReturn; + } + + /** + * Retrieve the current scope by using the portal manager + * @param b + * @return a GcubeUser object + */ + public static String getCurrentContext(HttpServletRequest request, boolean setInThread){ + + if(request == null) + throw new IllegalArgumentException("HttpServletRequest is null!"); + + PortalContext pContext = PortalContext.getConfiguration(); + String context = pContext.getCurrentScope(request); + logger.debug("Returning context " + context); + + if(context != null && setInThread) + ScopeProvider.instance.set(context); + + return context; + } + + /** + * Needed to get the url of the client + * @param httpServletRequest the httpServletRequest object + * @return the instance of the user + * @see the url at client side + */ + public static String getCurrentClientUrl(HttpServletRequest httpServletRequest) { + if(httpServletRequest == null) + throw new IllegalArgumentException("HttpServletRequest is null!"); + + return httpServletRequest.getHeader(GCUBE_REQUEST_URL); + } + + /** + * Needed to get the url of the client + * @param httpServletRequest the httpServletRequest object + * @return the instance of the user + * @see the url at client side + */ + public static String getUserInSession(HttpServletRequest httpServletRequest) { + + return PortalContext.getConfiguration().getCurrentUser(httpServletRequest).getUsername(); + + } + +} diff --git a/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/server/ShareServicesImpl.java b/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/server/ShareServicesImpl.java index e8d9fb8..58128d4 100644 --- a/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/server/ShareServicesImpl.java +++ b/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/server/ShareServicesImpl.java @@ -1,11 +1,5 @@ package org.gcube.portlets_widgets.catalogue_sharing_widget.server; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; - -import org.gcube.common.portal.PortalContext; -import org.gcube.common.scope.api.ScopeProvider; -import org.gcube.datacatalogue.ckanutillibrary.server.ApplicationProfileScopePerUrlReader; import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue; import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogueFactory; import org.gcube.portlets.user.urlshortener.UrlShortener; @@ -16,11 +10,12 @@ import com.google.gwt.user.server.rpc.RemoteServiceServlet; import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; +import eu.trentorise.opendata.jackan.model.CkanDataset; + public class ShareServicesImpl extends RemoteServiceServlet implements ShareServices{ private static final long serialVersionUID = -2060855544534802987L; private static final Log logger = LogFactoryUtil.getLog(ShareServicesImpl.class); - public static final String GCUBE_REQUEST_URL = "gcube-request-url"; /** * Retrieve an instance of the library for the scope @@ -30,7 +25,7 @@ public class ShareServicesImpl extends RemoteServiceServlet implements ShareServ */ public DataCatalogue getCatalogue(String scope) throws Exception{ - String scopeInWhichDiscover = (scope != null && !scope.isEmpty()) ? scope : getCurrentContext(getThreadLocalRequest(), false); + String scopeInWhichDiscover = (scope != null && !scope.isEmpty()) ? scope : ServerUtils.getCurrentContext(getThreadLocalRequest(), false); logger.debug("Discovering ckan instance into scope " + scopeInWhichDiscover); return DataCatalogueFactory.getFactory().getUtilsPerScope(scopeInWhichDiscover); @@ -39,83 +34,23 @@ public class ShareServicesImpl extends RemoteServiceServlet implements ShareServ @Override public ItemUrls getPackageUrl(String uuid) throws Exception{ - String scopePerCurrentUrl = getScopeFromClientUrl(getThreadLocalRequest()); + String scopePerCurrentUrl = ServerUtils.getScopeFromClientUrl(getThreadLocalRequest()); DataCatalogue catalogue = getCatalogue(scopePerCurrentUrl); + CkanDataset dataset = catalogue.getDataset(uuid, catalogue.getApiKeyFromUsername(ServerUtils.getUserInSession(getThreadLocalRequest()))); String longUrl = catalogue.getUnencryptedUrlFromDatasetIdOrName(uuid); + + if(longUrl == null || longUrl.isEmpty()) + throw new Exception("There was a problem while retrieving the item's url, retry later"); + String shortUrl = null; - + UrlShortener shortener = new UrlShortener(); if(shortener!=null && shortener.isAvailable()) shortUrl = shortener.shorten(longUrl); - - return new ItemUrls(shortUrl, longUrl, uuid); - } + else + logger.warn("Short url not available"); - - /** - * Get the scope in which ckan information needs to be discovered from the url - * @param httpServletRequest - * @return - */ - public static String getScopeFromClientUrl(HttpServletRequest httpServletRequest){ - - if(httpServletRequest == null) - throw new IllegalArgumentException("HttpServletRequest is null!"); - - String scopeToReturn = null; - try{ - String clientUrl = getCurrentClientUrl(httpServletRequest).split("\\?")[0]; - logger.debug("Client url is " + clientUrl); - - // check if this information is in session, otherwise set it and return - HttpSession session = httpServletRequest.getSession(); - - if((scopeToReturn = (String) session.getAttribute(clientUrl)) != null){ - logger.debug("Scope to return is " + scopeToReturn); - }else{ - // ask to the ckan library and set it - scopeToReturn = ApplicationProfileScopePerUrlReader.getScopePerUrl(clientUrl); - logger.debug("Scope to return is " + scopeToReturn); - session.setAttribute(clientUrl, scopeToReturn); - } - }catch(Exception e){ - scopeToReturn = getCurrentContext(httpServletRequest, false); - logger.warn("Failed to determine the scope from the client url, returning the current one: " + scopeToReturn); - } - return scopeToReturn; - } - - /** - * Retrieve the current scope by using the portal manager - * @param b - * @return a GcubeUser object - */ - public static String getCurrentContext(HttpServletRequest request, boolean setInThread){ - - if(request == null) - throw new IllegalArgumentException("HttpServletRequest is null!"); - - PortalContext pContext = PortalContext.getConfiguration(); - String context = pContext.getCurrentScope(request); - logger.debug("Returning context " + context); - - if(context != null && setInThread) - ScopeProvider.instance.set(context); - - return context; - } - - /** - * Needed to get the url of the client - * @param httpServletRequest the httpServletRequest object - * @return the instance of the user - * @see the url at client side - */ - public static String getCurrentClientUrl(HttpServletRequest httpServletRequest) { - if(httpServletRequest == null) - throw new IllegalArgumentException("HttpServletRequest is null!"); - - return httpServletRequest.getHeader(GCUBE_REQUEST_URL); + return new ItemUrls(shortUrl, longUrl, uuid, dataset.getName(), dataset.getTitle()); } } diff --git a/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/shared/ItemUrls.java b/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/shared/ItemUrls.java index 35536f0..118e747 100644 --- a/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/shared/ItemUrls.java +++ b/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/shared/ItemUrls.java @@ -13,8 +13,8 @@ public class ItemUrls implements Serializable { private String shortUrl; private String url; private String catalogueUUID; - - + private String productName; + private String productTitle; /** * @@ -29,11 +29,13 @@ public class ItemUrls implements Serializable { * @param url * @param catalogueUUID */ - public ItemUrls(String shortUrl, String url, String catalogueUUID) { + public ItemUrls(String shortUrl, String url, String catalogueUUID, String productName, String productTitle) { super(); this.shortUrl = shortUrl; this.url = url; this.catalogueUUID = catalogueUUID; + this.productName = productName; + this.productTitle = productTitle; } public String getShortUrl() { @@ -60,9 +62,26 @@ public class ItemUrls implements Serializable { this.catalogueUUID = catalogueUUID; } + public String getProductName() { + return productName; + } + + public void setProductName(String productName) { + this.productName = productName; + } + + public String getProductTitle() { + return productTitle; + } + + public void setProductTitle(String productTitle) { + this.productTitle = productTitle; + } + @Override public String toString() { return "ItemUrls [shortUrl=" + shortUrl + ", url=" + url - + ", catalogueUUID=" + catalogueUUID + "]"; + + ", catalogueUUID=" + catalogueUUID + ", productName=" + + productName + ", productTitle=" + productTitle + "]"; } } diff --git a/src/main/resources/org/gcube/portlets_widgets/catalogue_sharing_widget/ShareCatalogue.gwt.xml b/src/main/resources/org/gcube/portlets_widgets/catalogue_sharing_widget/ShareCatalogue.gwt.xml index 81dc423..839782f 100644 --- a/src/main/resources/org/gcube/portlets_widgets/catalogue_sharing_widget/ShareCatalogue.gwt.xml +++ b/src/main/resources/org/gcube/portlets_widgets/catalogue_sharing_widget/ShareCatalogue.gwt.xml @@ -1,22 +1,23 @@ - - + + - - - - - - + + - + + + - - + - - - + + + + + +