View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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.KuromojiPager;
24  import org.codelibs.fess.dict.DictionaryFile.PagingList;
25  import org.codelibs.fess.dict.DictionaryManager;
26  import org.codelibs.fess.dict.kuromoji.KuromojiFile;
27  import org.codelibs.fess.dict.kuromoji.KuromojiItem;
28  import org.codelibs.fess.mylasta.direction.FessConfig;
29  import org.dbflute.optional.OptionalEntity;
30  
31  import jakarta.annotation.Resource;
32  
33  /**
34   * Service class for Kuromoji.
35   */
36  public class KuromojiService {
37      /** The dictionary manager. */
38      @Resource
39      protected DictionaryManager dictionaryManager;
40  
41      /** The Fess config. */
42      @Resource
43      protected FessConfig fessConfig;
44  
45      /**
46       * Default constructor.
47       */
48      public KuromojiService() {
49          // do nothing
50      }
51  
52      /**
53       * Get a list of Kuromoji items.
54       *
55       * @param dictId The dictionary ID.
56       * @param kuromojiPager The pager for Kuromoji.
57       * @return A list of Kuromoji items.
58       */
59      public List<KuromojiItem> getKuromojiList(final String dictId, final KuromojiPager kuromojiPager) {
60          return getKuromojiFile(dictId).map(file -> {
61              final int pageSize = kuromojiPager.getPageSize();
62              final PagingList<KuromojiItem> kuromojiList = file.selectList((kuromojiPager.getCurrentPageNumber() - 1) * pageSize, pageSize);
63  
64              // update pager
65              BeanUtil.copyBeanToBean(kuromojiList, kuromojiPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
66              kuromojiList.setPageRangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
67              kuromojiPager.setPageNumberList(kuromojiList.createPageNumberList());
68  
69              return (List<KuromojiItem>) kuromojiList;
70          }).orElse(Collections.emptyList());
71      }
72  
73      /**
74       * Get a Kuromoji file.
75       *
76       * @param dictId The dictionary ID.
77       * @return An optional entity of the Kuromoji file.
78       */
79      public OptionalEntity<KuromojiFile> getKuromojiFile(final String dictId) {
80          return dictionaryManager.getDictionaryFile(dictId)
81                  .filter(KuromojiFile.class::isInstance)
82                  .map(file -> OptionalEntity.of((KuromojiFile) file))
83                  .orElse(OptionalEntity.empty());
84      }
85  
86      /**
87       * Get a Kuromoji item.
88       *
89       * @param dictId The dictionary ID.
90       * @param id The ID of the Kuromoji item.
91       * @return An optional entity of the Kuromoji item.
92       */
93      public OptionalEntity<KuromojiItem> getKuromojiItem(final String dictId, final long id) {
94          return getKuromojiFile(dictId).map(file -> file.get(id).get());
95      }
96  
97      /**
98       * Store a Kuromoji item.
99       *
100      * @param dictId The dictionary ID.
101      * @param kuromojiItem The Kuromoji item to store.
102      */
103     public void store(final String dictId, final KuromojiItem kuromojiItem) {
104         getKuromojiFile(dictId).ifPresent(file -> {
105             if (kuromojiItem.getId() == 0) {
106                 file.insert(kuromojiItem);
107             } else {
108                 file.update(kuromojiItem);
109             }
110         });
111     }
112 
113     /**
114      * Delete a Kuromoji item.
115      *
116      * @param dictId The dictionary ID.
117      * @param kuromojiItem The Kuromoji item to delete.
118      */
119     public void delete(final String dictId, final KuromojiItem kuromojiItem) {
120         getKuromojiFile(dictId).ifPresent(file -> {
121             file.delete(kuromojiItem);
122         });
123     }
124 }