Skip to content

Commit

Permalink
Excel feature framework operational.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefidel committed Feb 17, 2019
1 parent d4d21a1 commit 9a1a6e2
Show file tree
Hide file tree
Showing 12 changed files with 254 additions and 114 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.ndextools</groupId>
<artifactId>morphcx</artifactId>
<version>2.4.0-Beta-1d</version>
<version>2.4.0-Beta-1e</version>
<packaging>jar</packaging>

<url>http://www.ndextools.org</url>
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/org/ndextools/morphcx/shared/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
* finalizes the runtime configuration used by the application.
*/
public final class Configuration {
public final boolean EXCEL_FEATURE_DISABLED = false; // TODO: 2/7/19 temporary, remove when Excel feature goes alpha
public final boolean EXCEL_FEATURE_DISABLED = true; // TODO: temporary, remove when Excel feature goes alpha
private String[] args;
private boolean isHelp;
private Operation operation;
private boolean inputIsFile;
private boolean outputIsFile;
private Newline newline;
private boolean isServer;
private boolean isDebugMode;
private String inputFilename;
private String outputFilename;
private char delimiter;
Expand Down Expand Up @@ -130,6 +131,13 @@ private CommandLine defineParams() throws ParseException {
.build()
);

getCSVParameterOptions().addOption(
Option.builder(ConfigurationConstants.OPT_DEBUG)
.longOpt(ConfigurationConstants.LONG_OPT_DEBUG)
.desc("A flag used for debugging and development purposes. < -X | --DEBUG >")
.build()
);

// Apache Commons CLI - Step #2 of 3: Parse all command-line parameters
CommandLine parsed;
CommandLineParser parser = new DefaultParser();
Expand Down Expand Up @@ -224,6 +232,10 @@ private final void resolve(CommandLine parsed) {
if (parsed.hasOption(ConfigurationConstants.OPT_SERVER)) {
setIsServer(true);
}

if (parsed.hasOption(ConfigurationConstants.OPT_DEBUG)) {
setIsDebugMode(true);
}
}

private final void preValidationAdjustments() {
Expand Down Expand Up @@ -312,6 +324,10 @@ public void printHelpText() {

void setIsServer(boolean value) { this.isServer = value; }

public boolean isDebugMode() { return this.isDebugMode; }

void setIsDebugMode(boolean value) { this.isDebugMode = value; }

public String getInputFilename() { return this.inputFilename; }

void setInputFilename(String filename) { this.inputFilename = filename; }
Expand Down Expand Up @@ -346,6 +362,9 @@ public static class ConfigurationConstants {

private static final String OPT_SERVER = "S";

private static final String OPT_DEBUG = "X";
private static final String LONG_OPT_DEBUG = "DEBUG";

private static final String CONVERT_CSV = "CSV";
private static final String CONVERT_TSV = "TSV";
private static final String CONVERT_EXCEL = "EXCEL";
Expand Down
31 changes: 0 additions & 31 deletions src/main/java/org/ndextools/morphcx/tables/CXToExcel.java

This file was deleted.

50 changes: 50 additions & 0 deletions src/main/java/org/ndextools/morphcx/tables/ExcelApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.ndextools.morphcx.tables;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.ndexbio.model.cx.NiceCXNetwork;
import org.ndextools.morphcx.shared.Configuration;
import org.ndextools.morphcx.writers.TableToPOI;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

public class ExcelApp implements Table3D {
private final Configuration cfg;
private final NiceCXNetwork niceCX;
private final TableToPOI writer;
private final OutputStream outStream;
private final XSSFWorkbook wb;

public ExcelApp(Configuration cfg,
NiceCXNetwork niceCX,
TableToPOI writer,
XSSFWorkbook wb,
OutputStream outStream) {
this.cfg = cfg;
this.niceCX = niceCX;
this.writer = writer;
this.outStream = outStream;
this.wb = wb;
}

@Override
public void morphThisCX() throws IOException {

Sheet sheet1 = wb.createSheet("Mike 1");
Row row = sheet1.createRow(0);
row.createCell(0).setCellValue("Cell-0");
row.createCell(1).setCellValue("Cell-1");
row.createCell(2).setCellValue("Cell-2");
row.createCell(3).setCellValue("Cell-3");
writer.outputAll();

}

@Override
public List<String> buildColumnHeadings() {
return null; // TODO: 2/7/19 CANT BE NULL!
}
}
107 changes: 53 additions & 54 deletions src/main/java/org/ndextools/morphcx/tables/TablesRoot.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package org.ndextools.morphcx.tables;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

import org.apache.commons.csv.CSVPrinter;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.ndexbio.model.cx.NiceCXNetwork;
import org.ndextools.morphcx.shared.CXReader;
import org.ndextools.morphcx.shared.Configuration;
import org.ndextools.morphcx.shared.Utilities;
import org.ndextools.morphcx.writers.TableToCSV;
import org.ndextools.morphcx.writers.TableToPOI;
import org.ndextools.morphcx.writers.TableWritable;
import org.ndextools.morphcx.writers.WriterFactory;

Expand All @@ -23,85 +20,87 @@
public class TablesRoot {

/**
* Converts the JSON-format NDEx CX Network into a NiceCXNetwork object, then uses a factory
* class to instantiate the class of writer needed by the operation. Finally, control is passed
* to the class which morphs the NiceCXNetwork object to the desired output format.
* Converts the JSON-format NDEx CX Network into a NiceCXNetwork object, Then control
* is passed to the class which morphs the NiceCXNetwork object to the desired output format.
*
* @param cfg reference to a Configuration class object
* @throws Exception base class exception when there is an IO or other processing error
*/
public void execute(final Configuration cfg) throws Exception {

String msg = String.format("%s: expecting MorphCX.appConfig() !=null, actual=%s ",
TablesRoot.class.getSimpleName(), cfg);
Utilities.nullReferenceCheck(cfg, msg);

NiceCXNetwork niceCX = LoadInputCxNetwork(cfg);

switch (cfg.getOperation())
{
case EXCEL:
// TODO: BEGIN *************************************************************
// TODO: Remove feature disabled conditional when features goes alpha
if (cfg.EXCEL_FEATURE_DISABLED) {
System.err.println("The Excel feature is still being developed!");
return;
} else {

try ( Workbook writer = setupPOIOutputWriter(cfg) ) {
Table3D morph = new CXToExcel(cfg, niceCX, writer);
morph.morphThisCX();
} catch (Exception e) {
throw new Exception(e);
}
break;
}
// TODO: END **************************************************************
// Transform input stream to a NiceCXNetwork object.
CXReader cxReader = new CXReader(cfg);
NiceCXNetwork niceCX = cxReader.produceNiceCX();

// Prepare for and run a morphing operation on the NiceCXNetwork object.
switch (cfg.getOperation()) {
case TSV:
case CSV:
case NOT_SPECIFIED:

try ( TableWritable writer = setupCSVOutputWriter(cfg) ) {
Table2D morph = new Webapp( cfg, niceCX, writer);
Table2D morph = new WebApp( cfg, niceCX, writer);
morph.morphThisCX();
} catch (Exception e) {
throw new Exception(e);
}
break;

}
}
case EXCEL:

private static NiceCXNetwork LoadInputCxNetwork(Configuration cfg) throws IOException {
CXReader cxReader = new CXReader(cfg);
return cxReader.produceNiceCX();
// TODO: BEGIN ****************************************************************
// TODO: Remove/comment this code when excel feature is released for testing
if ( (cfg.EXCEL_FEATURE_DISABLED) && (!cfg.isDebugMode()) ) {
System.out.println("The Excel feature is being developed!");
return;
}
// TODO: END ******************************************************************

if (cfg.outputIsFile()) {
setupForPOIAsFile(cfg, niceCX);
} else {
setupForPOIAsStdout(cfg, niceCX);
return;
}
break;

default:
String errMsg = String.format("%s: \'%s\' is not a valid operation",
TablesRoot.class.getSimpleName(), cfg.getOperation());
throw new Exception(errMsg);
}
}

private static TableWritable setupCSVOutputWriter(Configuration cfg) throws Exception {
WriterFactory wf = new WriterFactory(cfg);
return wf.getWriter();
return wf.getCSVWriter();
}

void setupForPOIAsFile(Configuration cfg, NiceCXNetwork niceCX) throws Exception {

private Workbook setupPOIOutputWriter(Configuration cfg) throws Exception {
Workbook workbook;

try
{
try (OutputStream outputStream = new FileOutputStream(cfg.getOutputFilename());
XSSFWorkbook workbook = new XSSFWorkbook() ) {
TableToPOI writer = new TableToPOI(outputStream, workbook);
Table3D morph = new ExcelApp(cfg, niceCX, writer, workbook, outputStream);
morph.morphThisCX();
} catch (Exception e) {
throw new Exception(e);
}
}

// Determine whether the output is to a file or stdout.
if (cfg.outputIsFile()) {
Workbook wb = WorkbookFactory.create(new File(cfg.getOutputFilename()));
return wb;
} else {
Workbook wb = WorkbookFactory.create(new FileInputStream(cfg.getOutputFilename()));
return wb;
}
void setupForPOIAsStdout(Configuration cfg, NiceCXNetwork niceCX) throws Exception {

}
catch (Exception e) {
String msg = this.getClass().getSimpleName() + ": " + e.getMessage();
throw new Exception(msg);
try (OutputStream outputStream = new PrintStream(System.out);
XSSFWorkbook workbook = new XSSFWorkbook() ) {
TableToPOI writer = new TableToPOI(outputStream, workbook);
Table3D morph = new ExcelApp(cfg, niceCX, writer, workbook, outputStream);
morph.morphThisCX();
} catch (Exception e) {
throw new Exception(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import java.util.*;

/**
* The subclass of Table that customizes output according to the NDEx Webapp table schema.
* The subclass of Table that customizes output according to the NDEx WebApp table schema.
*/
public class Webapp extends Table implements Table2D {
public class WebApp extends Table implements Table2D {

// column header labels used in this table, presented in column sequence order
protected final static String LABEL_SOURCE_NODE = "Source Node";
Expand All @@ -38,7 +38,7 @@ public class Webapp extends Table implements Table2D {
* @param niceCX reference to NiceCXNetwork class object
* @param cxWriter reference to output writer class object
*/
public Webapp(Configuration cfg, NiceCXNetwork niceCX, TableWritable cxWriter) {
public WebApp(Configuration cfg, NiceCXNetwork niceCX, TableWritable cxWriter) {
super(cfg, niceCX, cxWriter);
}

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/ndextools/morphcx/writers/POIWritable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.ndextools.morphcx.writers;

public interface POIWritable extends AutoCloseable {

void outputAll() throws Exception;

}
18 changes: 13 additions & 5 deletions src/main/java/org/ndextools/morphcx/writers/TableToCSV.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,19 @@ public void outputRow(final List<String> columns) throws IOException {
*/
@Override
public void close() throws IOException {
if (printer != null) {
printer.flush();
}
if (printer != null) {
printer.close();

try
{
if (printer != null) {
printer.flush();
}

if (printer != null) {
printer.close();
}

} catch (IOException e) {
throw new IOException(e);
}
}

Expand Down
Loading

0 comments on commit 9a1a6e2

Please sign in to comment.