← Back to team overview

slub.team team mailing list archive

[Merge] lp:~slub.team/goobi-production/bug-1013632 into lp:goobi-production

 

Henning Gerhardt has proposed merging lp:~slub.team/goobi-production/bug-1013632 into lp:goobi-production.

Requested reviews:
  Ralf Claussnitzer (ralf-claussnitzer)
Related bugs:
  Bug #1013632 in Goobi.Production: "Exporting of additional OCR result directories"
  https://bugs.launchpad.net/goobi-production/+bug/1013632

For more details, see:
https://code.launchpad.net/~slub.team/goobi-production/bug-1013632/+merge/120156
-- 
https://code.launchpad.net/~slub.team/goobi-production/bug-1013632/+merge/120156
Your team Saxon State Library Team is subscribed to branch lp:goobi-production.
=== modified file 'src/de/sub/goobi/export/dms/ExportDms.java'
--- src/de/sub/goobi/export/dms/ExportDms.java	2012-05-30 08:24:19 +0000
+++ src/de/sub/goobi/export/dms/ExportDms.java	2012-08-17 12:41:45 +0000
@@ -23,6 +23,7 @@
 package de.sub.goobi.export.dms;
 
 import java.io.File;
+import java.io.FilenameFilter;
 import java.io.IOException;
 
 import org.apache.log4j.Logger;
@@ -197,6 +198,8 @@
 		try {
 			if (exportWithImages) {
 				imageDownload(myProzess, benutzerHome, atsPpnBand, DIRECTORY_SUFFIX);
+				File ocrDirectory = new File(myProzess.getOcrDirectory());
+				exportContentOfOcrDirectory(ocrDirectory, benutzerHome, atsPpnBand);
 			}
 		} catch (Exception e) {
 			Helper.setFehlerMeldung("Export canceled, Process: " + myProzess.getTitel(), e);
@@ -317,45 +320,57 @@
 				Helper.copyFile(dateien[i], meinZiel);
 			}
 		}
-		
-		File txtFolder = new File(myProzess.getTxtDirectory());
-		if (txtFolder.exists()) {
-			File destination = new File(benutzerHome + File.separator + atsPpnBand +"_txt");
-			if (!destination.exists()) {
-				destination.mkdir();
-			}
-			File[] dateien = txtFolder.listFiles();
-			for (int i = 0; i < dateien.length; i++) {
-				File meinZiel = new File(destination + File.separator + dateien[i].getName());
-				Helper.copyFile(dateien[i], meinZiel);
-			}
-		}
-		
-		
-		File wordFolder = new File(myProzess.getWordDirectory());
-		if (wordFolder.exists()) {
-			File destination = new File(benutzerHome + File.separator + atsPpnBand +"_wc");
-			if (!destination.exists()) {
-				destination.mkdir();
-			}
-			File[] dateien = wordFolder.listFiles();
-			for (int i = 0; i < dateien.length; i++) {
-				File meinZiel = new File(destination + File.separator + dateien[i].getName());
-				Helper.copyFile(dateien[i], meinZiel);
-			}
-		}
-		
-		File pdfFolder = new File(myProzess.getPdfDirectory());
-		if (pdfFolder.exists()) {
-			File destination = new File(benutzerHome + File.separator + atsPpnBand +"_pdf");
-			if (!destination.exists()) {
-				destination.mkdir();
-			}
-			File[] dateien = pdfFolder.listFiles();
-			for (int i = 0; i < dateien.length; i++) {
-				File meinZiel = new File(destination + File.separator + dateien[i].getName());
-				Helper.copyFile(dateien[i], meinZiel);
-			}
-		}
-	}	
+	}
+
+	protected void exportContentOfOcrDirectory(File ocrDirectory, File userHome, String atsPpnBand)
+			throws IOException, SwapException, DAOException, InterruptedException {
+
+		if (ocrDirectory.exists()) {
+			File[] folder = ocrDirectory.listFiles(new FilenameFilter() {
+				public boolean accept(File dir, String name) {
+					int liof = name.lastIndexOf("_");
+					int leng = name.length()-1;
+					return (liof > -1) && (liof < leng);
+				}
+			});
+			if (folder != null) {
+				for (File ocrSubDirectory : folder) {
+					if (ocrSubDirectory.isDirectory() && ocrSubDirectory.list().length > 0) {
+						String suffix = ocrSubDirectory.getName().substring(ocrSubDirectory.getName().lastIndexOf("_"));
+						File destination = new File(userHome + File.separator + atsPpnBand + suffix);
+						copyDirectory(ocrSubDirectory, destination);
+					}
+				}
+			}
+		} else {
+			myLogger.warn("OCR directory " + ocrDirectory.getAbsolutePath() + " does not exists.");
+		}
+	}
+
+	private void copyDirectory(File sourceDirectory, File destinationDirectory) throws IOException {
+
+		if (! sourceDirectory.isDirectory()) {
+			myLogger.error("Given source " + sourceDirectory.getPath() + " is not a directory!");
+			return;
+		}
+
+		if (! destinationDirectory.exists()) {
+			boolean result;
+			result = destinationDirectory.mkdir();
+			if (! result) {
+				myLogger.error("Could not create directory " + destinationDirectory.getPath() + "!");
+				return;
+			}
+		}
+
+		File[] sourceFiles = sourceDirectory.listFiles();
+		if (sourceFiles != null) {
+			for (File sourceFile : sourceFiles) {
+				File destinationFile = new File(destinationDirectory + File.separator + sourceFile.getName());
+				Helper.copyFile(sourceFile, destinationFile);
+			}
+		}
+
+	}
+
 }

