Skip to content

Commit

Permalink
Merge pull request #73 from OHDSI/export
Browse files Browse the repository at this point in the history
Export unapproved mappings
  • Loading branch information
Maxim Moinat authored May 14, 2020
2 parents 5f45b72 + 3edd19a commit bba6bda
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 136 deletions.
25 changes: 9 additions & 16 deletions src/org/ohdsi/usagi/ui/ExportSourceToConceptMapDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class ExportSourceToConceptMapDialog extends JDialog {

private JTextField sourceVocabularyIdField;
private static final long serialVersionUID = -6846083121849826818L;
private boolean exportUnapproved = false;

public ExportSourceToConceptMapDialog() {
setTitle("Export to source_to_concept_map");
Expand Down Expand Up @@ -73,23 +74,11 @@ public ExportSourceToConceptMapDialog() {
buttonPanel.add(Box.createHorizontalGlue());
JButton cancelButton = new JButton("Cancel");
cancelButton.setToolTipText("Cancel the export");
cancelButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
setVisible(false);
}
});
cancelButton.addActionListener(arg0 -> setVisible(false));
buttonPanel.add(cancelButton);
JButton exportButton = new JButton("Export");
exportButton.setToolTipText("Select the filename and export using these settings");
exportButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
export();
}
});
exportButton.addActionListener(arg0 -> export());
buttonPanel.add(exportButton);
add(buttonPanel, g);

Expand All @@ -98,6 +87,10 @@ public void actionPerformed(ActionEvent arg0) {
setLocationRelativeTo(Global.frame);
}

public void setExportUnapproved(boolean exportUnapproved) {
this.exportUnapproved = exportUnapproved;
}

