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.SynonymPager;
24  import org.codelibs.fess.dict.DictionaryFile.PagingList;
25  import org.codelibs.fess.dict.DictionaryManager;
26  import org.codelibs.fess.dict.synonym.SynonymFile;
27  import org.codelibs.fess.dict.synonym.SynonymItem;
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 synonyms.
35   * This class provides methods to interact with synonym dictionaries,
36   * including retrieving, storing, and deleting synonyms.
37   */
38  public class SynonymService {
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 synonym service.
49       */
50      public SynonymService() {
51          // do nothing
52      }
53  
54      /**
55       * Retrieves a list of synonyms for a given dictionary and pager.
56       *
57       * @param dictId       The ID of the dictionary.
58       * @param synonymPager The pager for controlling pagination.
59       * @return A list of synonyms.
60       */
61      public List<SynonymItem> getSynonymList(final String dictId, final SynonymPager synonymPager) {
62          return getSynonymFile(dictId).map(file -> {
63              final int pageSize = synonymPager.getPageSize();
64              final PagingList<SynonymItem> synonymList = file.selectList((synonymPager.getCurrentPageNumber() - 1) * pageSize, pageSize);
65  
66              // update pager
67              BeanUtil.copyBeanToBean(synonymList, synonymPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
68              synonymList.setPageRangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
69              synonymPager.setPageNumberList(synonymList.createPageNumberList());
70  
71              return (List<SynonymItem>) synonymList;
72          }).orElse(Collections.emptyList());
73      }
74  
75      /**
76       * Retrieves a synonym file for a given dictionary ID.
77       *
78       * @param dictId The ID of the dictionary.
79       * @return An optional entity containing the synonym file, or empty if not found.
80       */
81      public OptionalEntity<SynonymFile> getSynonymFile(final String dictId) {
82          return dictionaryManager.getDictionaryFile(dictId)
83                  .filter(SynonymFile.class::isInstance)
84                  .map(file -> OptionalEntity.of((SynonymFile) file))
85                  .orElse(OptionalEntity.empty());
86      }
87  
88      /**
89       * Retrieves a specific synonym item by its ID.
90       *
91       * @param dictId The ID of the dictionary.
92       * @param id     The ID of the synonym item.
93       * @return An optional entity containing the synonym item, or empty if not found.
94       */
95      public OptionalEntity<SynonymItem> getSynonymItem(final String dictId, final long id) {
96          return getSynonymFile(dictId).map(file -> file.get(id).get());
97      }
98  
99      /**
100      * Stores a synonym item in the specified dictionary.
101      *
102      * @param dictId      The ID of the dictionary.
103      * @param synonymItem The synonym item to store.
104      */
105     public void store(final String dictId, final SynonymItem synonymItem) {
106         getSynonymFile(dictId).ifPresent(file -> {
107             if (synonymItem.getId() == 0) {
108                 file.insert(synonymItem);
109             } else {
110                 file.update(synonymItem);
111             }
112         });
113     }
114 
115     /**
116      * Deletes a synonym item from the specified dictionary.
117      *
118      * @param dictId      The ID of the dictionary.
119      * @param synonymItem The synonym item to delete.
120      */
121     public void delete(final String dictId, final SynonymItem synonymItem) {
122         getSynonymFile(dictId).ifPresent(file -> {
123             file.delete(synonymItem);
124         });
125     }
126 }