=== added directory 'test/src/de/sub/goobi/export'
=== added directory 'test/src/de/sub/goobi/export/dms'
=== added file 'test/src/de/sub/goobi/export/dms/ExportDmsTest.java'
--- test/src/de/sub/goobi/export/dms/ExportDmsTest.java	1970-01-01 00:00:00 +0000
+++ test/src/de/sub/goobi/export/dms/ExportDmsTest.java	2012-08-17 12:41:45 +0000
@@ -0,0 +1,193 @@
+/*
+ * This file is part of the Goobi Application - a Workflow tool for the support of
+ * mass digitization.
+ *
+ * Visit the websites for more information.
+ *     - http://gdz.sub.uni-goettingen.de
+ *     - http://www.goobi.org
+ *     - http://launchpad.net/goobi-production
+ *
+ * This program is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 2 of the License, or (at your option) any later
+ * version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU General Public License for more details. You
+ * should have received a copy of the GNU General Public License along with this
+ * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
+ * Suite 330, Boston, MA 02111-1307 USA
+ */
+
+package de.sub.goobi.export.dms;
+
+import de.sub.goobi.helper.exceptions.DAOException;
+import de.sub.goobi.helper.exceptions.SwapException;
+import org.apache.log4j.BasicConfigurator;
+import org.apache.log4j.Level;
+import org.goobi.log4j.TestAppender;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.UUID;
+
+import static org.goobi.junit.AssertFileSystem.assertDirectoryIsEmpty;
+import static org.goobi.junit.AssertFileSystem.assertFileExists;
+import static org.goobi.junit.AssertFileSystem.assertFileNotExists;
+import static org.junit.Assert.assertEquals;
+
+public class ExportDmsTest {
+	private static final String DUMMY_ATS = "test123";
+
+	private final static String DIRECTORY_PREFIX = UUID.randomUUID().toString() + "-";
+	private final static String DESTINATION_DIRECTORY = DIRECTORY_PREFIX + "destination";
+	private final static String SOURCE_DIRECTORY = DIRECTORY_PREFIX + "source";
+
+	private final static File destinationDirectory = new File(DESTINATION_DIRECTORY);
+	private final static File sourceDirectory = new File(SOURCE_DIRECTORY);
+
+	private TestAppender testAppender;
+
+	@BeforeClass
+	public static void createDirectories() {
+		destinationDirectory.mkdir();
+		sourceDirectory.mkdir();
+	}
+
+	@Before
+	public void setupTestAppender() {
+		testAppender = new TestAppender();
+		BasicConfigurator.configure(testAppender);
+	}
+
+	@AfterClass
+	public static void removeDirectories() {
+		deleteRecursive(destinationDirectory);
+		deleteRecursive(sourceDirectory);
+	}
+
+	@Test
+	public void shoudlRaiseWarningIfOcrDirectoryDoesNotExist() throws IOException, SwapException, DAOException, InterruptedException {
+		ExportDms fixture = new ExportDms();
+
+		fixture.exportContentOfOcrDirectory(new File("/foo/bar"), new File("userHome"), "");
+
+		assertWarning("OCR directory /foo/bar does not exists.");
+	}
+
+	@Test
+	public void shouldDoNothingWithEmptySourceDirectory() throws IOException, SwapException, DAOException, InterruptedException {
+		ExportDms fixture = new ExportDms();
+
+		fixture.exportContentOfOcrDirectory(sourceDirectory, destinationDirectory, "");
+
+		assertDirectoryIsEmpty("Destination directory should be empty.", destinationDirectory);
+	}
+
+	@Test
+	public void shouldDoNothingIfSubSourceDirectoryIsEmpty() throws IOException, SwapException, DAOException, InterruptedException {
+		ExportDms fixture = new ExportDms();
+
+		fixture.exportContentOfOcrDirectory(sourceDirectory, destinationDirectory, DUMMY_ATS);
+
+		assertDirectoryIsEmpty("Destination directory should be empty.", destinationDirectory);
+	}
+
+	@Test
+	public void dummyFileShouldEndUpInDestinationDirectory() throws IOException, SwapException, DAOException, InterruptedException {
+		String dummyDestinationFilePath = DESTINATION_DIRECTORY + File.separator + DUMMY_ATS
+				+ "_xml" + File.separator + "dummy.xml";
+
+		String sourceSubDirectoryName = SOURCE_DIRECTORY + File.separator + "test_xml";
+
+		String dummySourceFilePath = sourceSubDirectoryName + File.separator + "dummy.xml";
+
+		File dummySourceFile = new File(dummySourceFilePath);
+		File sourceSubDirectory = new File(sourceSubDirectoryName);
+		File dummyDestinationFile = new File(dummyDestinationFilePath);
+
+		sourceSubDirectory.mkdir();
+		dummySourceFile.createNewFile();
+
+		ExportDms fixture = new ExportDms();
+		fixture.exportContentOfOcrDirectory(sourceDirectory, destinationDirectory, DUMMY_ATS);
+
+		assertFileExists(dummyDestinationFile.getAbsolutePath());
+	}
+
+	@Test
+	public void contentOfDirectoriesWithoutUnderscoreShouldBeIgnored()
+	throws IOException, SwapException, DAOException, InterruptedException {
+		String sourceSubDirectoryName = SOURCE_DIRECTORY + File.separator + "without-underscore";
+		String dummySourceFilePath = sourceSubDirectoryName + File.separator + "dummy-ws.xml";
+
+		File sourceSubDirectory = new File(sourceSubDirectoryName);
+		sourceSubDirectory.mkdir();
+
+		File dummySourceFile = new File(dummySourceFilePath);
+		dummySourceFile.createNewFile();
+
+		ExportDms fixture = new ExportDms();
+		fixture.exportContentOfOcrDirectory(sourceDirectory, destinationDirectory, DUMMY_ATS);
+
+		assertFileNotExists(destinationDirectory.getAbsolutePath() + File.separator + DUMMY_ATS + File.separator + "dummy-ws.xml");
+	} 
+
+	@Test
+	public void contentOfDirectoriesWithoutProperSuffixShouldBeIgnored()
+	throws IOException, SwapException, DAOException, InterruptedException {
+		String sourceSubDirectoryName = SOURCE_DIRECTORY + File.separator + "without-suffix_";
+		String dummySourceFilePath = sourceSubDirectoryName + File.separator + "dummy-ws.xml";
+
+		File sourceSubDirectory = new File(sourceSubDirectoryName);
+		sourceSubDirectory.mkdir();
+
+		File dummySourceFile = new File(dummySourceFilePath);
+		dummySourceFile.createNewFile();
+
+		ExportDms fixture = new ExportDms();
+		fixture.exportContentOfOcrDirectory(sourceDirectory, destinationDirectory, DUMMY_ATS);
+
+		assertFileNotExists(destinationDirectory.getAbsolutePath() + File.separator + DUMMY_ATS + "_" + File.separator + "dummy-ws.xml");
+	} 
+
+	@Test
+	public void contentOfDirectoriesWithOnlyUnderscoreInNameShouldBeIgnored()
+	throws IOException, SwapException, DAOException, InterruptedException {
+		String sourceSubDirectoryName = SOURCE_DIRECTORY + File.separator + "___";
+		String dummySourceFilePath = sourceSubDirectoryName + File.separator + "dummy-ws.xml";
+
+		File sourceSubDirectory = new File(sourceSubDirectoryName);
+		sourceSubDirectory.mkdir();
+
+		File dummySourceFile = new File(dummySourceFilePath);
+		dummySourceFile.createNewFile();
+
+		ExportDms fixture = new ExportDms();
+		fixture.exportContentOfOcrDirectory(sourceDirectory, destinationDirectory, DUMMY_ATS);
+
+		assertFileNotExists(destinationDirectory.getAbsolutePath() + File.separator + DUMMY_ATS + "_" + File.separator + "dummy-ws.xml");
+	} 
+
+	private void assertWarning(String message) {
+		assertEquals("Expecting WARN log level", Level.WARN, testAppender.getLastEvent().getLevel());
+		assertEquals("Unexected log message", message, testAppender.getLastEvent().getMessage());
+	}
+
+	private static void deleteRecursive(File f) {
+		if (f.exists()) {
+			if (f.isDirectory()) {
+				for (File sf : f.listFiles()) {
+					deleteRecursive(sf);
+				}
+			}
+			f.delete();
+		}
+	}
+
+}

