View Javadoc
1   /*
2    * Copyright 2012-2021 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 static org.codelibs.core.stream.StreamUtil.stream;
19  
20  import java.util.List;
21  
22  import javax.annotation.Resource;
23  
24  import org.codelibs.core.beans.util.BeanUtil;
25  import org.codelibs.fess.Constants;
26  import org.codelibs.fess.app.pager.GroupPager;
27  import org.codelibs.fess.es.user.cbean.GroupCB;
28  import org.codelibs.fess.es.user.exbhv.GroupBhv;
29  import org.codelibs.fess.es.user.exbhv.UserBhv;
30  import org.codelibs.fess.es.user.exentity.Group;
31  import org.codelibs.fess.mylasta.direction.FessConfig;
32  import org.codelibs.fess.util.ComponentUtil;
33  import org.dbflute.cbean.result.PagingResultBean;
34  import org.dbflute.optional.OptionalEntity;
35  
36  public class GroupService {
37  
38      @Resource
39      protected GroupBhv groupBhv;
40  
41      @Resource
42      protected FessConfig fessConfig;
43  
44      @Resource
45      protected UserBhv userBhv;
46  
47      public List<Group> getGroupList(final GroupPager groupPager) {
48  
49          final PagingResultBean<Group> groupList = groupBhv.selectPage(cb -> {
50              cb.paging(groupPager.getPageSize(), groupPager.getCurrentPageNumber());
51              setupListCondition(cb, groupPager);
52          });
53  
54          // update pager
55          BeanUtil.copyBeanToBean(groupList, groupPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
56          groupPager.setPageNumberList(groupList.pageRange(op -> {
57              op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
58          }).createPageNumberList());
59  
60          return groupList;
61      }
62  
63      public OptionalEntity<Group> getGroup(final String id) {
64          return groupBhv.selectByPK(id).map(g -> {
65              ComponentUtil.getLdapManager().apply(g);
66              return g;
67          });
68      }
69  
70      public void store(final Group group) {
71          ComponentUtil.getLdapManager().insert(group);
72  
73          groupBhv.insertOrUpdate(group, op -> {
74              op.setRefreshPolicy(Constants.TRUE);
75          });
76  
77      }
78  
79      public void delete(final Group group) {
80          ComponentUtil.getLdapManager().delete(group);
81  
82          groupBhv.delete(group, op -> {
83              op.setRefreshPolicy(Constants.TRUE);
84          });
85  
86          userBhv.selectCursor(cb -> cb.query().setGroups_Equal(group.getId()), entity -> {
87              entity.setGroups(
88                      stream(entity.getGroups()).get(stream -> stream.filter(s -> !s.equals(group.getId())).toArray(n -> new String[n])));
89              userBhv.insertOrUpdate(entity);
90          });
91  
92      }
93  
94      protected void setupListCondition(final GroupCB cb, final GroupPager groupPager) {
95          if (groupPager.id != null) {
96              cb.query().docMeta().setId_Equal(groupPager.id);
97          }
98          // TODO Long, Integer, String supported only.
99  
100         // setup condition
101         cb.query().addOrderBy_Name_Asc();
102 
103         // search
104 
105     }
106 
107     public List<Group> getAvailableGroupList() {
108         return groupBhv.selectList(cb -> {
109             cb.query().matchAll();
110             cb.query().addOrderBy_Name_Asc();
111             cb.paging(fessConfig.getPageGroupMaxFetchSizeAsInteger(), 1);
112         });
113     }
114 
115 }