ckan2zenodo-publisher-widget/src/main/java/org/gcube/portlets/widgets/ckan2zenodopublisher/client/ui/basicinformation/BasicInformationView.java

612 lines
16 KiB
Java

package org.gcube.portlets.widgets.ckan2zenodopublisher.client.ui.basicinformation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.gcube.portlets.widgets.ckan2zenodopublisher.client.ui.authors.CreatorsFormView;
import org.gcube.portlets.widgets.ckan2zenodopublisher.client.ui.tags.TagsPanel;
import org.gcube.portlets.widgets.ckan2zenodopublisher.client.ui.utils.FieldDescription;
import org.gcube.portlets.widgets.ckan2zenodopublisher.client.view.FieldUtil;
import org.gcube.portlets.widgets.ckan2zenodopublisher.client.view.FormValidator;
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.wrapped.ZenodoAuthor;
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.wrapped.ZenodoContributor;
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.wrapped.ZenodoCreator;
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.wrapped.ZenodoItem;
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.wrapped.ZenodoMetadata;
import com.github.gwtbootstrap.client.ui.Alert;
import com.github.gwtbootstrap.client.ui.ControlGroup;
import com.github.gwtbootstrap.client.ui.Fieldset;
import com.github.gwtbootstrap.client.ui.ListBox;
import com.github.gwtbootstrap.client.ui.TextArea;
import com.github.gwtbootstrap.client.ui.TextBox;
import com.github.gwtbootstrap.client.ui.constants.ControlGroupType;
import com.github.gwtbootstrap.datepicker.client.ui.DateBox;
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.Widget;
/**
* The Class CreateTaskConfigurationView.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it May 4, 2018
*/
public class BasicInformationView extends Composite implements FormValidator {
/** The ui binder. */
private static BasicInformationViewUiBinder uiBinder = GWT.create(BasicInformationViewUiBinder.class);
private List<CreatorsFormView> listOfCreatorsView;
private List<CreatorsFormView> listOfContributorsView;
/**
* The Interface CreateTaskConfigurationViewUiBinder.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it May 4, 2018
*/
interface BasicInformationViewUiBinder extends UiBinder<Widget, BasicInformationView> {
}
@UiField
TextBox field_title;
@UiField
TextBox field_doi;
@UiField
Fieldset fieldset_basic_informations;
@UiField
TextArea field_description;
@UiField
ListBox field_upload_type;
@UiField
ListBox field_access_right;
@UiField
ListBox field_license;
@UiField
ListBox field_publication_type;
@UiField
DateBox field_publication_date;
@UiField
Alert error_alert;
@UiField
TagsPanel the_tags_panel;
// @UiField
// Pager pager;
// public final static HandlerManager eventBus = new HandlerManager(null);
private boolean isEditConfiguration;
private ZenodoItem zenodoItem;
/**
* Because this class has a default constructor, it can be used as a binder
* template. In other words, it can be used in other *.ui.xml files as follows:
* <ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder" xmlns:g=
* "urn:import:**user's package**">
* <g:**UserClassName**>Hello!</g:**UserClassName> </ui:UiBinder> Note that
* depending on the widget that is used, it may be necessary to implement
* HasHTML instead of HasText.
*
* @param zenodoItem the zenodo item
* @param isUpdate the is update
* @param tabIndex the tab index
*/
public BasicInformationView(final ZenodoItem zenodoItem, boolean isUpdate, final int tabIndex) {
initWidget(uiBinder.createAndBindUi(this));
this.zenodoItem = zenodoItem;
fillForm();
setVisibleFieldsForUpdate(isUpdate);
//
// pager.getRight().addClickHandler(new ClickHandler() {
//
// @Override
// public void onClick(ClickEvent event) {
//
// Ckan2ZenodoViewManager.eventBus.fireEvent(new TabPagerClickedEvent(TabPagerClickedEvent.PAGER.NEXT));
//
// }
// });
// if(conf!=null)
// this.isEditConfiguration = true;
// this.editConfiguration = conf;
// bindEvents();
// if (isEditConfiguration) {
// pager.getRight().setText("Update Configuration");
// } else {
//
// }
}
/**
* Sets the visible fields for update.
*
* @param isUpdate the new visible fields for update
*/
protected void setVisibleFieldsForUpdate(boolean isUpdate) {
List<Widget> listWidgtes = new ArrayList<Widget>();
listWidgtes.add(field_doi);
listWidgtes.add(field_publication_date);
for (Widget widget : listWidgtes) {
Widget parent = FieldUtil.getParentControlGroupOfWidget(widget); // It should be the 'ControlGroup'
if (parent instanceof ControlGroup) {
parent.setVisible(isUpdate);
}
}
}
/**
* Fill parameters to operator.
*/
private void fillForm() {
field_title.setValue(zenodoItem.getTitle());
field_description.setValue(zenodoItem.getMetadata().getDescription());
field_doi.setValue(zenodoItem.getDoi().toString());
ZenodoMetadata zMeta = zenodoItem.getMetadata();
if (zMeta != null) {
String title = zenodoItem.getTitle()!=null?zenodoItem.getTitle():zMeta.getTitle();
String value = zenodoItem.getFieldsDescriptions().get("title");
if(value!=null) {
Widget parent = field_title.getParent();
FieldDescription fd = new FieldDescription(null, value);
parent.getElement().appendChild(fd.getElement());
}
field_title.setValue(title); //Re.fill title to be sure
//Upload type
if(zMeta.getUpload_type()!=null) {
FieldUtil.addValuesToListBox(field_upload_type, zMeta.getUpload_type().getSelectableValues());
FieldUtil.selectValueToListBox(field_upload_type, zMeta.getUpload_type().getSelectedValues());
}else
field_upload_type.setEnabled(false);
//Publication Type
if(zMeta.getPublication_type()!=null) {
FieldUtil.addValuesToListBox(field_publication_type, zMeta.getPublication_type().getSelectableValues());
FieldUtil.selectValueToListBox(field_publication_type, zMeta.getPublication_type().getSelectedValues());
}else
field_publication_type.setEnabled(false);
if(zMeta.getAccess_right()!=null) {
FieldUtil.addValuesToListBox(field_access_right, zMeta.getAccess_right().getSelectableValues());
FieldUtil.selectValueToListBox(field_access_right, zMeta.getAccess_right().getSelectedValues());
}else
field_access_right.setEnabled(false);
if(zMeta.getLicense()!=null) {
FieldUtil.addValuesToListBox(field_license, Arrays.asList(zMeta.getLicense().getId()));
FieldUtil.selectValueToListBox(field_license, Arrays.asList(zMeta.getLicense().getId()));
}
field_license.setEnabled(false); //because is not changeable
if(zMeta.getKeywords()!=null && zMeta.getKeywords().size()>0) {
for (String keyword : zMeta.getKeywords()) {
the_tags_panel.addTagElement(keyword);
}
}
if(listOfCreatorsView==null)
listOfCreatorsView = new ArrayList<CreatorsFormView>();
if(listOfContributorsView==null)
listOfContributorsView = new ArrayList<CreatorsFormView>();
// ADDING AUTHORS
GWT.log("Adding creators: "+zenodoItem.getMetadata().getCreators());
CreatorsFormView authorView = new CreatorsFormView(zenodoItem.getMetadata().getCreators());
listOfCreatorsView.add(authorView);
GWT.log("Adding contributors: "+zenodoItem.getMetadata().getContributors());
CreatorsFormView contributorView = new CreatorsFormView(zenodoItem.getMetadata().getContributors());
listOfContributorsView.add(contributorView);
for (CreatorsFormView cfv : listOfCreatorsView) {
fieldset_basic_informations.add(cfv);
}
for (CreatorsFormView cfv : listOfContributorsView) {
fieldset_basic_informations.add(cfv);
}
}
// if(zenodoItem.getFiles()!=null) {
// label_files.setVisible(true);
// for (ZenodoFile file : zenodoItem.getFiles()) {
// addFileForPublishing(file);
// }
// }
}
// /**
// * Adds the values to list box.
// *
// * @param list the list
// * @param listValues the list values
// */
// private void addValuesToListBox(ListBox list, List<String> listValues) {
// if (listValues == null)
// return;
//
// for (int i = 0; i < listValues.size(); i++) {
// list.addItem(listValues.get(i), listValues.get(i));
// }
// }
//
// /**
// * Select value to list box.
// *
// * @param list the list
// * @param values the values
// */
// private void selectValueToListBox(ListBox list, List<String> values) {
// GWT.log("Selecting value: "+values);
// String selectValue = null;
// if (values == null || values.isEmpty()) {
// selectValue = null;
// } else {
// selectValue = values.get(0);
// }
//
// try {
// if (list.getItemCount() > 0)
// list.setSelectedValue(selectValue);
// } catch (Exception e) {
// GWT.log("error: " + e);
// }
// }
// private void addFileForPublishing(ZenodoFile file) {
// PublishFileView pv = new PublishFileView();
// pv.getField_file_name().setValue(file.getFilename());
// pv.getSwitchButton().setValue(true);
// to_zenodo_form_unit_fields.add(pv);
// }
// /**
// * Bind events.
// */
// private void bindEvents() {
//
// pager.getLeft().addClickHandler(new ClickHandler() {
//
// @Override
// public void onClick(ClickEvent event) {
//
// }
// });
//
// pager.getRight().addClickHandler(new ClickHandler() {
//
// @Override
// public void onClick(ClickEvent event) {
// setError(false, "");
// boolean isValid = validateSubmit();
// if (isValid)
// submitHandler();
//
// }
// });
// }
/**
* Validate submit.
*
* @return true, if successful
*/
protected boolean validateSubmit() {
/*
* cg_select_task_id.setType(ControlGroupType.NONE);
* //cg_parameters_control.setType(ControlGroupType.NONE);
* //cg_remote_path.setType(ControlGroupType.NONE);
*
* if(field_select_scope.getSelectedItemText()==null){
* cg_select_vre.setType(ControlGroupType.ERROR); setError(true,
* "You must select a Scope!"); return false; }
*
* if(field_select_scope.getSelectedItemText() == null ||
* field_select_scope.getSelectedItemText().isEmpty()){
* cg_select_task_id.setType(ControlGroupType.ERROR); setError(true,
* "You must select an Algorithm!"); return false; }
*
* for (CustomFieldEntry cFE : customFieldEntriesList) {
* cFE.getControlGroup().setType(ControlGroupType.NONE); if(cFE.getKey()==null
* || cFE.getKey().isEmpty()){
* cFE.getControlGroup().setType(ControlGroupType.ERROR);
* //cg_parameters_control.setType(ControlGroupType.ERROR); setError(true,
* "You must type a valid key parameter!"); return false; } }
*/
return true;
}
/**
* Checks if is edits the configuration.
*
* @return the isEditConfiguration
*/
public boolean isEditConfiguration() {
return isEditConfiguration;
}
/*
* (non-Javadoc)
*
* @see
* org.gcube.portlets.widgets.ckan2zenodopublisher.client.view.FormValidator#
* validateForm()
*/
@Override
public String validateFormFields() {
error_alert.setVisible(false);
ControlGroup cgDOI = FieldUtil.getParentControlGroupOfWidget(field_doi);
ControlGroup cgTitle = FieldUtil.getParentControlGroupOfWidget(field_title);
FieldUtil.setControlGroup(cgDOI, ControlGroupType.NONE);
FieldUtil.setControlGroup(cgTitle, ControlGroupType.NONE);
//Validating DOI
if (cgDOI.isVisible()) {
GWT.log("Checking doi---");
String value = getTextValue(field_doi);
boolean isValid = FieldUtil.isValidValue(value);
if(!isValid) {
FieldUtil.setControlGroup(cgDOI, ControlGroupType.ERROR);
error_alert.setVisible(true);
error_alert.setText("This field DOI is required");
return "This field is required";
}
}
//Validating Title
if (cgTitle.isVisible()) {
GWT.log("Checking title---");
String value = getTextValue(field_title);
boolean isValid = FieldUtil.isValidValue(value);
GWT.log("isValid: "+isValid);
if(!isValid) {
FieldUtil.setControlGroup(cgTitle, ControlGroupType.ERROR);
error_alert.setVisible(true);
error_alert.setText("The field Title is required");
return "This field is required";
}
}
//Validating Creators
for (CreatorsFormView cfv : listOfCreatorsView) {
String error = cfv.validateFormFields();
if(error!=null) {
error_alert.setVisible(true);
error_alert.setText(error);
return error;
}
}
//Validating Contributors
for (CreatorsFormView cfv : listOfContributorsView) {
String error = cfv.validateFormFields();
if(error!=null) {
error_alert.setVisible(true);
error_alert.setText(error);
return error;
}
}
return null;
}
/* (non-Javadoc)
* @see org.gcube.portlets.widgets.ckan2zenodopublisher.client.view.FormValidator#isValidForm()
*/
@Override
public boolean isValidForm() {
return validateFormFields()==null;
}
/**
* Gets the text value.
*
* @param box the box
* @return the text value
*/
private String getTextValue(TextBox box) {
return box.getText();
}
/**
* Gets the list of contributors view.
*
* @return the list of contributors view
*/
public List<CreatorsFormView> getListOfContributorsView() {
return listOfContributorsView;
}
/**
* Gets the list of creators view.
*
* @return the list of creators view
*/
public List<CreatorsFormView> getListOfCreatorsView() {
return listOfCreatorsView;
}
/**
* Gets the list of authors.
*
* @return the list of authors
*/
public List<ZenodoCreator> getListOfCreators() {
List<ZenodoCreator> listOfCreators = new ArrayList<>();
for (CreatorsFormView cfv : listOfCreatorsView) {
List<? extends ZenodoAuthor> listOfUsers = cfv.readListOfCreatorsFromView();
for (ZenodoAuthor zenodoAuthor : listOfUsers) {
listOfCreators.add((ZenodoCreator) zenodoAuthor);
}
}
return listOfCreators;
}
/**
* Gets the list of contributors.
*
* @return the list of contributors
*/
public List<ZenodoContributor> getListOfContributors() {
List<ZenodoContributor> listOfContributors = new ArrayList<>();
for (CreatorsFormView cfv : listOfContributorsView) {
List<? extends ZenodoAuthor> listOfUsers = cfv.readListOfCreatorsFromView();
for (ZenodoAuthor zenodoAuthor : listOfUsers) {
listOfContributors.add((ZenodoContributor) zenodoAuthor);
}
}
return listOfContributors;
}
/**
* Gets the field title.
*
* @return the field title
*/
public TextBox getField_title() {
return field_title;
}
/**
* Gets the field doi.
*
* @return the field doi
*/
public TextBox getField_doi() {
return field_doi;
}
/**
* Gets the fieldset basic informations.
*
* @return the fieldset basic informations
*/
public Fieldset getFieldset_basic_informations() {
return fieldset_basic_informations;
}
/**
* Gets the field description.
*
* @return the field description
*/
public TextArea getField_description() {
return field_description;
}
/**
* Gets the field upload type.
*
* @return the field upload type
*/
public ListBox getField_upload_type() {
return field_upload_type;
}
/**
* Gets the field access right.
*
* @return the field access right
*/
public ListBox getField_access_right() {
return field_access_right;
}
/**
* Gets the field license.
*
* @return the field license
*/
public ListBox getField_license() {
return field_license;
}
/**
* Gets the field publication type.
*
* @return the field publication type
*/
public ListBox getField_publication_type() {
return field_publication_type;
}
/**
* Gets the field publication date.
*
* @return the field publication date
*/
public DateBox getField_publication_date() {
return field_publication_date;
}
/**
* Gets the error alert.
*
* @return the error alert
*/
public Alert getError_alert() {
return error_alert;
}
/**
* Gets the tags.
*
* @return the tags
*/
public List<String> getTags() {
return the_tags_panel.getTags();
}
/**
* Gets the list of keywords.
*
* @return the list of keywords
*/
public List<String> getListOfKeywords(){
return the_tags_panel.getTags();
}
}