1
|
|
|
package com.osomapps.pt.dictionary; |
2
|
|
|
|
3
|
|
|
import java.time.LocalDateTime; |
4
|
|
|
import java.util.List; |
5
|
|
|
import java.util.Objects; |
6
|
|
|
import org.springframework.stereotype.Service; |
7
|
|
|
|
8
|
|
|
@Service |
9
|
|
|
public class DictionaryService { |
10
|
|
|
|
11
|
|
|
private final DictionaryRepository dictionaryRepository; |
12
|
|
|
|
13
|
|
|
DictionaryService(DictionaryRepository dictionaryRepository) { |
14
|
|
|
this.dictionaryRepository = dictionaryRepository; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public String getNewDictionaryDataKey(DictionaryName dName) { |
18
|
|
|
final List<DictionaryData> allExerciseEnNames = |
19
|
|
|
dictionaryRepository.findDictionaryAllValues( |
20
|
|
|
DictionaryRepository.ENG_LANGUAGE, dName.name(), LocalDateTime.now()); |
21
|
|
|
if (allExerciseEnNames.isEmpty()) { |
22
|
|
|
return "10"; |
23
|
|
|
} |
24
|
|
|
final String biggestKey = |
25
|
|
|
allExerciseEnNames.stream() |
26
|
|
|
.filter(data -> Objects.nonNull(data.getDkey())) |
27
|
|
|
.filter(data -> data.getDkey().matches("\\d+")) |
28
|
|
|
.sorted( |
29
|
|
|
(d1, d2) -> |
30
|
|
|
Integer.compare( |
31
|
|
|
Integer.parseInt(d2.getDkey()), |
32
|
|
|
Integer.parseInt(d1.getDkey()))) |
33
|
|
|
.findFirst() |
34
|
|
|
.orElse(new DictionaryData().setDkey("0")) |
35
|
|
|
.getDkey(); |
36
|
|
|
return Integer.toString(Integer.parseInt(biggestKey) + 10); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public String createDictionaryDataKey( |
40
|
|
|
DictionaryName dName, String dKey, String dataNameEn, String dataNameNo) { |
41
|
|
|
final String localDkey = (dKey == null) ? getNewDictionaryDataKey(dName) : dKey; |
42
|
|
|
final DictionaryData dataEn = new DictionaryData(); |
43
|
|
|
dataEn.setDlanguage(DictionaryRepository.ENG_LANGUAGE); |
44
|
|
|
dataEn.setDname(dName.name()); |
45
|
|
|
dataEn.setDkey(localDkey); |
46
|
|
|
dataEn.setDvalue(dataNameEn); |
47
|
|
|
dictionaryRepository.save(dataEn); |
48
|
|
|
final DictionaryData dataNo = new DictionaryData(); |
49
|
|
|
dataNo.setDlanguage(DictionaryRepository.NOR_LANGUAGE); |
50
|
|
|
dataNo.setDname(dName.name()); |
51
|
|
|
dataNo.setDkey(localDkey); |
52
|
|
|
dataNo.setDvalue(dataNameNo); |
53
|
|
|
dictionaryRepository.save(dataNo); |
54
|
|
|
return localDkey; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public String getEnValue(DictionaryName dName, String dKey, String defaultValue) { |
58
|
|
|
final List<DictionaryData> dictionaryDatas = |
59
|
|
|
dictionaryRepository.findDictionaryValue( |
60
|
|
|
DictionaryRepository.ENG_LANGUAGE, dName.name(), dKey, LocalDateTime.now()); |
61
|
|
|
return dictionaryDatas.isEmpty() ? defaultValue : dictionaryDatas.get(0).getDvalue(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public String getNoValue(DictionaryName dName, String dKey, String defaultValue) { |
65
|
|
|
final List<DictionaryData> dictionaryDatas = |
66
|
|
|
dictionaryRepository.findDictionaryValue( |
67
|
|
|
DictionaryRepository.NOR_LANGUAGE, dName.name(), dKey, LocalDateTime.now()); |
68
|
|
|
return dictionaryDatas.isEmpty() ? defaultValue : dictionaryDatas.get(0).getDvalue(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public void deleteDatas(DictionaryName dName, String dKey) { |
72
|
|
|
dictionaryRepository.deleteAll( |
73
|
|
|
dictionaryRepository.findDictionaryByKey( |
74
|
|
|
DictionaryRepository.ENG_LANGUAGE, |
75
|
|
|
dName.name(), |
76
|
|
|
dKey, |
77
|
|
|
LocalDateTime.now())); |
78
|
|
|
dictionaryRepository.deleteAll( |
79
|
|
|
dictionaryRepository.findDictionaryByKey( |
80
|
|
|
DictionaryRepository.NOR_LANGUAGE, |
81
|
|
|
dName.name(), |
82
|
|
|
dKey, |
83
|
|
|
LocalDateTime.now())); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|