Skip to content

Commit

Permalink
Merge pull request #31 from mav7014/opt_counts
Browse files Browse the repository at this point in the history
Add option to exclude counts from RabbitInAHat output
  • Loading branch information
schuemie committed Apr 8, 2015
2 parents ff14574 + e60e4ee commit e3aa1d6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
10 changes: 7 additions & 3 deletions src/org/ohdsi/rabbitInAHat/ETLDocumentGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ public static void main(String[] args) throws IOException, ClassNotFoundExceptio
generate(etl, "C:/Users/mschuemi/Desktop/test.docx");
}

public static void generate(ETL etl, String filename) {
public static void generate(ETL etl, String filename, boolean includeCounts) {
try {
CustomXWPFDocument document = new CustomXWPFDocument();

addTableLevelSection(document, etl);

for (Table cdmTable : etl.getCDMDatabase().getTables())
addCDMTableSection(document, etl, cdmTable);
addSourceTablesAppendix(document, etl);

if (includeCounts) addSourceTablesAppendix(document, etl);

document.write(new FileOutputStream(new File(filename)));
} catch (FileNotFoundException e) {
Expand All @@ -72,6 +72,10 @@ public static void generate(ETL etl, String filename) {
e.printStackTrace();
}
}

public static void generate(ETL etl, String filename) {
generate(etl, filename, true);
}

private static void addSourceTablesAppendix(CustomXWPFDocument document, ETL etl) {
XWPFParagraph paragraph = document.createParagraph();
Expand Down
15 changes: 11 additions & 4 deletions src/org/ohdsi/rabbitInAHat/RabbitInAHatMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,19 @@ public class RabbitInAHatMain implements ResizeListener, ActionListener {
private JScrollPane scrollPane2;
private MappingPanel tableMappingPanel;
private JSplitPane tableFieldSplitPane;
private boolean includeCounts;

public static void main(String[] args) {
new RabbitInAHatMain();
boolean includeCounts = true;
for (String arg: args) {
if (arg.equals("--no-counts")) includeCounts = false;
}
new RabbitInAHatMain(includeCounts);
}

public RabbitInAHatMain() {
public RabbitInAHatMain(boolean includeCounts) {
this.includeCounts = includeCounts;

frame = new JFrame("Rabbit in a hat");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Expand Down Expand Up @@ -247,7 +254,7 @@ private void doSave(String filename) {
if (filename != null) {
frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
ETL.FileFormat fileFormat = filename.endsWith("json") ? ETL.FileFormat.Json : ETL.FileFormat.Binary;
ObjectExchange.etl.save(filename, fileFormat);
ObjectExchange.etl.save(filename, fileFormat, includeCounts);
frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
Expand Down Expand Up @@ -281,7 +288,7 @@ private void doOpenScanReport(String filename) {
private void doGenerateEtlDoc(String filename) {
if (filename != null) {
frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
ETLDocumentGenerator.generate(ObjectExchange.etl, filename);
ETLDocumentGenerator.generate(ObjectExchange.etl, filename, includeCounts);
frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
Expand Down
26 changes: 23 additions & 3 deletions src/org/ohdsi/rabbitInAHat/dataModel/ETL.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.cedarsoftware.util.io.JsonReader;
import com.cedarsoftware.util.io.JsonWriter;
import com.cedarsoftware.util.io.MetaUtils;

import java.io.*;
import java.nio.file.Files;
Expand Down Expand Up @@ -95,13 +96,32 @@ public ItemToItemMap createItemToItemMap(MappableItem source, MappableItem targe

/**
* Convert into pretty-print JSON
* @param includeCounts if false, exclude the valueCounts field from result
* @return JSON representation of object
*/
public String toJson(boolean includeCounts) {
Map<String, Object> args = new HashMap<>();
if (!includeCounts) {
Map<Class, List<String>> fieldSpecifier = new HashMap<>();
ArrayList<java.lang.reflect.Field> fieldFields =
new ArrayList<>(MetaUtils.getDeepDeclaredFields(Field.class).values());
ArrayList<String> fieldNames = new ArrayList<>();
for (java.lang.reflect.Field field : fieldFields) {
String fieldName = field.getName();
if (Objects.equals(fieldName, "valueCounts")) continue;
fieldNames.add(fieldName);
}
fieldSpecifier.put(Field.class, fieldNames);
args.put(JsonWriter.FIELD_SPECIFIERS, fieldSpecifier);
}
return JsonWriter.formatJson(JsonWriter.objectToJson(this, args));
}

public String toJson() {
return JsonWriter.formatJson(JsonWriter.objectToJson(this));
return toJson(true);
}

public void save(String filename, FileFormat format) {
public void save(String filename, FileFormat format, boolean includeCounts) {
try {
switch (format) {
case Binary:
Expand All @@ -113,7 +133,7 @@ public void save(String filename, FileFormat format) {
}
break;
case Json:
String json = toJson();
String json = toJson(includeCounts);
Files.write(Paths.get(filename), json.getBytes("utf-8"));
break;
}
Expand Down

0 comments on commit e3aa1d6

Please sign in to comment.