diff --git a/src/org/ohdsi/rabbitInAHat/RabbitInAHatMain.java b/src/org/ohdsi/rabbitInAHat/RabbitInAHatMain.java index 821e2f5a..c4be9ce0 100644 --- a/src/org/ohdsi/rabbitInAHat/RabbitInAHatMain.java +++ b/src/org/ohdsi/rabbitInAHat/RabbitInAHatMain.java @@ -73,6 +73,7 @@ public class RabbitInAHatMain implements ResizeListener, ActionListener { public final static String WIKI_URL = "http://www.ohdsi.org/web/wiki/doku.php?id=documentation:software:whiterabbit#rabbit-in-a-hat"; private final static FileFilter FILE_FILTER_GZ = new FileNameExtensionFilter("GZIP Files (*.gz)", "gz"); + private final static FileFilter FILE_FILTER_JSON = new FileNameExtensionFilter("JSON Files (*.json)", "json"); 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"); @@ -243,7 +244,7 @@ private JMenuBar createMenuBar() { targetCDMV5.addActionListener(this); targetCDMV5.setActionCommand(ACTION_CMD_SET_TARGET_V5); setTarget.add(targetCDMV5); - + JMenuItem targetCDMV501 = new JMenuItem(ACTION_CMD_SET_TARGET_V501); targetCDMV501.addActionListener(this); targetCDMV501.setActionCommand(ACTION_CMD_SET_TARGET_V501); @@ -322,13 +323,15 @@ public void notifyResized(int height, boolean minimized, boolean maximized) { * restrict files displayed * @return if file selected, absolute path of selected file otherwise null */ - private String choosePath(boolean saveMode, FileFilter filter) { + private String choosePath(boolean saveMode, FileFilter... filter) { String result = null; if (chooser == null) { chooser = new JFileChooser(); } - chooser.setFileFilter(filter); + chooser.setFileFilter(filter[0]); + for (int i = 1; i < filter.length; i++) + chooser.addChoosableFileFilter(filter[i]); int dialogResult = saveMode ? chooser.showSaveDialog(frame) : chooser.showOpenDialog(frame); if (dialogResult == JFileChooser.APPROVE_OPTION) @@ -336,17 +339,16 @@ private String choosePath(boolean saveMode, FileFilter filter) { return result; } - private String chooseSavePath(FileFilter fileFilter) { + private String chooseSavePath(FileFilter... fileFilter) { String path = choosePath(true, fileFilter); - if (path != null && fileFilter == FILE_FILTER_GZ && !path.toLowerCase().endsWith(".json.gz")) + if (path != null && fileFilter[0] == FILE_FILTER_GZ && !path.toLowerCase().endsWith(".json.gz") && !path.toLowerCase().endsWith(".json")) path += ".json.gz"; - if (path != null && fileFilter == FILE_FILTER_DOCX && !path.toLowerCase().endsWith(".docx")) + if (path != null && fileFilter[0] == FILE_FILTER_DOCX && !path.toLowerCase().endsWith(".docx")) path += ".docx"; - return path; } - private String chooseOpenPath(FileFilter fileFilter) { + private String chooseOpenPath(FileFilter... fileFilter) { return choosePath(false, fileFilter); } @@ -362,10 +364,10 @@ public void actionPerformed(ActionEvent event) { doSave((filename == null || !filename.toLowerCase().endsWith(".json.gz")) ? chooseSavePath(FILE_FILTER_GZ) : filename); break; case ACTION_CMD_SAVE_AS: - doSave(chooseSavePath(FILE_FILTER_GZ)); + doSave(chooseSavePath(FILE_FILTER_GZ, FILE_FILTER_JSON)); break; case ACTION_CMD_OPEN_ETL_SPECS: - doOpenSpecs(chooseOpenPath(FILE_FILTER_GZ)); + doOpenSpecs(chooseOpenPath(FILE_FILTER_GZ, FILE_FILTER_JSON)); break; case ACTION_CMD_OPEN_SCAN_REPORT: doOpenScanReport(chooseOpenPath()); @@ -494,7 +496,8 @@ private void doDiscardCounts() { private void doSave(String filename) { if (filename != null) { frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); - ETL.FileFormat fileFormat = filename.endsWith("json.gz") ? ETL.FileFormat.Json : ETL.FileFormat.Binary; + ETL.FileFormat fileFormat = filename.endsWith("json.gz") ? ETL.FileFormat.GzipJson : filename.endsWith("json") ? ETL.FileFormat.Json + : ETL.FileFormat.Binary; ObjectExchange.etl.save(filename, fileFormat); frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); } @@ -503,7 +506,8 @@ private void doSave(String filename) { private void doOpenSpecs(String filename) { if (filename != null) { frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); - ETL.FileFormat fileFormat = filename.endsWith(".json.gz") ? ETL.FileFormat.Json : ETL.FileFormat.Binary; + ETL.FileFormat fileFormat = filename.endsWith(".json.gz") ? ETL.FileFormat.GzipJson : filename.endsWith(".json") ? ETL.FileFormat.Json + : ETL.FileFormat.Binary; try { ObjectExchange.etl = ETL.fromFile(filename, fileFormat); tableMappingPanel.setMapping(ObjectExchange.etl.getTableToTableMapping()); diff --git a/src/org/ohdsi/rabbitInAHat/dataModel/ETL.java b/src/org/ohdsi/rabbitInAHat/dataModel/ETL.java index 96ca46d1..72ae7d6f 100644 --- a/src/org/ohdsi/rabbitInAHat/dataModel/ETL.java +++ b/src/org/ohdsi/rabbitInAHat/dataModel/ETL.java @@ -36,59 +36,59 @@ public class ETL implements Serializable { public enum FileFormat { - Binary, Json + Binary, Json, GzipJson } private Database sourceDb = new Database(); - private Database cdmDb = new Database(); + private Database cdmDb = new Database(); private List tableToTableMaps = new ArrayList(); private Map> tableMapToFieldToFieldMaps = new HashMap>(); private transient String filename = null; private static final long serialVersionUID = 8987388381751618498L; - public ETL(){ + public ETL() { } - - public ETL(Database sourceDB, Database targetDb){ + + public ETL(Database sourceDB, Database targetDb) { this.setSourceDatabase(sourceDB); this.setTargetDatabase(targetDb); } - - public void copyETLMappings(ETL etl){ + + public void copyETLMappings(ETL etl) { Mapping oldTableMapping = etl.getTableToTableMapping(); - Mapping
newTableMapping = this.getTableToTableMapping(); - - for(Table sourceTable : sourceDb.getTables()){ - for(Table targetTable : cdmDb.getTables()){ - + Mapping
newTableMapping = this.getTableToTableMapping(); + + for (Table sourceTable : sourceDb.getTables()) { + for (Table targetTable : cdmDb.getTables()) { + ItemToItemMap copyMapping = oldTableMapping.getSourceToTargetMapByName(sourceTable, targetTable); - - if( copyMapping != null ){ + + if (copyMapping != null) { copyMapping.setSourceItem(sourceTable); copyMapping.setTargetItem(targetTable); - + newTableMapping.addSourceToTargetMap(copyMapping); - + Mapping oldFieldMapping = etl.getFieldToFieldMapping(sourceTable, targetTable); Mapping newFieldMapping = this.getFieldToFieldMapping(sourceTable, targetTable); - - for(Field sourceField : sourceTable.getFields()){ - for(Field targetField : targetTable.getFields()){ + + for (Field sourceField : sourceTable.getFields()) { + for (Field targetField : targetTable.getFields()) { copyMapping = oldFieldMapping.getSourceToTargetMapByName(sourceField, targetField); - - if(copyMapping != null){ + + if (copyMapping != null) { copyMapping.setSourceItem(sourceField); copyMapping.setTargetItem(targetField); - + newFieldMapping.addSourceToTargetMap(copyMapping); } } } - } + } } } } - + public void saveCurrentState() { } @@ -125,7 +125,6 @@ public void setTargetDatabase(Database targetDb) { public void setSourceDatabase(Database sourceDb) { this.sourceDb = sourceDb; } - public Database getTargetDatabase() { return cdmDb; @@ -149,7 +148,7 @@ public void save(String filename, FileFormat format) { out.writeObject(this); } break; - case Json: + case GzipJson: String json = toJson(); try (FileOutputStream fileOutputStream = new FileOutputStream(filename); GZIPOutputStream gzipOutputStream = new GZIPOutputStream(fileOutputStream); @@ -157,6 +156,13 @@ public void save(String filename, FileFormat format) { out.write(json); } break; + case Json: + String json2 = toJson(); + try (FileOutputStream fileOutputStream = new FileOutputStream(filename); + OutputStreamWriter out = new OutputStreamWriter(fileOutputStream, "UTF-8")) { + out.write(json2); + } + break; } this.filename = filename; } catch (IOException e) { @@ -184,13 +190,18 @@ public static ETL fromFile(String filename, FileFormat format) { result = (ETL) objectInputStream.readObject(); } break; - case Json: + case GzipJson: try (FileInputStream fileInputStream = new FileInputStream(filename); GZIPInputStream gzipInputStream = new GZIPInputStream(fileInputStream); JsonReader jr = new JsonReader(gzipInputStream)) { result = (ETL) jr.readObject(); } break; + case Json: + try (FileInputStream fileInputStream = new FileInputStream(filename); JsonReader jr = new JsonReader(fileInputStream)) { + result = (ETL) jr.readObject(); + } + break; } result.filename = filename; } catch (Exception exception) {