2019-12-18 11:58:49 +01:00
|
|
|
package org.gcube.portlets.widgets.ckan2zenodopublisher.client.ui.authors;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2020-01-16 12:26:36 +01:00
|
|
|
import java.util.Arrays;
|
2019-12-18 11:58:49 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.gcube.portlets.widgets.ckan2zenodopublisher.client.events.RemoveCreatorEvent;
|
|
|
|
import org.gcube.portlets.widgets.ckan2zenodopublisher.client.events.RemoveCreatorEventHandler;
|
2020-01-13 17:48:41 +01:00
|
|
|
import org.gcube.portlets.widgets.ckan2zenodopublisher.client.view.FieldUtil;
|
|
|
|
import org.gcube.portlets.widgets.ckan2zenodopublisher.client.view.FormValidator;
|
2020-01-15 17:51:22 +01:00
|
|
|
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.SerializableEnum;
|
|
|
|
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.wrapped.ZenodoAuthor;
|
|
|
|
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.wrapped.ZenodoContributor;
|
2019-12-18 11:58:49 +01:00
|
|
|
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.wrapped.ZenodoCreator;
|
|
|
|
|
|
|
|
import com.github.gwtbootstrap.client.ui.Button;
|
2020-01-13 17:48:41 +01:00
|
|
|
import com.github.gwtbootstrap.client.ui.ControlGroup;
|
|
|
|
import com.github.gwtbootstrap.client.ui.constants.ControlGroupType;
|
2019-12-18 11:58:49 +01:00
|
|
|
import com.google.gwt.core.client.GWT;
|
|
|
|
import com.google.gwt.event.dom.client.ClickEvent;
|
|
|
|
import com.google.gwt.event.dom.client.ClickHandler;
|
|
|
|
import com.google.gwt.event.shared.HandlerManager;
|
|
|
|
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.VerticalPanel;
|
|
|
|
import com.google.gwt.user.client.ui.Widget;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Class CreatorsFormView.
|
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
|
|
|
|
*
|
|
|
|
* Dec 17, 2019
|
|
|
|
*/
|
2020-01-13 17:48:41 +01:00
|
|
|
public class CreatorsFormView extends Composite implements FormValidator{
|
2019-12-18 11:58:49 +01:00
|
|
|
|
|
|
|
private static CreatorsFormViewUiBinder uiBinder = GWT.create(CreatorsFormViewUiBinder.class);
|
|
|
|
|
|
|
|
// event bus
|
|
|
|
private HandlerManager eventBus = new HandlerManager(null);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Interface CreatorsFormViewUiBinder.
|
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
|
|
|
|
*
|
|
|
|
* Dec 17, 2019
|
|
|
|
*/
|
|
|
|
interface CreatorsFormViewUiBinder extends UiBinder<Widget, CreatorsFormView> {
|
|
|
|
}
|
|
|
|
|
|
|
|
@UiField
|
|
|
|
VerticalPanel fieldset_authors;
|
|
|
|
|
|
|
|
@UiField
|
|
|
|
Button button_add_author;
|
|
|
|
|
|
|
|
private List<CreatorView> listCreatorView;
|
|
|
|
|
2020-01-15 17:51:22 +01:00
|
|
|
private String userRole;
|
|
|
|
|
|
|
|
private List<? extends ZenodoAuthor> listAuthors;
|
|
|
|
|
|
|
|
private SerializableEnum<String> contributorTypeValues;
|
|
|
|
|
|
|
|
|
2019-12-18 11:58:49 +01:00
|
|
|
/**
|
|
|
|
* Instantiates a new creators form view.
|
|
|
|
*
|
2020-01-15 17:51:22 +01:00
|
|
|
* @param listAuthors the list authors
|
2019-12-18 11:58:49 +01:00
|
|
|
*/
|
2020-01-15 17:51:22 +01:00
|
|
|
public CreatorsFormView(List<? extends ZenodoAuthor> listAuthors) {
|
2019-12-18 11:58:49 +01:00
|
|
|
initWidget(uiBinder.createAndBindUi(this));
|
2020-01-15 17:51:22 +01:00
|
|
|
this.listAuthors = listAuthors;
|
2019-12-18 11:58:49 +01:00
|
|
|
|
|
|
|
button_add_author.addClickHandler(new ClickHandler() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(ClickEvent event) {
|
|
|
|
addEmptyCreator();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
eventBus.addHandler(RemoveCreatorEvent.TYPE, new RemoveCreatorEventHandler() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAddedResource(RemoveCreatorEvent addResourceEvent) {
|
|
|
|
try {
|
|
|
|
boolean removed = listCreatorView.remove(addResourceEvent.getCreatorView());
|
|
|
|
GWT.log("removed creator: "+removed);
|
|
|
|
fieldset_authors.remove(addResourceEvent.getCreatorView());
|
|
|
|
}catch (Exception e) {
|
|
|
|
// TODO: handle exception
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-01-15 17:51:22 +01:00
|
|
|
addAuthorsToView();
|
2019-12-18 11:58:49 +01:00
|
|
|
}
|
|
|
|
|
2020-01-15 17:51:22 +01:00
|
|
|
|
2019-12-18 11:58:49 +01:00
|
|
|
/**
|
2020-01-15 17:51:22 +01:00
|
|
|
* Adds the authors to view.
|
2019-12-18 11:58:49 +01:00
|
|
|
*/
|
2020-01-15 17:51:22 +01:00
|
|
|
private void addAuthorsToView() {
|
|
|
|
|
2019-12-18 11:58:49 +01:00
|
|
|
if(listCreatorView==null)
|
|
|
|
listCreatorView = new ArrayList<CreatorView>();
|
|
|
|
|
2020-01-15 17:51:22 +01:00
|
|
|
if(listAuthors!=null && listAuthors.size()>0) {
|
|
|
|
userRole = "Author";
|
|
|
|
|
|
|
|
for (ZenodoAuthor creator : listAuthors) {
|
2020-01-16 12:26:36 +01:00
|
|
|
GWT.log("Author class is "+creator.getClass());
|
2020-01-15 17:51:22 +01:00
|
|
|
if (creator instanceof ZenodoContributor) {
|
|
|
|
userRole = "Contributor";
|
2020-01-16 12:26:36 +01:00
|
|
|
contributorTypeValues = new SerializableEnum<String>(Arrays.asList(creator.getType().getSelectableValues().get(0)), creator.getType().getSelectableValues());
|
2020-01-15 17:51:22 +01:00
|
|
|
}else if(creator instanceof ZenodoCreator) {
|
|
|
|
userRole = "Creator";
|
|
|
|
}
|
|
|
|
CreatorView view = new CreatorView(creator, userRole, eventBus);
|
2019-12-18 11:58:49 +01:00
|
|
|
listCreatorView.add(view);
|
|
|
|
fieldset_authors.add(view);
|
|
|
|
}
|
2020-01-15 17:51:22 +01:00
|
|
|
|
|
|
|
button_add_author.setText("Add another "+userRole);
|
|
|
|
|
2019-12-18 11:58:49 +01:00
|
|
|
setRemovableCreators();
|
|
|
|
}else {
|
|
|
|
//ADDING EMPTY CREATOR
|
|
|
|
addEmptyCreator();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 17:51:22 +01:00
|
|
|
/**
|
|
|
|
* Gets the list creator view.
|
|
|
|
*
|
|
|
|
* @return the list creator view
|
|
|
|
*/
|
2019-12-18 11:58:49 +01:00
|
|
|
public List<CreatorView> getListCreatorView() {
|
|
|
|
return listCreatorView;
|
|
|
|
}
|
|
|
|
|
2020-01-15 17:51:22 +01:00
|
|
|
/**
|
|
|
|
* Adds the empty creator.
|
|
|
|
*/
|
2019-12-18 11:58:49 +01:00
|
|
|
private void addEmptyCreator() {
|
|
|
|
|
|
|
|
if(listCreatorView==null)
|
|
|
|
listCreatorView = new ArrayList<CreatorView>();
|
|
|
|
|
2020-01-15 17:51:22 +01:00
|
|
|
ZenodoAuthor author = null;
|
|
|
|
if(userRole.compareToIgnoreCase("Contributor")==0) {
|
|
|
|
author = new ZenodoContributor();
|
|
|
|
((ZenodoContributor) author).setType(contributorTypeValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CreatorView view = new CreatorView(author, userRole, eventBus);
|
2019-12-18 11:58:49 +01:00
|
|
|
listCreatorView.add(view);
|
|
|
|
fieldset_authors.add(view);
|
|
|
|
|
|
|
|
setRemovableCreators();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-15 17:51:22 +01:00
|
|
|
/**
|
|
|
|
* Sets the removable creators.
|
|
|
|
*/
|
2019-12-18 11:58:49 +01:00
|
|
|
private void setRemovableCreators() {
|
|
|
|
|
|
|
|
for (int i=0; i<listCreatorView.size(); i++) {
|
|
|
|
CreatorView creator = listCreatorView.get(i);
|
|
|
|
boolean removableCreator = i==0?false:true;
|
|
|
|
creator.setVisibleRemoveCreator(removableCreator);
|
|
|
|
}
|
|
|
|
}
|
2020-01-13 17:48:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read list of creators from view.
|
|
|
|
*
|
2020-01-15 17:51:22 +01:00
|
|
|
* @return the list<? extends zenodo author>
|
2020-01-13 17:48:41 +01:00
|
|
|
*/
|
2020-01-15 17:51:22 +01:00
|
|
|
public List<? extends ZenodoAuthor> readListOfCreatorsFromView(){
|
|
|
|
|
|
|
|
List<ZenodoAuthor> listAuthors = new ArrayList<>(listCreatorView.size());
|
|
|
|
|
2020-01-13 17:48:41 +01:00
|
|
|
for (CreatorView creatorView : listCreatorView) {
|
2020-01-15 17:51:22 +01:00
|
|
|
ZenodoAuthor author = creatorView.getAuthor();
|
2020-01-16 12:26:36 +01:00
|
|
|
GWT.log("Read ZenodoAuthor: "+author);
|
2020-01-15 17:51:22 +01:00
|
|
|
author.setName(creatorView.getField_name().getValue());
|
|
|
|
author.setAffiliation(creatorView.getField_affiliation().getValue());
|
|
|
|
author.setOrcid(creatorView.getField_orcid().getValue());
|
2020-01-16 12:26:36 +01:00
|
|
|
String typeSelected = creatorView.getAuthorTypeValue();
|
|
|
|
if(typeSelected!=null && !typeSelected.isEmpty()) {
|
|
|
|
author.setType(new SerializableEnum<>(Arrays.asList(typeSelected), null));
|
|
|
|
}
|
|
|
|
|
2020-01-15 17:51:22 +01:00
|
|
|
listAuthors.add(author);
|
2020-01-13 17:48:41 +01:00
|
|
|
}
|
|
|
|
|
2020-01-15 17:51:22 +01:00
|
|
|
return listAuthors;
|
2020-01-13 17:48:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-01-15 17:51:22 +01:00
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see org.gcube.portlets.widgets.ckan2zenodopublisher.client.view.FormValidator#isValidForm()
|
|
|
|
*/
|
2020-01-13 17:48:41 +01:00
|
|
|
@Override
|
|
|
|
public boolean isValidForm() {
|
|
|
|
return validateFormFields()==null;
|
|
|
|
}
|
|
|
|
|
2020-01-15 17:51:22 +01:00
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see org.gcube.portlets.widgets.ckan2zenodopublisher.client.view.FormValidator#validateFormFields()
|
|
|
|
*/
|
2020-01-13 17:48:41 +01:00
|
|
|
@Override
|
|
|
|
public String validateFormFields() {
|
|
|
|
|
|
|
|
for (CreatorView creatorView : listCreatorView) {
|
|
|
|
ControlGroup cgAuthor = creatorView.getControlGroup_Author();
|
|
|
|
FieldUtil.setControlGroup(cgAuthor, ControlGroupType.NONE);
|
|
|
|
String value = FieldUtil.getTextValue(creatorView.getField_name());
|
|
|
|
boolean isValid = FieldUtil.isValidValue(value);
|
|
|
|
if(!isValid) {
|
|
|
|
FieldUtil.setControlGroup(cgAuthor, ControlGroupType.ERROR);
|
2020-01-27 17:56:16 +01:00
|
|
|
return "The field "+userRole+" Name is required";
|
2020-01-13 17:48:41 +01:00
|
|
|
}
|
|
|
|
|
2020-01-14 12:28:20 +01:00
|
|
|
/*
|
2020-01-13 17:48:41 +01:00
|
|
|
String affiliation = FieldUtil.getTextValue(creatorView.getField_affiliation());
|
|
|
|
isValid = FieldUtil.isValidValue(affiliation);
|
|
|
|
if(!isValid) {
|
|
|
|
FieldUtil.setControlGroup(cgAuthor, ControlGroupType.ERROR);
|
2020-01-14 12:28:20 +01:00
|
|
|
return "The field affiliation is required";
|
2020-01-13 17:48:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String orcid = FieldUtil.getTextValue(creatorView.getField_orcid());
|
|
|
|
isValid = FieldUtil.isValidValue(orcid);
|
|
|
|
if(!isValid) {
|
|
|
|
FieldUtil.setControlGroup(cgAuthor, ControlGroupType.ERROR);
|
2020-01-14 12:28:20 +01:00
|
|
|
return "The field ORCID is required";
|
2020-01-13 17:48:41 +01:00
|
|
|
}
|
2020-01-14 12:28:20 +01:00
|
|
|
*/
|
2020-01-13 17:48:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2019-12-18 11:58:49 +01:00
|
|
|
|
|
|
|
}
|