Skip to content

Commit 8183a26

Browse files
author
Andrew Pikler
committed
categories are not unique - use heb title for translation
1 parent 35b526d commit 8183a26

3 files changed

Lines changed: 102 additions & 18 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.github.pyckle.oref.integration.translationstores;
2+
3+
import com.github.pyckle.oref.integration.dto.AlertTranslation;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.Objects;
8+
9+
import static java.util.Objects.requireNonNullElse;
10+
11+
public class AlertCategoryStore
12+
{
13+
private final CategoryType categoryType;
14+
private final int id;
15+
private final Map<String, AlertTranslation> hebTitle2Alert;
16+
17+
private AlertCategoryStore(CategoryType categoryType, int id, Map<String, AlertTranslation> hebTitle2Alert)
18+
{
19+
this.categoryType = categoryType;
20+
this.id = id;
21+
this.hebTitle2Alert = Map.copyOf(hebTitle2Alert);
22+
}
23+
24+
public AlertTranslation getAlertTranslation(String hebTitle)
25+
{
26+
if (hebTitle2Alert.size() == 1)
27+
{
28+
return hebTitle2Alert.values().iterator().next();
29+
}
30+
return hebTitle2Alert.get(hebTitle);
31+
}
32+
33+
public static class Builder
34+
{
35+
private final CategoryType categoryType;
36+
private final int id;
37+
private final HashMap<String, AlertTranslation> map = new HashMap<>();
38+
39+
public Builder(CategoryType categoryType, int id)
40+
{
41+
this.categoryType = categoryType;
42+
this.id = id;
43+
}
44+
45+
public void addAlertTranslation(AlertTranslation alertTranslation)
46+
{
47+
map.put(requireNonNullElse(alertTranslation.hebTitle(), requireNonNullElse(alertTranslation.heb(), "")),
48+
alertTranslation);
49+
}
50+
51+
AlertCategoryStore build()
52+
{
53+
return new AlertCategoryStore(categoryType, id, map);
54+
}
55+
}
56+
}

oref-integration/src/main/java/com/github/pyckle/oref/integration/translationstores/CategoryDescriptionStore.java

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,68 @@
66
import java.util.List;
77
import java.util.Map;
88

9-
public class CategoryDescriptionStore {
10-
private final Map<Integer, AlertTranslation> catIdToDescription;
11-
private final Map<Integer, AlertTranslation> matIdToDescription;
12-
13-
public CategoryDescriptionStore(List<AlertTranslation> alertDescriptionList) {
14-
catIdToDescription = new HashMap<>();
15-
matIdToDescription = new HashMap<>();
16-
for (AlertTranslation alertTranslation : alertDescriptionList) {
17-
catIdToDescription.put(alertTranslation.catId(), alertTranslation);
18-
matIdToDescription.put(alertTranslation.matrixCatId(), alertTranslation);
9+
import static com.github.pyckle.oref.integration.translationstores.CategoryType.CATEGORY;
10+
import static com.github.pyckle.oref.integration.translationstores.CategoryType.MATRIX;
11+
12+
public class CategoryDescriptionStore
13+
{
14+
private final Map<CategoryKey, AlertCategoryStore> matIdToDescription;
15+
16+
public CategoryDescriptionStore(List<AlertTranslation> alertDescriptionList)
17+
{
18+
Map<CategoryKey, AlertCategoryStore.Builder> builders = new HashMap<>();
19+
for (AlertTranslation alertTranslation : alertDescriptionList)
20+
{
21+
builders.computeIfAbsent(new CategoryKey(CATEGORY, alertTranslation.catId()),
22+
k -> new AlertCategoryStore.Builder(k.categoryType(), k.id()))
23+
.addAlertTranslation(alertTranslation);
24+
builders.computeIfAbsent(new CategoryKey(MATRIX, alertTranslation.matrixCatId()),
25+
k -> new AlertCategoryStore.Builder(k.categoryType(), k.id()))
26+
.addAlertTranslation(alertTranslation);
1927
}
28+
matIdToDescription = Map.ofEntries(
29+
builders.entrySet().stream().map(e -> Map.entry(e.getKey(), e.getValue().build()))
30+
.toArray(Map.Entry[]::new));
2031
}
2132

22-
public String getAlertStringFromCatId(String lang, int cat, String defaultVal) {
23-
return labelOrDefault(lang, defaultVal, catIdToDescription.get(cat));
33+
public String getAlertStringFromCatId(String lang, int cat, String defaultVal)
34+
{
35+
return labelOrDefault(lang, defaultVal, matIdToDescription.get(new CategoryKey(CATEGORY, cat)));
2436
}
2537

26-
public String getAlertStringFromMatId(String lang, String cat, String defaultVal) {
38+
public String getAlertStringFromMatId(String lang, String cat, String defaultVal)
39+
{
2740
if (isValidInteger(cat))
2841
return defaultVal;
2942
return getAlertStringFromMatId(lang, Integer.parseInt(cat), defaultVal);
3043
}
3144

32-
private static boolean isValidInteger(String cat) {
45+
private static boolean isValidInteger(String cat)
46+
{
3347
if (cat.length() > 8)
3448
return true;
35-
for (int i = 0; i < cat.length(); i++) {
49+
for (int i = 0; i < cat.length(); i++)
50+
{
3651
if (cat.charAt(i) < '0' || cat.charAt(i) > '9')
3752
return true;
3853
}
3954
return false;
4055
}
4156

42-
public String getAlertStringFromMatId(String lang, int cat, String defaultVal) {
43-
return labelOrDefault(lang, defaultVal, matIdToDescription.get(cat));
57+
public String getAlertStringFromMatId(String lang, int cat, String defaultVal)
58+
{
59+
return labelOrDefault(lang, defaultVal, matIdToDescription.get(new CategoryKey(MATRIX, cat)));
4460
}
4561

46-
private static String labelOrDefault(String lang, String defaultVal, AlertTranslation alertTranslation) {
62+
private static String labelOrDefault(String lang, String defaultVal, AlertCategoryStore alertCategoryStore)
63+
{
64+
var alertTranslation = alertCategoryStore == null ? null : alertCategoryStore.getAlertTranslation(defaultVal);
4765
if (alertTranslation == null)
4866
return defaultVal;
4967
return alertTranslation.title(lang, defaultVal);
5068
}
69+
70+
record CategoryKey(CategoryType categoryType, int id)
71+
{
72+
}
5173
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.github.pyckle.oref.integration.translationstores;
2+
3+
enum CategoryType
4+
{
5+
CATEGORY, MATRIX
6+
}

0 commit comments

Comments
 (0)