diff --git a/src/org/ohdsi/rabbitInAHat/RabbitInAHatMain.java b/src/org/ohdsi/rabbitInAHat/RabbitInAHatMain.java index c4be9ce0..96d658fc 100644 --- a/src/org/ohdsi/rabbitInAHat/RabbitInAHatMain.java +++ b/src/org/ohdsi/rabbitInAHat/RabbitInAHatMain.java @@ -46,6 +46,9 @@ import org.ohdsi.rabbitInAHat.dataModel.Database; import org.ohdsi.rabbitInAHat.dataModel.Database.CDMVersion; import org.ohdsi.rabbitInAHat.dataModel.ETL; +import org.ohdsi.rabbitInAHat.dataModel.Field; +import org.ohdsi.rabbitInAHat.dataModel.MappableItem; +import org.ohdsi.rabbitInAHat.dataModel.Table; import org.ohdsi.whiteRabbit.ObjectExchange; /** @@ -77,6 +80,8 @@ public class RabbitInAHatMain implements ResizeListener, ActionListener { private final static FileFilter FILE_FILTER_DOCX = new FileNameExtensionFilter("Microsoft Word documents (*.docx)", "docx"); private final static FileFilter FILE_FILTER_CSV = new FileNameExtensionFilter("Text Files (*.csv)", "csv"); private final static FileFilter FILE_FILTER_R = new FileNameExtensionFilter("R script (*.r)", "r"); + private final static FileFilter FILE_FILTER_XLSX = new FileNameExtensionFilter("XLSX files (*.xlsx)", "xlsx"); + private JFrame frame; private JScrollPane scrollPane1; private JScrollPane scrollPane2; @@ -329,6 +334,7 @@ private String choosePath(boolean saveMode, FileFilter... filter) { if (chooser == null) { chooser = new JFileChooser(); } + chooser.resetChoosableFileFilters(); chooser.setFileFilter(filter[0]); for (int i = 1; i < filter.length; i++) chooser.addChoosableFileFilter(filter[i]); @@ -352,10 +358,6 @@ private String chooseOpenPath(FileFilter... fileFilter) { return choosePath(false, fileFilter); } - private String chooseOpenPath() { - return chooseOpenPath(null); - } - @Override public void actionPerformed(ActionEvent event) { switch (event.getActionCommand()) { @@ -370,7 +372,7 @@ public void actionPerformed(ActionEvent event) { doOpenSpecs(chooseOpenPath(FILE_FILTER_GZ, FILE_FILTER_JSON)); break; case ACTION_CMD_OPEN_SCAN_REPORT: - doOpenScanReport(chooseOpenPath()); + doOpenScanReport(chooseOpenPath(FILE_FILTER_XLSX)); break; case ACTION_CMD_GENERATE_ETL_DOCUMENT: doGenerateEtlDoc(chooseSavePath(FILE_FILTER_DOCX)); @@ -521,20 +523,59 @@ private void doOpenSpecs(String filename) { private void doOpenScanReport(String filename) { if (filename != null) { + boolean replace = true; + if (ObjectExchange.etl.getSourceDatabase().getTables().size() != 0){ + Object[] options = {"Replace current data", + "Load data on field values only"}; + int result = JOptionPane.showOptionDialog(frame, "You already have source data loaded. Do you want to", "Replace source data?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); + if (result == -1) + return; + if (result == 1) + replace = false; + } frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); - ETL etl = new ETL(); - try { - etl.setSourceDatabase(Database.generateModelFromScanReport(filename)); - etl.setTargetDatabase(ObjectExchange.etl.getTargetDatabase()); - tableMappingPanel.setMapping(etl.getTableToTableMapping()); - ObjectExchange.etl = etl; - } catch (Exception e) { - e.printStackTrace(); - JOptionPane.showMessageDialog(null, "Invalid File Format", "Error", JOptionPane.ERROR_MESSAGE); + if (replace) { + ETL etl = new ETL(); + try { + etl.setSourceDatabase(Database.generateModelFromScanReport(filename)); + etl.setTargetDatabase(ObjectExchange.etl.getTargetDatabase()); + tableMappingPanel.setMapping(etl.getTableToTableMapping()); + ObjectExchange.etl = etl; + } catch (Exception e) { + e.printStackTrace(); + JOptionPane.showMessageDialog(null, "Invalid File Format", "Error", JOptionPane.ERROR_MESSAGE); + } + } else { + try { + Database newData = Database.generateModelFromScanReport(filename); + Database oldData = ObjectExchange.etl.getSourceDatabase(); + for (Table newTable : newData.getTables()) { + Table oldTable = (Table)findByName(newTable.getName(), oldData.getTables()); + if (oldTable != null) { + for (Field newField : newTable.getFields()) { + Field oldField = (Field)findByName(newField.getName(), oldTable.getFields()); + if (oldField != null) { + oldField.setValueCounts(newField.getValueCounts()); + } + } + } + } + } catch (Exception e) { + e.printStackTrace(); + JOptionPane.showMessageDialog(null, "Invalid File Format", "Error", JOptionPane.ERROR_MESSAGE); + } + } frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); } } + + private MappableItem findByName(String name, List list) { + for (MappableItem item : list) + if (item.getName().toLowerCase().equals(name.toLowerCase())) + return item; + return null; + } private void doGenerateEtlDoc(String filename) { if (filename != null) {