From 4e168d2c6feb053b39172fa905effd21412d9a25 Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Wed, 13 Dec 2017 17:34:45 +0000 Subject: [PATCH] almost finished git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/widgets/grsf-manage-widget@160475 82a268e6-3cf1-43bd-a215-b396298e98cf --- .settings/org.eclipse.wst.common.component | 6 + .../client/view/ManageProductWidget.java | 2 +- .../view/subwidgets/ConnectToWidget.java | 86 +++++++- .../subwidgets/SimilarGRSFRecordWidget.java | 46 ++-- .../client/view/subwidgets/SourceWidget.java | 7 +- .../manage/GRSFNotificationService.java | 11 +- .../server/manage/Utils.java | 197 ++++++------------ .../shared/ConnectedBean.java | 59 ++++-- .../shared/SimilarGRSFRecord.java | 4 + 9 files changed, 225 insertions(+), 193 deletions(-) diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index 38e18cb..0556e5b 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -4,6 +4,12 @@ + + uses + + + uses + 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 3e5f5d5..8633ac6 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 @@ -381,7 +381,7 @@ public class ManageProductWidget extends Composite{ cancelButton.setEnabled(true); confirmButton.setEnabled(true); loaderIcon.setVisible(false); - showInfo(STATUS_UPDATE_ERROR, AlertType.ERROR); + showInfo(STATUS_UPDATE_ERROR + ": " + caught.getMessage(), AlertType.ERROR); } }); diff --git a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/subwidgets/ConnectToWidget.java b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/subwidgets/ConnectToWidget.java index e67b2e7..df8f2f8 100644 --- a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/subwidgets/ConnectToWidget.java +++ b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/subwidgets/ConnectToWidget.java @@ -9,11 +9,13 @@ import org.gcube.datacatalogue.grsf_manage_widget.shared.ConnectedBean; import org.gcube.datacatalogue.grsf_manage_widget.shared.ManageProductBean; import com.github.gwtbootstrap.client.ui.Button; +import com.github.gwtbootstrap.client.ui.CheckBox; import com.github.gwtbootstrap.client.ui.Paragraph; import com.github.gwtbootstrap.client.ui.TextBox; import com.github.gwtbootstrap.client.ui.constants.IconType; import com.google.gwt.core.client.GWT; import com.google.gwt.dom.client.Style.Float; +import com.google.gwt.dom.client.Style.FontWeight; import com.google.gwt.dom.client.Style.Unit; import com.google.gwt.event.dom.client.ChangeEvent; import com.google.gwt.event.dom.client.ChangeHandler; @@ -23,6 +25,7 @@ import com.google.gwt.event.dom.client.KeyPressEvent; import com.google.gwt.event.dom.client.KeyPressHandler; import com.google.gwt.uibinder.client.UiBinder; import com.google.gwt.uibinder.client.UiField; +import com.google.gwt.user.client.ui.Anchor; import com.google.gwt.user.client.ui.Composite; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.HorizontalPanel; @@ -46,9 +49,23 @@ public class ConnectToWidget extends Composite{ //private GRSFManageWidgetServiceAsync service; private List connectList = new ArrayList(0); + private List connectedRecords = null; + public ConnectToWidget(final ManageProductBean bean/*, GRSFManageWidgetServiceAsync service*/) { initWidget(uiBinder.createAndBindUi(this)); + // get already connected beans, if any + connectedRecords = bean.getConnectTo(); + + if(connectedRecords != null){ + connectPanel.add(new HTML("
")); + for (ConnectedBean connected : connectedRecords) { + Widget widget = buildWidgetForConnectedRecords(connected); + connectPanel.add(widget); + connectPanel.add(new HTML("
")); + } + + } //this.service = service; String acceptedDomain = bean.getGrsfDomain().equalsIgnoreCase(Product_Type.STOCK.getOrigName()) ? Product_Type.FISHERY.getOrigName() : Product_Type.STOCK.getOrigName(); // inverted suggestRecord.setTitle("Connect this " + bean.getGrsfDomain() + " record to a " + acceptedDomain + " record "); @@ -62,6 +79,7 @@ public class ConnectToWidget extends Composite{ public void onClick(ClickEvent arg0) { ConnectedBean cb = new ConnectedBean(); + cb.setExtra(true); cb.setSourceDomain(bean.getGrsfDomain()); Tuple t = buildWidgetConnect(cb); connectList.add(t); @@ -72,6 +90,57 @@ public class ConnectToWidget extends Composite{ } + /** + * + * @param similarGRSFRecord + * @return + */ + public static Widget buildWidgetForConnectedRecords(final ConnectedBean connected){ + + HorizontalPanel hp = new HorizontalPanel(); + hp.setWidth("95%"); + VerticalPanel leftPanel = new VerticalPanel(); + leftPanel.setWidth("80%"); + leftPanel.getElement().getStyle().setMarginLeft(20, Unit.PX); + Paragraph name = new Paragraph("Record name: " + (connected.getDestShortName() != null? connected.getDestShortName() : "Unavailable")); + leftPanel.add(name); + Paragraph semanticIdentifier = new Paragraph("Semantic Identifier: " + + connected.getDestSemanticIdentifier()); + leftPanel.add(semanticIdentifier); + Paragraph identifier = new Paragraph("Record UUID: " + + connected.getDestKnowledgeBaseId()); + leftPanel.add(identifier); + + Anchor view = new Anchor(); + view.setHref(connected.getUrl()); + view.setText("View"); + view.setTitle("Click to view the similar record"); + view.setTarget("_blank"); + view.getElement().getStyle().setFontWeight(FontWeight.BOLD); + leftPanel.add(view); + + VerticalPanel rightPanel = new VerticalPanel(); + rightPanel.setWidth("20%"); + + final CheckBox removeExtra = new CheckBox("Remove"); + removeExtra.setTitle("Remove this record among the similar ones"); + removeExtra.addClickHandler(new ClickHandler() { + + @Override + public void onClick(ClickEvent arg0) { + connected.setToBeKept(!removeExtra.getValue()); + } + }); + + rightPanel.getElement().getStyle().setFloat(Float.RIGHT); + rightPanel.add(removeExtra); + hp.add(leftPanel); + hp.add(rightPanel); + hp.getElement().getStyle().setPadding(10, Unit.PX); + hp.getElement().getStyle().setMarginBottom(10, Unit.PX); + return hp; + } + /** * Builds up a widget for connecting records. * @param w the widget @@ -80,14 +149,14 @@ public class ConnectToWidget extends Composite{ private Tuple buildWidgetConnect(final ConnectedBean cb){ VerticalPanel main = new VerticalPanel(); - main.setWidth("90%"); + main.setWidth("95%"); HorizontalPanel hp = new HorizontalPanel(); hp.setWidth("100%"); VerticalPanel vpLeft = new VerticalPanel(); vpLeft.getElement().getStyle().setMarginLeft(15, Unit.PX); vpLeft.setWidth("80%"); - Paragraph semanticIdentifier = new Paragraph("Record Identifier (UUID):"); + Paragraph semanticIdentifier = new Paragraph("Identifier (UUID):"); final TextBox box = new TextBox(); box.setId("identifier-text"); box.addKeyPressHandler(new KeyPressHandler() { @@ -97,7 +166,7 @@ public class ConnectToWidget extends Composite{ String currentText = box.getText().trim(); GWT.log("Text changed to " + currentText); - cb.setDestIdentifier(currentText); + cb.setDestKnowledgeBaseId(currentText); } }); @@ -108,7 +177,7 @@ public class ConnectToWidget extends Composite{ String currentText = box.getText().trim(); GWT.log("Text changed to " + currentText); - cb.setDestIdentifier(currentText); + cb.setDestKnowledgeBaseId(currentText); } }); @@ -158,17 +227,18 @@ public class ConnectToWidget extends Composite{ */ public List getConnectList() { - List toReturn = new ArrayList(0); + if(connectedRecords == null) + connectedRecords = new ArrayList(0); for (Tuple p : connectList) { - String suggestedIdentifier = ((ConnectedBean)p.getO()).getDestIdentifier(); + String suggestedIdentifier = ((ConnectedBean)p.getO()).getDestKnowledgeBaseId(); if(suggestedIdentifier == null || suggestedIdentifier.isEmpty()) continue; else - toReturn.add((ConnectedBean) p.getO()); + connectedRecords.add((ConnectedBean) p.getO()); } - return toReturn; + return connectedRecords; } } diff --git a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/subwidgets/SimilarGRSFRecordWidget.java b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/subwidgets/SimilarGRSFRecordWidget.java index ab9a561..e0240fb 100644 --- a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/subwidgets/SimilarGRSFRecordWidget.java +++ b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/subwidgets/SimilarGRSFRecordWidget.java @@ -59,22 +59,27 @@ public class SimilarGRSFRecordWidget extends Composite { */ public SimilarGRSFRecordWidget(List availableGRSFSimilarRecords/*, GRSFManageWidgetServiceAsync service*/) { initWidget(uiBinder.createAndBindUi(this)); + //this.service = service; this.availableGRSFSimilarRecords = availableGRSFSimilarRecords; - addSimilarRecord.getElement().getStyle().setFloat(Float.RIGHT); - // add the existing ones, if any - for (final SimilarGRSFRecord similarGRSFRecord : availableGRSFSimilarRecords) { - Widget widget = buildWidgetForSimilarRecord(similarGRSFRecord); - similarGrsfRecordsPanel.add(widget); - HTML separator = new HTML("
"); - similarGrsfRecordsPanel.add(separator); + if(availableGRSFSimilarRecords != null){ + + similarGrsfRecordsPanel.add(new HTML("
")); + + // add the existing ones, if any + for (final SimilarGRSFRecord similarGRSFRecord : availableGRSFSimilarRecords) { + Widget widget = buildWidgetForSimilarRecord(similarGRSFRecord); + similarGrsfRecordsPanel.add(widget); + similarGrsfRecordsPanel.add(new HTML("
")); + } + } - // manage the "suggest button" addSimilarRecord.setIcon(IconType.PLUS_SIGN); addSimilarRecord.getElement().getStyle().setFloat(Float.RIGHT); addSimilarRecord.setTitle("Suggest a Similar Record by using its Identifier (UUID)"); + addSimilarRecord.getElement().getStyle().setFloat(Float.RIGHT); // add handler addSimilarRecord.addClickHandler(new ClickHandler() { @@ -101,10 +106,10 @@ public class SimilarGRSFRecordWidget extends Composite { public static Widget buildWidgetForSimilarRecord(final SimilarGRSFRecord similarGRSFRecord){ HorizontalPanel hp = new HorizontalPanel(); - hp.setWidth("90%"); + hp.setWidth("95%"); VerticalPanel leftPanel = new VerticalPanel(); leftPanel.setWidth("80%"); - leftPanel.getElement().getStyle().setMarginLeft(15, Unit.PX); + leftPanel.getElement().getStyle().setMarginLeft(20, Unit.PX); Paragraph name = new Paragraph("Record name: " + (similarGRSFRecord.getShortName() != null? similarGRSFRecord.getShortName() : "Unavailable")); leftPanel.add(name); if(similarGRSFRecord.getDescription() != null){ @@ -117,23 +122,24 @@ public class SimilarGRSFRecordWidget extends Composite { leftPanel.add(description); } Paragraph identifier = new Paragraph("Record UUID: " + - similarGRSFRecord.getIdentifier()); + similarGRSFRecord.getKnowledgeBaseId()); leftPanel.add(identifier); - VerticalPanel rightPanel = new VerticalPanel(); - rightPanel.setWidth("20%"); Anchor view = new Anchor(); view.setHref(similarGRSFRecord.getUrl()); view.setText("View"); view.setTitle("Click to view the similar record"); view.setTarget("_blank"); view.getElement().getStyle().setFontWeight(FontWeight.BOLD); - rightPanel.add(view); + leftPanel.add(view); + + VerticalPanel rightPanel = new VerticalPanel(); + rightPanel.setWidth("20%"); // add merge checkbox final CheckBox mergeSuggested = new CheckBox("Merge"); mergeSuggested.setTitle("Suggest to merge the current record with this similar record"); - mergeSuggested.getElement().getStyle().setPaddingTop(3, Unit.PC); + //mergeSuggested.getElement().getStyle().setPaddingTop(3, Unit.PC); mergeSuggested.setValue(false); mergeSuggested.addClickHandler(new ClickHandler() { @@ -153,7 +159,7 @@ public class SimilarGRSFRecordWidget extends Composite { @Override public void onClick(ClickEvent arg0) { - similarGRSFRecord.setTobeKept(!removeExtra.getValue()); + similarGRSFRecord.setToBeKept(!removeExtra.getValue()); } }); @@ -176,7 +182,7 @@ public class SimilarGRSFRecordWidget extends Composite { private Widget buildWidgetForExtraSimilarRecord(final SimilarGRSFRecord s){ VerticalPanel main = new VerticalPanel(); - main.setWidth("90%"); + main.setWidth("95%"); HorizontalPanel hp = new HorizontalPanel(); hp.setWidth("100%"); @@ -192,7 +198,7 @@ public class SimilarGRSFRecordWidget extends Composite { String currentText = box.getText().trim(); GWT.log("Text changed to " + currentText); - s.setIdentifier(currentText); + s.setKnowledgeBaseId(currentText); } }); @@ -203,7 +209,7 @@ public class SimilarGRSFRecordWidget extends Composite { String currentText = box.getText().trim(); GWT.log("Text changed to " + currentText); - s.setIdentifier(currentText); + s.setKnowledgeBaseId(currentText); } }); @@ -274,7 +280,7 @@ public class SimilarGRSFRecordWidget extends Composite { for (Tuple p : extraSimilarRecordsList) { SimilarGRSFRecord similarRecord = ((SimilarGRSFRecord)p.getO()); - if(similarRecord.isExtra() && similarRecord.getIdentifier() == null || similarRecord.getIdentifier().isEmpty()) + if(similarRecord.isExtra() && similarRecord.getKnowledgeBaseId() == null || similarRecord.getKnowledgeBaseId().isEmpty()) continue; availableGRSFSimilarRecords.add((SimilarGRSFRecord) p.getO()); } diff --git a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/subwidgets/SourceWidget.java b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/subwidgets/SourceWidget.java index eefda32..92da735 100644 --- a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/subwidgets/SourceWidget.java +++ b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/subwidgets/SourceWidget.java @@ -11,6 +11,7 @@ import com.google.gwt.uibinder.client.UiBinder; import com.google.gwt.uibinder.client.UiField; import com.google.gwt.user.client.ui.Anchor; import com.google.gwt.user.client.ui.Composite; +import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwt.user.client.ui.Widget; @@ -29,8 +30,9 @@ public class SourceWidget extends Composite { initWidget(uiBinder.createAndBindUi(this)); sourcesInnerPanel.getElement().getStyle().setMarginLeft(15, Unit.PX); + for (SourceRecord sourceRecord : availableSources) { - + VerticalPanel subPanel = new VerticalPanel(); subPanel.setWidth("90%"); String name = sourceRecord.getName(); @@ -41,10 +43,11 @@ public class SourceWidget extends Composite { url.setText(name); url.setTitle("Click to view the source record"); url.setTarget("_blank"); + subPanel.getElement().getStyle().setMarginTop(10, Unit.PX); subPanel.add(url); subPanel.getElement().getStyle().setMarginBottom(15, Unit.PX); sourcesInnerPanel.add(subPanel); - + } } 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 fe6fa8c..006551d 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 @@ -69,13 +69,14 @@ public class GRSFNotificationService extends RemoteServiceServlet implements GRS ManageProductBean toReturn = null; - + if(!Utils.isIntoPortal()){ toReturn = new ManageProductBean(); toReturn.setCatalogueIdentifier(UUID.randomUUID().toString()); List connectTo = new ArrayList<>(); - connectTo.add(new ConnectedBean("asfis:WRO+fao:67;FAO", "Stock", "aksldsam asd", "asdasjnk:fas", UUID.randomUUID().toString())); + connectTo.add(new ConnectedBean("91f1e413-dc9f-3b4e-b1c5-0e8560177253","Stock", + "aksldsam asd", "asdasjnk:fas", UUID.randomUUID().toString(), "http://data.d4science.org/ctlg/GRSF_Admin/91f1e413-dc9f-3b4e-b1c5-0e8560177253")); toReturn.setConnectTo(connectTo); toReturn.setGrsfDomain("Stock"); toReturn.setGrsfType("Assessment Unit"); @@ -88,6 +89,8 @@ public class GRSFNotificationService extends RemoteServiceServlet implements GRS toReturn.setSemanticIdentifier("asfis:WRO+fao:67;FAO"); ArrayList sources = new ArrayList(); sources.add(new SourceRecord("RAM", "http://www.google.it")); + sources.add(new SourceRecord("FIRMS", "http://www.google.it")); + sources.add(new SourceRecord("FishSource", "http://www.google.it")); toReturn.setSources(sources); List similarGrsfRecords = new ArrayList(); similarGrsfRecords.add(new SimilarGRSFRecord("same species overlapping water areas", @@ -169,7 +172,7 @@ public class GRSFNotificationService extends RemoteServiceServlet implements GRS List connectedBeans = new ArrayList(0); if(connectedBeansAsStrings != null){ for (String connectedBean : connectedBeansAsStrings) { - connectedBeans.add(Utils.connectedBeanRecordFromJson(connectedBean, uuidKB, grsfDomain)); + connectedBeans.add(Utils.connectedBeanRecordFromJson(connectedBean, uuidKB, grsfDomain, catalogue)); } } @@ -329,7 +332,7 @@ public class GRSFNotificationService extends RemoteServiceServlet implements GRS baseUrl = Utils.discoverEndPoint(context); getThreadLocalRequest().getSession().setAttribute(keyPerContext, baseUrl); } - return Utils.updateCatalogueRecord(baseUrl, bean, catalogue, Utils.getCurrentUser(getThreadLocalRequest()).getUsername()); + return Utils.updateRecord(baseUrl, bean, catalogue, Utils.getCurrentUser(getThreadLocalRequest()).getUsername()); }catch(Exception e){ logger.error("Unable to update the product.." + e.getMessage()); diff --git a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/Utils.java b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/Utils.java index b3b4230..71f6b4b 100644 --- a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/Utils.java +++ b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/Utils.java @@ -5,9 +5,7 @@ import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; import java.io.StringReader; -import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -38,7 +36,6 @@ import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.queries.api.Query; import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.gcube.resources.discovery.client.queries.impl.QueryBox; -import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager; import org.gcube.vomanagement.usermanagement.model.GCubeUser; import org.json.simple.JSONArray; import org.json.simple.JSONObject; @@ -59,9 +56,7 @@ import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.Closea import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.HttpClientBuilder; import eu.trentorise.opendata.jackan.internal.org.apache.http.util.EntityUtils; import eu.trentorise.opendata.jackan.model.CkanDataset; -import eu.trentorise.opendata.jackan.model.CkanGroup; import eu.trentorise.opendata.jackan.model.CkanPair; -import eu.trentorise.opendata.jackan.model.CkanTag; /** * Utility methods for GRSF Management panel widget. @@ -72,9 +67,7 @@ public class Utils { private static final String GENERIC_RESOURCE_NAME = "GRSFManageEntries"; private static final String GENERIC_RESOURCE_SECONDARY_TYPE = "ApplicationProfile"; private static final Logger logger = LoggerFactory.getLogger(Utils.class); - private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static final String REGEX_UUID = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"; - private static final int MAX_TRIAL = 5; /** * Look up from the IS other information that can be potentially displayed in read only mode in the management panel. @@ -265,7 +258,7 @@ public class Utils { * @return true on success, false otherwise */ @SuppressWarnings("unchecked") - public static String updateCatalogueRecord(String serviceUrl, ManageProductBean bean, DataCatalogue catalogue, String username){ + public static String updateRecord(String serviceUrl, ManageProductBean bean, DataCatalogue catalogue, String username) throws Exception{ if(serviceUrl == null) throw new IllegalArgumentException("GRSF Updater service url cannot be null"); @@ -278,13 +271,50 @@ public class Utils { JSONObject obj = new JSONObject(); obj.put(Constants.CATALOGUE_ID, bean.getCatalogueIdentifier()); obj.put(Constants.KB_ID, bean.getKnowledgeBaseIdentifier()); - obj.put(Constants.PRODUCT_TYPE, bean.getGrsfDomain().toLowerCase()); - obj.put(Constants.STATUS, bean.getNewStatus().toString().toLowerCase()); + obj.put(Constants.NEW_STATUS, bean.getNewStatus().toString().toLowerCase()); + obj.put(Constants.OLD_STATUS, bean.getCurrentStatus().toString().toLowerCase()); + obj.put(Constants.TRACEABILITY_FLAG, bean.isTraceabilityFlag()); String annotation = bean.getAnnotation(); if(annotation != null) obj.put(Constants.ANNOTATION, annotation.replaceAll("\"", "")); + obj.put(Constants.SHORT_NAME_OLD, bean.getShortName()); + + if(bean.getShortNameUpdated() == null || bean.getShortNameUpdated().isEmpty()) + bean.setShortNameUpdated(bean.getShortName()); + + obj.put(Constants.SHORT_NAME_NEW, bean.getShortNameUpdated()); + obj.put(Constants.OLD_STATUS, bean.getCurrentStatus().toString().toLowerCase()); + + // prepare connections + List connections = bean.getConnectTo(); + JSONArray connectionsJson = new JSONArray(); + + for(ConnectedBean c: connections){ + JSONObject cc = new JSONObject(); + cc.put(Constants.SOURCE_KNOWLEDGE_BASE_ID, c.getSourceKnowledgeBaseId()); + cc.put(Constants.DEST_KNOWLEDGE_BASE_ID, c.getDestKnowledgeBaseId()); + cc.put(Constants.SOURCE_DOMAIN, c.getSourceDomain()); + cc.put(Constants.SUGGESTED, c.isExtra()); + cc.put(Constants.TO_BE_KEPT, c.isToBeKept()); + connectionsJson.add(cc); + } + obj.put(Constants.CONNECTIONS, connectionsJson); + + // prepare similar grsf records + List similarRecords = bean.getSimilarGrsfRecords(); + JSONArray similarRecordsJson = new JSONArray(); + for(SimilarGRSFRecord s: similarRecords){ + JSONObject ss = new JSONObject(); + ss.put(Constants.KB_ID, s.getKnowledgeBaseId()); + ss.put(Constants.SUGGESTED, s.isExtra()); + ss.put(Constants.TO_BE_KEPT, s.isToBeKept()); + ss.put(Constants.MERGE, s.isSuggestedMerge()); + similarRecordsJson.add(ss); + } + obj.put(Constants.SIMILAR_GRSF_RECORDS, similarRecordsJson); + logger.debug("Update request looks like " + obj.toJSONString()); HttpPost request = new HttpPost(serviceUrl + Constants.SERVICE_POST_METHOD); @@ -300,130 +330,22 @@ public class Utils { JSONParser parser = new JSONParser(); JSONObject parsedJSON = (JSONObject)parser.parse(result); - if(response.getStatusLine().getStatusCode() != Constants.STATUS_SUCCESS) + if(response.getStatusLine().getStatusCode() != Constants.STATUS_SUCCESS){ + throw new Exception("Update failed at knowledge base side!"); + }else if(!(boolean) parsedJSON.get(Constants.UPDATE_RESULT)) throw new IllegalArgumentException( - "Error while performing the update request: " + response.getStatusLine().getReasonPhrase() + - "and error in the result bean is " + parsedJSON.get(Constants.ERROR)); + "Update failed for the following reason " + parsedJSON.get(Constants.ERROR_MESSAGE)); - // patch the catalogue product - return patchProduct(catalogue, bean, username); + // patch the catalogue product (no longer needed) + // return patchProduct(catalogue, bean, username); }catch(Exception e){ logger.error("Unable to update this Item " + e.getMessage()); - return e.getMessage(); + throw e; } - } + return null; - /** - * Patch the product - * @param catalogue - * @param bean - * @param username - */ - @SuppressWarnings("unchecked") - private static String patchProduct(DataCatalogue catalogue, - ManageProductBean bean, String username) { - - logger.info("Going to patch record in the catalogue with identifier " + bean.getCatalogueIdentifier() + - " from user " + username); - - String apiKey = catalogue.getApiKeyFromUsername(username); - CkanDataset dataset = catalogue.getDataset(bean.getCatalogueIdentifier(), apiKey); - String errorMessage = null; - - for (int i = 0; i < MAX_TRIAL; i++) { - - try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();){ - - JSONObject jsonRequest = new JSONObject(); - JSONArray tagsAsJson = new JSONArray(); - JSONArray groupsAsJson = new JSONArray(); - JSONArray customFieldsAsJson = new JSONArray(); - - // manage the custom fields - List extras = dataset.getExtras(); - for (CkanPair ckanPair : extras) { - if(ckanPair.getKey().equals(Constants.STATUS_OF_THE_GRSF_RECORD_CUSTOM_KEY) && ckanPair.getValue().equals(bean.getCurrentStatus().toString())) - continue; - - JSONObject obj = new JSONObject(); - obj.put("key", ckanPair.getKey()); - obj.put("value", ckanPair.getValue()); - customFieldsAsJson.add(obj); - } - - // add the new one and the annotation message - JSONObject newStatus = new JSONObject(); - newStatus.put("key", Constants.STATUS_OF_THE_GRSF_RECORD_CUSTOM_KEY); - newStatus.put("value", bean.getNewStatus().toString()); - customFieldsAsJson.add(newStatus); - - JSONObject newAnnotation = new JSONObject(); - newAnnotation.put("key", Constants.ANNOTATION_CUSTOM_KEY); - newAnnotation.put("value", "date: " + DATE_FORMAT.format(new Date()) - + ", admin: " + new LiferayUserManager().getUserByUsername(username).getFullname() - + ", message: " + (bean.getAnnotation() != null ? bean.getAnnotation().replaceAll("\"", "") : "none") - + ", old status: " + bean.getCurrentStatus().toString() - + ", new status: " + bean.getNewStatus().toString() - ); - customFieldsAsJson.add(newAnnotation); - - // manage the tags - List tags = dataset.getTags(); - - for(CkanTag ckanTag : tags){ - if(!ckanTag.getName().equals(bean.getCurrentStatus().toString())){ - JSONObject obj = new JSONObject(); - obj.put("vocabulary_id", ckanTag.getVocabularyId()); - obj.put("state", ckanTag.getState().toString()); - obj.put("display_name", ckanTag.getDisplayName()); - obj.put("id", ckanTag.getId()); - obj.put("name", ckanTag.getName()); - tagsAsJson.add(obj); - } - } - - // add the new one - JSONObject newTag = new JSONObject(); - newTag.put("name", bean.getNewStatus().toString()); - newTag.put("display_name", bean.getNewStatus().toString()); - tagsAsJson.add(newTag); - - // manage the groups - List groups = dataset.getGroups(); - for (CkanGroup ckanGroup : groups) { - if(!ckanGroup.getName().equals("grsf" + "-" + bean.getCurrentStatus().toString().toLowerCase())){ - JSONObject obj = new JSONObject(); - obj.put("name", ckanGroup.getName()); - groupsAsJson.add(obj); - } - } - - JSONObject newGroup = new JSONObject(); - newGroup.put("name", "grsf" + "-" + bean.getNewStatus().toString().toLowerCase()); - groupsAsJson.add(newGroup); - - // perform the request - jsonRequest.put("id", bean.getCatalogueIdentifier()); - jsonRequest.put("tags", tagsAsJson); - jsonRequest.put("extras", customFieldsAsJson); - jsonRequest.put("groups", groupsAsJson); - - logger.debug("Request param is going to be " + jsonRequest); - - if((errorMessage = catalogue.patchProductWithJSON(bean.getCatalogueIdentifier(), jsonRequest, apiKey)) == null){ - logger.info("Record patched ..."); - break; - }else - continue; // retry - - }catch(Exception e){ - logger.error("Error while trying to patch grsf record (iteration " + i + " of " + MAX_TRIAL + ")" + e.getMessage()); - errorMessage = e.getMessage(); - } - } - return errorMessage; } /** @@ -529,7 +451,7 @@ public class Utils { logger.warn(message); throw new Exception(message); } - + logger.info("Result size is " + datasets.size()); if(datasets.size() == 1) @@ -542,7 +464,7 @@ public class Utils { if(extra.getKey().contains(Constants.GRSF_SEMANTIC_IDENTIFIER_CUSTOM_KEY) && extra.getValue().equals(suggestedRecordSemanticIdentifier)){ logger.info("Matching dataset has id " + dataset.getId() + " with value " + Constants.GRSF_SEMANTIC_IDENTIFIER_CUSTOM_KEY); return dataset; - } + } } @@ -576,7 +498,7 @@ public class Utils { return null; } - + /** * Exploits the fact that in GRSF the url of a record contains the name (which is unique) of the record itself * @param url @@ -630,7 +552,7 @@ public class Utils { // } // // } - + /** * Get a {@link SimilarGRSFRecord} from a json string * @param json @@ -654,7 +576,7 @@ public class Utils { ); } - + /** * Checks if is into portal. * @@ -678,21 +600,26 @@ public class Utils { * @return * @throws ParseException */ - public static ConnectedBean connectedBeanRecordFromJson(String json, String sourceIdentifier, String sourceDomain) throws ParseException { + public static ConnectedBean connectedBeanRecordFromJson(String json, String sourceIdentifier, String sourceDomain, + DataCatalogue clg) throws ParseException { + if(json == null) return null; JSONParser parser = new JSONParser(); JSONObject object = (JSONObject)parser.parse(json); - + + String uuidDest = (String)object.get(Constants.CONNECTED_RECORD_KNOWLEDGE_BASE_ID_JSON_KEY); + String url = clg.getUrlFromDatasetIdOrName(uuidDest); return new ConnectedBean( sourceIdentifier, sourceDomain, (String)object.get(Constants.CONNECTED_RECORD_SHORT_NAME_JSON_KEY), (String)object.get(Constants.CONNECTED_RECORD_SEMANTIC_IDENTIFIER_JSON_KEY), - (String)object.get(Constants.CONNECTED_RECORD_KNOWLEDGE_BASE_ID_JSON_KEY) + uuidDest, + url ); - + } } diff --git a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/shared/ConnectedBean.java b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/shared/ConnectedBean.java index 7aa5ed1..f447ed4 100644 --- a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/shared/ConnectedBean.java +++ b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/shared/ConnectedBean.java @@ -10,11 +10,12 @@ import java.io.Serializable; public class ConnectedBean implements Serializable{ private static final long serialVersionUID = -4863776727351488790L; - private String sourceIdentifier; + private String sourceKnowledgeBaseId; private String sourceDomain; // i.e. Stock or Fishery private String destShortName; private String destSemanticIdentifier; - private String destIdentifier; // the dest indentifier of a Fishery or Stock (the link is from a Stock to a Fishery and viceversa) + private String destKnowledgeBaseId; // the dest indentifier of a Fishery or Stock (the link is from a Stock to a Fishery and viceversa) + private String url; private boolean isExtra; private boolean toBeKept = true; @@ -23,22 +24,25 @@ public class ConnectedBean implements Serializable{ } /** - * @param sourceIdentifier + * + * @param sourceKnowledgeBaseId * @param sourceDomain * @param destShortName * @param destSemanticIdentifier - * @param destIdentifier - * @param toBeKept + * @param destKnowledgeBaseId + * @param url */ - public ConnectedBean(String sourceIdentifier, String sourceDomain, + public ConnectedBean(String sourceKnowledgeBaseId, String sourceDomain, String destShortName, String destSemanticIdentifier, - String destIdentifier) { + String destKnowledgeBaseId, String url) { super(); - this.sourceIdentifier = sourceIdentifier; + this.sourceKnowledgeBaseId = sourceKnowledgeBaseId; this.sourceDomain = sourceDomain; this.destShortName = destShortName; this.destSemanticIdentifier = destSemanticIdentifier; - this.destIdentifier = destIdentifier; + this.destKnowledgeBaseId = destKnowledgeBaseId; + this.url = url; + } public String getDestShortName() { @@ -56,19 +60,22 @@ public class ConnectedBean implements Serializable{ public void setDestSemanticIdentifier(String destSemanticIdentifier) { this.destSemanticIdentifier = destSemanticIdentifier; } + public String getSourceKnowledgeBaseId() { + return sourceKnowledgeBaseId; + } - public String getSourceIdentifier() { - return sourceIdentifier; + public void setSourceKnowledgeBaseId(String sourceKnowledgeBaseId) { + this.sourceKnowledgeBaseId = sourceKnowledgeBaseId; } - public void setSourceIdentifier(String sourceIdentifier) { - this.sourceIdentifier = sourceIdentifier; + + public String getDestKnowledgeBaseId() { + return destKnowledgeBaseId; } - public String getDestIdentifier() { - return destIdentifier; - } - public void setDestIdentifier(String destIdentifier) { - this.destIdentifier = destIdentifier; + + public void setDestKnowledgeBaseId(String destKnowledgeBaseId) { + this.destKnowledgeBaseId = destKnowledgeBaseId; } + public String getSourceDomain() { return sourceDomain; } @@ -91,14 +98,20 @@ public class ConnectedBean implements Serializable{ public void setExtra(boolean isExtra) { this.isExtra = isExtra; } - + + public String getUrl() { + return url; + } + public void setUrl(String url) { + this.url = url; + } @Override public String toString() { - return "ConnectedBean [sourceIdentifier=" + sourceIdentifier + return "ConnectedBean [sourceKnowledgeBaseId=" + sourceKnowledgeBaseId + ", sourceDomain=" + sourceDomain + ", destShortName=" + destShortName + ", destSemanticIdentifier=" - + destSemanticIdentifier + ", destIdentifier=" + destIdentifier - + ", isExtra=" + isExtra + ", toBeKept=" + toBeKept + "]"; + + destSemanticIdentifier + ", destKnowledgeBaseId=" + + destKnowledgeBaseId + ", url=" + url + ", isExtra=" + isExtra + + ", toBeKept=" + toBeKept + "]"; } - } diff --git a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/shared/SimilarGRSFRecord.java b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/shared/SimilarGRSFRecord.java index 218bf53..3147243 100644 --- a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/shared/SimilarGRSFRecord.java +++ b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/shared/SimilarGRSFRecord.java @@ -66,6 +66,10 @@ public class SimilarGRSFRecord implements Serializable{ this.toBeKept = toBeKept; } + public boolean isToBeKept() { + return toBeKept; + } + public String getShortName() { return shortName; }