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.fess.Constants;
22 import org.codelibs.fess.app.pager.ReqHeaderPager;
23 import org.codelibs.fess.mylasta.direction.FessConfig;
24 import org.codelibs.fess.opensearch.config.cbean.RequestHeaderCB;
25 import org.codelibs.fess.opensearch.config.exbhv.RequestHeaderBhv;
26 import org.codelibs.fess.opensearch.config.exentity.RequestHeader;
27 import org.dbflute.cbean.result.PagingResultBean;
28 import org.dbflute.optional.OptionalEntity;
29
30 import jakarta.annotation.Resource;
31
32 /**
33 * Service class for managing request headers used in web crawling configurations.
34 * This service provides CRUD operations for request headers that are applied
35 * during web crawling to configure HTTP request behavior.
36 *
37 */
38 public class RequestHeaderService {
39
40 /**
41 * Behavior for request header database operations.
42 */
43 @Resource
44 protected RequestHeaderBhv requestHeaderBhv;
45
46 /**
47 * Fess configuration settings.
48 */
49 @Resource
50 protected FessConfig fessConfig;
51
52 /**
53 * Default constructor for RequestHeaderService.
54 * Initializes the service with dependency injection.
55 */
56 public RequestHeaderService() {
57 // Default constructor
58 }
59
60 /**
61 * Retrieves a paginated list of request headers based on the provided pager criteria.
62 *
63 * @param requestHeaderPager the pager containing pagination and search criteria
64 * @return a list of request headers matching the specified criteria
65 */
66 public List<RequestHeader> getRequestHeaderList(final ReqHeaderPager requestHeaderPager) {
67
68 final PagingResultBean<RequestHeader> requestHeaderList = requestHeaderBhv.selectPage(cb -> {
69 cb.paging(requestHeaderPager.getPageSize(), requestHeaderPager.getCurrentPageNumber());
70 setupListCondition(cb, requestHeaderPager);
71 });
72
73 // update pager
74 BeanUtil.copyBeanToBean(requestHeaderList, requestHeaderPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
75 requestHeaderPager.setPageNumberList(requestHeaderList.pageRange(op -> {
76 op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
77 }).createPageNumberList());
78
79 return requestHeaderList;
80 }
81
82 /**
83 * Retrieves a specific request header by its ID.
84 *
85 * @param id the unique identifier of the request header
86 * @return an OptionalEntity containing the request header if found, empty otherwise
87 */
88 public OptionalEntity<RequestHeader> getRequestHeader(final String id) {
89 return requestHeaderBhv.selectByPK(id);
90 }
91
92 /**
93 * Stores a request header configuration to the database.
94 * This method performs either insert or update based on whether the request header already exists.
95 *
96 * @param requestHeader the request header configuration to store
97 */
98 public void store(final RequestHeader requestHeader) {
99
100 requestHeaderBhv.insertOrUpdate(requestHeader, op -> {
101 op.setRefreshPolicy(Constants.TRUE);
102 });
103
104 }
105
106 /**
107 * Deletes a request header configuration from the database.
108 *
109 * @param requestHeader the request header configuration to delete
110 */
111 public void delete(final RequestHeader requestHeader) {
112
113 requestHeaderBhv.delete(requestHeader, op -> {
114 op.setRefreshPolicy(Constants.TRUE);
115 });
116
117 }
118
119 /**
120 * Sets up the database query conditions for retrieving request headers based on pager criteria.
121 *
122 * @param cb the condition bean for building the database query
123 * @param requestHeaderPager the pager containing search and filter criteria
124 */
125 protected void setupListCondition(final RequestHeaderCB cb, final ReqHeaderPager requestHeaderPager) {
126 if (requestHeaderPager.id != null) {
127 cb.query().docMeta().setId_Equal(requestHeaderPager.id);
128 }
129 // TODO Long, Integer, String supported only.
130
131 // setup condition
132 cb.query().addOrderBy_Name_Asc();
133
134 // search
135
136 }
137
138 /**
139 * Retrieves all request headers associated with a specific web configuration.
140 *
141 * @param webConfigId the unique identifier of the web configuration
142 * @return a list of request headers associated with the specified web configuration
143 */
144 public List<RequestHeader> getRequestHeaderList(final String webConfigId) {
145 return requestHeaderBhv.selectList(cb -> {
146 cb.query().setWebConfigId_Equal(webConfigId);
147 cb.fetchFirst(fessConfig.getPageRequestHeaderMaxFetchSizeAsInteger());
148 });
149 }
150
151 }