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.Collection;
19  import java.util.List;
20  
21  import org.codelibs.core.beans.util.BeanUtil;
22  import org.codelibs.core.lang.StringUtil;
23  import org.codelibs.fess.Constants;
24  import org.codelibs.fess.app.pager.PathMapPager;
25  import org.codelibs.fess.mylasta.direction.FessConfig;
26  import org.codelibs.fess.opensearch.config.cbean.PathMappingCB;
27  import org.codelibs.fess.opensearch.config.exbhv.PathMappingBhv;
28  import org.codelibs.fess.opensearch.config.exentity.PathMapping;
29  import org.codelibs.fess.util.ComponentUtil;
30  import org.dbflute.cbean.result.PagingResultBean;
31  import org.dbflute.optional.OptionalEntity;
32  
33  import jakarta.annotation.Resource;
34  
35  /**
36   * Service for path mapping operations.
37   */
38  public class PathMappingService extends FessAppService {
39  
40      /**
41       * Default constructor.
42       */
43      public PathMappingService() {
44          super();
45      }
46  
47      /** Path mapping behavior. */
48      @Resource
49      protected PathMappingBhv pathMappingBhv;
50  
51      /** Fess configuration. */
52      @Resource
53      protected FessConfig fessConfig;
54  
55      /**
56       * Gets the path mapping list with paging.
57       *
58       * @param pathMappingPager the path mapping pager
59       * @return the path mapping list
60       */
61      public List<PathMapping> getPathMappingList(final PathMapPager pathMappingPager) {
62  
63          final PagingResultBean<PathMapping> pathMappingList = pathMappingBhv.selectPage(cb -> {
64              cb.paging(pathMappingPager.getPageSize(), pathMappingPager.getCurrentPageNumber());
65              setupListCondition(cb, pathMappingPager);
66          });
67  
68          // update pager
69          BeanUtil.copyBeanToBean(pathMappingList, pathMappingPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
70          pathMappingPager.setPageNumberList(pathMappingList.pageRange(op -> {
71              op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
72          }).createPageNumberList());
73  
74          return pathMappingList;
75      }
76  
77      /**
78       * Gets a path mapping by ID.
79       *
80       * @param id the path mapping ID
81       * @return the path mapping
82       */
83      public OptionalEntity<PathMapping> getPathMapping(final String id) {
84          return pathMappingBhv.selectByPK(id);
85      }
86  
87      /**
88       * Stores a path mapping.
89       *
90       * @param pathMapping the path mapping to store
91       */
92      public void store(final PathMapping pathMapping) {
93  
94          pathMappingBhv.insertOrUpdate(pathMapping, op -> {
95              op.setRefreshPolicy(Constants.TRUE);
96          });
97  
98          ComponentUtil.getPathMappingHelper().init();
99      }
100 
101     /**
102      * Deletes a path mapping.
103      *
104      * @param pathMapping the path mapping to delete
105      */
106     public void delete(final PathMapping pathMapping) {
107 
108         pathMappingBhv.delete(pathMapping, op -> {
109             op.setRefreshPolicy(Constants.TRUE);
110         });
111 
112         ComponentUtil.getPathMappingHelper().init();
113     }
114 
115     /**
116      * Gets the path mapping list by process types.
117      *
118      * @param processTypeList the process type list
119      * @return the path mapping list
120      */
121     public List<PathMapping> getPathMappingList(final Collection<String> processTypeList) {
122 
123         return pathMappingBhv.selectList(cb -> {
124             cb.query().addOrderBy_SortOrder_Asc();
125             cb.query().setProcessType_InScope(processTypeList);
126             cb.fetchFirst(fessConfig.getPagePathMappingMaxFetchSizeAsInteger());
127         });
128     }
129 
130     /**
131      * Sets up list condition for path mapping search.
132      *
133      * @param cb the condition bean
134      * @param pathMappingPager the path mapping pager
135      */
136     protected void setupListCondition(final PathMappingCB cb, final PathMapPager pathMappingPager) {
137         if (StringUtil.isNotBlank(pathMappingPager.regex)) {
138             cb.query().setRegex_Wildcard(wrapQuery(pathMappingPager.regex));
139         }
140         if (StringUtil.isNotBlank(pathMappingPager.replacement)) {
141             cb.query().setReplacement_Wildcard(wrapQuery(pathMappingPager.replacement));
142         }
143         // TODO Long, Integer, String supported only.
144 
145         // setup condition
146         cb.query().addOrderBy_SortOrder_Asc();
147         cb.query().addOrderBy_CreatedTime_Asc();
148 
149         // search
150 
151     }
152 
153 }