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.ProtwordsPager;
24  import org.codelibs.fess.dict.DictionaryFile.PagingList;
25  import org.codelibs.fess.dict.DictionaryManager;
26  import org.codelibs.fess.dict.protwords.ProtwordsFile;
27  import org.codelibs.fess.dict.protwords.ProtwordsItem;
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 protected words dictionary.
35   * This service provides operations for managing protected words dictionary files and items.
36   */
37  public class ProtwordsService {
38  
39      /**
40       * Default constructor.
41       */
42      public ProtwordsService() {
43          // Default constructor
44      }
45  
46      /** Dictionary manager for handling dictionary files */
47      @Resource
48      protected DictionaryManager dictionaryManager;
49  
50      /** Configuration for Fess */
51      @Resource
52      protected FessConfig fessConfig;
53  
54      /**
55       * Gets a paginated list of protected words items.
56       * @param dictId the dictionary ID
57       * @param protwordsPager the pager for pagination
58       * @return the list of protected words items
59       */
60      public List<ProtwordsItem> getProtwordsList(final String dictId, final ProtwordsPager protwordsPager) {
61          return getProtwordsFile(dictId).map(file -> {
62              final int pageSize = protwordsPager.getPageSize();
63              final PagingList<ProtwordsItem> protwordsList =
64                      file.selectList((protwordsPager.getCurrentPageNumber() - 1) * pageSize, pageSize);
65  
66              // update pager
67              BeanUtil.copyBeanToBean(protwordsList, protwordsPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
68              protwordsList.setPageRangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
69              protwordsPager.setPageNumberList(protwordsList.createPageNumberList());
70  
71              return (List<ProtwordsItem>) protwordsList;
72          }).orElse(Collections.emptyList());
73      }
74  
75      /**
76       * Gets the protected words file for the specified dictionary ID.
77       * @param dictId the dictionary ID
78       * @return the protected words file if found
79       */
80      public OptionalEntity<ProtwordsFile> getProtwordsFile(final String dictId) {
81          return dictionaryManager.getDictionaryFile(dictId)
82                  .filter(ProtwordsFile.class::isInstance)
83                  .map(file -> OptionalEntity.of((ProtwordsFile) file))
84                  .orElse(OptionalEntity.empty());
85      }
86  
87      /**
88       * Gets a specific protected words item by ID.
89       * @param dictId the dictionary ID
90       * @param id the item ID
91       * @return the protected words item if found
92       */
93      public OptionalEntity<ProtwordsItem> getProtwordsItem(final String dictId, final long id) {
94          return getProtwordsFile(dictId).map(file -> file.get(id).get());
95      }
96  
97      /**
98       * Stores a protected words item (insert or update).
99       * @param dictId the dictionary ID
100      * @param protwordsItem the item to store
101      */
102     public void store(final String dictId, final ProtwordsItem protwordsItem) {
103         getProtwordsFile(dictId).ifPresent(file -> {
104             if (protwordsItem.getId() == 0) {
105                 file.insert(protwordsItem);
106             } else {
107                 file.update(protwordsItem);
108             }
109         });
110     }
111 
112     /**
113      * Deletes a protected words item.
114      * @param dictId the dictionary ID
115      * @param protwordsItem the item to delete
116      */
117     public void delete(final String dictId, final ProtwordsItem protwordsItem) {
118         getProtwordsFile(dictId).ifPresent(file -> {
119             file.delete(protwordsItem);
120         });
121     }
122 }