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.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.dbflute.cbean.result.ListResultBean;
32  
33  public class FavoriteLogService {
34      @Resource
35      protected SystemHelper systemHelper;
36  
37      @Resource
38      protected UserInfoBhv userInfoBhv;
39  
40      @Resource
41      protected FavoriteLogBhv favoriteLogBhv;
42  
43      @Resource
44      protected FessConfig fessConfig;
45  
46      public boolean addUrl(final String userCode, final BiConsumer<UserInfo, FavoriteLog> favoriteLogLambda) {
47          return userInfoBhv.selectByPK(userCode).map(userInfo -> {
48              final FavoriteLog favoriteLog = new FavoriteLog();
49              favoriteLogLambda.accept(userInfo, favoriteLog);
50              favoriteLogBhv.insert(favoriteLog);
51              return true;
52          }).orElse(false);
53      }
54  
55      public List<String> getUrlList(final String userCode, final List<String> urlList) {
56          if (urlList.isEmpty()) {
57              return urlList;
58          }
59  
60          return userInfoBhv.selectByPK(userCode).map(userInfo -> {
61              final ListResultBean<FavoriteLog> list = favoriteLogBhv.selectList(cb2 -> {
62                  cb2.query().setUserInfoId_Equal(userInfo.getId());
63                  cb2.query().setUrl_InScope(urlList);
64                  cb2.fetchFirst(fessConfig.getPageFavoriteLogMaxFetchSizeAsInteger());
65              });
66              if (!list.isEmpty()) {
67                  final List<String> newUrlList = new ArrayList<>(list.size());
68                  for (final FavoriteLog favoriteLog : list) {
69                      newUrlList.add(favoriteLog.getUrl());
70                  }
71                  return newUrlList;
72              }
73              return Collections.<String> emptyList();
74          }).orElse(Collections.<String> emptyList());
75  
76      }
77  
78  }