package org.gcube.portlets.widgets.wstaskexecutor.client.view.binder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.gcube.common.workspacetaskexecutor.shared.TaskParameter; import org.gcube.common.workspacetaskexecutor.shared.TaskParameterType; import org.gcube.common.workspacetaskexecutor.shared.dataminer.TaskConfiguration; import org.gcube.portlets.widgets.wstaskexecutor.client.WsTaskExecutorWidget; import org.gcube.portlets.widgets.wstaskexecutor.client.event.DeleteCustomFieldEvent; import org.gcube.portlets.widgets.wstaskexecutor.client.event.DeleteCustomFieldEventHandler; import org.gcube.portlets.widgets.wstaskexecutor.client.view.FormatUtil; import org.gcube.portlets.widgets.wstaskexecutor.shared.GcubeScope; import org.gcube.portlets.widgets.wstaskexecutor.shared.WSItem; import com.github.gwtbootstrap.client.ui.Button; import com.github.gwtbootstrap.client.ui.ControlGroup; import com.github.gwtbootstrap.client.ui.Controls; import com.github.gwtbootstrap.client.ui.Fieldset; import com.github.gwtbootstrap.client.ui.ListBox; import com.github.gwtbootstrap.client.ui.Pager; import com.github.gwtbootstrap.client.ui.TextArea; import com.github.gwtbootstrap.client.ui.TextBox; import com.github.gwtbootstrap.client.ui.constants.ControlGroupType; import com.google.gwt.core.client.GWT; import com.google.gwt.dom.client.Document; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.event.dom.client.DomEvent; 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.uibinder.client.UiHandler; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.ui.Composite; import com.google.gwt.user.client.ui.HTMLPanel; import com.google.gwt.user.client.ui.Widget; /** * The Class CreateTaskConfigurationView. * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * May 4, 2018 */ public abstract class CreateTaskConfigurationView extends Composite { /** The ui binder. */ private static CreateTaskConfigurationViewUiBinder uiBinder = GWT.create(CreateTaskConfigurationViewUiBinder.class); /** * The Interface CreateTaskConfigurationViewUiBinder. * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * May 4, 2018 */ interface CreateTaskConfigurationViewUiBinder extends UiBinder { } /** The pager. */ @UiField Pager pager; @UiField ListBox field_select_scope; @UiField ControlGroup cg_input_task_id; @UiField ControlGroup cg_select_vre; @UiField ControlGroup cg_parameters_control; @UiField Fieldset form_unit_fields; @UiField TextBox field_task_id; @UiField Controls task_parameters_control; @UiField Button addCustomFieldButton; @UiField HTMLPanel html_panel_field; @UiField TextArea field_task_description; /** The map VR es. */ private Map mapScopes = new HashMap(); private String currentScope; public final static HandlerManager eventBus = new HandlerManager(null); // added custom field entries (by the user) private List customFieldEntriesList = new ArrayList(); private WSItem folder; private boolean isEditConfiguration; private TaskConfiguration editConfiguration; /** * Submit handler. */ public abstract void submitHandler(); /** * Sets the error. * * @param visible the visible * @param error the error */ public abstract void setError(boolean visible, String error); /** * Sets the confirm. * * @param visible the visible * @param msg the msg */ public abstract void setConfirm(boolean visible, String msg); /** * 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: * * Hello! * * Note that depending on the widget that is used, it may be necessary to * implement HasHTML instead of HasText. * * @param folder the folder * @param conf the conf */ public CreateTaskConfigurationView(WSItem folder, TaskConfiguration conf) { this.folder = folder; if(conf!=null) this.isEditConfiguration = true; this.editConfiguration = conf; initWidget(uiBinder.createAndBindUi(this)); bindEvents(); pager.getLeft().setVisible(false); pager.getRight().addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { setError(false, ""); boolean isValid = validateSubmit(); if(isValid) submitHandler(); } }); WsTaskExecutorWidget.wsTaskService.getListOfScopesForLoggedUser(new AsyncCallback>() { @Override public void onSuccess(List result) { for (GcubeScope gcubeScope : result) { String toValue = FormatUtil.toScopeValue(gcubeScope); mapScopes.put(gcubeScope.getScopeName(), gcubeScope); field_select_scope.addItem(toValue, gcubeScope.getScopeName()); } if(result.size()>0){ if(isEditConfiguration){ String vreName = FormatUtil.toVREName(editConfiguration.getScope()); field_select_scope.setSelectedValue(vreName); } else field_select_scope.setSelectedValue(result.get(0).getScopeName()); //field_select_vre.setSelectedIndex(0); //field_select_vre.fireEvent(DomEvent.fireNativeEvent(nativeEvent, handlerSource);); DomEvent.fireNativeEvent(Document.get().createChangeEvent(), field_select_scope); } } @Override public void onFailure(Throwable caught) { // TODO Auto-generated method stub } }); if(isEditConfiguration){ field_task_id.setValue(editConfiguration.getTaskId()); field_task_description.setValue(editConfiguration.getTaskDescription()); List params = editConfiguration.getListParameters(); for (TaskParameter taskParameter : params) { appendCustomField(taskParameter.getKey(), taskParameter.getValue(), taskParameter.getType().getType()); } pager.getRight().setText("Update Configuration"); } else addEmptyCustomFieldEvent(null); } /** * Adds the empty custom field event. * * @param e the e */ @UiHandler("addCustomFieldButton") void addEmptyCustomFieldEvent(ClickEvent e){ appendCustomField(null, null, null); } /** * Append custom field. * * @param key the key * @param value the value * @param parameterType the parameter type */ private void appendCustomField(String key, String value, String parameterType){ CustomFieldEntry toAdd = new CustomFieldEntry(eventBus, key, value, parameterType); customFieldEntriesList.add(toAdd); cg_parameters_control.add(toAdd); } /** * Bind events. */ private void bindEvents() { // when a custom field is removed, remove it from the list eventBus.addHandler(DeleteCustomFieldEvent.TYPE, new DeleteCustomFieldEventHandler() { @Override public void onRemoveEntry(DeleteCustomFieldEvent event) { customFieldEntriesList.remove(event.getRemovedEntry()); cg_parameters_control.remove(event.getRemovedEntry()); } }); } /** * Validate submit. * * @return true, if successful */ protected boolean validateSubmit() { cg_input_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_task_id.getValue() == null || field_task_id.getValue().isEmpty()){ cg_input_task_id.setType(ControlGroupType.ERROR); setError(true, "You must type an Algorithm Identifier!"); 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; } /** * Gets the selected scope. * * @return the selected scope */ public GcubeScope getSelectedScope(){ //String item = field_select_scope.getSelectedItemText(); String text = field_select_scope.getSelectedValue(); return mapScopes.get(text); } /** * Gets the task id. * * @return the task id */ public String getTaskId(){ return field_task_id.getValue(); } /** * Gets the parameters. * * @return the parameters */ public List getParameters(){ List listParameters = new ArrayList(); for (CustomFieldEntry cFE : customFieldEntriesList) { TaskParameter tp = new TaskParameter(cFE.getKey(), cFE.getValue(), null, new TaskParameterType(cFE.getType())); listParameters.add(tp); } return listParameters; } /** * Gets the description. * * @return the description */ public String getDescription(){ return field_task_description.getValue(); } }