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.List;
19  
20  import org.codelibs.core.beans.util.BeanUtil;
21  import org.codelibs.fess.Constants;
22  import org.codelibs.fess.app.pager.RoleTypePager;
23  import org.codelibs.fess.mylasta.direction.FessConfig;
24  import org.codelibs.fess.opensearch.config.cbean.RoleTypeCB;
25  import org.codelibs.fess.opensearch.config.exbhv.RoleTypeBhv;
26  import org.codelibs.fess.opensearch.config.exentity.RoleType;
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 role types.
34   */
35  public class RoleTypeService {
36  
37      /**
38       * Constructor.
39       */
40      public RoleTypeService() {
41          super();
42      }
43  
44      /**
45       * The behavior for role types.
46       */
47      @Resource
48      protected RoleTypeBhv roleTypeBhv;
49  
50      /**
51       * The Fess configuration.
52       */
53      @Resource
54      protected FessConfig fessConfig;
55  
56      /**
57       * Gets a list of role types based on the pager.
58       * @param roleTypePager The pager for role types.
59       * @return A list of role types.
60       */
61      public List<RoleType> getRoleTypeList(final RoleTypePager roleTypePager) {
62  
63          final PagingResultBean<RoleType> roleTypeList = roleTypeBhv.selectPage(cb -> {
64              cb.paging(roleTypePager.getPageSize(), roleTypePager.getCurrentPageNumber());
65              setupListCondition(cb, roleTypePager);
66          });
67  
68          // update pager
69          BeanUtil.copyBeanToBean(roleTypeList, roleTypePager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
70          roleTypePager.setPageNumberList(roleTypeList.pageRange(op -> {
71              op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
72          }).createPageNumberList());
73  
74          return roleTypeList;
75      }
76  
77      /**
78       * Gets a role type by its ID.
79       * @param id The ID of the role type.
80       * @return An optional entity of the role type.
81       */
82      public OptionalEntity<RoleType> getRoleType(final String id) {
83          return roleTypeBhv.selectByPK(id);
84      }
85  
86      /**
87       * Stores a role type.
88       * @param roleType The role type to store.
89       */
90      public void store(final RoleType roleType) {
91  
92          roleTypeBhv.insertOrUpdate(roleType, op -> {
93              op.setRefreshPolicy(Constants.TRUE);
94          });
95  
96      }
97  
98      /**
99       * Deletes a role type.
100      * @param roleType The role type to delete.
101      */
102     public void delete(final RoleType roleType) {
103 
104         roleTypeBhv.delete(roleType, op -> {
105             op.setRefreshPolicy(Constants.TRUE);
106         });
107 
108     }
109 
110     /**
111      * Sets up the list condition for the role type query.
112      * @param cb The role type condition bean.
113      * @param roleTypePager The role type pager.
114      */
115     protected void setupListCondition(final RoleTypeCB cb, final RoleTypePager roleTypePager) {
116         if (roleTypePager.id != null) {
117             cb.query().docMeta().setId_Equal(roleTypePager.id);
118         }
119         // TODO Long, Integer, String supported only.
120 
121         // setup condition
122         cb.query().addOrderBy_SortOrder_Asc();
123         cb.query().addOrderBy_Name_Asc();
124         // search
125 
126     }
127 
128     /**
129      * Gets a list of all role types.
130      * @return A list of all role types.
131      */
132     public List<RoleType> getRoleTypeList() {
133         return roleTypeBhv.selectList(cb -> {
134             cb.query().addOrderBy_SortOrder_Asc();
135             cb.query().addOrderBy_Name_Asc();
136             cb.paging(fessConfig.getPageRoletypeMaxFetchSizeAsInteger(), 1);
137         });
138     }
139 
140 }