Skip to content

Commit

Permalink
refactor: #98 Create the Links object just once and, put it into the …
Browse files Browse the repository at this point in the history
…ExportContext and reuse from there.
  • Loading branch information
dmitry-weirdo committed Dec 3, 2024
1 parent 90147f5 commit a892bda
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ public abstract class FreemarkerTemplate {
public abstract String getTemplatePath();

public void export(ExportContext context, String filePath) {
Links links = Links.create(context);
templateData.put(LINKS_KEY, links);
templateData.put(LINKS_KEY, context.links);

exportFreemarkerToFile(getTemplatePath(), filePath, templateData);
}

public String exportToString(ExportContext context) {
Links links = Links.create(context);
templateData.put(LINKS_KEY, links);
templateData.put(LINKS_KEY, context.links);

return exportFreemarkerToString(getTemplatePath(), templateData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import ru.klavogonki.common.NonStandardDictionary;
import ru.klavogonki.statistics.export.ExportContext;
import ru.klavogonki.statistics.export.vocabulary.NonStandardVocabularyGeneratorContext;
import ru.klavogonki.statistics.export.vocabulary.standard.AbraTopExporter;
import ru.klavogonki.statistics.export.vocabulary.standard.CharsTopExporter;
Expand Down Expand Up @@ -118,32 +117,30 @@ public class Links {
private Links() {
}

public static Links create(ExportContext context) {
public static Links create(NonStandardVocabularyGeneratorContext context) {
Links links = new Links();

NonStandardVocabularyGeneratorContext dictionariesContext = context.nonStandardDictionariesGeneratorContext;

// todo: pass a map to Links instead of multiple separate hardcoded fields

links.normalInEnglishTopBySpeedPage1 = dictionariesContext.getTopBySpeedFirstPageFilePath(NonStandardDictionary.NORMAL_IN_ENGLISH);
links.miniMarathonTopBySpeedPage1 = dictionariesContext.getTopBySpeedFirstPageFilePath(NonStandardDictionary.MINI_MARATHON);
links.shortTextsTopBySpeedPage1 = dictionariesContext.getTopBySpeedFirstPageFilePath(NonStandardDictionary.SHORT_TEXTS);
links.frequencyVocabularyTopBySpeedPage1 = dictionariesContext.getTopBySpeedFirstPageFilePath(NonStandardDictionary.FREQUENCY_VOCABULARY);
links.oneHundredRussianTopBySpeedPage1 = dictionariesContext.getTopBySpeedFirstPageFilePath(NonStandardDictionary.ONE_HUNDRED_RUSSIAN);
links.digitsOneHundredTopBySpeedPage1 = dictionariesContext.getTopBySpeedFirstPageFilePath(NonStandardDictionary.DIGITS_ONE_HUNDRED);
links.trainingIndexFingersTopBySpeedPage1 = dictionariesContext.getTopBySpeedFirstPageFilePath(NonStandardDictionary.TRAINING_INDEX_FINGERS);
links.ringFingersTopBySpeedPage1 = dictionariesContext.getTopBySpeedFirstPageFilePath(NonStandardDictionary.RING_FINGERS);
links.pinkiesPlusTopBySpeedPage1 = dictionariesContext.getTopBySpeedFirstPageFilePath(NonStandardDictionary.PINKIES_PLUS);

links.normalInEnglishTopHeader = dictionariesContext.getHeaderName(NonStandardDictionary.NORMAL_IN_ENGLISH);
links.miniMarathonTopHeader = dictionariesContext.getHeaderName(NonStandardDictionary.MINI_MARATHON);
links.shortTextsTopHeader = dictionariesContext.getHeaderName(NonStandardDictionary.SHORT_TEXTS);
links.frequencyVocabularyTopHeader = dictionariesContext.getHeaderName(NonStandardDictionary.FREQUENCY_VOCABULARY);
links.oneHundredRussianTopHeader = dictionariesContext.getHeaderName(NonStandardDictionary.ONE_HUNDRED_RUSSIAN);
links.digitsOneHundredTopHeader = dictionariesContext.getHeaderName(NonStandardDictionary.DIGITS_ONE_HUNDRED);
links.trainingIndexFingersTopHeader = dictionariesContext.getHeaderName(NonStandardDictionary.TRAINING_INDEX_FINGERS);
links.ringFingersTopHeader = dictionariesContext.getHeaderName(NonStandardDictionary.RING_FINGERS);
links.pinkiesPlusTopHeader = dictionariesContext.getHeaderName(NonStandardDictionary.PINKIES_PLUS);
links.normalInEnglishTopBySpeedPage1 = context.getTopBySpeedFirstPageFilePath(NonStandardDictionary.NORMAL_IN_ENGLISH);
links.miniMarathonTopBySpeedPage1 = context.getTopBySpeedFirstPageFilePath(NonStandardDictionary.MINI_MARATHON);
links.shortTextsTopBySpeedPage1 = context.getTopBySpeedFirstPageFilePath(NonStandardDictionary.SHORT_TEXTS);
links.frequencyVocabularyTopBySpeedPage1 = context.getTopBySpeedFirstPageFilePath(NonStandardDictionary.FREQUENCY_VOCABULARY);
links.oneHundredRussianTopBySpeedPage1 = context.getTopBySpeedFirstPageFilePath(NonStandardDictionary.ONE_HUNDRED_RUSSIAN);
links.digitsOneHundredTopBySpeedPage1 = context.getTopBySpeedFirstPageFilePath(NonStandardDictionary.DIGITS_ONE_HUNDRED);
links.trainingIndexFingersTopBySpeedPage1 = context.getTopBySpeedFirstPageFilePath(NonStandardDictionary.TRAINING_INDEX_FINGERS);
links.ringFingersTopBySpeedPage1 = context.getTopBySpeedFirstPageFilePath(NonStandardDictionary.RING_FINGERS);
links.pinkiesPlusTopBySpeedPage1 = context.getTopBySpeedFirstPageFilePath(NonStandardDictionary.PINKIES_PLUS);

links.normalInEnglishTopHeader = context.getHeaderName(NonStandardDictionary.NORMAL_IN_ENGLISH);
links.miniMarathonTopHeader = context.getHeaderName(NonStandardDictionary.MINI_MARATHON);
links.shortTextsTopHeader = context.getHeaderName(NonStandardDictionary.SHORT_TEXTS);
links.frequencyVocabularyTopHeader = context.getHeaderName(NonStandardDictionary.FREQUENCY_VOCABULARY);
links.oneHundredRussianTopHeader = context.getHeaderName(NonStandardDictionary.ONE_HUNDRED_RUSSIAN);
links.digitsOneHundredTopHeader = context.getHeaderName(NonStandardDictionary.DIGITS_ONE_HUNDRED);
links.trainingIndexFingersTopHeader = context.getHeaderName(NonStandardDictionary.TRAINING_INDEX_FINGERS);
links.ringFingersTopHeader = context.getHeaderName(NonStandardDictionary.RING_FINGERS);
links.pinkiesPlusTopHeader = context.getHeaderName(NonStandardDictionary.PINKIES_PLUS);

return links;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ru.klavogonki.statistics.export

import ru.klavogonki.statistics.Config
import ru.klavogonki.statistics.export.vocabulary.NonStandardVocabularyGeneratorContext
import ru.klavogonki.statistics.freemarker.Links
import ru.klavogonki.statistics.repository.PlayerVocabularyStatsRepository
import ru.klavogonki.statistics.util.DateUtils
import java.time.LocalDateTime
Expand All @@ -15,12 +16,14 @@ data class ExportContext(
@JvmField val dataDownloadStartDate: LocalDateTime,
@JvmField val dataDownloadEndDate: LocalDateTime,
@JvmField val repository: PlayerVocabularyStatsRepository,
@JvmField val nonStandardDictionariesGeneratorContext: NonStandardVocabularyGeneratorContext
@JvmField val nonStandardDictionariesGeneratorContext: NonStandardVocabularyGeneratorContext,
@JvmField val links: Links
) {
constructor(
config: Config,
repository: PlayerVocabularyStatsRepository,
nonStandardDictionariesGeneratorContext: NonStandardVocabularyGeneratorContext
nonStandardDictionariesGeneratorContext: NonStandardVocabularyGeneratorContext,
links: Links
) : this( // we can use property-access from a Java class when there is an explicit geter
// but the lombok-generated getters do NOT work

Expand All @@ -38,6 +41,7 @@ data class ExportContext(

repository = repository,

nonStandardDictionariesGeneratorContext = nonStandardDictionariesGeneratorContext
nonStandardDictionariesGeneratorContext = nonStandardDictionariesGeneratorContext,
links = links
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ru.klavogonki.statistics.export

import ru.klavogonki.statistics.entity.PlayerVocabularyStatsEntity
import ru.klavogonki.statistics.export.vocabulary.NonStandardVocabularyTopExporterGenerator.generateContext
import ru.klavogonki.statistics.freemarker.Links
import ru.klavogonki.statistics.repository.PlayerVocabularyStatsRepository
import ru.klavogonki.statistics.util.JacksonUtils
import java.util.Optional
Expand All @@ -25,13 +26,16 @@ object ExportContextFactory {
generatorConfig.nonStandardDictionariesCodes
)

val links = Links.create(nonStandardDictionariesGeneratorContext)

// no repository required for freemarker templates
val fakeRepository = createFakeRepository()

return ExportContext(
config,
fakeRepository,
nonStandardDictionariesGeneratorContext
nonStandardDictionariesGeneratorContext,
links
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import ru.klavogonki.statistics.export.vocabulary.standard.NoErrorTopExporter
import ru.klavogonki.statistics.export.vocabulary.standard.NormalTopExporter
import ru.klavogonki.statistics.export.vocabulary.standard.ReferatsTopExporter
import ru.klavogonki.statistics.export.vocabulary.standard.SprintTopExporter
import ru.klavogonki.statistics.freemarker.Links
import ru.klavogonki.statistics.repository.PlayerVocabularyStatsRepository
import ru.klavogonki.statistics.springboot.Profiles

Expand Down Expand Up @@ -74,7 +75,14 @@ class StatisticsGenerator : Logging {
val nonStandardDictionariesGeneratorContext =
NonStandardVocabularyTopExporterGenerator.generateContext(generatorConfig.nonStandardDictionariesCodes)

val context = ExportContext(config, repository, nonStandardDictionariesGeneratorContext)
val links = Links.create(nonStandardDictionariesGeneratorContext)

val context = ExportContext(
config,
repository,
nonStandardDictionariesGeneratorContext,
links
)

/*
context.webRootDir = "C:/java/kgparser/kgstats/src/main/webapp/";
Expand Down

0 comments on commit a892bda

Please sign in to comment.