Skip to content

Commit

Permalink
This closes #85. -- automatically load digipres defaults for csv output.
Browse files Browse the repository at this point in the history
  • Loading branch information
tballison committed Nov 29, 2022
1 parent 47edbb0 commit ee25dff
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 57 deletions.
5 changes: 5 additions & 0 deletions tika-gui-app/assemblies/assembly-linux_x64.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
</fileSet>
</fileSets>
<files>
<file>
<source>${project.basedir}/examples/example-digipres-metadata-mappings-jdbc.csv</source>
<destName>default-metadata-mappings.csv</destName>
<outputDirectory>config</outputDirectory>
</file>
<file>
<source>${project.basedir}/target/jres/linux_x64/zulu17.38.21-ca-fx-jre17.0.5-linux_x64.tar.gz</source>
<outputDirectory>jre</outputDirectory>
Expand Down
5 changes: 5 additions & 0 deletions tika-gui-app/assemblies/assembly-macosx_aarch64.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
</fileSet>
</fileSets>
<files>
<file>
<source>${project.basedir}/examples/example-digipres-metadata-mappings-jdbc.csv</source>
<destName>default-metadata-mappings.csv</destName>
<outputDirectory>config</outputDirectory>
</file>
<file>
<source>${project.basedir}/target/jres/macosx_aarch64/zulu17.38.21-ca-fx-jre17.0.5-macosx_aarch64.zip</source>
<outputDirectory>jre</outputDirectory>
Expand Down
5 changes: 5 additions & 0 deletions tika-gui-app/assemblies/assembly-macosx_x64.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
</fileSet>
</fileSets>
<files>
<file>
<source>${project.basedir}/examples/example-digipres-metadata-mappings-jdbc.csv</source>
<destName>default-metadata-mappings.csv</destName>
<outputDirectory>config</outputDirectory>
</file>
<file>
<source>${project.basedir}/target/jres/macosx_x64/zulu17.38.21-ca-fx-jre17.0.5-macosx_x64.zip</source>
<outputDirectory>jre</outputDirectory>
Expand Down
5 changes: 5 additions & 0 deletions tika-gui-app/assemblies/assembly-win_x64.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
</fileSet>
</fileSets>
<files>
<file>
<source>${project.basedir}/examples/example-digipres-metadata-mappings-jdbc.csv</source>
<destName>default-metadata-mappings.csv</destName>
<outputDirectory>config</outputDirectory>
</file>
<file>
<source>${project.basedir}/bin/tika-gui.bat</source>
<outputDirectory></outputDirectory>
Expand Down
40 changes: 0 additions & 40 deletions tika-gui-app/examples/example-digipres-metadata-mappings-csv.csv

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ pdf:producer,pdf_producer,VARCHAR(512)
pdfa:PDFVersion,pdfa_version,VARCHAR(32)
pdfaid:conformance,pdfaid_conformance,VARCHAR(12)
pdfx:conformance,pdfx_conformance,VARCHAR(12)
xmp:CreatorTool,xmp_creator_tool,VARCHAR(128)
xmp:CreatorTool,xmp_creator_tool,VARCHAR(128)
csv:num_rows,csv_rows,INTEGER
csv:num_columns,csv_columns,INTEGER
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ public class Constants {
//used as working directory for csv emitter
public static final String BASE_PATH = "basePath";

public static final String DEFAULT_METADATA_MAPPINGS = "default-metadata-mappings.csv";


}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.commons.csv.CSVRecord;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.tallison.tika.app.fx.Constants;
import org.tallison.tika.app.fx.ControllerBase;
import org.tallison.tika.app.fx.batch.BatchProcessConfig;
import org.tallison.tika.app.fx.ctx.AppContext;
Expand Down Expand Up @@ -86,6 +87,24 @@ protected void safelySetCsvMetadataPath(String csvMetadataFilePath) {
}
}

