From c05c77ca85500c7aff1fa6455e80ccbbed142787 Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Mon, 20 Feb 2017 11:21:52 +0000 Subject: [PATCH] The set of information that could be shown now is extensible. A Generic Resource named GRSFManageEntries is looked up to check if extras info need to be shown git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/widgets/grsf-manage-widget@142757 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../client/view/FormEntryModel.java | 37 ++++++++ .../client/view/FormEntryModel.ui.xml | 15 +++ .../client/view/ManageProductWidget.java | 44 +++++++++ .../manage/GRSFNotificationService.java | 24 ++++- .../manage/GenericResourceReaderExtras.java | 95 +++++++++++++++++++ .../shared/ManageProductBean.java | 19 +++- .../grsf_manage_widget/client/TestClass.java | 15 +++ 7 files changed, 242 insertions(+), 7 deletions(-) create mode 100644 src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/FormEntryModel.java create mode 100644 src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/FormEntryModel.ui.xml create mode 100644 src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/GenericResourceReaderExtras.java create mode 100644 src/test/java/org/gcube/datacatalogue/grsf_manage_widget/client/TestClass.java diff --git a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/FormEntryModel.java b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/FormEntryModel.java new file mode 100644 index 0000000..44aa1a7 --- /dev/null +++ b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/FormEntryModel.java @@ -0,0 +1,37 @@ +package org.gcube.datacatalogue.grsf_manage_widget.client.view; + +import com.github.gwtbootstrap.client.ui.ControlLabel; +import com.github.gwtbootstrap.client.ui.TextBox; +import com.google.gwt.core.client.GWT; +import com.google.gwt.uibinder.client.UiBinder; +import com.google.gwt.uibinder.client.UiField; +import com.google.gwt.user.client.ui.Composite; +import com.google.gwt.user.client.ui.HTML; +import com.google.gwt.user.client.ui.Widget; + + +/** + * A dynamic ControlGroup to add to a form + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) + */ +public class FormEntryModel extends Composite{ + + private static FormEntryModelUiBinder uiBinder = GWT + .create(FormEntryModelUiBinder.class); + + interface FormEntryModelUiBinder extends UiBinder { + } + + @UiField + ControlLabel labelEntry; + + @UiField + TextBox entryValue; + + public FormEntryModel(String label, String value) { + initWidget(uiBinder.createAndBindUi(this)); + labelEntry.add(new HTML("" + label + "")); + entryValue.setText(value); + } + +} diff --git a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/FormEntryModel.ui.xml b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/FormEntryModel.ui.xml new file mode 100644 index 0000000..8a2ad0c --- /dev/null +++ b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/FormEntryModel.ui.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/ManageProductWidget.java b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/ManageProductWidget.java index 5616761..cac2dcd 100644 --- a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/ManageProductWidget.java +++ b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/ManageProductWidget.java @@ -2,7 +2,10 @@ package org.gcube.datacatalogue.grsf_manage_widget.client.view; import java.util.ArrayList; import java.util.Arrays; +import java.util.Iterator; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import org.gcube.datacatalogue.grsf_manage_widget.client.GRSFManageWidgetService; import org.gcube.datacatalogue.grsf_manage_widget.client.GRSFManageWidgetServiceAsync; @@ -88,6 +91,12 @@ public class ManageProductWidget extends Composite{ @UiField ControlGroup listBoxStatusGroup; + + @UiField + ControlGroup annotationAreaGroup; + + @UiField + ControlGroup productTypeGroup; @UiField Form formUpdate; @@ -162,6 +171,10 @@ public class ManageProductWidget extends Composite{ productShortTitle.setText(bean.getShortTitle()); productSource.setText(bean.getSource()); + // check if we need to show more + if(bean.getExtrasIfAvailable() != null && !bean.getExtrasIfAvailable().isEmpty()) + addExtrasAfter(bean, productTypeGroup); + List statusToShow = new ArrayList(STATUS); statusToShow.remove(bean.getCurrentStatus()); listBoxStatus.addItem("Select the new status"); @@ -212,6 +225,29 @@ public class ManageProductWidget extends Composite{ }); } + + /** + * Add extras if available after controlGroupBefore + * @param bean + * @param productTypeGroup + */ + private void addExtrasAfter(ManageProductBean bean, + ControlGroup controlGroupBefore) { + + int index = formUpdate.getWidgetIndex(controlGroupBefore); + + Map extras = bean.getExtrasIfAvailable(); + Iterator> iterator = extras.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry entry = (Map.Entry) iterator + .next(); + + formUpdate.insert(new FormEntryModel(entry.getKey(), entry.getValue()), index); + index++; + + } + + } @UiHandler("cancelButton") void onCancelButton(ClickEvent ce){ @@ -230,6 +266,14 @@ public class ManageProductWidget extends Composite{ listBoxStatusGroup.setType(ControlGroupType.ERROR); return; } + + annotationAreaGroup.setType(ControlGroupType.NONE); + + if(annotationArea.getText() == null || annotationArea.getText().isEmpty()){ + annotationArea.setPlaceholder("An annotation message to send along the update (mandatory)"); + annotationAreaGroup.setType(ControlGroupType.ERROR); + return; + } manageProductModal.setCloseVisible(false); cancelButton.setEnabled(false); diff --git a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/GRSFNotificationService.java b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/GRSFNotificationService.java index 796810c..293fb18 100644 --- a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/GRSFNotificationService.java +++ b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/GRSFNotificationService.java @@ -5,9 +5,11 @@ import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; @@ -154,10 +156,12 @@ public class GRSFNotificationService extends RemoteServiceServlet implements GRS throw new Exception("Unable to retrieve information for the selected item, sorry"); else{ + toReturn = new ManageProductBean(); + // check it is a grsf item, else.. List groups = product.getGroups(); boolean isGrsf = false; - + for (CkanGroup ckanGroup : groups) { if(ckanGroup.getName().contains("grsf")){ isGrsf = true; @@ -178,12 +182,24 @@ public class GRSFNotificationService extends RemoteServiceServlet implements GRS String shortTitle = extras.get(SHORT_TITLE_FIELD_KEY); String sources = extras.get(SOURCES_TITLE_FIELD_KEY); String title = product.getTitle(); + + // fetch extras + GenericResourceReaderExtras entries = new GenericResourceReaderExtras(); + Set extrasToShow = entries.getLookedUpExtrasKeys(); + if(extrasToShow != null && !extrasToShow.isEmpty()){ + Map extrasKeyValuePair = new HashMap(); + for (String entryKey : extrasToShow) { + String value = extras.get(entryKey); + if(value != null && !value.isEmpty()) + extrasKeyValuePair.put(entryKey, value); + } + toReturn.setExtrasIfAvailable(extrasKeyValuePair); + } - if(status == null || uuidKB == null || productGRSFType == null) + if(status == null || uuidKB == null) throw new Exception("Some information is missing in this record: Status = " + status + ", knowledge_base_uuid = " + uuidKB + ", and grsf type is = " + productType); - - toReturn = new ManageProductBean(); + toReturn.setCatalogueIdentifier(productIdentifier); toReturn.setCurrentStatus(GRSFStatus.fromString(status)); toReturn.setKnowledgeBaseIdentifier(uuidKB); diff --git a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/GenericResourceReaderExtras.java b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/GenericResourceReaderExtras.java new file mode 100644 index 0000000..e7a00db --- /dev/null +++ b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/GenericResourceReaderExtras.java @@ -0,0 +1,95 @@ +package org.gcube.datacatalogue.grsf_manage_widget.server.manage; + +import static org.gcube.resources.discovery.icclient.ICFactory.client; + +import java.io.StringReader; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.gcube.common.resources.gcore.utils.XPathHelper; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.datacatalogue.ckanutillibrary.server.exceptions.ApplicationProfileNotFoundException; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.Query; +import org.gcube.resources.discovery.client.queries.impl.QueryBox; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Node; +import org.xml.sax.InputSource; + +/** + * Look up from the IS other information that the widget should lookup + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) + */ +public class GenericResourceReaderExtras { + + private Set lookedUpExtrasKeys = new HashSet(); + + private static final String GENERIC_RESOURCE_NAME = "GRSFManageEntries"; + private static final String GENERIC_RESOURCE_SECONDARY_TYPE = "ApplicationProfile"; + + private static final Logger logger = LoggerFactory.getLogger(GenericResourceReaderExtras.class); + + // generic resource properties (a list of information) + public GenericResourceReaderExtras(){ + + String scope = ScopeProvider.instance.get(); + logger.debug("Trying to fetch applicationProfile profile from the infrastructure for " + GENERIC_RESOURCE_NAME + " scope: " + scope); + + try { + Query q = new QueryBox("for $profile in collection('/db/Profiles/GenericResource')//Resource " + + "where $profile/Profile/SecondaryType/string() eq '"+ GENERIC_RESOURCE_SECONDARY_TYPE + "' and $profile/Profile/Name/string() " + + " eq '" + GENERIC_RESOURCE_NAME + "'" + + "return $profile"); + + DiscoveryClient client = client(); + List appProfile = client.submit(q); + + if (appProfile == null || appProfile.size() == 0) + throw new ApplicationProfileNotFoundException("Your applicationProfile is not registered in the infrastructure"); + else { + + String elem = appProfile.get(0); + DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Node node = docBuilder.parse(new InputSource(new StringReader(elem))).getDocumentElement(); + XPathHelper helper = new XPathHelper(node); + + List currValue = null; + currValue = helper.evaluate("/Resource/Profile/Body/text()"); + + if (currValue != null && currValue.size() > 0) { + String body = currValue.get(0); + String[] splittedSet = body.split(","); + if(splittedSet != null && splittedSet.length > 0) + for (String entry : splittedSet) { + String trimmed = entry.trim(); + if(trimmed.isEmpty()) + continue; + lookedUpExtrasKeys.add(trimmed); + } + } + } + + logger.info("Extras entries are " + lookedUpExtrasKeys); + + } catch (Exception e) { + logger.error("Error while trying to fetch applicationProfile profile from the infrastructure", e); + } + + } + + public Set getLookedUpExtrasKeys() { + return lookedUpExtrasKeys; + } + + @Override + public String toString() { + return "GenericResourceReaderExtras [lookedUpExtrasKeys=" + + lookedUpExtrasKeys + "]"; + } + +} diff --git a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/shared/ManageProductBean.java b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/shared/ManageProductBean.java index 301a78f..9e3937b 100644 --- a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/shared/ManageProductBean.java +++ b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/shared/ManageProductBean.java @@ -1,6 +1,7 @@ package org.gcube.datacatalogue.grsf_manage_widget.shared; import java.io.Serializable; +import java.util.Map; /** * The bean to be managed by some people (e.g., GRSF). @@ -17,6 +18,7 @@ public class ManageProductBean implements Serializable{ private String type; // Fishery or Stock type (e.g., Assessment_Unit, Marine Resource and so on) private String grsfType; // fishery/stock private String source; // the current source + private Map extrasIfAvailable; // info that could change private GRSFStatus currentStatus; @@ -36,6 +38,7 @@ public class ManageProductBean implements Serializable{ * @param type * @param grsfType * @param source + * @param extrasIfAvailable * @param currentStatus * @param newStatus * @param annotation @@ -43,8 +46,8 @@ public class ManageProductBean implements Serializable{ public ManageProductBean(String itemTitle, String semanticId, String shortTitle, String catalogueIdentifier, String knowledgeBaseIdentifier, String type, String grsfType, - String source, GRSFStatus currentStatus, GRSFStatus newStatus, - String annotation) { + String source, Map extrasIfAvailable, + GRSFStatus currentStatus, GRSFStatus newStatus, String annotation) { super(); this.itemTitle = itemTitle; this.semanticId = semanticId; @@ -54,6 +57,7 @@ public class ManageProductBean implements Serializable{ this.type = type; this.grsfType = grsfType; this.source = source; + this.extrasIfAvailable = extrasIfAvailable; this.currentStatus = currentStatus; this.newStatus = newStatus; this.annotation = annotation; @@ -146,6 +150,14 @@ public class ManageProductBean implements Serializable{ this.shortTitle = shortTitle; } + public Map getExtrasIfAvailable() { + return extrasIfAvailable; + } + + public void setExtrasIfAvailable(Map extrasIfAvailable) { + this.extrasIfAvailable = extrasIfAvailable; + } + @Override public String toString() { return "ManageProductBean [itemTitle=" + itemTitle + ", semanticId=" @@ -153,7 +165,8 @@ public class ManageProductBean implements Serializable{ + ", catalogueIdentifier=" + catalogueIdentifier + ", knowledgeBaseIdentifier=" + knowledgeBaseIdentifier + ", type=" + type + ", grsfType=" + grsfType + ", source=" - + source + ", currentStatus=" + currentStatus + ", newStatus=" + + source + ", extrasIfAvailable=" + extrasIfAvailable + + ", currentStatus=" + currentStatus + ", newStatus=" + newStatus + ", annotation=" + annotation + "]"; } diff --git a/src/test/java/org/gcube/datacatalogue/grsf_manage_widget/client/TestClass.java b/src/test/java/org/gcube/datacatalogue/grsf_manage_widget/client/TestClass.java new file mode 100644 index 0000000..30de29f --- /dev/null +++ b/src/test/java/org/gcube/datacatalogue/grsf_manage_widget/client/TestClass.java @@ -0,0 +1,15 @@ +package org.gcube.datacatalogue.grsf_manage_widget.client; + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.datacatalogue.grsf_manage_widget.server.manage.GenericResourceReaderExtras; + +public class TestClass { + + //@Test + public void test() { + ScopeProvider.instance.set("/gcube/devNext/NextNext"); + GenericResourceReaderExtras entries = new GenericResourceReaderExtras(); + System.out.println(entries.getLookedUpExtrasKeys()); + } + +}