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