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.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  import java.util.function.BiConsumer;
22  
23  import org.codelibs.fess.helper.SystemHelper;
24  import org.codelibs.fess.mylasta.direction.FessConfig;
25  import org.codelibs.fess.opensearch.log.exbhv.FavoriteLogBhv;
26  import org.codelibs.fess.opensearch.log.exbhv.UserInfoBhv;
27  import org.codelibs.fess.opensearch.log.exentity.FavoriteLog;
28  import org.codelibs.fess.opensearch.log.exentity.UserInfo;
29  import org.codelibs.fess.util.ComponentUtil;
30  import org.dbflute.cbean.result.ListResultBean;
31  
32  import jakarta.annotation.Resource;
33  
34  /**
35   * Service class for managing favorite log operations.
36   * Provides functionality to add URLs to user favorites and retrieve favorite URL lists.
37   * This service handles the persistence and retrieval of favorite log entries in the search system.
38   */
39  public class FavoriteLogService {
40  
41      /**
42       * Default constructor.
43       */
44      public FavoriteLogService() {
45          // Default constructor
46      }
47  
48      /** System helper for common system operations and utilities. */
49      @Resource
50      protected SystemHelper systemHelper;
51  
52      /** Behavior class for user information database operations. */
53      @Resource
54      protected UserInfoBhv userInfoBhv;
55  
56      /** Behavior class for favorite log database operations. */
57      @Resource
58      protected FavoriteLogBhv favoriteLogBhv;
59  
60      /** Configuration settings for the Fess search system. */
61      @Resource
62      protected FessConfig fessConfig;
63  
64      /**
65       * Adds a URL to a user's favorite list.
66       * This method looks up the user by their code and creates a new favorite log entry
67       * using the provided lambda function to populate the favorite log data.
68       *
69       * @param userCode the unique code identifying the user
70       * @param favoriteLogLambda a lambda function that accepts UserInfo and FavoriteLog to populate the favorite log data
71       * @return true if the URL was successfully added to favorites, false if the user was not found
72       */
73      public boolean addUrl(final String userCode, final BiConsumer<UserInfo, FavoriteLog> favoriteLogLambda) {
74          return userInfoBhv.selectByPK(userCode).map(userInfo -> {
75              final FavoriteLog favoriteLog = new FavoriteLog();
76              favoriteLogLambda.accept(userInfo, favoriteLog);
77              favoriteLogBhv.insert(favoriteLog);
78              if (fessConfig.isLoggingSearchUseLogfile()) {
79                  ComponentUtil.getSearchLogHelper().writeSearchLogEvent(favoriteLog);
80              }
81              return true;
82          }).orElse(false);
83      }
84  
85      /**
86       * Retrieves a list of URLs that are in the user's favorites from the provided URL list.
87       * This method filters the input URL list to return only those URLs that the specified user
88       * has marked as favorites.
89       *
90       * @param userCode the unique code identifying the user
91       * @param urlList the list of URLs to check against the user's favorites
92       * @return a list of URLs from the input list that are in the user's favorites, or an empty list if the user is not found or has no matching favorites
93       */
94      public List<String> getUrlList(final String userCode, final List<String> urlList) {
95          if (urlList.isEmpty()) {
96              return urlList;
97          }
98  
99          return userInfoBhv.selectByPK(userCode).map(userInfo -> {
100             final ListResultBean<FavoriteLog> list = favoriteLogBhv.selectList(cb2 -> {
101                 cb2.query().setUserInfoId_Equal(userInfo.getId());
102                 cb2.query().setUrl_InScope(urlList);
103                 cb2.fetchFirst(fessConfig.getPageFavoriteLogMaxFetchSizeAsInteger());
104             });
105             if (!list.isEmpty()) {
106                 final List<String> newUrlList = new ArrayList<>(list.size());
107                 for (final FavoriteLog favoriteLog : list) {
108                     newUrlList.add(favoriteLog.getUrl());
109                 }
110                 return newUrlList;
111             }
112             return Collections.<String> emptyList();
113         }).orElse(Collections.<String> emptyList());
114 
115     }
116 
117 }