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 javax.annotation.Resource;
22
23 import org.codelibs.core.beans.util.BeanUtil;
24 import org.codelibs.fess.Constants;
25 import org.codelibs.fess.app.pager.ProtwordsPager;
26 import org.codelibs.fess.dict.DictionaryFile.PagingList;
27 import org.codelibs.fess.dict.DictionaryManager;
28 import org.codelibs.fess.dict.protwords.ProtwordsFile;
29 import org.codelibs.fess.dict.protwords.ProtwordsItem;
30 import org.codelibs.fess.mylasta.direction.FessConfig;
31 import org.dbflute.optional.OptionalEntity;
32
33 public class ProtwordsService {
34 @Resource
35 protected DictionaryManager dictionaryManager;
36
37 @Resource
38 protected FessConfig fessConfig;
39
40 public List<ProtwordsItem> getProtwordsList(final String dictId, final ProtwordsPager protwordsPager) {
41 return getProtwordsFile(dictId).map(
42 file -> {
43 final int pageSize = protwordsPager.getPageSize();
44 final PagingList<ProtwordsItem> protwordsList =
45 file.selectList((protwordsPager.getCurrentPageNumber() - 1) * pageSize, pageSize);
46
47
48 BeanUtil.copyBeanToBean(protwordsList, protwordsPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
49 protwordsList.setPageRangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
50 protwordsPager.setPageNumberList(protwordsList.createPageNumberList());
51
52 return (List<ProtwordsItem>) protwordsList;
53 }).orElse(Collections.emptyList());
54 }
55
56 public OptionalEntity<ProtwordsFile> getProtwordsFile(final String dictId) {
57 return dictionaryManager.getDictionaryFile(dictId).filter(file -> file instanceof ProtwordsFile)
58 .map(file -> OptionalEntity.of((ProtwordsFile) file)).orElse(OptionalEntity.empty());
59 }
60
61 public OptionalEntity<ProtwordsItem> getProtwordsItem(final String dictId, final long id) {
62 return getProtwordsFile(dictId).map(file -> file.get(id).get());
63 }
64
65 public void store(final String dictId, final ProtwordsItem protwordsItem) {
66 getProtwordsFile(dictId).ifPresent(file -> {
67 if (protwordsItem.getId() == 0) {
68 file.insert(protwordsItem);
69 } else {
70 file.update(protwordsItem);
71 }
72 });
73 }
74
75 public void delete(final String dictId, final ProtwordsItem protwordsItem) {
76 getProtwordsFile(dictId).ifPresent(file -> {
77 file.delete(protwordsItem);
78 });
79 }
80 }