1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.app.service;
17
18 import java.util.Collections;
19 import java.util.List;
20
21 import org.codelibs.core.beans.util.BeanUtil;
22 import org.codelibs.fess.Constants;
23 import org.codelibs.fess.app.pager.ProtwordsPager;
24 import org.codelibs.fess.dict.DictionaryFile.PagingList;
25 import org.codelibs.fess.dict.DictionaryManager;
26 import org.codelibs.fess.dict.protwords.ProtwordsFile;
27 import org.codelibs.fess.dict.protwords.ProtwordsItem;
28 import org.codelibs.fess.mylasta.direction.FessConfig;
29 import org.dbflute.optional.OptionalEntity;
30
31 import jakarta.annotation.Resource;
32
33
34
35
36
37 public class ProtwordsService {
38
39
40
41
42 public ProtwordsService() {
43
44 }
45
46
47 @Resource
48 protected DictionaryManager dictionaryManager;
49
50
51 @Resource
52 protected FessConfig fessConfig;
53
54
55
56
57
58
59
60 public List<ProtwordsItem> getProtwordsList(final String dictId, final ProtwordsPager protwordsPager) {
61 return getProtwordsFile(dictId).map(file -> {
62 final int pageSize = protwordsPager.getPageSize();
63 final PagingList<ProtwordsItem> protwordsList =
64 file.selectList((protwordsPager.getCurrentPageNumber() - 1) * pageSize, pageSize);
65
66
67 BeanUtil.copyBeanToBean(protwordsList, protwordsPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
68 protwordsList.setPageRangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
69 protwordsPager.setPageNumberList(protwordsList.createPageNumberList());
70
71 return (List<ProtwordsItem>) protwordsList;
72 }).orElse(Collections.emptyList());
73 }
74
75
76
77
78
79
80 public OptionalEntity<ProtwordsFile> getProtwordsFile(final String dictId) {
81 return dictionaryManager.getDictionaryFile(dictId)
82 .filter(ProtwordsFile.class::isInstance)
83 .map(file -> OptionalEntity.of((ProtwordsFile) file))
84 .orElse(OptionalEntity.empty());
85 }
86
87
88
89
90
91
92
93 public OptionalEntity<ProtwordsItem> getProtwordsItem(final String dictId, final long id) {
94 return getProtwordsFile(dictId).map(file -> file.get(id).get());
95 }
96
97
98
99
100
101
102 public void store(final String dictId, final ProtwordsItem protwordsItem) {
103 getProtwordsFile(dictId).ifPresent(file -> {
104 if (protwordsItem.getId() == 0) {
105 file.insert(protwordsItem);
106 } else {
107 file.update(protwordsItem);
108 }
109 });
110 }
111
112
113
114
115
116
117 public void delete(final String dictId, final ProtwordsItem protwordsItem) {
118 getProtwordsFile(dictId).ifPresent(file -> {
119 file.delete(protwordsItem);
120 });
121 }
122 }