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.DuplicateHostPager;
24 import org.codelibs.fess.mylasta.direction.FessConfig;
25 import org.codelibs.fess.opensearch.config.cbean.DuplicateHostCB;
26 import org.codelibs.fess.opensearch.config.exbhv.DuplicateHostBhv;
27 import org.codelibs.fess.opensearch.config.exentity.DuplicateHost;
28 import org.dbflute.cbean.result.PagingResultBean;
29 import org.dbflute.optional.OptionalEntity;
30
31 import jakarta.annotation.Resource;
32
33 /**
34 * Service class for managing duplicate host configuration CRUD operations.
35 * This service provides functionality to create, read, update, and delete
36 * duplicate host configurations used by the Fess crawler system.
37 *
38 * <p>Duplicate host configurations allow administrators to define hostname patterns
39 * that should be treated as equivalent during crawling. This helps avoid indexing
40 * duplicate content from the same logical site that may be accessible via different
41 * hostnames (e.g., www.example.com and example.com).</p>
42 */
43 public class DuplicateHostService extends FessAppService {
44
45 /**
46 * DBFlute behavior for duplicate host operations.
47 * Provides database access methods for DuplicateHost entities.
48 */
49 @Resource
50 protected DuplicateHostBhv duplicateHostBhv;
51
52 /**
53 * Fess configuration containing application settings.
54 * Used to retrieve paging and other configuration parameters.
55 */
56 @Resource
57 protected FessConfig fessConfig;
58
59 /**
60 * Creates a new instance of DuplicateHostService.
61 * This constructor initializes the service for managing duplicate host configuration operations
62 * including CRUD operations and search functionality.
63 */
64 public DuplicateHostService() {
65 super();
66 }
67
68 /**
69 * Retrieves a paginated list of duplicate host configurations based on search criteria.
70 *
71 * <p>This method performs a paginated search through all duplicate host configurations,
72 * applying any search filters specified in the pager. The results are sorted
73 * by sort order, creation time, regular name, and duplicate hostname.</p>
74 *
75 * @param duplicateHostPager the pager containing search criteria and pagination settings
76 * @return a list of DuplicateHost entities matching the search criteria
77 * @throws IllegalArgumentException if duplicateHostPager is null
78 */
79 public List<DuplicateHost> getDuplicateHostList(final DuplicateHostPager duplicateHostPager) {
80
81 final PagingResultBean<DuplicateHost> duplicateHostList = duplicateHostBhv.selectPage(cb -> {
82 cb.paging(duplicateHostPager.getPageSize(), duplicateHostPager.getCurrentPageNumber());
83 setupListCondition(cb, duplicateHostPager);
84 });
85
86 // update pager
87 BeanUtil.copyBeanToBean(duplicateHostList, duplicateHostPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
88 duplicateHostPager.setPageNumberList(duplicateHostList.pageRange(op -> {
89 op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
90 }).createPageNumberList());
91
92 return duplicateHostList;
93 }
94
95 /**
96 * Retrieves a duplicate host configuration by its unique identifier.
97 *
98 * @param id the unique identifier of the duplicate host configuration
99 * @return an OptionalEntity containing the DuplicateHost if found, empty otherwise
100 * @throws IllegalArgumentException if id is null or empty
101 */
102 public OptionalEntity<DuplicateHost> getDuplicateHost(final String id) {
103 return duplicateHostBhv.selectByPK(id);
104 }
105
106 /**
107 * Stores (inserts or updates) a duplicate host configuration.
108 *
109 * <p>This method immediately refreshes the index to ensure the change is visible.
110 * If the configuration already exists (based on ID), it will be updated;
111 * otherwise, a new configuration will be created.</p>
112 *
113 * @param duplicateHost the duplicate host configuration to store
114 * @throws IllegalArgumentException if duplicateHost is null
115 */
116 public void store(final DuplicateHost duplicateHost) {
117
118 duplicateHostBhv.insertOrUpdate(duplicateHost, op -> {
119 op.setRefreshPolicy(Constants.TRUE);
120 });
121
122 }
123
124 /**
125 * Deletes the specified duplicate host configuration from the system.
126 *
127 * <p>This operation permanently removes the duplicate host configuration and
128 * immediately refreshes the index to ensure the change is visible.</p>
129 *
130 * @param duplicateHost the duplicate host configuration to delete
131 * @throws IllegalArgumentException if duplicateHost is null
132 * @throws org.dbflute.exception.EntityAlreadyDeletedException if the entity has already been deleted
133 */
134 public void delete(final DuplicateHost duplicateHost) {
135
136 duplicateHostBhv.delete(duplicateHost, op -> {
137 op.setRefreshPolicy(Constants.TRUE);
138 });
139
140 }
141
142 /**
143 * Retrieves all duplicate host configurations without pagination.
144 *
145 * <p>This method returns all duplicate host configurations in the system,
146 * ordered by sort order, regular name, and duplicate hostname. The results
147 * are limited by the configured maximum fetch size to prevent memory issues.</p>
148 *
149 * @return a list of all DuplicateHost entities
150 */
151 public List<DuplicateHost> getDuplicateHostList() {
152
153 return duplicateHostBhv.selectList(cb -> {
154 cb.query().addOrderBy_SortOrder_Asc();
155 cb.query().addOrderBy_RegularName_Asc();
156 cb.query().addOrderBy_DuplicateHostName_Asc();
157 cb.fetchFirst(fessConfig.getPageDuplicateHostMaxFetchSizeAsInteger());
158 });
159 }
160
161 /**
162 * Sets up the search conditions for listing duplicate host configurations.
163 *
164 * <p>This method configures the condition bean with search criteria from the pager,
165 * including regular name wildcards and duplicate hostname wildcards.
166 * Results are ordered by sort order and creation time in ascending order.</p>
167 *
168 * @param cb the condition bean to configure
169 * @param duplicateHostPager the pager containing search criteria
170 */
171 protected void setupListCondition(final DuplicateHostCB cb, final DuplicateHostPager duplicateHostPager) {
172 if (StringUtil.isNotBlank(duplicateHostPager.regularName)) {
173 cb.query().setRegularName_Wildcard(wrapQuery(duplicateHostPager.regularName));
174 }
175 if (StringUtil.isNotBlank(duplicateHostPager.duplicateHostName)) {
176 cb.query().setDuplicateHostName_Wildcard(wrapQuery(duplicateHostPager.duplicateHostName));
177 }
178 // TODO Long, Integer, String supported only.
179
180 // setup condition
181 cb.query().addOrderBy_SortOrder_Asc();
182 cb.query().addOrderBy_CreatedTime_Asc();
183
184 // search
185
186 }
187
188 }