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.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  import java.util.function.BiConsumer;
22  
23  import javax.annotation.Resource;
24  
25  import org.codelibs.fess.es.log.exbhv.FavoriteLogBhv;
26  import org.codelibs.fess.es.log.exbhv.UserInfoBhv;
27  import org.codelibs.fess.es.log.exentity.FavoriteLog;
28  import org.codelibs.fess.es.log.exentity.UserInfo;
29  import org.codelibs.fess.helper.SystemHelper;
30  import org.codelibs.fess.mylasta.direction.FessConfig;
31  import org.codelibs.fess.util.ComponentUtil;
32  import org.dbflute.cbean.result.ListResultBean;
33  
34  public class FavoriteLogService {
35      @Resource
36      protected SystemHelper systemHelper;
37  
38      @Resource
39      protected UserInfoBhv userInfoBhv;
40  
41      @Resource
42      protected FavoriteLogBhv favoriteLogBhv;
43  
44      @Resource
45      protected FessConfig fessConfig;
46  
47      public boolean addUrl(final String userCode, final BiConsumer<UserInfo, FavoriteLog> favoriteLogLambda) {
48          return userInfoBhv.selectByPK(userCode).map(userInfo -> {
49              final FavoriteLog favoriteLog = new FavoriteLog();
50              favoriteLogLambda.accept(userInfo, favoriteLog);
51              favoriteLogBhv.insert(favoriteLog);
52              if (fessConfig.isLoggingSearchUseLogfile()) {
53                  ComponentUtil.getSearchLogHelper().writeSearchLogEvent(favoriteLog);
54              }
55              return true;
56          }).orElse(false);
57      }
58  
59      public List<String> getUrlList(final String userCode, final List<String> urlList) {
60          if (urlList.isEmpty()) {
61              return urlList;
62          }
63  
64          return userInfoBhv.selectByPK(userCode).map(userInfo -> {
65              final ListResultBean<FavoriteLog> list = favoriteLogBhv.selectList(cb2 -> {
66                  cb2.query().setUserInfoId_Equal(userInfo.getId());
67                  cb2.query().setUrl_InScope(urlList);
68                  cb2.fetchFirst(fessConfig.getPageFavoriteLogMaxFetchSizeAsInteger());
69              });
70              if (!list.isEmpty()) {
71                  final List<String> newUrlList = new ArrayList<>(list.size());
72                  for (final FavoriteLog favoriteLog : list) {
73                      newUrlList.add(favoriteLog.getUrl());
74                  }
75                  return newUrlList;
76              }
77              return Collections.<String> emptyList();
78          }).orElse(Collections.<String> emptyList());
79  
80      }
81  
82  }