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