View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.flowable.support;
20  
21  import java.util.List;
22  import java.util.stream.Collectors;
23  import org.apache.syncope.core.persistence.api.dao.GroupDAO;
24  import org.apache.syncope.core.persistence.api.dao.UserDAO;
25  import org.flowable.idm.api.Group;
26  import org.flowable.idm.engine.impl.GroupQueryImpl;
27  import org.flowable.idm.engine.impl.persistence.entity.GroupEntity;
28  import org.flowable.idm.engine.impl.persistence.entity.GroupEntityImpl;
29  import org.springframework.beans.factory.annotation.Autowired;
30  import org.springframework.transaction.annotation.Transactional;
31  
32  public class SyncopeGroupQueryImpl extends GroupQueryImpl {
33  
34      private static final long serialVersionUID = -2595069675443343682L;
35  
36      @Autowired
37      private UserDAO userDAO;
38  
39      @Autowired
40      private GroupDAO groupDAO;
41  
42      private List<Group> result;
43  
44      private static Group fromSyncopeGroup(final String name) {
45          GroupEntity group = new GroupEntityImpl();
46          group.setId(name);
47          return group;
48      }
49  
50      private void execute() {
51          if (id != null) {
52              org.apache.syncope.core.persistence.api.entity.group.Group syncopeGroup = groupDAO.findByName(id);
53              if (syncopeGroup == null) {
54                  result = List.of();
55              } else {
56                  result = List.of(fromSyncopeGroup(syncopeGroup.getName()));
57              }
58          } else if (userId != null) {
59              result = userDAO.findAllGroupNames(userDAO.findByUsername(userId)).stream().
60                      map(SyncopeGroupQueryImpl::fromSyncopeGroup).
61                      collect(Collectors.toList());
62          }
63      }
64  
65      @Transactional(readOnly = true)
66      @Override
67      public long count() {
68          checkQueryOk();
69  
70          this.resultType = ResultType.COUNT;
71          if (result == null) {
72              execute();
73          }
74          return result.size();
75      }
76  
77      @Transactional(readOnly = true)
78      @Override
79      public List<Group> list() {
80          checkQueryOk();
81  
82          this.resultType = ResultType.LIST;
83          if (result == null) {
84              execute();
85          }
86          return result;
87      }
88  }