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.core.lang.StringUtil;
22  import org.codelibs.fess.Constants;
23  import org.codelibs.fess.app.pager.LabelTypePager;
24  import org.codelibs.fess.helper.LabelTypeHelper;
25  import org.codelibs.fess.mylasta.direction.FessConfig;
26  import org.codelibs.fess.opensearch.config.cbean.LabelTypeCB;
27  import org.codelibs.fess.opensearch.config.exbhv.LabelTypeBhv;
28  import org.codelibs.fess.opensearch.config.exentity.LabelType;
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 class for label types.
37   */
38  public class LabelTypeService extends FessAppService {
39  
40      /** The LabelType behavior. */
41      @Resource
42      protected LabelTypeBhv labelTypeBhv;
43  
44      /** The Fess config. */
45      @Resource
46      protected FessConfig fessConfig;
47  
48      /**
49       * Default constructor.
50       */
51      public LabelTypeService() {
52          super();
53      }
54  
55      /**
56       * Get a list of label types.
57       *
58       * @param labelTypePager The pager for label types.
59       * @return A list of label types.
60       */
61      public List<LabelType> getLabelTypeList(final LabelTypePager labelTypePager) {
62  
63          final PagingResultBean<LabelType> labelTypeList = labelTypeBhv.selectPage(cb -> {
64              cb.paging(labelTypePager.getPageSize(), labelTypePager.getCurrentPageNumber());
65              setupListCondition(cb, labelTypePager);
66          });
67  
68          // update pager
69          BeanUtil.copyBeanToBean(labelTypeList, labelTypePager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
70          labelTypePager.setPageNumberList(labelTypeList.pageRange(op -> {
71              op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
72          }).createPageNumberList());
73  
74          return labelTypeList;
75      }
76  
77      /**
78       * Delete a label type.
79       *
80       * @param labelType The label type to delete.
81       */
82      public void delete(final LabelType labelType) {
83  
84          labelTypeBhv.delete(labelType, op -> {
85              op.setRefreshPolicy(Constants.TRUE);
86          });
87  
88          final LabelTypeHelper labelTypeHelper = ComponentUtil.getLabelTypeHelper();
89          if (labelTypeHelper != null) {
90              labelTypeHelper.refresh(getLabelTypeList());
91          }
92      }
93  
94      /**
95       * Set up list conditions.
96       *
97       * @param cb The condition bean.
98       * @param labelTypePager The pager for label types.
99       */
100     protected void setupListCondition(final LabelTypeCB cb, final LabelTypePager labelTypePager) {
101         if (StringUtil.isNotBlank(labelTypePager.name)) {
102             cb.query().setName_Wildcard(wrapQuery(labelTypePager.name));
103         }
104         if (StringUtil.isNotBlank(labelTypePager.value)) {
105             cb.query().setValue_Wildcard(wrapQuery(labelTypePager.value));
106         }
107         // TODO Long, Integer, String supported only.
108 
109         // setup condition
110         cb.query().addOrderBy_SortOrder_Asc();
111         cb.query().addOrderBy_Name_Asc();
112         // search
113 
114     }
115 
116     /**
117      * Get a list of all label types.
118      *
119      * @return A list of all label types.
120      */
121     public List<LabelType> getLabelTypeList() {
122         return labelTypeBhv.selectList(cb -> {
123             cb.query().addOrderBy_SortOrder_Asc();
124             cb.query().addOrderBy_Name_Asc();
125             cb.paging(fessConfig.getPageLabeltypeMaxFetchSizeAsInteger(), 1);
126         });
127     }
128 
129     /**
130      * Store a label type.
131      *
132      * @param labelType The label type to store.
133      */
134     public void store(final LabelType labelType) {
135         labelTypeBhv.insertOrUpdate(labelType, op -> {
136             op.setRefreshPolicy(Constants.TRUE);
137         });
138 
139         final LabelTypeHelper labelTypeHelper = ComponentUtil.getLabelTypeHelper();
140         if (labelTypeHelper != null) {
141             labelTypeHelper.refresh(getLabelTypeList());
142         }
143     }
144 
145     /**
146      * Get a label type.
147      *
148      * @param id The ID of the label type.
149      * @return An optional entity of the label type.
150      */
151     public OptionalEntity<LabelType> getLabelType(final String id) {
152         return labelTypeBhv.selectByPK(id);
153     }
154 
155 }