=== modified file 'test/src/org/goobi/io/BackupFileRotationTest.java'
--- test/src/org/goobi/io/BackupFileRotationTest.java	2012-04-16 08:26:50 +0000
+++ test/src/org/goobi/io/BackupFileRotationTest.java	2012-08-17 12:41:45 +0000
@@ -29,8 +29,7 @@
 
 import java.io.*;
 
-import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.fail;
+import static org.goobi.junit.AssertFileSystem.*;
 
 import org.apache.log4j.BasicConfigurator;
 
@@ -166,16 +165,6 @@
 		assertFileNotExists(BACKUP_FILE_PATH + BACKUP_FILE_NAME + ".1");
 	}
 
-	private void assertLastModifiedDate(String fileName, long expectedLastModifiedDate) {
-		long currentLastModifiedDate = getLastModifiedFileDate(fileName);
-		assertEquals("Last modified date of file " + fileName + " differ:", expectedLastModifiedDate, currentLastModifiedDate);
-	}
-
-	private long getLastModifiedFileDate(String fileName) {
-		File testFile = new File(fileName);
-		return testFile.lastModified();
-	}
-
 	private void runBackup(int numberOfBackups) {
 		runBackup(numberOfBackups, BACKUP_FILE_NAME);
 	}
@@ -188,30 +177,6 @@
 		bfr.performBackup();
 	}
 
