diff --git a/modules/DataLaboratoryAPI/src/main/java/org/gephi/datalab/impl/AttributeColumnsControllerImpl.java b/modules/DataLaboratoryAPI/src/main/java/org/gephi/datalab/impl/AttributeColumnsControllerImpl.java index 7014c84a20a87804e774425a51320ba545b19283..5e0c6029f1779e2b6a6c22de51fac5f74d6926cf 100644 --- a/modules/DataLaboratoryAPI/src/main/java/org/gephi/datalab/impl/AttributeColumnsControllerImpl.java +++ b/modules/DataLaboratoryAPI/src/main/java/org/gephi/datalab/impl/AttributeColumnsControllerImpl.java @@ -685,7 +685,7 @@ public class AttributeColumnsControllerImpl implements AttributeColumnsControlle //Create edges: GraphElementsController gec = Lookup.getDefault().lookup(GraphElementsController.class); Graph graph = Lookup.getDefault().lookup(GraphController.class).getGraphModel().getGraph(); - String id = null; + String id; Edge edge; String sourceId, targetId; Node source, target; @@ -694,11 +694,15 @@ public class AttributeColumnsControllerImpl implements AttributeColumnsControlle reader = new CsvReader(new FileInputStream(file), separator, charset); reader.setTrimWhitespace(false); reader.readHeaders(); + + int recordNumber = 0; while (reader.readRecord()) { + recordNumber++; sourceId = reader.get(sourceColumn); targetId = reader.get(targetColumn); - if (sourceId == null || sourceId.isEmpty() || targetId == null || targetId.isEmpty()) { + if (sourceId == null || sourceId.trim().isEmpty() || targetId == null || targetId.trim().isEmpty()) { + Logger.getLogger("").log(Level.WARNING, "Ignoring record {0} due to empty source and/or target node ids", recordNumber); continue;//No correct source and target ids were provided, ignore row } @@ -791,9 +795,9 @@ public class AttributeColumnsControllerImpl implements AttributeColumnsControlle } } } catch (FileNotFoundException ex) { - Exceptions.printStackTrace(ex); + Logger.getLogger("").log(Level.SEVERE, null, ex); } catch (IOException ex) { - Exceptions.printStackTrace(ex); + Logger.getLogger("").log(Level.SEVERE, null, ex); } finally { if(reader != null){ reader.close(); diff --git a/modules/DataLaboratoryPlugin/src/main/java/org/gephi/datalab/plugin/manipulators/general/ui/ImportCSVUIVisualPanel1.java b/modules/DataLaboratoryPlugin/src/main/java/org/gephi/datalab/plugin/manipulators/general/ui/ImportCSVUIVisualPanel1.java index f030633d293480e59ffe777c0331e73f9228142f..e4d499e009fdd4df8d7cf060ebd5523a7a2366b0 100644 --- a/modules/DataLaboratoryPlugin/src/main/java/org/gephi/datalab/plugin/manipulators/general/ui/ImportCSVUIVisualPanel1.java +++ b/modules/DataLaboratoryPlugin/src/main/java/org/gephi/datalab/plugin/manipulators/general/ui/ImportCSVUIVisualPanel1.java @@ -50,6 +50,7 @@ import java.lang.reflect.InvocationTargetException; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.Set; import javax.swing.JFileChooser; import javax.swing.JOptionPane; @@ -81,7 +82,8 @@ public class ImportCSVUIVisualPanel1 extends javax.swing.JPanel { private int columnCount = 0; private boolean hasSourceNodeColumn = false; private boolean hasTargetNodeColumn = false; - private boolean columnNamesRepeated = false; + private boolean hasColumnNamesRepeated = false; + private boolean hasRowsMissingSourcesOrTargets = false; private ValidationPanel validationPanel; /** Creates new form ImportCSVUIVisualPanel1 */ @@ -143,7 +145,7 @@ public class ImportCSVUIVisualPanel1 extends javax.swing.JPanel { prblms.add(getMessage("ImportCSVUIVisualPanel1.validation.no-columns")); return false; } - if (columnNamesRepeated) { + if (hasColumnNamesRepeated()) { prblms.add(getMessage("ImportCSVUIVisualPanel1.validation.repeated-columns")); return false; } @@ -151,6 +153,12 @@ public class ImportCSVUIVisualPanel1 extends javax.swing.JPanel { prblms.add(getMessage("ImportCSVUIVisualPanel1.validation.edges.no-source-target-columns")); return false; } + if (hasRowsMissingSourcesOrTargets()) { + prblms.add(NbBundle.getMessage(ImportCSVUIVisualPanel1.class, + "ImportCSVUIVisualPanel1.validation.edges.empty-sources-or-targets" + )); + return false; + } return true; } }); @@ -182,32 +190,55 @@ public class ImportCSVUIVisualPanel1 extends javax.swing.JPanel { //Check for repeated column names: Set columnNamesSet = new HashSet(); - columnNamesRepeated = false; + hasColumnNamesRepeated = false; hasSourceNodeColumn = false; hasTargetNodeColumn = false; + int sourceColumnIndex = 0, + targetColumnIndex = 0, + currentColumn = 0; for (String header : headers) { if (header.equalsIgnoreCase("source")) { hasSourceNodeColumn = true; + sourceColumnIndex = currentColumn; } if (header.equalsIgnoreCase("target")) { hasTargetNodeColumn = true; + targetColumnIndex = currentColumn; } if (columnNamesSet.contains(header)) { - columnNamesRepeated = true; + hasColumnNamesRepeated = true; break; } columnNamesSet.add(header); + currentColumn++; } ArrayList records = new ArrayList(); + hasRowsMissingSourcesOrTargets = false; + ImportCSVUIWizardAction.Mode mode = getMode(); if (columnCount > 0) { String[] currentRecord; - while (reader.readRecord() && records.size() < MAX_ROWS_PREVIEW) { - currentRecord = new String[reader.getColumnCount()]; + + while (reader.readRecord()) { + int recordColumnCount = reader.getColumnCount(); + currentRecord = new String[recordColumnCount]; for (int i = 0; i < currentRecord.length; i++) { currentRecord[i] = reader.get(i); } - records.add(currentRecord); + + // Search for missing source or target columns for edges table + if(mode == ImportCSVUIWizardAction.Mode.EDGES_TABLE){ + if (recordColumnCount < sourceColumnIndex + || currentRecord[sourceColumnIndex].trim().isEmpty() + || recordColumnCount < targetColumnIndex + || currentRecord[targetColumnIndex].trim().isEmpty()) { + hasRowsMissingSourcesOrTargets = true; + } + } + + if (records.size() < MAX_ROWS_PREVIEW) { + records.add(currentRecord); + } } } reader.close(); @@ -265,7 +296,7 @@ public class ImportCSVUIVisualPanel1 extends javax.swing.JPanel { Exceptions.printStackTrace(ex); } catch (IOException ex) { JOptionPane.showMessageDialog(this, getMessage("ImportCSVUIVisualPanel1.validation.error"), getMessage("ImportCSVUIVisualPanel1.validation.file-permissions-error"), JOptionPane.ERROR_MESSAGE); - } + } } wizard1.fireChangeEvent(); pathTextField.setText(pathTextField.getText());//To fire validation panel messages. @@ -308,8 +339,8 @@ public class ImportCSVUIVisualPanel1 extends javax.swing.JPanel { return columnCount; } - public boolean isColumnNamesRepeated() { - return columnNamesRepeated; + public boolean hasColumnNamesRepeated() { + return hasColumnNamesRepeated; } public boolean isValidFile() { @@ -332,7 +363,11 @@ public class ImportCSVUIVisualPanel1 extends javax.swing.JPanel { } public boolean isCSVValid() { - return isValidFile() && hasColumns() && !columnNamesRepeated && areValidColumnsForTable(); + return isValidFile() && hasColumns() && !hasColumnNamesRepeated && areValidColumnsForTable() &&!hasRowsMissingSourcesOrTargets(); + } + + public boolean hasRowsMissingSourcesOrTargets() { + return hasRowsMissingSourcesOrTargets; } class SeparatorWrapper { diff --git a/modules/DataLaboratoryPlugin/src/main/resources/org/gephi/datalab/plugin/manipulators/general/ui/Bundle.properties b/modules/DataLaboratoryPlugin/src/main/resources/org/gephi/datalab/plugin/manipulators/general/ui/Bundle.properties index dfb48260f5c6cd9c1a12090f5376413b311b4d0c..e0448446b1c8f6c057a35372f2947c4885c69581 100644 --- a/modules/DataLaboratoryPlugin/src/main/resources/org/gephi/datalab/plugin/manipulators/general/ui/Bundle.properties +++ b/modules/DataLaboratoryPlugin/src/main/resources/org/gephi/datalab/plugin/manipulators/general/ui/Bundle.properties @@ -50,6 +50,7 @@ ImportCSVUIVisualPanel1.validation.repeated-columns:The file can't have repeated ImportCSVUIVisualPanel1.validation.error=Error ImportCSVUIVisualPanel1.validation.file-permissions-error=An error happened when reading the file. Make sure the file is not in use and you have permissions. ImportCSVUIVisualPanel1.validation.edges.no-source-target-columns=Edges table needs a 'Source' and 'Target' column with nodes ids. +ImportCSVUIVisualPanel1.validation.edges.empty-sources-or-targets=Found row(s) with empty Source and/or Target columns ImportCSVUIVisualPanel2.name=Import settings ImportCSVUIVisualPanel2.columnsLabel.text=Imported columns: ImportCSVUIVisualPanel2.nodes.description=New columns are created with the specified type.
A generated id is assigned if missing.
Unless the option 'Force nodes to be created as new ones' is enabled, already existing nodes will be updated.