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.List;
19
20 import org.codelibs.core.beans.util.BeanUtil;
21 import org.codelibs.core.lang.StringUtil;
22 import org.codelibs.fess.Constants;
23 import org.codelibs.fess.app.pager.RelatedContentPager;
24 import org.codelibs.fess.mylasta.direction.FessConfig;
25 import org.codelibs.fess.opensearch.config.cbean.RelatedContentCB;
26 import org.codelibs.fess.opensearch.config.exbhv.RelatedContentBhv;
27 import org.codelibs.fess.opensearch.config.exentity.RelatedContent;
28 import org.codelibs.fess.util.ComponentUtil;
29 import org.dbflute.cbean.result.PagingResultBean;
30 import org.dbflute.optional.OptionalEntity;
31
32 import jakarta.annotation.Resource;
33
34 /**
35 * Service class for managing related content entities.
36 * This service provides CRUD operations for related content, including
37 * retrieval, storage, deletion, and search functionality.
38 */
39 public class RelatedContentService extends FessAppService {
40
41 /**
42 * Default constructor.
43 * Creates a new instance of RelatedContentService.
44 */
45 public RelatedContentService() {
46 super();
47 }
48
49 /**
50 * Behavior class for RelatedContent entity operations.
51 * Provides database access methods for related content management.
52 */
53 @Resource
54 protected RelatedContentBhv relatedContentBhv;
55
56 /**
57 * Configuration settings for Fess application.
58 * Contains various configuration parameters used throughout the application.
59 */
60 @Resource
61 protected FessConfig fessConfig;
62
63 /**
64 * Retrieves a paginated list of related content entities.
65 *
66 * @param relatedContentPager the pager object containing pagination and search parameters
67 * @return a list of RelatedContent entities matching the specified criteria
68 */
69 public List<RelatedContent> getRelatedContentList(final RelatedContentPager relatedContentPager) {
70
71 final PagingResultBean<RelatedContent> relatedContentList = relatedContentBhv.selectPage(cb -> {
72 cb.paging(relatedContentPager.getPageSize(), relatedContentPager.getCurrentPageNumber());
73 setupListCondition(cb, relatedContentPager);
74 });
75
76 // update pager
77 BeanUtil.copyBeanToBean(relatedContentList, relatedContentPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
78 relatedContentPager.setPageNumberList(
79 relatedContentList.pageRange(op -> op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger())).createPageNumberList());
80
81 return relatedContentList;
82 }
83
84 /**
85 * Retrieves a specific related content entity by its ID.
86 *
87 * @param id the unique identifier of the related content
88 * @return an OptionalEntity containing the RelatedContent if found, empty otherwise
89 */
90 public OptionalEntity<RelatedContent> getRelatedContent(final String id) {
91 return relatedContentBhv.selectByPK(id);
92 }
93
94 /**
95 * Stores (inserts or updates) a related content entity.
96 * After storing, updates the related content helper to refresh the cache.
97 *
98 * @param relatedContent the RelatedContent entity to store
99 */
100 public void store(final RelatedContent relatedContent) {
101
102 relatedContentBhv.insertOrUpdate(relatedContent, op -> op.setRefreshPolicy(Constants.TRUE));
103 ComponentUtil.getRelatedContentHelper().update();
104 }
105
106 /**
107 * Deletes a related content entity from the database.
108 * After deletion, updates the related content helper to refresh the cache.
109 *
110 * @param relatedContent the RelatedContent entity to delete
111 */
112 public void delete(final RelatedContent relatedContent) {
113
114 relatedContentBhv.delete(relatedContent, op -> op.setRefreshPolicy(Constants.TRUE));
115 ComponentUtil.getRelatedContentHelper().update();
116 }
117
118 /**
119 * Sets up the search conditions for listing related content entities.
120 * Configures wildcard searches for term and content fields, and sets up ordering.
121 *
122 * @param cb the condition bean for building the query
123 * @param relatedContentPager the pager containing search parameters
124 */
125 protected void setupListCondition(final RelatedContentCB cb, final RelatedContentPager relatedContentPager) {
126 if (StringUtil.isNotBlank(relatedContentPager.term)) {
127 cb.query().setTerm_Wildcard(wrapQuery(relatedContentPager.term));
128 }
129 if (StringUtil.isNotBlank(relatedContentPager.content)) {
130 cb.query().setContent_Wildcard(wrapQuery(relatedContentPager.content));
131 }
132 // TODO Long, Integer, String supported only.
133
134 // setup condition
135 cb.query().addOrderBy_Term_Asc();
136 cb.query().addOrderBy_CreatedTime_Asc();
137
138 // search
139
140 }
141
142 /**
143 * Retrieves a list of all available related content entities.
144 * Results are ordered by term and limited by the configured maximum fetch size.
145 *
146 * @return a list of all available RelatedContent entities
147 */
148 public List<RelatedContent> getAvailableRelatedContentList() {
149 return relatedContentBhv.selectList(cb -> {
150 cb.query().matchAll();
151 cb.query().addOrderBy_Term_Asc();
152 cb.fetchFirst(fessConfig.getPageDocboostMaxFetchSizeAsInteger());
153 });
154 }
155
156 }