408 lines
12 KiB
Java
408 lines
12 KiB
Java
package org.gcube.portlets.user.td.csvimportwidget.client;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import org.gcube.portlets.user.td.csvimportwidget.client.csvgrid.CSVGrid;
|
|
import org.gcube.portlets.user.td.wizardwidget.client.WizardCard;
|
|
import org.gcube.portlets.user.td.csvimportwidget.client.util.ErrorMessageBox;
|
|
import org.gcube.portlets.user.td.gwtservice.client.rpc.TDGWTServiceAsync;
|
|
import org.gcube.portlets.user.td.gwtservice.shared.csv.AvailableCharsetList;
|
|
import org.gcube.portlets.user.td.gwtservice.shared.csv.CSVImportSession;
|
|
import org.gcube.portlets.user.td.gwtservice.shared.csv.CSVRowError;
|
|
import org.gcube.portlets.user.td.gwtservice.shared.file.HeaderPresence;
|
|
|
|
import com.allen_sauer.gwt.log.client.Log;
|
|
import com.google.gwt.core.client.GWT;
|
|
import com.google.gwt.event.dom.client.ChangeEvent;
|
|
import com.google.gwt.event.dom.client.ChangeHandler;
|
|
import com.google.gwt.event.logical.shared.SelectionEvent;
|
|
import com.google.gwt.event.logical.shared.SelectionHandler;
|
|
import com.google.gwt.event.logical.shared.ValueChangeEvent;
|
|
import com.google.gwt.event.logical.shared.ValueChangeHandler;
|
|
import com.google.gwt.user.client.Command;
|
|
import com.google.gwt.user.client.rpc.AsyncCallback;
|
|
import com.google.gwt.user.client.ui.HTML;
|
|
import com.google.gwt.user.client.ui.HasValue;
|
|
import com.google.gwt.user.client.ui.HorizontalPanel;
|
|
import com.sencha.gxt.cell.core.client.form.ComboBoxCell.TriggerAction;
|
|
import com.sencha.gxt.core.client.util.Padding;
|
|
import com.sencha.gxt.core.client.util.ToggleGroup;
|
|
import com.sencha.gxt.data.shared.LabelProvider;
|
|
import com.sencha.gxt.data.shared.StringLabelProvider;
|
|
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
|
|
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
|
|
import com.sencha.gxt.widget.core.client.event.SelectEvent;
|
|
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
|
|
import com.sencha.gxt.widget.core.client.form.FieldLabel;
|
|
import com.sencha.gxt.widget.core.client.form.FormPanel;
|
|
import com.sencha.gxt.widget.core.client.form.Radio;
|
|
import com.sencha.gxt.widget.core.client.form.SimpleComboBox;
|
|
import com.sencha.gxt.widget.core.client.form.TextField;
|
|
import com.sencha.gxt.widget.core.client.form.validator.MaxLengthValidator;
|
|
import com.sencha.gxt.widget.core.client.tips.ToolTip;
|
|
import com.sencha.gxt.widget.core.client.tips.ToolTipConfig;
|
|
|
|
/**
|
|
*
|
|
* @author "Giancarlo Panichi" <a
|
|
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
|
|
*
|
|
*/
|
|
public class CSVConfigCard extends WizardCard {
|
|
|
|
private static final String DEFAULT_DELIMETER = ",";
|
|
private static final String DEFAULT_COMMENT = "#";
|
|
protected CSVImportSession csvImportSession;
|
|
|
|
protected SimpleComboBox<String> encodings;
|
|
protected SimpleComboBox<HeaderPresence> header;
|
|
protected TextField customDelimiterField;
|
|
protected Radio otherDelimiter;
|
|
protected Radio commaDelimiter;
|
|
protected Radio spaceDelimiter;
|
|
protected Radio tabDelimiter;
|
|
protected Radio semicoloDelimiter;
|
|
protected TextField commentField;
|
|
protected CsvCheckPanel csvCheckPanel;
|
|
|
|
protected CSVGrid csvSample;
|
|
|
|
public CSVConfigCard(final CSVImportSession csvImportSession) {
|
|
super("CSV configuration", "");
|
|
if (csvImportSession == null) {
|
|
Log.error("CSVImportSession is null");
|
|
}
|
|
this.csvImportSession = csvImportSession;
|
|
|
|
FormPanel panel = createPanel();
|
|
setContent(panel);
|
|
|
|
}
|
|
|
|
protected FormPanel createPanel() {
|
|
FormPanel panel = new FormPanel();
|
|
panel.setLabelWidth(90);
|
|
panel.getElement().setPadding(new Padding(5));
|
|
|
|
VerticalLayoutContainer content = new VerticalLayoutContainer();
|
|
panel.add(content);
|
|
|
|
encodings = new SimpleComboBox<String>(
|
|
new StringLabelProvider<String>());
|
|
encodings.setToolTip("The CSV file encoding");
|
|
encodings.setTabIndex(0);
|
|
encodings.setEditable(false);
|
|
encodings.setForceSelection(true);
|
|
encodings.setTriggerAction(TriggerAction.ALL);
|
|
encodings.addSelectionHandler(new SelectionHandler<String>() {
|
|
|
|
|
|
public void onSelection(SelectionEvent<String> event) {
|
|
updateGrid();
|
|
}
|
|
});
|
|
|
|
content.add(new FieldLabel(encodings, "File encoding"));
|
|
|
|
LabelProvider<HeaderPresence> labelProvider = new LabelProvider<HeaderPresence>() {
|
|
|
|
public String getLabel(HeaderPresence item) {
|
|
return item.getLabel();
|
|
}
|
|
};
|
|
|
|
header = new SimpleComboBox<HeaderPresence>(labelProvider);
|
|
header.setToolTip("The CSV file header");
|
|
header.setTabIndex(0);
|
|
header.setEditable(false);
|
|
header.setForceSelection(true);
|
|
header.setTriggerAction(TriggerAction.ALL);
|
|
for (HeaderPresence headerPresence : HeaderPresence.values())
|
|
header.add(headerPresence);
|
|
header.setValue(HeaderPresence.NONE);
|
|
header.addSelectionHandler(new SelectionHandler<HeaderPresence>() {
|
|
|
|
public void onSelection(SelectionEvent<HeaderPresence> event) {
|
|
updateGrid();
|
|
}
|
|
});
|
|
|
|
content.add(new FieldLabel(header, "Header"));
|
|
|
|
commaDelimiter = new Radio();
|
|
commaDelimiter.setBoxLabel("Comma");
|
|
commaDelimiter.setValue(true);
|
|
|
|
spaceDelimiter = new Radio();
|
|
spaceDelimiter.setBoxLabel("Space");
|
|
|
|
tabDelimiter = new Radio();
|
|
tabDelimiter.setBoxLabel("Tab");
|
|
|
|
semicoloDelimiter = new Radio();
|
|
semicoloDelimiter.setBoxLabel("Semicolon");
|
|
|
|
otherDelimiter = new Radio();
|
|
otherDelimiter.setBoxLabel("Other delimiter");
|
|
|
|
customDelimiterField = new TextField();
|
|
customDelimiterField.setEnabled(false);
|
|
customDelimiterField.setValue(DEFAULT_DELIMETER);
|
|
customDelimiterField.setAllowBlank(false);
|
|
customDelimiterField.setWidth(20);
|
|
|
|
ToggleGroup delimitersGroup = new ToggleGroup();
|
|
delimitersGroup.add(commaDelimiter);
|
|
delimitersGroup.add(spaceDelimiter);
|
|
delimitersGroup.add(tabDelimiter);
|
|
delimitersGroup.add(semicoloDelimiter);
|
|
delimitersGroup.add(otherDelimiter);
|
|
|
|
delimitersGroup
|
|
.addValueChangeHandler(new ValueChangeHandler<HasValue<Boolean>>() {
|
|
|
|
public void onValueChange(
|
|
ValueChangeEvent<HasValue<Boolean>> event) {
|
|
|
|
customDelimiterField.setEnabled(otherDelimiter
|
|
.getValue());
|
|
|
|
if (!otherDelimiter.getValue())
|
|
customDelimiterField.clearInvalid();
|
|
else
|
|
customDelimiterField.validate();
|
|
|
|
if (otherDelimiter.getValue()
|
|
&& !customDelimiterField.isValid())
|
|
return;
|
|
|
|
updateGrid();
|
|
}
|
|
});
|
|
|
|
HorizontalPanel delimitersPanel = new HorizontalPanel();
|
|
delimitersPanel.add(commaDelimiter);
|
|
delimitersPanel.add(spaceDelimiter);
|
|
delimitersPanel.add(tabDelimiter);
|
|
delimitersPanel.add(semicoloDelimiter);
|
|
delimitersPanel.add(otherDelimiter);
|
|
delimitersPanel.add(customDelimiterField);
|
|
|
|
new ToolTip(delimitersPanel, new ToolTipConfig(
|
|
"The delimiter use to delimit the CSV fields"));
|
|
content.add(new FieldLabel(delimitersPanel, "Delimiter"));
|
|
|
|
commentField = new TextField();
|
|
commentField.setToolTip("The character used as comment line prefix");
|
|
commentField.setValue(DEFAULT_COMMENT);
|
|
commentField.setAllowBlank(false);
|
|
commentField.getValidators().add(new MaxLengthValidator(1));
|
|
commentField.setWidth(20);
|
|
commentField.addChangeHandler(new ChangeHandler() {
|
|
|
|
public void onChange(ChangeEvent event) {
|
|
if (commentField.isValid())
|
|
updateGrid();
|
|
}
|
|
});
|
|
|
|
content.add(new FieldLabel(commentField, "Comment"));
|
|
|
|
csvSample = new CSVGrid();
|
|
content.add(csvSample, new VerticalLayoutData(1, -1));
|
|
|
|
content.add(new HTML("<BR>"));
|
|
|
|
csvCheckPanel = new CsvCheckPanel();
|
|
content.add(csvCheckPanel);
|
|
|
|
csvCheckPanel.getCheckConfiguration().addSelectHandler(
|
|
new SelectHandler() {
|
|
|
|
public void onSelect(SelectEvent event) {
|
|
checkConfiguration();
|
|
}
|
|
});
|
|
|
|
csvCheckPanel.getSkipInvalidCheckBox().addValueChangeHandler(
|
|
new ValueChangeHandler<Boolean>() {
|
|
|
|
public void onValueChange(ValueChangeEvent<Boolean> event) {
|
|
boolean skip = csvCheckPanel.getSkipInvalidCheckBox()
|
|
.getValue();
|
|
setEnableNextButton(skip);
|
|
CSVConfigCard.this.csvImportSession
|
|
.setSkipInvalidLines(skip);
|
|
}
|
|
});
|
|
|
|
|
|
return panel;
|
|
}
|
|
|
|
protected void checkConfiguration() {
|
|
csvCheckPanel.setActiveCheckingPanel();
|
|
csvCheckPanel.getCheckConfiguration().setEnabled(false);
|
|
|
|
TDGWTServiceAsync.INSTANCE.checkCSV(100,
|
|
new AsyncCallback<ArrayList<CSVRowError>>() {
|
|
|
|
public void onFailure(Throwable caught) {
|
|
ErrorMessageBox
|
|
.showError(
|
|
"An error occured checking the file",
|
|
"Please retry, if the error perstists change the CSV configuration",
|
|
"");
|
|
}
|
|
|
|
public void onSuccess(ArrayList<CSVRowError> errors) {
|
|
csvCheckPanel.getCheckConfiguration().setEnabled(true);
|
|
|
|
if (errors.size() == 0) {
|
|
setCheckCorrectMessage();
|
|
} else {
|
|
setCheckErrorMessage(errors);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
protected void updateGrid() {
|
|
GWT.log("Started updating GRID");
|
|
|
|
resetCheckMessage();
|
|
|
|
csvSample.mask("Updating...");
|
|
|
|
GWT.log("updating CSV config");
|
|
|
|
HeaderPresence headerPresence = header.getCurrentValue();
|
|
|
|
char delimiter = getSelectedDelimiter();
|
|
|
|
String encoding = encodings.getCurrentValue();
|
|
char commentChar = commentField.getValue().charAt(0);
|
|
|
|
TDGWTServiceAsync.INSTANCE.configureCSVParser(encoding, headerPresence,
|
|
delimiter, commentChar, new AsyncCallback<ArrayList<String>>() {
|
|
|
|
public void onFailure(Throwable caught) {
|
|
Log.error("Failed updating CSV config", caught);
|
|
setEnableNextButton(false);
|
|
ErrorMessageBox
|
|
.showError(
|
|
"An error occured checking the file",
|
|
"Please retry, if the error perstists change the CSV configuration",
|
|
caught.getLocalizedMessage());
|
|
|
|
}
|
|
|
|
public void onSuccess(ArrayList<String> result) {
|
|
Log.info("CSV header getted");
|
|
|
|
csvImportSession.setHeaders(result);
|
|
|
|
for (String name : result)
|
|
Log.info("Column HEADER: " + name);
|
|
|
|
// csvImportSession.getId()
|
|
csvSample.configureColumns(result);
|
|
csvSample.unmask();
|
|
|
|
setEnableNextButton(false);
|
|
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
protected char getSelectedDelimiter() {
|
|
if (otherDelimiter.getValue())
|
|
return customDelimiterField.getValue().charAt(0);
|
|
if (commaDelimiter.getValue())
|
|
return ',';
|
|
if (spaceDelimiter.getValue())
|
|
return ' ';
|
|
if (tabDelimiter.getValue())
|
|
return '\t';
|
|
if (semicoloDelimiter.getValue())
|
|
return ';';
|
|
return DEFAULT_DELIMETER.charAt(0);
|
|
}
|
|
|
|
@Override
|
|
public void setup() {
|
|
setEnableBackButton(false);
|
|
setEnableNextButton(false);
|
|
encodings.focus();
|
|
|
|
TDGWTServiceAsync.INSTANCE
|
|
.getAvailableCharset(new AsyncCallback<AvailableCharsetList>() {
|
|
|
|
public void onSuccess(AvailableCharsetList result) {
|
|
GWT.log("CharsetInfo: "
|
|
+ result.getCharsetList().size()
|
|
+ " charset, default: "
|
|
+ result.getDefaultCharset());
|
|
|
|
for (String charset : result.getCharsetList())
|
|
encodings.add(charset);
|
|
|
|
encodings.setValue(result.getDefaultCharset());
|
|
|
|
updateGrid();
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
public void onFailure(Throwable caught) {
|
|
GWT.log("Error loading charset list", caught);
|
|
showErrorAndHide("Error loading charset list",
|
|
"Error loading charset list", "", caught);
|
|
}
|
|
});
|
|
|
|
resetCheckMessage();
|
|
|
|
Command sayNextCard = new Command() {
|
|
|
|
public void execute() {
|
|
CSVTableDetailCard csvTableDetailCard = new CSVTableDetailCard(
|
|
csvImportSession);
|
|
getWizardWindow()
|
|
.addCard(csvTableDetailCard);
|
|
Log.info("NextCard SDMXTableDetailCard");
|
|
getWizardWindow().nextCard();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
getWizardWindow().setNextButtonCommand(sayNextCard);
|
|
|
|
}
|
|
|
|
protected void resetCheckMessage() {
|
|
csvCheckPanel.setActiveInfoPanel();
|
|
}
|
|
|
|
protected void setCheckErrorMessage(ArrayList<CSVRowError> errors) {
|
|
csvCheckPanel.setActiveFailure(errors);
|
|
setEnableNextButton(false);
|
|
|
|
}
|
|
|
|
protected void setCheckCorrectMessage() {
|
|
csvCheckPanel.setActiveSuccess();
|
|
setEnableNextButton(true);
|
|
}
|
|
|
|
@Override
|
|
public void dispose() {
|
|
csvImportSession
|
|
.setColumnToImportMask(csvSample.getImportColumnsMask());
|
|
}
|
|
|
|
}
|