private void export() {
JFileChooser fileChooser = new JFileChooser(Global.folder);
FileFilter csvFilter = new FileNameExtensionFilter("CSV files", "csv");
Expand All @@ -117,7 +110,7 @@ private void export() {
private void writeToCsvFile(String filename) {
WriteCSVFileWithHeader out = new WriteCSVFileWithHeader(filename);
for (CodeMapping mapping : Global.mapping)
if (mapping.mappingStatus == MappingStatus.APPROVED) {
if (exportUnapproved || mapping.mappingStatus == MappingStatus.APPROVED) {
List<Concept> targetConcepts;
if (mapping.targetConcepts.size() == 0) {
targetConcepts = new ArrayList<Concept>(1);
Expand All @@ -132,7 +125,7 @@ private void writeToCsvFile(String filename) {
row.add("source_vocabulary_id", sourceVocabularyIdField.getText());
row.add("source_code_description", mapping.sourceCode.sourceName);
row.add("target_concept_id", targetConcept.conceptId);
row.add("target_vocabulary_id", targetConcept.vocabularyId);
row.add("target_vocabulary_id", targetConcept.conceptId == 0 ? "None" : targetConcept.vocabularyId );
row.add("valid_start_date", "1970-01-01");
row.add("valid_end_date", "2099-12-31");
row.add("invalid_reason", "");
Expand Down
51 changes: 19 additions & 32 deletions src/org/ohdsi/usagi/ui/UsagiMain.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*******************************************************************************
* Copyright 2019 Observational Health Data Sciences and Informatics
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -28,50 +28,28 @@
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.*;

import org.ohdsi.usagi.BerkeleyDbEngine;
import org.ohdsi.usagi.UsagiSearchEngine;
import org.ohdsi.usagi.ui.actions.AboutAction;
import org.ohdsi.usagi.ui.actions.ApplyPreviousMappingAction;
import org.ohdsi.usagi.ui.actions.ApproveAction;
import org.ohdsi.usagi.ui.actions.ApproveAllAction;
import org.ohdsi.usagi.ui.actions.ClearAllAction;
import org.ohdsi.usagi.ui.actions.ConceptInformationAction;
import org.ohdsi.usagi.ui.actions.ExitAction;
import org.ohdsi.usagi.ui.actions.ExportForReviewAction;
import org.ohdsi.usagi.ui.actions.ExportSourceToConceptMapAction;
import org.ohdsi.usagi.ui.actions.ImportAction;
import org.ohdsi.usagi.ui.actions.OpenAction;
import org.ohdsi.usagi.ui.actions.RebuildIndexAction;
import org.ohdsi.usagi.ui.actions.SaveAction;
import org.ohdsi.usagi.ui.actions.SaveAsAction;
import org.ohdsi.usagi.ui.actions.ShowStatsAction;
import org.ohdsi.usagi.ui.actions.*;
import org.ohdsi.utilities.files.ReadTextFile;

/**
* The main application class
*/
public class UsagiMain implements ActionListener {

private JFrame frame;

public static void main(String[] args) {
new UsagiMain(args);
}

public UsagiMain(String[] args) {
frame = new JFrame("Usagi");
JFrame frame = new JFrame("Usagi");

// Initialize global variables:
Global.mapping = new Mapping();
if (args.length != 0)
Global.folder = args[0];
else
Global.folder = new File("").getAbsolutePath();

Global.folder = new File("").getAbsolutePath();
Global.usagiSearchEngine = new UsagiSearchEngine(Global.folder);
Global.dbEngine = new BerkeleyDbEngine(Global.folder);
if (Global.usagiSearchEngine.mainIndexExists()) {
Expand Down Expand Up @@ -107,10 +85,13 @@ public UsagiMain(String[] args) {
Global.clearAllAction.setEnabled(false);
Global.conceptInfoAction.setEnabled(false);

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Global.dbEngine.shutdown();
System.exit(0);
if (UsagiDialogs.askBeforeExit()) {
Global.dbEngine.shutdown();
System.exit(0);
}
}
});
frame.setLayout(new BorderLayout());
Expand Down Expand Up @@ -139,6 +120,12 @@ public void windowClosing(WindowEvent e) {

if (!Global.usagiSearchEngine.mainIndexExists())
Global.rebuildIndexAction.actionPerformed(null);

if (args.length == 1) {
Global.folder = args[0];
} else if (args.length > 1 && args[0].equals("--file")) {
OpenAction.open(new File(args[1]));
}
}

private String loadVocabularyVersion(String folder) {
Expand All @@ -147,7 +134,7 @@ private String loadVocabularyVersion(String folder) {
if (new File(versionFileName).exists()) {
for (String line : new ReadTextFile(versionFileName))
version = line;
}
}
return version;
}

Expand Down
10 changes: 7 additions & 3 deletions src/org/ohdsi/usagi/ui/actions/ExitAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
******************************************************************************/
package org.ohdsi.usagi.ui.actions;

import org.ohdsi.usagi.ui.Global;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.*;

public class ExitAction extends AbstractAction {

Expand All @@ -31,7 +32,10 @@ public ExitAction() {

@Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
if (UsagiDialogs.askBeforeExit()) {
Global.dbEngine.shutdown();
System.exit(0);
}
}

}
100 changes: 53 additions & 47 deletions src/org/ohdsi/usagi/ui/actions/ExportForReviewAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
******************************************************************************/
package org.ohdsi.usagi.ui.actions;

import java.awt.Cursor;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

Expand All @@ -42,59 +40,67 @@ public class ExportForReviewAction extends AbstractAction {
public ExportForReviewAction() {
putValue(Action.NAME, "Export for review");
putValue(Action.SHORT_DESCRIPTION, "Export mapping to a human readable format for reviewing");
putValue(Action.MNEMONIC_KEY, KeyEvent.VK_R);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_R, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
}

@Override
public void actionPerformed(ActionEvent arg0) {
boolean exportUnapproved = UsagiDialogs.askExportUnapprovedMappings();

boolean hasApprovedMappings = false;
for (CodeMapping mapping : Global.mapping)
for (CodeMapping mapping : Global.mapping) {
if (mapping.mappingStatus == MappingStatus.APPROVED) {
hasApprovedMappings = true;
break;
}
if (hasApprovedMappings) {
JFileChooser fileChooser = new JFileChooser(Global.folder);
FileFilter csvFilter = new FileNameExtensionFilter("CSV files", "csv");
fileChooser.setFileFilter(csvFilter);
if (fileChooser.showSaveDialog(Global.frame) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
Global.folder = file.getParentFile().getAbsolutePath();
if (!file.getName().toLowerCase().endsWith(".csv"))
file = new File(file.getAbsolutePath() + ".csv");
Global.frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
WriteCSVFileWithHeader out = new WriteCSVFileWithHeader(file.getAbsolutePath());
for (CodeMapping mapping : Global.mapping)
if (mapping.mappingStatus == MappingStatus.APPROVED) {
List<Concept> targetConcepts;
if (mapping.targetConcepts.size() == 0) {
targetConcepts = new ArrayList<Concept>(1);
targetConcepts.add(Concept.EMPTY_CONCEPT);
} else
targetConcepts = mapping.targetConcepts;
}

if (!exportUnapproved && !hasApprovedMappings) {
UsagiDialogs.warningNothingToExport();
return;
}

JFileChooser fileChooser = new JFileChooser(Global.folder);
FileFilter csvFilter = new FileNameExtensionFilter("CSV files", "csv");
fileChooser.setFileFilter(csvFilter);
if (fileChooser.showSaveDialog(Global.frame) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
Global.folder = file.getParentFile().getAbsolutePath();
if (!file.getName().toLowerCase().endsWith(".csv"))
file = new File(file.getAbsolutePath() + ".csv");
Global.frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
WriteCSVFileWithHeader out = new WriteCSVFileWithHeader(file.getAbsolutePath());
for (CodeMapping mapping : Global.mapping)
if (exportUnapproved || mapping.mappingStatus == MappingStatus.APPROVED) {
List<Concept> targetConcepts;
if (mapping.targetConcepts.size() == 0) {
targetConcepts = new ArrayList<Concept>(1);
targetConcepts.add(Concept.EMPTY_CONCEPT);
} else
targetConcepts = mapping.targetConcepts;

for (Concept targetConcept : targetConcepts) {
Row row = mapping.sourceCode.toRow();
row.add("matchScore", mapping.matchScore);
row.add("targetConceptId", targetConcept.conceptId);
row.add("targetConceptName", targetConcept.conceptName);
row.add("targetVocabularyId", targetConcept.vocabularyId);
row.add("targetDomainId", targetConcept.domainId);
row.add("targetStandardConcept", targetConcept.standardConcept);
row.add("targetChildCount", targetConcept.childCount);
row.add("targetParentCount", targetConcept.parentCount);
row.add("targetConceptClassId", targetConcept.conceptClassId);
row.add("targetConceptCode", targetConcept.conceptCode);
row.add("targetValidStartDate", targetConcept.validStartDate);
row.add("targetValidEndDate", targetConcept.validEndDate);
row.add("targetInvalidReason", targetConcept.invalidReason);
out.write(row);
}
for (Concept targetConcept : targetConcepts) {
Row row = mapping.sourceCode.toRow();
row.add("matchScore", mapping.matchScore);
if (exportUnapproved) row.add("mappingStatus", mapping.mappingStatus.toString());
row.add("targetConceptId", targetConcept.conceptId);
row.add("targetConceptName", targetConcept.conceptName);
row.add("targetVocabularyId", targetConcept.vocabularyId);
row.add("targetDomainId", targetConcept.domainId);
row.add("targetStandardConcept", targetConcept.standardConcept);
row.add("targetChildCount", targetConcept.childCount);
row.add("targetParentCount", targetConcept.parentCount);
row.add("targetConceptClassId", targetConcept.conceptClassId);
row.add("targetConceptCode", targetConcept.conceptCode);
row.add("targetValidStartDate", targetConcept.validStartDate);
row.add("targetValidEndDate", targetConcept.validEndDate);
row.add("targetInvalidReason", targetConcept.invalidReason);
out.write(row);
}
out.close();
Global.frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
} else {
JOptionPane.showMessageDialog(Global.frame, "There are no approved mappings, so nothing to export", "Warning", JOptionPane.WARNING_MESSAGE);
}
out.close();
Global.frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}

Expand Down
26 changes: 16 additions & 10 deletions src/org/ohdsi/usagi/ui/actions/ExportSourceToConceptMapAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
******************************************************************************/
package org.ohdsi.usagi.ui.actions;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JOptionPane;
import javax.swing.*;

import org.ohdsi.usagi.CodeMapping;
import org.ohdsi.usagi.CodeMapping.MappingStatus;
Expand All @@ -34,23 +33,30 @@ public class ExportSourceToConceptMapAction extends AbstractAction {
public ExportSourceToConceptMapAction() {
putValue(Action.NAME, "Export source_to_concept_map");
putValue(Action.SHORT_DESCRIPTION, "Export mapping to source_to_concept_map");
putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_E));
putValue(Action.MNEMONIC_KEY, KeyEvent.VK_E);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_E, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
}

@Override
public void actionPerformed(ActionEvent arg0) {
boolean exportUnapproved = UsagiDialogs.askExportUnapprovedMappings();

boolean hasApprovedMappings = false;
for (CodeMapping mapping : Global.mapping)
for (CodeMapping mapping : Global.mapping) {
if (mapping.mappingStatus == MappingStatus.APPROVED) {
hasApprovedMappings = true;
break;
}
if (hasApprovedMappings) {
ExportSourceToConceptMapDialog dialog = new ExportSourceToConceptMapDialog();
dialog.setVisible(true);
} else {
JOptionPane.showMessageDialog(Global.frame, "There are no approved mappings, so nothing to export", "Warning", JOptionPane.WARNING_MESSAGE);
}

if (!exportUnapproved && !hasApprovedMappings) {
UsagiDialogs.warningNothingToExport();
return;
}

ExportSourceToConceptMapDialog exportDialog = new ExportSourceToConceptMapDialog();
exportDialog.setExportUnapproved(exportUnapproved);
exportDialog.setVisible(true);
}

}
5 changes: 3 additions & 2 deletions src/org/ohdsi/usagi/ui/actions/ImportAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
******************************************************************************/
package org.ohdsi.usagi.ui.actions;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
Expand All @@ -36,8 +37,8 @@ public class ImportAction extends AbstractAction {
public ImportAction() {
putValue(Action.NAME, "Import codes");
putValue(Action.SHORT_DESCRIPTION, "Import a file containing codes");
putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_I));
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_I, ActionEvent.CTRL_MASK));
putValue(Action.MNEMONIC_KEY, KeyEvent.VK_I);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_I, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
}

@Override
Expand Down
Loading

0 comments on commit bba6bda

Please sign in to comment.