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.StopwordsPager;
24  import org.codelibs.fess.dict.DictionaryFile.PagingList;
25  import org.codelibs.fess.dict.DictionaryManager;
26  import org.codelibs.fess.dict.stopwords.StopwordsFile;
27  import org.codelibs.fess.dict.stopwords.StopwordsItem;
28  import org.codelibs.fess.mylasta.direction.FessConfig;
29  import org.dbflute.optional.OptionalEntity;
30  
31  import jakarta.annotation.Resource;
32  
33  /**
34   * Service for managing stopwords.
35   * This class provides methods to interact with stopwords dictionaries,
36   * including retrieving, storing, and deleting stopwords.
37   */
38  public class StopwordsService {
39      /** The dictionary manager for accessing dictionary files. */
40      @Resource
41      protected DictionaryManager dictionaryManager;
42  
43      /** The Fess configuration for accessing system settings. */
44      @Resource
45      protected FessConfig fessConfig;
46  
47      /**
48       * Constructs a new stopwords service.
49       */
50      public StopwordsService() {
51          // do nothing
52      }
53  
54      /**
55       * Retrieves a list of stopwords for a given dictionary and pager.
56       *
57       * @param dictId         The ID of the dictionary.
58       * @param stopwordsPager The pager for controlling pagination.
59       * @return A list of stopwords.
60       */
61      public List<StopwordsItem> getStopwordsList(final String dictId, final StopwordsPager stopwordsPager) {
62          return getStopwordsFile(dictId).map(file -> {
63              final int pageSize = stopwordsPager.getPageSize();
64              final PagingList<StopwordsItem> stopwordsList =
65                      file.selectList((stopwordsPager.getCurrentPageNumber() - 1) * pageSize, pageSize);
66  
67              // update pager
68              BeanUtil.copyBeanToBean(stopwordsList, stopwordsPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
69              stopwordsList.setPageRangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
70              stopwordsPager.setPageNumberList(stopwordsList.createPageNumberList());
71  
72              return (List<StopwordsItem>) stopwordsList;
73          }).orElse(Collections.emptyList());
74      }
75  
76      /**
77       * Retrieves a stopwords file for a given dictionary ID.
78       *
79       * @param dictId The ID of the dictionary.
80       * @return An optional entity containing the stopwords file, or empty if not found.
81       */
82      public OptionalEntity<StopwordsFile> getStopwordsFile(final String dictId) {
83          return dictionaryManager.getDictionaryFile(dictId)
84                  .filter(StopwordsFile.class::isInstance)
85                  .map(file -> OptionalEntity.of((StopwordsFile) file))
86                  .orElse(OptionalEntity.empty());
87      }
88  
89      /**
90       * Retrieves a specific stopword item by its ID.
91       *
92       * @param dictId The ID of the dictionary.
93       * @param id     The ID of the stopword item.
94       * @return An optional entity containing the stopword item, or empty if not found.
95       */
96      public OptionalEntity<StopwordsItem> getStopwordsItem(final String dictId, final long id) {
97          return getStopwordsFile(dictId).map(file -> file.get(id).get());
98      }
99  
100     /**
101      * Stores a stopword item in the specified dictionary.
102      *
103      * @param dictId        The ID of the dictionary.
104      * @param stopwordsItem The stopword item to store.
105      */
106     public void store(final String dictId, final StopwordsItem stopwordsItem) {
107         getStopwordsFile(dictId).ifPresent(file -> {
108             if (stopwordsItem.getId() == 0) {
109                 file.insert(stopwordsItem);
110             } else {
111                 file.update(stopwordsItem);
112             }
113         });
114     }
115 
116     /**
117      * Deletes a stopword item from the specified dictionary.
118      *
119      * @param dictId        The ID of the dictionary.
120      * @param stopwordsItem The stopword item to delete.
121      */
122     public void delete(final String dictId, final StopwordsItem stopwordsItem) {
123         getStopwordsFile(dictId).ifPresent(file -> {
124             file.delete(stopwordsItem);
125         });
126     }
127 }