Skip to content

Commit

Permalink
Allowing users to save ETL specs as either .json.gz or plain .json, so
Browse files Browse the repository at this point in the history
ETL specs can work with version control.
  • Loading branch information
schuemie committed Jul 19, 2016
1 parent a2e07d3 commit 1f28066
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 39 deletions.
28 changes: 16 additions & 12 deletions src/org/ohdsi/rabbitInAHat/RabbitInAHatMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -322,31 +323,32 @@ 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)
result = chooser.getSelectedFile().getAbsolutePath();
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);
}

Expand All @@ -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());
Expand Down Expand Up @@ -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));
}
Expand All @@ -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());
Expand Down
65 changes: 38 additions & 27 deletions src/org/ohdsi/rabbitInAHat/dataModel/ETL.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ItemToItemMap> tableToTableMaps = new ArrayList<ItemToItemMap>();
private Map<ItemToItemMap, List<ItemToItemMap>> tableMapToFieldToFieldMaps = new HashMap<ItemToItemMap, List<ItemToItemMap>>();
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<Table> oldTableMapping = etl.getTableToTableMapping();
Mapping<Table> newTableMapping = this.getTableToTableMapping();
for(Table sourceTable : sourceDb.getTables()){
for(Table targetTable : cdmDb.getTables()){
Mapping<Table> 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<Field> oldFieldMapping = etl.getFieldToFieldMapping(sourceTable, targetTable);
Mapping<Field> 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() {

}
Expand Down Expand Up @@ -125,7 +125,6 @@ public void setTargetDatabase(Database targetDb) {
public void setSourceDatabase(Database sourceDb) {
this.sourceDb = sourceDb;
}


public Database getTargetDatabase() {
return cdmDb;
Expand All @@ -149,14 +148,21 @@ 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);
OutputStreamWriter out = new OutputStreamWriter(gzipOutputStream, "UTF-8")) {
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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1f28066

Please sign in to comment.