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