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.KuromojiPager;
26 import org.codelibs.fess.dict.DictionaryFile.PagingList;
27 import org.codelibs.fess.dict.DictionaryManager;
28 import org.codelibs.fess.dict.kuromoji.KuromojiFile;
29 import org.codelibs.fess.dict.kuromoji.KuromojiItem;
30 import org.codelibs.fess.mylasta.direction.FessConfig;
31 import org.dbflute.optional.OptionalEntity;
32
33 public class KuromojiService {
34 @Resource
35 protected DictionaryManager dictionaryManager;
36
37 @Resource
38 protected FessConfig fessConfig;
39
40 public List<KuromojiItem> getKuromojiList(final String dictId, final KuromojiPager kuromojiPager) {
41 return getKuromojiFile(dictId).map(file -> {
42 final int pageSize = kuromojiPager.getPageSize();
43 final PagingList<KuromojiItem> kuromojiList = file.selectList((kuromojiPager.getCurrentPageNumber() - 1) * pageSize, pageSize);
44
45
46 BeanUtil.copyBeanToBean(kuromojiList, kuromojiPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
47 kuromojiList.setPageRangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
48 kuromojiPager.setPageNumberList(kuromojiList.createPageNumberList());
49
50 return (List<KuromojiItem>) kuromojiList;
51 }).orElse(Collections.emptyList());
52 }
53
54 public OptionalEntity<KuromojiFile> getKuromojiFile(final String dictId) {
55 return dictionaryManager.getDictionaryFile(dictId).filter(file -> file instanceof KuromojiFile)
56 .map(file -> OptionalEntity.of((KuromojiFile) file)).orElse(OptionalEntity.empty());
57 }
58
59 public OptionalEntity<KuromojiItem> getKuromojiItem(final String dictId, final long id) {
60 return getKuromojiFile(dictId).map(file -> file.get(id).get());
61 }
62
63 public void store(final String dictId, final KuromojiItem kuromojiItem) {
64 getKuromojiFile(dictId).ifPresent(file -> {
65 if (kuromojiItem.getId() == 0) {
66 file.insert(kuromojiItem);
67 } else {
68 file.update(kuromojiItem);
69 }
70 });
71 }
72
73 public void delete(final String dictId, final KuromojiItem kuromojiItem) {
74 getKuromojiFile(dictId).ifPresent(file -> {
75 file.delete(kuromojiItem);
76 });
77 }
78 }