From 92914f15f30a90ed89b4f18502b82986e404bcd0 Mon Sep 17 00:00:00 2001 From: Patrick Tang <36460266+patricksptang@users.noreply.github.com> Date: Sun, 17 Nov 2024 23:11:36 -0800 Subject: [PATCH] Add serialization to Translation tips --- .../step/core/data/create/Loader.java | 8 ++- .../core/service/TranslationTipsService.java | 15 +++-- .../impl/TranslationTipsServiceImpl.java | 57 ++++++++++++++++--- .../helpers/JSwordStrongNumberHelper.java | 13 ++--- .../step/guice/StepServletConfig.java | 5 +- 5 files changed, 75 insertions(+), 23 deletions(-) diff --git a/step-core/src/main/java/com/tyndalehouse/step/core/data/create/Loader.java b/step-core/src/main/java/com/tyndalehouse/step/core/data/create/Loader.java index 24be1455c2..b5ed7bd0eb 100644 --- a/step-core/src/main/java/com/tyndalehouse/step/core/data/create/Loader.java +++ b/step-core/src/main/java/com/tyndalehouse/step/core/data/create/Loader.java @@ -213,7 +213,7 @@ private void loadData() { this.totalProgress += 1; loadAugmentedStrongs(true); this.totalProgress += 1; - loadTranslationTips(); + loadTranslationTips(true); LOGGER.info("Finished loading..."); } @@ -225,10 +225,12 @@ public void loadAugmentedStrongs(boolean loadAugmentedFile) { else this.strongAugmentationService.loadFromSerialization(appManager.getStepInstallFile().toString()); } - public void loadTranslationTips() { + public void loadTranslationTips(boolean loadTranslationTips) { LOGGER.debug("Indexing translation tips"); String translationTipsPath = this.coreProperties.getProperty("test.data.path.translationtips"); - this.translationTipsService.readAndLoad(translationTipsPath); + String installFile = appManager.getStepInstallFile().toString(); + if (loadTranslationTips) this.translationTipsService.readAndLoad(translationTipsPath, installFile); + else this.translationTipsService.loadFromSerialization(appManager.getStepInstallFile().toString()); } /** diff --git a/step-core/src/main/java/com/tyndalehouse/step/core/service/TranslationTipsService.java b/step-core/src/main/java/com/tyndalehouse/step/core/service/TranslationTipsService.java index 7f178f8e20..1f19d4293c 100644 --- a/step-core/src/main/java/com/tyndalehouse/step/core/service/TranslationTipsService.java +++ b/step-core/src/main/java/com/tyndalehouse/step/core/service/TranslationTipsService.java @@ -4,6 +4,7 @@ import org.crosswire.jsword.passage.RocketPassage; import org.crosswire.jsword.versification.system.Versifications; +import java.io.Serializable; import java.util.HashMap; /** @@ -11,9 +12,15 @@ */ public interface TranslationTipsService { - void readAndLoad(final String translationTipsPath); + void readAndLoad(final String translationTipsPath, final String installFile); + void loadFromSerialization(final String installFilePath); + + public static class TranslationTips implements Serializable { + public BitwisePassage regularFormatedFN = new RocketPassage(Versifications.instance().getVersification("NRSV")); + public BitwisePassage alternativeFormatedFN = new RocketPassage(Versifications.instance().getVersification("NRSV")); + public HashMap customFN = new HashMap(); - public static BitwisePassage regularFormatedFN = new RocketPassage(Versifications.instance().getVersification("NRSV")); - public static BitwisePassage alternativeFormatedFN = new RocketPassage(Versifications.instance().getVersification("NRSV")); - public static HashMap customFN = new HashMap(); + } + + public static TranslationTips translationTips = new TranslationTips(); } diff --git a/step-core/src/main/java/com/tyndalehouse/step/core/service/impl/TranslationTipsServiceImpl.java b/step-core/src/main/java/com/tyndalehouse/step/core/service/impl/TranslationTipsServiceImpl.java index 67db614169..022973fe4d 100644 --- a/step-core/src/main/java/com/tyndalehouse/step/core/service/impl/TranslationTipsServiceImpl.java +++ b/step-core/src/main/java/com/tyndalehouse/step/core/service/impl/TranslationTipsServiceImpl.java @@ -20,7 +20,7 @@ public class TranslationTipsServiceImpl implements TranslationTipsService { private static final Logger LOGGER = LoggerFactory.getLogger(StrongAugmentationServiceImpl.class); - public void loadVersesToBitwise(String translationTipsPath) { + public void readAndLoad(final String translationTipsPath, final String installFilePath) { Reader fileReader = null; BufferedInputStream bufferedStream = null; InputStream stream = null; @@ -81,21 +81,21 @@ public void loadVersesToBitwise(String translationTipsPath) { if (fileNameFromURL.length() < 1) continue; int ordinal = key.getOrdinal(); if (fileNameFromURL.equals(bookName + "-" + chapter + verse)) { - if (regularFormatedFN.store.get(ordinal)) { + if (translationTips.regularFormatedFN.store.get(ordinal)) { System.out.println("Duplicate definition regular: " + data + ", " + ordinal); continue; } - regularFormatedFN.addAll(keyf.getKey(Versifications.instance().getVersification("NRSV"), parts[0])); + translationTips.regularFormatedFN.addAll(keyf.getKey(Versifications.instance().getVersification("NRSV"), parts[0])); } else if (fileNameFromURL.equals(bookName + "-" + chapter + "-" + verse)) { - if (regularFormatedFN.store.get(ordinal)) { + if (translationTips.regularFormatedFN.store.get(ordinal)) { System.out.println("Duplicate definition alternate: " + data + ", " + ordinal); continue; } - alternativeFormatedFN.addAll(keyf.getKey(Versifications.instance().getVersification("NRSV"), parts[0])); + translationTips.alternativeFormatedFN.addAll(keyf.getKey(Versifications.instance().getVersification("NRSV"), parts[0])); } else { - customFN.put(ordinal, fileNameFromURL); + translationTips.customFN.put(ordinal, fileNameFromURL); } } catch (Exception e) { @@ -108,13 +108,54 @@ else if (fileNameFromURL.equals(bookName + "-" + chapter + "-" + verse)) { LOGGER.error("Unable to read a line from the translation tip file"); throw new StepInternalException("Unable to read a line from the translation tip file ", e); } + // Output to serialized data + String installFileFolder = ""; + int pos = installFilePath.lastIndexOf('\\'); + if (pos == -1) + pos = installFilePath.lastIndexOf('/'); + if (pos > 1) + installFileFolder = installFilePath.substring(0, pos+1); + try { + FileOutputStream fileOut = + new FileOutputStream(installFileFolder + "translator_tips.dat"); + ObjectOutputStream out = new ObjectOutputStream(fileOut); + out.writeObject(translationTips); + out.close(); + fileOut.close(); + LOGGER.info("Serialized data is saved in " + installFileFolder + "translator_tips.dat"); + } catch (IOException i) { + LOGGER.error("Serialized data cannot be saved in " + installFileFolder + "translator_tips.dat"); + i.printStackTrace(); + } + } finally { closeQuietly(fileReader); closeQuietly(bufferedStream); closeQuietly(stream); } } - public void readAndLoad(final String translationTipsPath) { - loadVersesToBitwise(translationTipsPath); + + public void loadFromSerialization(final String installFilePath) { + String installFileFolder = ""; + int pos = installFilePath.lastIndexOf('\\'); + if (pos == -1) + pos = installFilePath.lastIndexOf('/'); + if (pos > 1) + installFileFolder = installFilePath.substring(0, pos+1); + try { + FileInputStream fileIn = new FileInputStream(installFileFolder + "translator_tips.dat"); + ObjectInputStream in = new ObjectInputStream(fileIn); + TranslationTips tipsReadIn = (TranslationTips) in.readObject(); + translationTips.regularFormatedFN = tipsReadIn.regularFormatedFN; + translationTips.alternativeFormatedFN = tipsReadIn.alternativeFormatedFN; + translationTips.customFN = tipsReadIn.customFN; + in.close(); + fileIn.close(); + } catch (IOException i) { + i.printStackTrace(); + } catch (ClassNotFoundException c) { + System.out.println("augmented strong class not found"); + c.printStackTrace(); + } } } diff --git a/step-core/src/main/java/com/tyndalehouse/step/core/service/jsword/helpers/JSwordStrongNumberHelper.java b/step-core/src/main/java/com/tyndalehouse/step/core/service/jsword/helpers/JSwordStrongNumberHelper.java index f562c3d059..0da309a9a9 100644 --- a/step-core/src/main/java/com/tyndalehouse/step/core/service/jsword/helpers/JSwordStrongNumberHelper.java +++ b/step-core/src/main/java/com/tyndalehouse/step/core/service/jsword/helpers/JSwordStrongNumberHelper.java @@ -35,9 +35,8 @@ import java.util.*; import java.util.Map.Entry; -import static com.tyndalehouse.step.core.service.TranslationTipsService.regularFormatedFN; -import static com.tyndalehouse.step.core.service.TranslationTipsService.alternativeFormatedFN; -import static com.tyndalehouse.step.core.service.TranslationTipsService.customFN; +import static com.tyndalehouse.step.core.service.TranslationTipsService.*; + /** * Provides each strong number given a verse. *

@@ -120,12 +119,12 @@ private void calculateCounts(String userLanguage) { } else targetVersification = ntV11n; int curOrdinal = verseInNRSV.getOrdinal(); - if (regularFormatedFN.store.get(curOrdinal)) + if (translationTips.regularFormatedFN.store.get(curOrdinal)) this.translationTipsFN = verseInNRSV.getBook().toString().toLowerCase() + "-" + verseInNRSV.getChapter() + verseInNRSV.getVerse(); - else if (alternativeFormatedFN.store.get(curOrdinal)) + else if (translationTips.alternativeFormatedFN.store.get(curOrdinal)) this.translationTipsFN = verseInNRSV.getBook().toString().toLowerCase() + "-" + verseInNRSV.getChapter() + "-" + verseInNRSV.getVerse(); - else if (customFN.containsKey(curOrdinal)) - this.translationTipsFN = customFN.get(curOrdinal); + else if (translationTips.customFN.containsKey(curOrdinal)) + this.translationTipsFN = translationTips.customFN.get(curOrdinal); else this.translationTipsFN = ""; // If there are no tips, it will be an empty string final Key key = VersificationsMapper.instance().mapVerse(curReference, targetVersification); diff --git a/step-web/src/main/java/com/tyndalehouse/step/guice/StepServletConfig.java b/step-web/src/main/java/com/tyndalehouse/step/guice/StepServletConfig.java index 3d418f2907..6ba8a19d40 100644 --- a/step-web/src/main/java/com/tyndalehouse/step/guice/StepServletConfig.java +++ b/step-web/src/main/java/com/tyndalehouse/step/guice/StepServletConfig.java @@ -98,7 +98,10 @@ public void contextInitialized(final ServletContextEvent servletContextEvent) { if (Boolean.getBoolean("step.loader")) { getInjector().getInstance(Loader.class).init(); } - else getInjector().getInstance(Loader.class).loadAugmentedStrongs(false); + else { + getInjector().getInstance(Loader.class).loadAugmentedStrongs(false); + getInjector().getInstance(Loader.class).loadTranslationTips(false); + } }