protected void tryToLoadDefaultMetadataMappings() {
Path defaultMetadataMappingCSV =
AppContext.CONFIG_PATH.resolve(Constants.DEFAULT_METADATA_MAPPINGS);
if (! Files.exists(defaultMetadataMappingCSV)) {
LOGGER.debug("Can't find default metadata mappings path: {}",
defaultMetadataMappingCSV.toAbsolutePath().toString());
return;
}
try {
LOGGER.debug("loading default csv");
loadMetadataCSV(defaultMetadataMappingCSV.toFile());
} catch (IOException e) {
LOGGER.warn("couldn't load default metadata mappings file "
+ defaultMetadataMappingCSV.toAbsolutePath(), e);
}

}

protected Optional<Path> getCsvMetadataPath() {
return csvMetadataPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public void selectCSVOutputDirectory(ActionEvent actionEvent) {
}
this.csvWorkingDirectory = Optional.of(directory.toPath());
this.csvDirectory.setText(directory.getName());
saveState(false);
}


Expand Down Expand Up @@ -204,9 +205,15 @@ public void validateCSV(ActionEvent actionEvent) {
}

if (getMetadataRows().size() == 0) {
alert(ALERT_TITLE, "Metadata Not Configured", "Need to configure metadata");
csvAccordion.setExpandedPane(csvAccordion.getPanes().get(1));
return;
LOGGER.debug("metadata rows size == 0");
tryToLoadDefaultMetadataMappings();
LOGGER.debug("after trying default metadata mappings: {}", getMetadataRows().size());
if (getMetadataRows().size() == 0) {
LOGGER.debug("metadata rows size still == 0");
alert(ALERT_TITLE, "Metadata Not Configured", "Need to configure metadata");
csvAccordion.setExpandedPane(csvAccordion.getPanes().get(1));
return;
}
}
ValidationResult validationResult = validateMetadataRows();
if (validationResult != ValidationResult.OK) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,18 @@ public class CSVEmitterSpec extends JDBCEmitterSpec {
private Optional<Path> tmpDbDirectory = Optional.empty();
private Optional<Path> csvDirectory = Optional.empty();
private Optional<String> csvFileName = Optional.empty();
private volatile boolean closed = false;

public CSVEmitterSpec(@JsonProperty("metadataTuples") List<MetadataTuple> metadataTuples) {
super(metadataTuples);
setTableName(CSV_DB_TABLE_NAME);
}

private static void writeRow(ResultSet rs, CSVPrinter printer, List<String> cells,
int columnCount) throws SQLException, IOException {
for (int i = 1; i <= columnCount; i++) {
String val = rs.getString(i);
if (rs.wasNull()) {
val = StringUtils.EMPTY;
}
cells.add(val);
}
printer.printRecord(cells);
}

@Override
public ValidationResult initialize() throws IOException {
if (closed) {
throw new IOException("This csv emitter has been closed");
}
tmpDbDirectory = Optional.of(Files.createTempDirectory("tika-app-csv-tmp"));
LOGGER.debug("tmp db directory: {}", tmpDbDirectory.get().toAbsolutePath());
setConnectionString("jdbc:sqlite:" + tmpDbDirectory.get().toAbsolutePath() +
Expand All @@ -92,7 +84,7 @@ public Optional<Path> getCsvDirectory() {
}

public void setCsvDirectory(Path csvDirectory) {
this.csvDirectory = Optional.of(csvDirectory);
this.csvDirectory = Optional.ofNullable(csvDirectory);
}

private void createTable() throws SQLException {
Expand Down Expand Up @@ -122,12 +114,17 @@ private void createTable() throws SQLException {

@Override
public void close() throws IOException {
//avoid double closures -- TODO figure out a more elegant way of handling this
if (closed) {
return;
}
try {
writeCSV();
} catch (IOException e) {
LOGGER.warn("problem writing csv", e);
} finally {
cleanCSVTempResources();
closed = true;
}
}

Expand Down Expand Up @@ -235,4 +232,16 @@ private void cleanCSVTempResources() throws IOException {
}
FileUtils.deleteDirectory(tmpDbDirectory.get().toFile());
}

private static void writeRow(ResultSet rs, CSVPrinter printer, List<String> cells,
int columnCount) throws SQLException, IOException {
for (int i = 1; i <= columnCount; i++) {
String val = rs.getString(i);
if (rs.wasNull()) {
val = StringUtils.EMPTY;
}
cells.add(val);
}
printer.printRecord(cells);
}
}

0 comments on commit ee25dff

Please sign in to comment.