-	private void assertFileHasContent(String fileName, String expectedContent) throws IOException {
-		File testFile = new File(fileName);
-		FileReader reader = new FileReader(testFile);
-		BufferedReader br = new BufferedReader(reader);
-		String content = br.readLine();
-		br.close();
-		reader.close();
-		assertEquals("File " + fileName + " does not contain expected content:", expectedContent, content);
-	}
-
-	private void assertFileExists(String fileName) {
-		File newFile = new File(fileName);
-		if (!newFile.exists()) {
-			fail("File " + fileName + " does not exist.");
-		}
-	}
-
-	private void assertFileNotExists(String fileName) {
-		File newFile = new File(fileName);
-		if (newFile.exists()) {
-			fail("File " + fileName + " should not exist.");
-		}
-	}
-
 	private void createFile(String fileName) throws IOException {
 		File testFile = new File(fileName);
 		FileWriter writer = new FileWriter(testFile);

=== added directory 'test/src/org/goobi/junit'
=== added file 'test/src/org/goobi/junit/AssertFileSystem.java'
--- test/src/org/goobi/junit/AssertFileSystem.java	1970-01-01 00:00:00 +0000
+++ test/src/org/goobi/junit/AssertFileSystem.java	2012-08-17 12:41:45 +0000
@@ -0,0 +1,76 @@
+/*
+ * This file is part of the Goobi Application - a Workflow tool for the support of
+ * mass digitization.
+ *
+ * Visit the websites for more information.
+ *     - http://gdz.sub.uni-goettingen.de
+ *     - http://www.goobi.org
+ *     - http://launchpad.net/goobi-production
+ *
+ * This program is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 2 of the License, or (at your option) any later
+ * version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU General Public License for more details. You
+ * should have received a copy of the GNU General Public License along with this
+ * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
+ * Suite 330, Boston, MA 02111-1307 USA
+ */
+
+package org.goobi.junit;
+
+import junit.framework.Assert;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+
+public class AssertFileSystem {
+
+	public static void assertLastModifiedDate(String fileName, long expectedLastModifiedDate) {
+		long currentLastModifiedDate = getLastModifiedFileDate(fileName);
+		Assert.assertEquals("Last modified date of file " + fileName + " differ:", expectedLastModifiedDate, currentLastModifiedDate);
+	}
+
+	public static long getLastModifiedFileDate(String fileName) {
+		File testFile = new File(fileName);
+		return testFile.lastModified();
+	}
+
+	public static void assertFileHasContent(String fileName, String expectedContent) throws IOException {
+		File testFile = new File(fileName);
+		FileReader reader = new FileReader(testFile);
+		BufferedReader br = new BufferedReader(reader);
+		String content = br.readLine();
+		br.close();
+		reader.close();
+		Assert.assertEquals("File " + fileName + " does not contain expected content:", expectedContent, content);
+	}
+
+	public static void assertFileExists(String fileName) {
+		File newFile = new File(fileName);
+		if (!newFile.exists()) {
+			Assert.fail("File " + fileName + " does not exist.");
+		}
+	}
+
+	public static void assertFileNotExists(String fileName) {
+		File newFile = new File(fileName);
+		if (newFile.exists()) {
+			Assert.fail("File " + fileName + " should not exist.");
+		}
+	}
+
+	public static void assertDirectoryIsEmpty(String message, File directory) {
+		if (! directory.isDirectory()) {
+		    Assert.fail(directory.getAbsolutePath() + " is not a directory!");
+		}
+
+		Assert.assertEquals(message, 0, directory.list().length);
+	}
+
+}

=== added directory 'test/src/org/goobi/log4j'
=== added file 'test/src/org/goobi/log4j/TestAppender.java'
--- test/src/org/goobi/log4j/TestAppender.java	1970-01-01 00:00:00 +0000
+++ test/src/org/goobi/log4j/TestAppender.java	2012-08-17 12:41:45 +0000
@@ -0,0 +1,53 @@
+/*
+ * This file is part of the Goobi Application - a Workflow tool for the support of
+ * mass digitization.
+ *
+ * Visit the websites for more information.
+ *     - http://gdz.sub.uni-goettingen.de
+ *     - http://www.goobi.org
+ *     - http://launchpad.net/goobi-production
+ *
+ * This program is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 2 of the License, or (at your option) any later
+ * version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU General Public License for more details. You
+ * should have received a copy of the GNU General Public License along with this
+ * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
+ * Suite 330, Boston, MA 02111-1307 USA
+ */
+
+package org.goobi.log4j;
+
+import org.apache.log4j.AppenderSkeleton;
+import org.apache.log4j.spi.LoggingEvent;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class TestAppender extends AppenderSkeleton{
+
+	private List<LoggingEvent> log = new ArrayList<LoggingEvent>();
+
+	@Override
+	protected void append(LoggingEvent loggingEvent) {
+		log.add(loggingEvent);
+	}
+
+	@Override
+	public boolean requiresLayout() {
+		return false;
+	}
+
+	@Override
+	public void close() {
+	}
+
+	public LoggingEvent getLastEvent() {
+		return log.get(log.size() - 1);
+	}
+
+}


Follow ups